/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-12-25 23:13:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091225231347-gg9u9ru0wj0f24hh
More consistent terminology: Clients are no longer "invalid" - they
are "disabled".  All code and documentation changed to reflect this.

D=Bus API change: The "properties" argument was removed from the
"ClientAdded" signal on interface "se.bsnet.fukt.Mandos".  All code in
both "mandos" and "mandos-monitor" changed to reflect this.

* mandos: Replaced "with closing(F)" with simply "with F" in all
          places where F is a file object.
  (Client.still_valid): Removed.  All callers changed to look at
                        "Client.enabled" instead.
  (dbus_service_property): Check for unsupported signatures with the
                           "byte_arrays" option.
  (DBusObjectWithProperties.Set): - '' -
  (ClientHandler.handle): Use the reverse pipe to receive the
                          "Client.enabled" attribute instead of the
                          now-removed "Client.still_valid()" method.
  (ForkingMixInWithPipe): Renamed to "ForkingMixInWithPipes" (all
                          users changed).  Now also create a reverse
                          pipe for sending data to the child process.
  (ForkingMixInWithPipes.add_pipe): Now takes two pipe fd's as
                                    arguments.  All callers changed.
  (IPv6_TCPServer.handle_ipc): Take an additional "reply_fd" argument
                               (all callers changed).  Close the reply
                               pipe when the child data pipe is
                               closed.  New "GETATTR" IPC method; will
                               pickle client attribute and send it
                               over the reply pipe FD.
  (MandosDBusService.ClientAdded): Removed "properties" argument.  All
                                   emitters changed.
* mandos-clients.conf.xml (DESCRIPTION, OPTIONS): Use
                                                  "enabled/disabled"
                                                  terminology.
* mandos-ctl: Option "--is-valid" renamed to "--is-enabled".
* mandos-monitor: Enable user locale.  Try to log exceptions.
  (MandosClientPropertyCache.__init__): Removed "properties" argument.
                                        All callers changed.
  (UserInterface.add_new_client): Remove "properties" argument.  All
                                  callers changed.  Supply "logger"
                                  argument to MandosClientWidget().
  (UserInterface.add_client): New "logger" argument.  All callers
                              changed.
* mandos.xml (BUGS, SECURITY/CLIENTS): Use "enabled/disabled"
                                       terminology.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@recompile.se>.
 
29
 * Contact the authors at <mandos@fukt.bsnet.se>.
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open(), S_ISREG */
 
56
#include <sys/stat.h>           /* open() */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno,
66
 
                                   program_invocation_short_name */
67
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
70
69
                                   if_nametoindex(), IF_NAMESIZE */
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
123
122
#define PATHDIR "/conf/conf.d/mandos"
124
123
#define SECKEY "seckey.txt"
125
124
#define PUBKEY "pubkey.txt"
126
 
#define HOOKDIR "/lib/mandos/network-hooks.d"
127
125
 
128
126
bool debug = false;
129
127
static const char mandos_protocol_version[] = "1";
130
128
const char *argp_program_version = "mandos-client " VERSION;
131
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
132
 
static const char sys_class_net[] = "/sys/class/net";
133
 
char *connect_to = NULL;
134
 
 
135
 
/* Doubly linked list that need to be circularly linked when used */
136
 
typedef struct server{
137
 
  const char *ip;
138
 
  uint16_t port;
139
 
  AvahiIfIndex if_index;
140
 
  int af;
141
 
  struct timespec last_seen;
142
 
  struct server *next;
143
 
  struct server *prev;
144
 
} server;
 
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
145
130
 
146
131
/* Used for passing in values through the Avahi callback functions */
147
132
typedef struct {
152
137
  gnutls_dh_params_t dh_params;
153
138
  const char *priority;
154
139
  gpgme_ctx_t ctx;
155
 
  server *current_server;
156
140
} mandos_context;
157
141
 
158
142
/* global context so signal handler can reach it*/
159
143
mandos_context mc = { .simple_poll = NULL, .server = NULL,
160
144
                      .dh_bits = 1024, .priority = "SECURE256"
161
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
162
 
                      .current_server = NULL };
 
145
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
163
146
 
164
147
sig_atomic_t quit_now = 0;
165
148
int signal_received = 0;
166
149
 
167
 
/* Function to use when printing errors */
168
 
void perror_plus(const char *print_text){
169
 
  fprintf(stderr, "Mandos plugin %s: ",
170
 
          program_invocation_short_name);
171
 
  perror(print_text);
172
 
}
173
 
 
174
150
/*
175
151
 * Make additional room in "buffer" for at least BUFFER_SIZE more
176
152
 * bytes. "buffer_capacity" is how much is currently allocated,
188
164
  return buffer_capacity;
189
165
}
190
166
 
191
 
/* Add server to set of servers to retry periodically */
192
 
int add_server(const char *ip, uint16_t port,
193
 
                 AvahiIfIndex if_index,
194
 
                 int af){
195
 
  int ret;
196
 
  server *new_server = malloc(sizeof(server));
197
 
  if(new_server == NULL){
198
 
    perror_plus("malloc");
199
 
    return -1;
200
 
  }
201
 
  *new_server = (server){ .ip = strdup(ip),
202
 
                         .port = port,
203
 
                         .if_index = if_index,
204
 
                         .af = af };
205
 
  if(new_server->ip == NULL){
206
 
    perror_plus("strdup");
207
 
    return -1;
208
 
  }
209
 
  /* Special case of first server */
210
 
  if (mc.current_server == NULL){
211
 
    new_server->next = new_server;
212
 
    new_server->prev = new_server;
213
 
    mc.current_server = new_server;
214
 
  /* Place the new server last in the list */
215
 
  } else {
216
 
    new_server->next = mc.current_server;
217
 
    new_server->prev = mc.current_server->prev;
218
 
    new_server->prev->next = new_server;
219
 
    mc.current_server->prev = new_server;
220
 
  }
221
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
222
 
  if(ret == -1){
223
 
    perror_plus("clock_gettime");
224
 
    return -1;
225
 
  }
226
 
  return 0;
227
 
}
228
 
 
229
167
/* 
230
168
 * Initialize GPGME.
231
169
 */
245
183
    
246
184
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
247
185
    if(fd == -1){
248
 
      perror_plus("open");
 
186
      perror("open");
249
187
      return false;
250
188
    }
251
189
    
265
203
    
266
204
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
267
205
    if(ret == -1){
268
 
      perror_plus("close");
 
206
      perror("close");
269
207
    }
270
208
    gpgme_data_release(pgp_data);
271
209
    return true;
284
222
    return false;
285
223
  }
286
224
  
287
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
225
    /* Set GPGME home directory for the OpenPGP engine only */
288
226
  rc = gpgme_get_engine_info(&engine_info);
289
227
  if(rc != GPG_ERR_NO_ERROR){
290
228
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
396
334
  
397
335
  /* Seek back to the beginning of the GPGME plaintext data buffer */
398
336
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
399
 
    perror_plus("gpgme_data_seek");
 
337
    perror("gpgme_data_seek");
400
338
    plaintext_length = -1;
401
339
    goto decrypt_end;
402
340
  }
407
345
                                      (size_t)plaintext_length,
408
346
                                      plaintext_capacity);
409
347
    if(plaintext_capacity == 0){
410
 
        perror_plus("incbuffer");
 
348
        perror("incbuffer");
411
349
        plaintext_length = -1;
412
350
        goto decrypt_end;
413
351
    }
420
358
      break;
421
359
    }
422
360
    if(ret < 0){
423
 
      perror_plus("gpgme_data_read");
 
361
      perror("gpgme_data_read");
424
362
      plaintext_length = -1;
425
363
      goto decrypt_end;
426
364
    }
483
421
  }
484
422
  
485
423
  /* OpenPGP credentials */
486
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
487
425
  if(ret != GNUTLS_E_SUCCESS){
488
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
489
430
            safer_gnutls_strerror(ret));
490
431
    gnutls_global_deinit();
491
432
    return -1;
646
587
  tcp_sd = socket(pf, SOCK_STREAM, 0);
647
588
  if(tcp_sd < 0){
648
589
    int e = errno;
649
 
    perror_plus("socket");
 
590
    perror("socket");
650
591
    errno = e;
651
592
    goto mandos_end;
652
593
  }
666
607
  }
667
608
  if(ret < 0 ){
668
609
    int e = errno;
669
 
    perror_plus("inet_pton");
 
610
    perror("inet_pton");
670
611
    errno = e;
671
612
    goto mandos_end;
672
613
  }
708
649
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
709
650
      char interface[IF_NAMESIZE];
710
651
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
711
 
        perror_plus("if_indextoname");
 
652
        perror("if_indextoname");
712
653
      } else {
713
654
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
714
655
                ip, interface, port);
728
669
                        sizeof(addrstr));
729
670
    }
730
671
    if(pcret == NULL){
731
 
      perror_plus("inet_ntop");
 
672
      perror("inet_ntop");
732
673
    } else {
733
674
      if(strcmp(addrstr, ip) != 0){
734
675
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
747
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
748
689
  }
749
690
  if(ret < 0){
750
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
751
 
      int e = errno;
752
 
      perror_plus("connect");
753
 
      errno = e;
754
 
    }
 
691
    int e = errno;
 
692
    perror("connect");
 
693
    errno = e;
755
694
    goto mandos_end;
756
695
  }
757
696
  
768
707
                                   out_size - written));
769
708
    if(ret == -1){
770
709
      int e = errno;
771
 
      perror_plus("write");
 
710
      perror("write");
772
711
      errno = e;
773
712
      goto mandos_end;
774
713
    }
799
738
    goto mandos_end;
800
739
  }
801
740
  
802
 
  /* Spurious warning from -Wint-to-pointer-cast */
803
741
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
804
742
  
805
743
  if(quit_now){
842
780
                                   buffer_capacity);
843
781
    if(buffer_capacity == 0){
844
782
      int e = errno;
845
 
      perror_plus("incbuffer");
 
783
      perror("incbuffer");
846
784
      errno = e;
847
785
      goto mandos_end;
848
786
    }
953
891
      if(e == 0){
954
892
        e = errno;
955
893
      }
956
 
      perror_plus("close");
 
894
      perror("close");
957
895
    }
958
896
    gnutls_deinit(session);
959
 
    errno = e;
960
897
    if(quit_now){
961
 
      errno = EINTR;
 
898
      e = EINTR;
962
899
      retval = -1;
963
900
    }
 
901
    errno = e;
964
902
  }
965
903
  return retval;
966
904
}
1009
947
                                           avahi_proto_to_af(proto));
1010
948
      if(ret == 0){
1011
949
        avahi_simple_poll_quit(mc.simple_poll);
1012
 
      } else {
1013
 
        ret = add_server(ip, port, interface,
1014
 
                         avahi_proto_to_af(proto));
1015
950
      }
1016
951
    }
1017
952
  }
1071
1006
  }
1072
1007
}
1073
1008
 
1074
 
/* Signal handler that stops main loop after SIGTERM */
 
1009
/* stop main loop after sigterm has been called */
1075
1010
static void handle_sigterm(int sig){
1076
1011
  if(quit_now){
1077
1012
    return;
1079
1014
  quit_now = 1;
1080
1015
  signal_received = sig;
1081
1016
  int old_errno = errno;
1082
 
  /* set main loop to exit */
1083
1017
  if(mc.simple_poll != NULL){
1084
1018
    avahi_simple_poll_quit(mc.simple_poll);
1085
1019
  }
1086
1020
  errno = old_errno;
1087
1021
}
1088
1022
 
1089
 
bool get_flags(const char *ifname, struct ifreq *ifr){
1090
 
  int ret;
1091
 
  
1092
 
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1093
 
  if(s < 0){
1094
 
    perror_plus("socket");
1095
 
    return false;
1096
 
  }
1097
 
  strcpy(ifr->ifr_name, ifname);
1098
 
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1099
 
  if(ret == -1){
1100
 
    if(debug){
1101
 
      perror_plus("ioctl SIOCGIFFLAGS");
1102
 
    }
1103
 
    return false;
1104
 
  }
1105
 
  return true;
1106
 
}
1107
 
 
1108
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1109
 
  
1110
 
  /* Reject the loopback device */
1111
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
1112
 
    if(debug){
1113
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1114
 
              ifname);
1115
 
    }
1116
 
    return false;
1117
 
  }
1118
 
  /* Accept point-to-point devices only if connect_to is specified */
1119
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
 
    if(debug){
1121
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1122
 
              ifname);
1123
 
    }
1124
 
    return true;
1125
 
  }
1126
 
  /* Otherwise, reject non-broadcast-capable devices */
1127
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
 
    if(debug){
1129
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1130
 
              ifname);
1131
 
    }
1132
 
    return false;
1133
 
  }
1134
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1135
 
  if(ifr->ifr_flags & IFF_NOARP){
1136
 
    if(debug){
1137
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1138
 
    }
1139
 
    return false;
1140
 
  }
1141
 
  
1142
 
  /* Accept this device */
1143
 
  if(debug){
1144
 
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1145
 
  }
1146
 
  return true;
1147
 
}
1148
 
 
1149
 
/* 
1150
 
 * This function determines if a directory entry in /sys/class/net
1151
 
 * corresponds to an acceptable network device.
1152
 
 * (This function is passed to scandir(3) as a filter function.)
1153
 
 */
1154
 
int good_interface(const struct dirent *if_entry){
1155
 
  if(if_entry->d_name[0] == '.'){
1156
 
    return 0;
1157
 
  }
1158
 
  
1159
 
  struct ifreq ifr;
1160
 
  if(not get_flags(if_entry->d_name, &ifr)){
1161
 
    if(debug){
1162
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1163
 
              if_entry->d_name);
1164
 
    }
1165
 
    return 0;
1166
 
  }
1167
 
  
1168
 
  if(not good_flags(if_entry->d_name, &ifr)){
1169
 
    return 0;
1170
 
  }
1171
 
  return 1;
1172
 
}
1173
 
 
1174
 
/* 
1175
 
 * This function determines if a directory entry in /sys/class/net
1176
 
 * corresponds to an acceptable network device which is up.
1177
 
 * (This function is passed to scandir(3) as a filter function.)
1178
 
 */
1179
 
int up_interface(const struct dirent *if_entry){
1180
 
  if(if_entry->d_name[0] == '.'){
1181
 
    return 0;
1182
 
  }
1183
 
  
1184
 
  struct ifreq ifr;
1185
 
  if(not get_flags(if_entry->d_name, &ifr)){
1186
 
    if(debug){
1187
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1188
 
              if_entry->d_name);
1189
 
    }
1190
 
    return 0;
1191
 
  }
1192
 
  
1193
 
  /* Reject down interfaces */
1194
 
  if(not (ifr.ifr_flags & IFF_UP)){
1195
 
    if(debug){
1196
 
      fprintf(stderr, "Rejecting down interface \"%s\"\n",
1197
 
              if_entry->d_name);
1198
 
    }
1199
 
    return 0;
1200
 
  }
1201
 
  
1202
 
  /* Reject non-running interfaces */
1203
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1204
 
    if(debug){
1205
 
      fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1206
 
              if_entry->d_name);
1207
 
    }
1208
 
    return 0;
1209
 
  }
1210
 
  
1211
 
  if(not good_flags(if_entry->d_name, &ifr)){
1212
 
    return 0;
1213
 
  }
1214
 
  return 1;
1215
 
}
1216
 
 
1217
 
int notdotentries(const struct dirent *direntry){
1218
 
  /* Skip "." and ".." */
1219
 
  if(direntry->d_name[0] == '.'
1220
 
     and (direntry->d_name[1] == '\0'
1221
 
          or (direntry->d_name[1] == '.'
1222
 
              and direntry->d_name[2] == '\0'))){
1223
 
    return 0;
1224
 
  }
1225
 
  return 1;
1226
 
}
1227
 
 
1228
 
/* Is this directory entry a runnable program? */
1229
 
int runnable_hook(const struct dirent *direntry){
1230
 
  int ret;
1231
 
  struct stat st;
1232
 
  
1233
 
  if((direntry->d_name)[0] == '\0'){
1234
 
    /* Empty name? */
1235
 
    return 0;
1236
 
  }
1237
 
  
1238
 
  /* Save pointer to last character */
1239
 
  char *end = strchr(direntry->d_name, '\0')-1;
1240
 
  
1241
 
  if(*end == '~'){
1242
 
    /* Backup name~ */
1243
 
    return 0;
1244
 
  }
1245
 
  
1246
 
  if(((direntry->d_name)[0] == '#')
1247
 
     and (*end == '#')){
1248
 
    /* Temporary #name# */
1249
 
    return 0;
1250
 
  }
1251
 
  
1252
 
  /* XXX more rules here */
1253
 
  
1254
 
  ret = stat(direntry->d_name, &st);
1255
 
  if(ret == -1){
1256
 
    if(debug){
1257
 
      perror_plus("Could not stat plugin");
1258
 
    }
1259
 
    return 0;
1260
 
  }
1261
 
  if(not (S_ISREG(st.st_mode))){
1262
 
    /* Not a regular file */
1263
 
    return 0;
1264
 
  }
1265
 
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
 
    /* Not executable */
1267
 
    return 0;
1268
 
  }
1269
 
  return 1;
1270
 
}
1271
 
 
1272
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1273
 
  int ret;
1274
 
  struct timespec now;
1275
 
  struct timespec waited_time;
1276
 
  intmax_t block_time;
1277
 
  
1278
 
  while(true){
1279
 
    if(mc.current_server == NULL){
1280
 
      if (debug){
1281
 
        fprintf(stderr,
1282
 
                "Wait until first server is found. No timeout!\n");
1283
 
      }
1284
 
      ret = avahi_simple_poll_iterate(s, -1);
1285
 
    } else {
1286
 
      if (debug){
1287
 
        fprintf(stderr, "Check current_server if we should run it,"
1288
 
                " or wait\n");
1289
 
      }
1290
 
      /* the current time */
1291
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1292
 
      if(ret == -1){
1293
 
        perror_plus("clock_gettime");
1294
 
        return -1;
1295
 
      }
1296
 
      /* Calculating in ms how long time between now and server
1297
 
         who we visted longest time ago. Now - last seen.  */
1298
 
      waited_time.tv_sec = (now.tv_sec
1299
 
                            - mc.current_server->last_seen.tv_sec);
1300
 
      waited_time.tv_nsec = (now.tv_nsec
1301
 
                             - mc.current_server->last_seen.tv_nsec);
1302
 
      /* total time is 10s/10,000ms.
1303
 
         Converting to s from ms by dividing by 1,000,
1304
 
         and ns to ms by dividing by 1,000,000. */
1305
 
      block_time = ((retry_interval
1306
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1307
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1308
 
      
1309
 
      if (debug){
1310
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1311
 
      }
1312
 
      
1313
 
      if(block_time <= 0){
1314
 
        ret = start_mandos_communication(mc.current_server->ip,
1315
 
                                         mc.current_server->port,
1316
 
                                         mc.current_server->if_index,
1317
 
                                         mc.current_server->af);
1318
 
        if(ret == 0){
1319
 
          avahi_simple_poll_quit(mc.simple_poll);
1320
 
          return 0;
1321
 
        }
1322
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1323
 
                            &mc.current_server->last_seen);
1324
 
        if(ret == -1){
1325
 
          perror_plus("clock_gettime");
1326
 
          return -1;
1327
 
        }
1328
 
        mc.current_server = mc.current_server->next;
1329
 
        block_time = 0;         /* Call avahi to find new Mandos
1330
 
                                   servers, but don't block */
1331
 
      }
1332
 
      
1333
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1334
 
    }
1335
 
    if(ret != 0){
1336
 
      if (ret > 0 or errno != EINTR) {
1337
 
        return (ret != 1) ? ret : 0;
1338
 
      }
1339
 
    }
1340
 
  }
1341
 
}
1342
 
 
1343
1023
int main(int argc, char *argv[]){
1344
1024
  AvahiSServiceBrowser *sb = NULL;
1345
1025
  int error;
1347
1027
  intmax_t tmpmax;
1348
1028
  char *tmp;
1349
1029
  int exitcode = EXIT_SUCCESS;
1350
 
  const char *interface = "";
 
1030
  const char *interface = "eth0";
1351
1031
  struct ifreq network;
1352
1032
  int sd = -1;
1353
1033
  bool take_down_interface = false;
1354
1034
  uid_t uid;
1355
1035
  gid_t gid;
 
1036
  char *connect_to = NULL;
1356
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
1357
1038
  bool tempdir_created = false;
1358
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1362
1043
  bool gnutls_initialized = false;
1363
1044
  bool gpgme_initialized = false;
1364
1045
  float delay = 2.5f;
1365
 
  double retry_interval = 10; /* 10s between trying a server and
1366
 
                                 retrying the same server again */
1367
1046
  
1368
1047
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1369
1048
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1375
1054
  errno = 0;
1376
1055
  ret = setgid(gid);
1377
1056
  if(ret == -1){
1378
 
    perror_plus("setgid");
 
1057
    perror("setgid");
1379
1058
  }
1380
1059
  
1381
1060
  /* Lower user privileges (temporarily) */
1382
1061
  errno = 0;
1383
1062
  ret = seteuid(uid);
1384
1063
  if(ret == -1){
1385
 
    perror_plus("seteuid");
 
1064
    perror("seteuid");
1386
1065
  }
1387
1066
  
1388
1067
  if(quit_now){
1423
1102
        .arg = "SECONDS",
1424
1103
        .doc = "Maximum delay to wait for interface startup",
1425
1104
        .group = 2 },
1426
 
      { .name = "retry", .key = 132,
1427
 
        .arg = "SECONDS",
1428
 
        .doc = "Retry interval used when denied by the mandos server",
1429
 
        .group = 2 },
1430
1105
      /*
1431
1106
       * These reproduce what we would get without ARGP_NO_HELP
1432
1107
       */
1476
1151
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1477
1152
          argp_error(state, "Bad delay");
1478
1153
        }
1479
 
      case 132:                 /* --retry */
1480
 
        errno = 0;
1481
 
        retry_interval = strtod(arg, &tmp);
1482
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1483
 
           or (retry_interval * 1000) > INT_MAX
1484
 
           or retry_interval < 0){
1485
 
          argp_error(state, "Bad retry interval");
1486
 
        }
1487
1154
        break;
1488
1155
        /*
1489
1156
         * These reproduce what we would get without ARGP_NO_HELP
1517
1184
    case ENOMEM:
1518
1185
    default:
1519
1186
      errno = ret;
1520
 
      perror_plus("argp_parse");
 
1187
      perror("argp_parse");
1521
1188
      exitcode = EX_OSERR;
1522
1189
      goto end;
1523
1190
    case EINVAL:
1525
1192
      goto end;
1526
1193
    }
1527
1194
  }
1528
 
    
1529
 
  {
1530
 
    /* Work around Debian bug #633582:
1531
 
       <http://bugs.debian.org/633582> */
1532
 
    struct stat st;
1533
 
    
1534
 
    /* Re-raise priviliges */
1535
 
    errno = 0;
1536
 
    ret = seteuid(0);
1537
 
    if(ret == -1){
1538
 
      perror_plus("seteuid");
1539
 
    }
1540
 
    
1541
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
 
      int seckey_fd = open(seckey, O_RDONLY);
1543
 
      if(seckey_fd == -1){
1544
 
        perror_plus("open");
1545
 
      } else {
1546
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1547
 
        if(ret == -1){
1548
 
          perror_plus("fstat");
1549
 
        } else {
1550
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
 
            ret = fchown(seckey_fd, uid, gid);
1552
 
            if(ret == -1){
1553
 
              perror_plus("fchown");
1554
 
            }
1555
 
          }
1556
 
        }
1557
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1558
 
      }
1559
 
    }
1560
 
    
1561
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1563
 
      if(pubkey_fd == -1){
1564
 
        perror_plus("open");
1565
 
      } else {
1566
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1567
 
        if(ret == -1){
1568
 
          perror_plus("fstat");
1569
 
        } else {
1570
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
 
            ret = fchown(pubkey_fd, uid, gid);
1572
 
            if(ret == -1){
1573
 
              perror_plus("fchown");
1574
 
            }
1575
 
          }
1576
 
        }
1577
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1578
 
      }
1579
 
    }
1580
 
    
1581
 
    /* Lower privileges */
1582
 
    errno = 0;
1583
 
    ret = seteuid(uid);
1584
 
    if(ret == -1){
1585
 
      perror_plus("seteuid");
1586
 
    }
1587
 
  }
1588
 
  
1589
 
  /* Find network hooks and run them */
1590
 
  {
1591
 
    struct dirent **direntries;
1592
 
    struct dirent *direntry;
1593
 
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1594
 
                           alphasort);
1595
 
    int devnull = open("/dev/null", O_RDONLY);
1596
 
    for(int i = 0; i < numhooks; i++){
1597
 
      direntry = direntries[0];
1598
 
      char *fullname = NULL;
1599
 
      ret = asprintf(&fullname, "%s/%s", tempdir,
1600
 
                     direntry->d_name);
1601
 
      if(ret < 0){
1602
 
        perror_plus("asprintf");
1603
 
        continue;
1604
 
      }
1605
 
      pid_t hook_pid = fork();
1606
 
      if(hook_pid == 0){
1607
 
        /* Child */
1608
 
        dup2(devnull, STDIN_FILENO);
1609
 
        close(devnull);
1610
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1611
 
        ret = setenv("DEVICE", interface, 1);
1612
 
        if(ret == -1){
1613
 
          perror_plus("setenv");
1614
 
          exit(1);
1615
 
        }
1616
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1617
 
        if(ret == -1){
1618
 
          perror_plus("setenv");
1619
 
          exit(1);
1620
 
        }
1621
 
        ret = setenv("MODE", "start", 1);
1622
 
        if(ret == -1){
1623
 
          perror_plus("setenv");
1624
 
          exit(1);
1625
 
        }
1626
 
        char *delaystring;
1627
 
        ret = asprintf(&delaystring, "%f", delay);
1628
 
        if(ret == -1){
1629
 
          perror_plus("asprintf");
1630
 
          exit(1);
1631
 
        }
1632
 
        ret = setenv("DELAY", delaystring, 1);
1633
 
        if(ret == -1){
1634
 
          free(delaystring);
1635
 
          perror_plus("setenv");
1636
 
          exit(1);
1637
 
        }
1638
 
        free(delaystring);
1639
 
        ret = execl(fullname, direntry->d_name, "start", NULL);
1640
 
        perror_plus("execl");
1641
 
      }
1642
 
      free(fullname);
1643
 
      if(quit_now){
1644
 
        goto end;
1645
 
      }
1646
 
    }
1647
 
    close(devnull);
1648
 
  }
1649
1195
  
1650
1196
  if(not debug){
1651
1197
    avahi_set_log_function(empty_log);
1652
1198
  }
1653
1199
  
1654
 
  if(interface[0] == '\0'){
1655
 
    struct dirent **direntries;
1656
 
    /* First look for interfaces that are up */
1657
 
    ret = scandir(sys_class_net, &direntries, up_interface,
1658
 
                  alphasort);
1659
 
    if(ret == 0){
1660
 
      /* No up interfaces, look for any good interfaces */
1661
 
      free(direntries);
1662
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1663
 
                    alphasort);
1664
 
    }
1665
 
    if(ret >= 1){
1666
 
      /* Pick the first interface returned */
1667
 
      interface = strdup(direntries[0]->d_name);
1668
 
      if(debug){
1669
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1670
 
      }
1671
 
      if(interface == NULL){
1672
 
        perror_plus("malloc");
1673
 
        free(direntries);
1674
 
        exitcode = EXIT_FAILURE;
1675
 
        goto end;
1676
 
      }
1677
 
      free(direntries);
1678
 
    } else {
1679
 
      free(direntries);
1680
 
      fprintf(stderr, "Could not find a network interface\n");
1681
 
      exitcode = EXIT_FAILURE;
1682
 
      goto end;
1683
 
    }
1684
 
  }
1685
 
  
1686
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1687
1201
     from the signal handler */
1688
1202
  /* Initialize the pseudo-RNG for Avahi */
1697
1211
  sigemptyset(&sigterm_action.sa_mask);
1698
1212
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1699
1213
  if(ret == -1){
1700
 
    perror_plus("sigaddset");
 
1214
    perror("sigaddset");
1701
1215
    exitcode = EX_OSERR;
1702
1216
    goto end;
1703
1217
  }
1704
1218
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1705
1219
  if(ret == -1){
1706
 
    perror_plus("sigaddset");
 
1220
    perror("sigaddset");
1707
1221
    exitcode = EX_OSERR;
1708
1222
    goto end;
1709
1223
  }
1710
1224
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1711
1225
  if(ret == -1){
1712
 
    perror_plus("sigaddset");
 
1226
    perror("sigaddset");
1713
1227
    exitcode = EX_OSERR;
1714
1228
    goto end;
1715
1229
  }
1719
1233
  */
1720
1234
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1721
1235
  if(ret == -1){
1722
 
    perror_plus("sigaction");
 
1236
    perror("sigaction");
1723
1237
    return EX_OSERR;
1724
1238
  }
1725
1239
  if(old_sigterm_action.sa_handler != SIG_IGN){
1726
1240
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1727
1241
    if(ret == -1){
1728
 
      perror_plus("sigaction");
 
1242
      perror("sigaction");
1729
1243
      exitcode = EX_OSERR;
1730
1244
      goto end;
1731
1245
    }
1732
1246
  }
1733
1247
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1734
1248
  if(ret == -1){
1735
 
    perror_plus("sigaction");
 
1249
    perror("sigaction");
1736
1250
    return EX_OSERR;
1737
1251
  }
1738
1252
  if(old_sigterm_action.sa_handler != SIG_IGN){
1739
1253
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1740
1254
    if(ret == -1){
1741
 
      perror_plus("sigaction");
 
1255
      perror("sigaction");
1742
1256
      exitcode = EX_OSERR;
1743
1257
      goto end;
1744
1258
    }
1745
1259
  }
1746
1260
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1747
1261
  if(ret == -1){
1748
 
    perror_plus("sigaction");
 
1262
    perror("sigaction");
1749
1263
    return EX_OSERR;
1750
1264
  }
1751
1265
  if(old_sigterm_action.sa_handler != SIG_IGN){
1752
1266
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1753
1267
    if(ret == -1){
1754
 
      perror_plus("sigaction");
 
1268
      perror("sigaction");
1755
1269
      exitcode = EX_OSERR;
1756
1270
      goto end;
1757
1271
    }
1758
1272
  }
1759
1273
  
1760
1274
  /* If the interface is down, bring it up */
1761
 
  if(strcmp(interface, "none") != 0){
 
1275
  if(interface[0] != '\0'){
1762
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1763
1277
    if(if_index == 0){
1764
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1774
1288
    errno = 0;
1775
1289
    ret = seteuid(0);
1776
1290
    if(ret == -1){
1777
 
      perror_plus("seteuid");
 
1291
      perror("seteuid");
1778
1292
    }
1779
1293
    
1780
1294
#ifdef __linux__
1784
1298
    bool restore_loglevel = true;
1785
1299
    if(ret == -1){
1786
1300
      restore_loglevel = false;
1787
 
      perror_plus("klogctl");
 
1301
      perror("klogctl");
1788
1302
    }
1789
1303
#endif  /* __linux__ */
1790
1304
    
1791
1305
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1792
1306
    if(sd < 0){
1793
 
      perror_plus("socket");
 
1307
      perror("socket");
1794
1308
      exitcode = EX_OSERR;
1795
1309
#ifdef __linux__
1796
1310
      if(restore_loglevel){
1797
1311
        ret = klogctl(7, NULL, 0);
1798
1312
        if(ret == -1){
1799
 
          perror_plus("klogctl");
 
1313
          perror("klogctl");
1800
1314
        }
1801
1315
      }
1802
1316
#endif  /* __linux__ */
1804
1318
      errno = 0;
1805
1319
      ret = seteuid(uid);
1806
1320
      if(ret == -1){
1807
 
        perror_plus("seteuid");
 
1321
        perror("seteuid");
1808
1322
      }
1809
1323
      goto end;
1810
1324
    }
1811
1325
    strcpy(network.ifr_name, interface);
1812
1326
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1813
1327
    if(ret == -1){
1814
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1328
      perror("ioctl SIOCGIFFLAGS");
1815
1329
#ifdef __linux__
1816
1330
      if(restore_loglevel){
1817
1331
        ret = klogctl(7, NULL, 0);
1818
1332
        if(ret == -1){
1819
 
          perror_plus("klogctl");
 
1333
          perror("klogctl");
1820
1334
        }
1821
1335
      }
1822
1336
#endif  /* __linux__ */
1825
1339
      errno = 0;
1826
1340
      ret = seteuid(uid);
1827
1341
      if(ret == -1){
1828
 
        perror_plus("seteuid");
 
1342
        perror("seteuid");
1829
1343
      }
1830
1344
      goto end;
1831
1345
    }
1835
1349
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1836
1350
      if(ret == -1){
1837
1351
        take_down_interface = false;
1838
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1352
        perror("ioctl SIOCSIFFLAGS");
1839
1353
        exitcode = EX_OSERR;
1840
1354
#ifdef __linux__
1841
1355
        if(restore_loglevel){
1842
1356
          ret = klogctl(7, NULL, 0);
1843
1357
          if(ret == -1){
1844
 
            perror_plus("klogctl");
 
1358
            perror("klogctl");
1845
1359
          }
1846
1360
        }
1847
1361
#endif  /* __linux__ */
1849
1363
        errno = 0;
1850
1364
        ret = seteuid(uid);
1851
1365
        if(ret == -1){
1852
 
          perror_plus("seteuid");
 
1366
          perror("seteuid");
1853
1367
        }
1854
1368
        goto end;
1855
1369
      }
1856
1370
    }
1857
 
    /* Sleep checking until interface is running.
1858
 
       Check every 0.25s, up to total time of delay */
 
1371
    /* sleep checking until interface is running */
1859
1372
    for(int i=0; i < delay * 4; i++){
1860
1373
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1861
1374
      if(ret == -1){
1862
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1375
        perror("ioctl SIOCGIFFLAGS");
1863
1376
      } else if(network.ifr_flags & IFF_RUNNING){
1864
1377
        break;
1865
1378
      }
1866
1379
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1867
1380
      ret = nanosleep(&sleeptime, NULL);
1868
1381
      if(ret == -1 and errno != EINTR){
1869
 
        perror_plus("nanosleep");
 
1382
        perror("nanosleep");
1870
1383
      }
1871
1384
    }
1872
1385
    if(not take_down_interface){
1873
1386
      /* We won't need the socket anymore */
1874
1387
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1875
1388
      if(ret == -1){
1876
 
        perror_plus("close");
 
1389
        perror("close");
1877
1390
      }
1878
1391
    }
1879
1392
#ifdef __linux__
1881
1394
      /* Restores kernel loglevel to default */
1882
1395
      ret = klogctl(7, NULL, 0);
1883
1396
      if(ret == -1){
1884
 
        perror_plus("klogctl");
 
1397
        perror("klogctl");
1885
1398
      }
1886
1399
    }
1887
1400
#endif  /* __linux__ */
1891
1404
      /* Lower privileges */
1892
1405
      ret = seteuid(uid);
1893
1406
      if(ret == -1){
1894
 
        perror_plus("seteuid");
 
1407
        perror("seteuid");
1895
1408
      }
1896
1409
    } else {
1897
1410
      /* Lower privileges permanently */
1898
1411
      ret = setuid(uid);
1899
1412
      if(ret == -1){
1900
 
        perror_plus("setuid");
 
1413
        perror("setuid");
1901
1414
      }
1902
1415
    }
1903
1416
  }
1919
1432
    goto end;
1920
1433
  }
1921
1434
  
 
1435
  tempdir_created = true;
1922
1436
  if(mkdtemp(tempdir) == NULL){
1923
 
    perror_plus("mkdtemp");
 
1437
    tempdir_created = false;
 
1438
    perror("mkdtemp");
1924
1439
    goto end;
1925
1440
  }
1926
 
  tempdir_created = true;
1927
1441
  
1928
1442
  if(quit_now){
1929
1443
    goto end;
1971
1485
    
1972
1486
    port = (uint16_t)tmpmax;
1973
1487
    *address = '\0';
 
1488
    address = connect_to;
1974
1489
    /* Colon in address indicates IPv6 */
1975
1490
    int af;
1976
 
    if(strchr(connect_to, ':') != NULL){
 
1491
    if(strchr(address, ':') != NULL){
1977
1492
      af = AF_INET6;
1978
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1979
 
      if(connect_to[0] == '[' and address[-1] == ']')
1980
 
        {
1981
 
          connect_to++;
1982
 
          address[-1] = '\0';
1983
 
        }
1984
1493
    } else {
1985
1494
      af = AF_INET;
1986
1495
    }
1987
 
    address = connect_to;
1988
1496
    
1989
1497
    if(quit_now){
1990
1498
      goto end;
1991
1499
    }
1992
1500
    
1993
 
    while(not quit_now){
1994
 
      ret = start_mandos_communication(address, port, if_index, af);
1995
 
      if(quit_now or ret == 0){
1996
 
        break;
1997
 
      }
1998
 
      if(debug){
1999
 
        fprintf(stderr, "Retrying in %d seconds\n",
2000
 
                (int)retry_interval);
2001
 
      }
2002
 
      sleep((int)retry_interval);
2003
 
    }
2004
 
    
2005
 
    if (not quit_now){
 
1501
    ret = start_mandos_communication(address, port, if_index, af);
 
1502
    if(ret < 0){
 
1503
      switch(errno){
 
1504
      case ENETUNREACH:
 
1505
      case EHOSTDOWN:
 
1506
      case EHOSTUNREACH:
 
1507
        exitcode = EX_NOHOST;
 
1508
        break;
 
1509
      case EINVAL:
 
1510
        exitcode = EX_USAGE;
 
1511
        break;
 
1512
      case EIO:
 
1513
        exitcode = EX_IOERR;
 
1514
        break;
 
1515
      case EPROTO:
 
1516
        exitcode = EX_PROTOCOL;
 
1517
        break;
 
1518
      default:
 
1519
        exitcode = EX_OSERR;
 
1520
        break;
 
1521
      }
 
1522
    } else {
2006
1523
      exitcode = EXIT_SUCCESS;
2007
1524
    }
2008
 
    
2009
1525
    goto end;
2010
1526
  }
2011
1527
  
2063
1579
  if(debug){
2064
1580
    fprintf(stderr, "Starting Avahi loop search\n");
2065
1581
  }
2066
 
 
2067
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2068
 
                                (int)(retry_interval * 1000));
2069
 
  if(debug){
2070
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2071
 
            (ret == 0) ? "successfully" : "with error");
2072
 
  }
 
1582
  
 
1583
  avahi_simple_poll_loop(mc.simple_poll);
2073
1584
  
2074
1585
 end:
2075
1586
  
2096
1607
  if(gpgme_initialized){
2097
1608
    gpgme_release(mc.ctx);
2098
1609
  }
2099
 
 
2100
 
  /* Cleans up the circular linked list of Mandos servers the client
2101
 
     has seen */
2102
 
  if(mc.current_server != NULL){
2103
 
    mc.current_server->prev->next = NULL;
2104
 
    while(mc.current_server != NULL){
2105
 
      server *next = mc.current_server->next;
2106
 
      free(mc.current_server);
2107
 
      mc.current_server = next;
2108
 
    }
2109
 
  }
2110
 
  
2111
 
  /* XXX run network hooks "stop" here  */
2112
1610
  
2113
1611
  /* Take down the network interface */
2114
1612
  if(take_down_interface){
2116
1614
    errno = 0;
2117
1615
    ret = seteuid(0);
2118
1616
    if(ret == -1){
2119
 
      perror_plus("seteuid");
 
1617
      perror("seteuid");
2120
1618
    }
2121
1619
    if(geteuid() == 0){
2122
1620
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2123
1621
      if(ret == -1){
2124
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1622
        perror("ioctl SIOCGIFFLAGS");
2125
1623
      } else if(network.ifr_flags & IFF_UP) {
2126
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2127
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2128
1626
        if(ret == -1){
2129
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1627
          perror("ioctl SIOCSIFFLAGS");
2130
1628
        }
2131
1629
      }
2132
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2133
1631
      if(ret == -1){
2134
 
        perror_plus("close");
 
1632
        perror("close");
2135
1633
      }
2136
1634
      /* Lower privileges permanently */
2137
1635
      errno = 0;
2138
1636
      ret = setuid(uid);
2139
1637
      if(ret == -1){
2140
 
        perror_plus("setuid");
 
1638
        perror("setuid");
2141
1639
      }
2142
1640
    }
2143
1641
  }
2144
1642
  
2145
 
  /* Removes the GPGME temp directory and all files inside */
 
1643
  /* Removes the temp directory used by GPGME */
2146
1644
  if(tempdir_created){
2147
 
    struct dirent **direntries = NULL;
2148
 
    struct dirent *direntry = NULL;
2149
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2150
 
                             alphasort);
2151
 
    if (numentries > 0){
2152
 
      for(int i = 0; i < numentries; i++){
2153
 
        direntry = direntries[i];
 
1645
    DIR *d;
 
1646
    struct dirent *direntry;
 
1647
    d = opendir(tempdir);
 
1648
    if(d == NULL){
 
1649
      if(errno != ENOENT){
 
1650
        perror("opendir");
 
1651
      }
 
1652
    } else {
 
1653
      while(true){
 
1654
        direntry = readdir(d);
 
1655
        if(direntry == NULL){
 
1656
          break;
 
1657
        }
 
1658
        /* Skip "." and ".." */
 
1659
        if(direntry->d_name[0] == '.'
 
1660
           and (direntry->d_name[1] == '\0'
 
1661
                or (direntry->d_name[1] == '.'
 
1662
                    and direntry->d_name[2] == '\0'))){
 
1663
          continue;
 
1664
        }
2154
1665
        char *fullname = NULL;
2155
1666
        ret = asprintf(&fullname, "%s/%s", tempdir,
2156
1667
                       direntry->d_name);
2157
1668
        if(ret < 0){
2158
 
          perror_plus("asprintf");
 
1669
          perror("asprintf");
2159
1670
          continue;
2160
1671
        }
2161
1672
        ret = remove(fullname);
2165
1676
        }
2166
1677
        free(fullname);
2167
1678
      }
2168
 
    }
2169
 
 
2170
 
    /* need to clean even if 0 because man page doesn't specify */
2171
 
    free(direntries);
2172
 
    if (numentries == -1){
2173
 
      perror_plus("scandir");
 
1679
      closedir(d);
2174
1680
    }
2175
1681
    ret = rmdir(tempdir);
2176
1682
    if(ret == -1 and errno != ENOENT){
2177
 
      perror_plus("rmdir");
 
1683
      perror("rmdir");
2178
1684
    }
2179
1685
  }
2180
1686
  
2185
1691
                                            &old_sigterm_action,
2186
1692
                                            NULL));
2187
1693
    if(ret == -1){
2188
 
      perror_plus("sigaction");
 
1694
      perror("sigaction");
2189
1695
    }
2190
1696
    do {
2191
1697
      ret = raise(signal_received);
2192
1698
    } while(ret != 0 and errno == EINTR);
2193
1699
    if(ret != 0){
2194
 
      perror_plus("raise");
 
1700
      perror("raise");
2195
1701
      abort();
2196
1702
    }
2197
1703
    TEMP_FAILURE_RETRY(pause());