/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

* plugins.d/mandos-client.c (good_interface): Add error message.
  (up_interface): Use get_flags() and good_flags().  Also check the
                  IFF_RUNNING flags.
  (runnable_hook): Bug fix: Add NULL to execl() call.
  (main): Use network interfaces which are up and running in
          preference to any other interfaces.

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-2010 Teddy Hogeborn
13
 
 * Copyright © 2008-2010 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 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@fukt.bsnet.se>.
 
29
 * Contact the authors at <mandos@recompile.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() */
 
56
#include <sys/stat.h>           /* open(), S_ISREG */
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 */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
66
67
#include <time.h>               /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
                                   SIOCSIFFLAGS, if_indextoname(),
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
122
123
#define PATHDIR "/conf/conf.d/mandos"
123
124
#define SECKEY "seckey.txt"
124
125
#define PUBKEY "pubkey.txt"
 
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
125
127
 
126
128
bool debug = false;
127
129
static const char mandos_protocol_version[] = "1";
128
130
const char *argp_program_version = "mandos-client " VERSION;
129
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
130
132
static const char sys_class_net[] = "/sys/class/net";
131
133
char *connect_to = NULL;
132
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;
 
145
 
133
146
/* Used for passing in values through the Avahi callback functions */
134
147
typedef struct {
135
148
  AvahiSimplePoll *simple_poll;
139
152
  gnutls_dh_params_t dh_params;
140
153
  const char *priority;
141
154
  gpgme_ctx_t ctx;
 
155
  server *current_server;
142
156
} mandos_context;
143
157
 
144
158
/* global context so signal handler can reach it*/
145
159
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
160
                      .dh_bits = 1024, .priority = "SECURE256"
147
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
161
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
162
                      .current_server = NULL };
148
163
 
149
164
sig_atomic_t quit_now = 0;
150
165
int signal_received = 0;
151
166
 
 
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
 
152
174
/*
153
175
 * Make additional room in "buffer" for at least BUFFER_SIZE more
154
176
 * bytes. "buffer_capacity" is how much is currently allocated,
166
188
  return buffer_capacity;
167
189
}
168
190
 
 
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
 
169
229
/* 
170
230
 * Initialize GPGME.
171
231
 */
185
245
    
186
246
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
187
247
    if(fd == -1){
188
 
      perror("open");
 
248
      perror_plus("open");
189
249
      return false;
190
250
    }
191
251
    
205
265
    
206
266
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
207
267
    if(ret == -1){
208
 
      perror("close");
 
268
      perror_plus("close");
209
269
    }
210
270
    gpgme_data_release(pgp_data);
211
271
    return true;
224
284
    return false;
225
285
  }
226
286
  
227
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
287
  /* Set GPGME home directory for the OpenPGP engine only */
228
288
  rc = gpgme_get_engine_info(&engine_info);
229
289
  if(rc != GPG_ERR_NO_ERROR){
230
290
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
336
396
  
337
397
  /* Seek back to the beginning of the GPGME plaintext data buffer */
338
398
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
339
 
    perror("gpgme_data_seek");
 
399
    perror_plus("gpgme_data_seek");
340
400
    plaintext_length = -1;
341
401
    goto decrypt_end;
342
402
  }
347
407
                                      (size_t)plaintext_length,
348
408
                                      plaintext_capacity);
349
409
    if(plaintext_capacity == 0){
350
 
        perror("incbuffer");
 
410
        perror_plus("incbuffer");
351
411
        plaintext_length = -1;
352
412
        goto decrypt_end;
353
413
    }
360
420
      break;
361
421
    }
362
422
    if(ret < 0){
363
 
      perror("gpgme_data_read");
 
423
      perror_plus("gpgme_data_read");
364
424
      plaintext_length = -1;
365
425
      goto decrypt_end;
366
426
    }
423
483
  }
424
484
  
425
485
  /* OpenPGP credentials */
426
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
486
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
487
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
429
 
                                                    from
430
 
                                                    -Wunreachable-code
431
 
                                                 */
 
488
    fprintf(stderr, "GnuTLS memory error: %s\n",
432
489
            safer_gnutls_strerror(ret));
433
490
    gnutls_global_deinit();
434
491
    return -1;
589
646
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
647
  if(tcp_sd < 0){
591
648
    int e = errno;
592
 
    perror("socket");
 
649
    perror_plus("socket");
593
650
    errno = e;
594
651
    goto mandos_end;
595
652
  }
609
666
  }
610
667
  if(ret < 0 ){
611
668
    int e = errno;
612
 
    perror("inet_pton");
 
669
    perror_plus("inet_pton");
613
670
    errno = e;
614
671
    goto mandos_end;
615
672
  }
651
708
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
709
      char interface[IF_NAMESIZE];
653
710
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
 
        perror("if_indextoname");
 
711
        perror_plus("if_indextoname");
655
712
      } else {
656
713
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
714
                ip, interface, port);
671
728
                        sizeof(addrstr));
672
729
    }
673
730
    if(pcret == NULL){
674
 
      perror("inet_ntop");
 
731
      perror_plus("inet_ntop");
675
732
    } else {
676
733
      if(strcmp(addrstr, ip) != 0){
677
734
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
692
749
  if(ret < 0){
693
750
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
751
      int e = errno;
695
 
      perror("connect");
 
752
      perror_plus("connect");
696
753
      errno = e;
697
754
    }
698
755
    goto mandos_end;
711
768
                                   out_size - written));
712
769
    if(ret == -1){
713
770
      int e = errno;
714
 
      perror("write");
 
771
      perror_plus("write");
715
772
      errno = e;
716
773
      goto mandos_end;
717
774
    }
742
799
    goto mandos_end;
743
800
  }
744
801
  
 
802
  /* Spurious warning from -Wint-to-pointer-cast */
745
803
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
746
804
  
747
805
  if(quit_now){
784
842
                                   buffer_capacity);
785
843
    if(buffer_capacity == 0){
786
844
      int e = errno;
787
 
      perror("incbuffer");
 
845
      perror_plus("incbuffer");
788
846
      errno = e;
789
847
      goto mandos_end;
790
848
    }
895
953
      if(e == 0){
896
954
        e = errno;
897
955
      }
898
 
      perror("close");
 
956
      perror_plus("close");
899
957
    }
900
958
    gnutls_deinit(session);
 
959
    errno = e;
901
960
    if(quit_now){
902
 
      e = EINTR;
 
961
      errno = EINTR;
903
962
      retval = -1;
904
963
    }
905
 
    errno = e;
906
964
  }
907
965
  return retval;
908
966
}
951
1009
                                           avahi_proto_to_af(proto));
952
1010
      if(ret == 0){
953
1011
        avahi_simple_poll_quit(mc.simple_poll);
 
1012
      } else {
 
1013
        ret = add_server(ip, port, interface,
 
1014
                         avahi_proto_to_af(proto));
954
1015
      }
955
1016
    }
956
1017
  }
1010
1071
  }
1011
1072
}
1012
1073
 
1013
 
/* stop main loop after sigterm has been called */
 
1074
/* Signal handler that stops main loop after SIGTERM */
1014
1075
static void handle_sigterm(int sig){
1015
1076
  if(quit_now){
1016
1077
    return;
1018
1079
  quit_now = 1;
1019
1080
  signal_received = sig;
1020
1081
  int old_errno = errno;
 
1082
  /* set main loop to exit */
1021
1083
  if(mc.simple_poll != NULL){
1022
1084
    avahi_simple_poll_quit(mc.simple_poll);
1023
1085
  }
1024
1086
  errno = old_errno;
1025
1087
}
1026
1088
 
1027
 
/* 
1028
 
 * This function determines if a directory entry in /sys/class/net
1029
 
 * corresponds to an acceptable network device.
1030
 
 * (This function is passed to scandir(3) as a filter function.)
1031
 
 */
1032
 
int good_interface(const struct dirent *if_entry){
1033
 
  ssize_t ssret;
1034
 
  char *flagname = NULL;
1035
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
 
                     if_entry->d_name);
1037
 
  if(ret < 0){
1038
 
    perror("asprintf");
1039
 
    return 0;
1040
 
  }
1041
 
  if(if_entry->d_name[0] == '.'){
1042
 
    return 0;
1043
 
  }
1044
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
 
  if(flags_fd == -1){
1046
 
    perror("open");
1047
 
    return 0;
1048
 
  }
1049
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1050
 
  /* read line from flags_fd */
1051
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
 
  flagstring[(size_t)to_read] = '\0';
1054
 
  if(flagstring == NULL){
1055
 
    perror("malloc");
1056
 
    close(flags_fd);
1057
 
    return 0;
1058
 
  }
1059
 
  while(to_read > 0){
1060
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1061
 
                                             (size_t)to_read));
1062
 
    if(ssret == -1){
1063
 
      perror("read");
1064
 
      free(flagstring);
1065
 
      close(flags_fd);
1066
 
      return 0;
1067
 
    }
1068
 
    to_read -= ssret;
1069
 
    if(ssret == 0){
1070
 
      break;
1071
 
    }
1072
 
  }
1073
 
  close(flags_fd);
1074
 
  intmax_t tmpmax;
1075
 
  char *tmp;
1076
 
  errno = 0;
1077
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1078
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
 
                                         and not (isspace(*tmp)))
1080
 
     or tmpmax != (ifreq_flags)tmpmax){
 
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){
1081
1100
    if(debug){
1082
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
 
              flagstring, if_entry->d_name);
 
1101
      perror_plus("ioctl SIOCGIFFLAGS");
1084
1102
    }
1085
 
    free(flagstring);
1086
 
    return 0;
 
1103
    return false;
1087
1104
  }
1088
 
  free(flagstring);
1089
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1105
  return true;
 
1106
}
 
1107
 
 
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1109
  
1090
1110
  /* Reject the loopback device */
1091
 
  if(flags & IFF_LOOPBACK){
 
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
1092
1112
    if(debug){
1093
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1094
 
              if_entry->d_name);
 
1114
              ifname);
1095
1115
    }
1096
 
    return 0;
 
1116
    return false;
1097
1117
  }
1098
1118
  /* Accept point-to-point devices only if connect_to is specified */
1099
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1100
1120
    if(debug){
1101
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1102
 
              if_entry->d_name);
 
1122
              ifname);
1103
1123
    }
1104
 
    return 1;
 
1124
    return true;
1105
1125
  }
1106
1126
  /* Otherwise, reject non-broadcast-capable devices */
1107
 
  if(not (flags & IFF_BROADCAST)){
 
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1108
1128
    if(debug){
1109
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1110
 
              if_entry->d_name);
1111
 
    }
1112
 
    return 0;
1113
 
  }
 
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
  
1114
1142
  /* Accept this device */
1115
1143
  if(debug){
1116
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1117
 
            if_entry->d_name);
1118
 
  }
1119
 
  return 1;
 
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
  }
1120
1341
}
1121
1342
 
1122
1343
int main(int argc, char *argv[]){
1141
1362
  bool gnutls_initialized = false;
1142
1363
  bool gpgme_initialized = false;
1143
1364
  float delay = 2.5f;
 
1365
  double retry_interval = 10; /* 10s between trying a server and
 
1366
                                 retrying the same server again */
1144
1367
  
1145
1368
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1146
1369
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1152
1375
  errno = 0;
1153
1376
  ret = setgid(gid);
1154
1377
  if(ret == -1){
1155
 
    perror("setgid");
 
1378
    perror_plus("setgid");
1156
1379
  }
1157
1380
  
1158
1381
  /* Lower user privileges (temporarily) */
1159
1382
  errno = 0;
1160
1383
  ret = seteuid(uid);
1161
1384
  if(ret == -1){
1162
 
    perror("seteuid");
 
1385
    perror_plus("seteuid");
1163
1386
  }
1164
1387
  
1165
1388
  if(quit_now){
1200
1423
        .arg = "SECONDS",
1201
1424
        .doc = "Maximum delay to wait for interface startup",
1202
1425
        .group = 2 },
 
1426
      { .name = "retry", .key = 132,
 
1427
        .arg = "SECONDS",
 
1428
        .doc = "Retry interval used when denied by the mandos server",
 
1429
        .group = 2 },
1203
1430
      /*
1204
1431
       * These reproduce what we would get without ARGP_NO_HELP
1205
1432
       */
1249
1476
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1250
1477
          argp_error(state, "Bad delay");
1251
1478
        }
 
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
        }
1252
1487
        break;
1253
1488
        /*
1254
1489
         * These reproduce what we would get without ARGP_NO_HELP
1282
1517
    case ENOMEM:
1283
1518
    default:
1284
1519
      errno = ret;
1285
 
      perror("argp_parse");
 
1520
      perror_plus("argp_parse");
1286
1521
      exitcode = EX_OSERR;
1287
1522
      goto end;
1288
1523
    case EINVAL:
1290
1525
      goto end;
1291
1526
    }
1292
1527
  }
 
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
        setenv("DEVICE", interface, 1);
 
1612
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1613
        setenv("MODE", "start", 1);
 
1614
        /* setenv( XXX more here */
 
1615
        ret = execl(fullname, direntry->d_name, "start", NULL);
 
1616
        perror_plus("execl");
 
1617
      }
 
1618
      free(fullname);
 
1619
      if(quit_now){
 
1620
        goto end;
 
1621
      }
 
1622
    }
 
1623
    close(devnull);
 
1624
  }
1293
1625
  
1294
1626
  if(not debug){
1295
1627
    avahi_set_log_function(empty_log);
1296
1628
  }
1297
 
 
 
1629
  
1298
1630
  if(interface[0] == '\0'){
1299
1631
    struct dirent **direntries;
1300
 
    ret = scandir(sys_class_net, &direntries, good_interface,
 
1632
    /* First look for interfaces that are up */
 
1633
    ret = scandir(sys_class_net, &direntries, up_interface,
1301
1634
                  alphasort);
 
1635
    if(ret == 0){
 
1636
      /* No up interfaces, look for any good interfaces */
 
1637
      free(direntries);
 
1638
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1639
                    alphasort);
 
1640
    }
1302
1641
    if(ret >= 1){
1303
 
      /* Pick the first good interface */
 
1642
      /* Pick the first interface returned */
1304
1643
      interface = strdup(direntries[0]->d_name);
1305
1644
      if(debug){
1306
1645
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1307
1646
      }
1308
1647
      if(interface == NULL){
1309
 
        perror("malloc");
 
1648
        perror_plus("malloc");
1310
1649
        free(direntries);
1311
1650
        exitcode = EXIT_FAILURE;
1312
1651
        goto end;
1334
1673
  sigemptyset(&sigterm_action.sa_mask);
1335
1674
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1336
1675
  if(ret == -1){
1337
 
    perror("sigaddset");
 
1676
    perror_plus("sigaddset");
1338
1677
    exitcode = EX_OSERR;
1339
1678
    goto end;
1340
1679
  }
1341
1680
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1342
1681
  if(ret == -1){
1343
 
    perror("sigaddset");
 
1682
    perror_plus("sigaddset");
1344
1683
    exitcode = EX_OSERR;
1345
1684
    goto end;
1346
1685
  }
1347
1686
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1348
1687
  if(ret == -1){
1349
 
    perror("sigaddset");
 
1688
    perror_plus("sigaddset");
1350
1689
    exitcode = EX_OSERR;
1351
1690
    goto end;
1352
1691
  }
1356
1695
  */
1357
1696
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1358
1697
  if(ret == -1){
1359
 
    perror("sigaction");
 
1698
    perror_plus("sigaction");
1360
1699
    return EX_OSERR;
1361
1700
  }
1362
1701
  if(old_sigterm_action.sa_handler != SIG_IGN){
1363
1702
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1364
1703
    if(ret == -1){
1365
 
      perror("sigaction");
 
1704
      perror_plus("sigaction");
1366
1705
      exitcode = EX_OSERR;
1367
1706
      goto end;
1368
1707
    }
1369
1708
  }
1370
1709
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1371
1710
  if(ret == -1){
1372
 
    perror("sigaction");
 
1711
    perror_plus("sigaction");
1373
1712
    return EX_OSERR;
1374
1713
  }
1375
1714
  if(old_sigterm_action.sa_handler != SIG_IGN){
1376
1715
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1377
1716
    if(ret == -1){
1378
 
      perror("sigaction");
 
1717
      perror_plus("sigaction");
1379
1718
      exitcode = EX_OSERR;
1380
1719
      goto end;
1381
1720
    }
1382
1721
  }
1383
1722
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1384
1723
  if(ret == -1){
1385
 
    perror("sigaction");
 
1724
    perror_plus("sigaction");
1386
1725
    return EX_OSERR;
1387
1726
  }
1388
1727
  if(old_sigterm_action.sa_handler != SIG_IGN){
1389
1728
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1390
1729
    if(ret == -1){
1391
 
      perror("sigaction");
 
1730
      perror_plus("sigaction");
1392
1731
      exitcode = EX_OSERR;
1393
1732
      goto end;
1394
1733
    }
1411
1750
    errno = 0;
1412
1751
    ret = seteuid(0);
1413
1752
    if(ret == -1){
1414
 
      perror("seteuid");
 
1753
      perror_plus("seteuid");
1415
1754
    }
1416
1755
    
1417
1756
#ifdef __linux__
1421
1760
    bool restore_loglevel = true;
1422
1761
    if(ret == -1){
1423
1762
      restore_loglevel = false;
1424
 
      perror("klogctl");
 
1763
      perror_plus("klogctl");
1425
1764
    }
1426
1765
#endif  /* __linux__ */
1427
1766
    
1428
1767
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1429
1768
    if(sd < 0){
1430
 
      perror("socket");
 
1769
      perror_plus("socket");
1431
1770
      exitcode = EX_OSERR;
1432
1771
#ifdef __linux__
1433
1772
      if(restore_loglevel){
1434
1773
        ret = klogctl(7, NULL, 0);
1435
1774
        if(ret == -1){
1436
 
          perror("klogctl");
 
1775
          perror_plus("klogctl");
1437
1776
        }
1438
1777
      }
1439
1778
#endif  /* __linux__ */
1441
1780
      errno = 0;
1442
1781
      ret = seteuid(uid);
1443
1782
      if(ret == -1){
1444
 
        perror("seteuid");
 
1783
        perror_plus("seteuid");
1445
1784
      }
1446
1785
      goto end;
1447
1786
    }
1448
1787
    strcpy(network.ifr_name, interface);
1449
1788
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1450
1789
    if(ret == -1){
1451
 
      perror("ioctl SIOCGIFFLAGS");
 
1790
      perror_plus("ioctl SIOCGIFFLAGS");
1452
1791
#ifdef __linux__
1453
1792
      if(restore_loglevel){
1454
1793
        ret = klogctl(7, NULL, 0);
1455
1794
        if(ret == -1){
1456
 
          perror("klogctl");
 
1795
          perror_plus("klogctl");
1457
1796
        }
1458
1797
      }
1459
1798
#endif  /* __linux__ */
1462
1801
      errno = 0;
1463
1802
      ret = seteuid(uid);
1464
1803
      if(ret == -1){
1465
 
        perror("seteuid");
 
1804
        perror_plus("seteuid");
1466
1805
      }
1467
1806
      goto end;
1468
1807
    }
1472
1811
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1473
1812
      if(ret == -1){
1474
1813
        take_down_interface = false;
1475
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1814
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1476
1815
        exitcode = EX_OSERR;
1477
1816
#ifdef __linux__
1478
1817
        if(restore_loglevel){
1479
1818
          ret = klogctl(7, NULL, 0);
1480
1819
          if(ret == -1){
1481
 
            perror("klogctl");
 
1820
            perror_plus("klogctl");
1482
1821
          }
1483
1822
        }
1484
1823
#endif  /* __linux__ */
1486
1825
        errno = 0;
1487
1826
        ret = seteuid(uid);
1488
1827
        if(ret == -1){
1489
 
          perror("seteuid");
 
1828
          perror_plus("seteuid");
1490
1829
        }
1491
1830
        goto end;
1492
1831
      }
1493
1832
    }
1494
 
    /* sleep checking until interface is running */
 
1833
    /* Sleep checking until interface is running.
 
1834
       Check every 0.25s, up to total time of delay */
1495
1835
    for(int i=0; i < delay * 4; i++){
1496
1836
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1497
1837
      if(ret == -1){
1498
 
        perror("ioctl SIOCGIFFLAGS");
 
1838
        perror_plus("ioctl SIOCGIFFLAGS");
1499
1839
      } else if(network.ifr_flags & IFF_RUNNING){
1500
1840
        break;
1501
1841
      }
1502
1842
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1503
1843
      ret = nanosleep(&sleeptime, NULL);
1504
1844
      if(ret == -1 and errno != EINTR){
1505
 
        perror("nanosleep");
 
1845
        perror_plus("nanosleep");
1506
1846
      }
1507
1847
    }
1508
1848
    if(not take_down_interface){
1509
1849
      /* We won't need the socket anymore */
1510
1850
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1511
1851
      if(ret == -1){
1512
 
        perror("close");
 
1852
        perror_plus("close");
1513
1853
      }
1514
1854
    }
1515
1855
#ifdef __linux__
1517
1857
      /* Restores kernel loglevel to default */
1518
1858
      ret = klogctl(7, NULL, 0);
1519
1859
      if(ret == -1){
1520
 
        perror("klogctl");
 
1860
        perror_plus("klogctl");
1521
1861
      }
1522
1862
    }
1523
1863
#endif  /* __linux__ */
1527
1867
      /* Lower privileges */
1528
1868
      ret = seteuid(uid);
1529
1869
      if(ret == -1){
1530
 
        perror("seteuid");
 
1870
        perror_plus("seteuid");
1531
1871
      }
1532
1872
    } else {
1533
1873
      /* Lower privileges permanently */
1534
1874
      ret = setuid(uid);
1535
1875
      if(ret == -1){
1536
 
        perror("setuid");
 
1876
        perror_plus("setuid");
1537
1877
      }
1538
1878
    }
1539
1879
  }
1555
1895
    goto end;
1556
1896
  }
1557
1897
  
 
1898
  if(mkdtemp(tempdir) == NULL){
 
1899
    perror_plus("mkdtemp");
 
1900
    goto end;
 
1901
  }
1558
1902
  tempdir_created = true;
1559
 
  if(mkdtemp(tempdir) == NULL){
1560
 
    tempdir_created = false;
1561
 
    perror("mkdtemp");
1562
 
    goto end;
1563
 
  }
1564
1903
  
1565
1904
  if(quit_now){
1566
1905
    goto end;
1608
1947
    
1609
1948
    port = (uint16_t)tmpmax;
1610
1949
    *address = '\0';
1611
 
    address = connect_to;
1612
1950
    /* Colon in address indicates IPv6 */
1613
1951
    int af;
1614
 
    if(strchr(address, ':') != NULL){
 
1952
    if(strchr(connect_to, ':') != NULL){
1615
1953
      af = AF_INET6;
 
1954
      /* Accept [] around IPv6 address - see RFC 5952 */
 
1955
      if(connect_to[0] == '[' and address[-1] == ']')
 
1956
        {
 
1957
          connect_to++;
 
1958
          address[-1] = '\0';
 
1959
        }
1616
1960
    } else {
1617
1961
      af = AF_INET;
1618
1962
    }
 
1963
    address = connect_to;
1619
1964
    
1620
1965
    if(quit_now){
1621
1966
      goto end;
1622
1967
    }
1623
 
 
 
1968
    
1624
1969
    while(not quit_now){
1625
1970
      ret = start_mandos_communication(address, port, if_index, af);
1626
1971
      if(quit_now or ret == 0){
1627
1972
        break;
1628
1973
      }
1629
 
      sleep(15);
1630
 
    };
1631
 
 
 
1974
      if(debug){
 
1975
        fprintf(stderr, "Retrying in %d seconds\n",
 
1976
                (int)retry_interval);
 
1977
      }
 
1978
      sleep((int)retry_interval);
 
1979
    }
 
1980
    
1632
1981
    if (not quit_now){
1633
1982
      exitcode = EXIT_SUCCESS;
1634
1983
    }
1635
 
 
 
1984
    
1636
1985
    goto end;
1637
1986
  }
1638
1987
  
1690
2039
  if(debug){
1691
2040
    fprintf(stderr, "Starting Avahi loop search\n");
1692
2041
  }
1693
 
  
1694
 
  avahi_simple_poll_loop(mc.simple_poll);
 
2042
 
 
2043
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
2044
                                (int)(retry_interval * 1000));
 
2045
  if(debug){
 
2046
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
2047
            (ret == 0) ? "successfully" : "with error");
 
2048
  }
1695
2049
  
1696
2050
 end:
1697
2051
  
1718
2072
  if(gpgme_initialized){
1719
2073
    gpgme_release(mc.ctx);
1720
2074
  }
 
2075
 
 
2076
  /* Cleans up the circular linked list of Mandos servers the client
 
2077
     has seen */
 
2078
  if(mc.current_server != NULL){
 
2079
    mc.current_server->prev->next = NULL;
 
2080
    while(mc.current_server != NULL){
 
2081
      server *next = mc.current_server->next;
 
2082
      free(mc.current_server);
 
2083
      mc.current_server = next;
 
2084
    }
 
2085
  }
 
2086
  
 
2087
  /* XXX run network hooks "stop" here  */
1721
2088
  
1722
2089
  /* Take down the network interface */
1723
2090
  if(take_down_interface){
1725
2092
    errno = 0;
1726
2093
    ret = seteuid(0);
1727
2094
    if(ret == -1){
1728
 
      perror("seteuid");
 
2095
      perror_plus("seteuid");
1729
2096
    }
1730
2097
    if(geteuid() == 0){
1731
2098
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1732
2099
      if(ret == -1){
1733
 
        perror("ioctl SIOCGIFFLAGS");
 
2100
        perror_plus("ioctl SIOCGIFFLAGS");
1734
2101
      } else if(network.ifr_flags & IFF_UP) {
1735
2102
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1736
2103
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1737
2104
        if(ret == -1){
1738
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
2105
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1739
2106
        }
1740
2107
      }
1741
2108
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1742
2109
      if(ret == -1){
1743
 
        perror("close");
 
2110
        perror_plus("close");
1744
2111
      }
1745
2112
      /* Lower privileges permanently */
1746
2113
      errno = 0;
1747
2114
      ret = setuid(uid);
1748
2115
      if(ret == -1){
1749
 
        perror("setuid");
 
2116
        perror_plus("setuid");
1750
2117
      }
1751
2118
    }
1752
2119
  }
1753
2120
  
1754
 
  /* Removes the temp directory used by GPGME */
 
2121
  /* Removes the GPGME temp directory and all files inside */
1755
2122
  if(tempdir_created){
1756
 
    DIR *d;
1757
 
    struct dirent *direntry;
1758
 
    d = opendir(tempdir);
1759
 
    if(d == NULL){
1760
 
      if(errno != ENOENT){
1761
 
        perror("opendir");
1762
 
      }
1763
 
    } else {
1764
 
      while(true){
1765
 
        direntry = readdir(d);
1766
 
        if(direntry == NULL){
1767
 
          break;
1768
 
        }
1769
 
        /* Skip "." and ".." */
1770
 
        if(direntry->d_name[0] == '.'
1771
 
           and (direntry->d_name[1] == '\0'
1772
 
                or (direntry->d_name[1] == '.'
1773
 
                    and direntry->d_name[2] == '\0'))){
1774
 
          continue;
1775
 
        }
 
2123
    struct dirent **direntries = NULL;
 
2124
    struct dirent *direntry = NULL;
 
2125
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2126
                             alphasort);
 
2127
    if (numentries > 0){
 
2128
      for(int i = 0; i < numentries; i++){
 
2129
        direntry = direntries[i];
1776
2130
        char *fullname = NULL;
1777
2131
        ret = asprintf(&fullname, "%s/%s", tempdir,
1778
2132
                       direntry->d_name);
1779
2133
        if(ret < 0){
1780
 
          perror("asprintf");
 
2134
          perror_plus("asprintf");
1781
2135
          continue;
1782
2136
        }
1783
2137
        ret = remove(fullname);
1787
2141
        }
1788
2142
        free(fullname);
1789
2143
      }
1790
 
      closedir(d);
 
2144
    }
 
2145
 
 
2146
    /* need to clean even if 0 because man page doesn't specify */
 
2147
    free(direntries);
 
2148
    if (numentries == -1){
 
2149
      perror_plus("scandir");
1791
2150
    }
1792
2151
    ret = rmdir(tempdir);
1793
2152
    if(ret == -1 and errno != ENOENT){
1794
 
      perror("rmdir");
 
2153
      perror_plus("rmdir");
1795
2154
    }
1796
2155
  }
1797
2156
  
1802
2161
                                            &old_sigterm_action,
1803
2162
                                            NULL));
1804
2163
    if(ret == -1){
1805
 
      perror("sigaction");
 
2164
      perror_plus("sigaction");
1806
2165
    }
1807
2166
    do {
1808
2167
      ret = raise(signal_received);
1809
2168
    } while(ret != 0 and errno == EINTR);
1810
2169
    if(ret != 0){
1811
 
      perror("raise");
 
2170
      perror_plus("raise");
1812
2171
      abort();
1813
2172
    }
1814
2173
    TEMP_FAILURE_RETRY(pause());