/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

merge

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
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 */
 
65
#include <errno.h>              /* perror(), errno */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
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,
131
130
static const char sys_class_net[] = "/sys/class/net";
132
131
char *connect_to = NULL;
133
132
 
134
 
/* Doubly linked list that need to be circularly linked when used */
135
 
typedef struct server{
136
 
  const char *ip;
137
 
  uint16_t port;
138
 
  AvahiIfIndex if_index;
139
 
  int af;
140
 
  struct timespec last_seen;
141
 
  struct server *next;
142
 
  struct server *prev;
143
 
} server;
144
 
 
145
133
/* Used for passing in values through the Avahi callback functions */
146
134
typedef struct {
147
135
  AvahiSimplePoll *simple_poll;
151
139
  gnutls_dh_params_t dh_params;
152
140
  const char *priority;
153
141
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
142
} mandos_context;
156
143
 
157
144
/* global context so signal handler can reach it*/
158
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
146
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
148
 
163
149
sig_atomic_t quit_now = 0;
164
150
int signal_received = 0;
165
151
 
166
 
/* Function to use when printing errors */
167
 
void perror_plus(const char *print_text){
168
 
  fprintf(stderr, "Mandos plugin %s: ",
169
 
          program_invocation_short_name);
170
 
  perror(print_text);
171
 
}
172
 
 
173
152
/*
174
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
154
 * bytes. "buffer_capacity" is how much is currently allocated,
187
166
  return buffer_capacity;
188
167
}
189
168
 
190
 
int add_server(const char *ip, uint16_t port,
191
 
                 AvahiIfIndex if_index,
192
 
                 int af){
193
 
  int ret;
194
 
  server *new_server = malloc(sizeof(server));
195
 
  if(new_server == NULL){
196
 
    perror_plus("malloc");
197
 
    return -1;
198
 
  }
199
 
  *new_server = (server){ .ip = strdup(ip),
200
 
                         .port = port,
201
 
                         .if_index = if_index,
202
 
                         .af = af };
203
 
  if(new_server->ip == NULL){
204
 
    perror_plus("strdup");
205
 
    return -1;
206
 
  }
207
 
  /* unique case of first server */
208
 
  if (mc.current_server == NULL){
209
 
    new_server->next = new_server;
210
 
    new_server->prev = new_server;
211
 
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
213
 
  } else {
214
 
    new_server->next = mc.current_server;
215
 
    new_server->prev = mc.current_server->prev;
216
 
    new_server->prev->next = new_server;
217
 
    mc.current_server->prev = new_server;
218
 
  }
219
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
220
 
  if(ret == -1){
221
 
    perror_plus("clock_gettime");
222
 
    return -1;
223
 
  }
224
 
  return 0;
225
 
}
226
 
 
227
169
/* 
228
170
 * Initialize GPGME.
229
171
 */
243
185
    
244
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
245
187
    if(fd == -1){
246
 
      perror_plus("open");
 
188
      perror("open");
247
189
      return false;
248
190
    }
249
191
    
263
205
    
264
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
265
207
    if(ret == -1){
266
 
      perror_plus("close");
 
208
      perror("close");
267
209
    }
268
210
    gpgme_data_release(pgp_data);
269
211
    return true;
394
336
  
395
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
396
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
397
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
398
340
    plaintext_length = -1;
399
341
    goto decrypt_end;
400
342
  }
405
347
                                      (size_t)plaintext_length,
406
348
                                      plaintext_capacity);
407
349
    if(plaintext_capacity == 0){
408
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
409
351
        plaintext_length = -1;
410
352
        goto decrypt_end;
411
353
    }
418
360
      break;
419
361
    }
420
362
    if(ret < 0){
421
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
422
364
      plaintext_length = -1;
423
365
      goto decrypt_end;
424
366
    }
481
423
  }
482
424
  
483
425
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  gnutls_certificate_allocate_credentials(&mc.cred);
485
427
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
429
                                                    from
 
430
                                                    -Wunreachable-code
 
431
                                                 */
487
432
            safer_gnutls_strerror(ret));
488
433
    gnutls_global_deinit();
489
434
    return -1;
644
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
590
  if(tcp_sd < 0){
646
591
    int e = errno;
647
 
    perror_plus("socket");
 
592
    perror("socket");
648
593
    errno = e;
649
594
    goto mandos_end;
650
595
  }
664
609
  }
665
610
  if(ret < 0 ){
666
611
    int e = errno;
667
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
668
613
    errno = e;
669
614
    goto mandos_end;
670
615
  }
706
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
652
      char interface[IF_NAMESIZE];
708
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
710
655
      } else {
711
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
657
                ip, interface, port);
726
671
                        sizeof(addrstr));
727
672
    }
728
673
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
730
675
    } else {
731
676
      if(strcmp(addrstr, ip) != 0){
732
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
747
692
  if(ret < 0){
748
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
694
      int e = errno;
750
 
      perror_plus("connect");
 
695
      perror("connect");
751
696
      errno = e;
752
697
    }
753
698
    goto mandos_end;
766
711
                                   out_size - written));
767
712
    if(ret == -1){
768
713
      int e = errno;
769
 
      perror_plus("write");
 
714
      perror("write");
770
715
      errno = e;
771
716
      goto mandos_end;
772
717
    }
797
742
    goto mandos_end;
798
743
  }
799
744
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
801
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
746
  
803
747
  if(quit_now){
840
784
                                   buffer_capacity);
841
785
    if(buffer_capacity == 0){
842
786
      int e = errno;
843
 
      perror_plus("incbuffer");
 
787
      perror("incbuffer");
844
788
      errno = e;
845
789
      goto mandos_end;
846
790
    }
951
895
      if(e == 0){
952
896
        e = errno;
953
897
      }
954
 
      perror_plus("close");
 
898
      perror("close");
955
899
    }
956
900
    gnutls_deinit(session);
957
 
    errno = e;
958
901
    if(quit_now){
959
 
      errno = EINTR;
 
902
      e = EINTR;
960
903
      retval = -1;
961
904
    }
 
905
    errno = e;
962
906
  }
963
907
  return retval;
964
908
}
1007
951
                                           avahi_proto_to_af(proto));
1008
952
      if(ret == 0){
1009
953
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
954
      }
1014
955
    }
1015
956
  }
1069
1010
  }
1070
1011
}
1071
1012
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
1013
/* stop main loop after sigterm has been called */
1073
1014
static void handle_sigterm(int sig){
1074
1015
  if(quit_now){
1075
1016
    return;
1077
1018
  quit_now = 1;
1078
1019
  signal_received = sig;
1079
1020
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
1021
  if(mc.simple_poll != NULL){
1082
1022
    avahi_simple_poll_quit(mc.simple_poll);
1083
1023
  }
1092
1032
int good_interface(const struct dirent *if_entry){
1093
1033
  ssize_t ssret;
1094
1034
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
1035
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
1036
                     if_entry->d_name);
1100
1037
  if(ret < 0){
1101
 
    perror_plus("asprintf");
 
1038
    perror("asprintf");
 
1039
    return 0;
 
1040
  }
 
1041
  if(if_entry->d_name[0] == '.'){
1102
1042
    return 0;
1103
1043
  }
1104
1044
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
1045
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
 
1046
    perror("open");
1108
1047
    return 0;
1109
1048
  }
1110
 
  free(flagname);
1111
1049
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1050
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1051
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1052
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1053
  flagstring[(size_t)to_read] = '\0';
1116
1054
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
 
1055
    perror("malloc");
1118
1056
    close(flags_fd);
1119
1057
    return 0;
1120
1058
  }
1122
1060
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
1061
                                             (size_t)to_read));
1124
1062
    if(ssret == -1){
1125
 
      perror_plus("read");
 
1063
      perror("read");
1126
1064
      free(flagstring);
1127
1065
      close(flags_fd);
1128
1066
      return 0;
1173
1111
    }
1174
1112
    return 0;
1175
1113
  }
1176
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1177
 
  if(flags & IFF_NOARP){
1178
 
    if(debug){
1179
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1180
 
              if_entry->d_name);
1181
 
    }
1182
 
    return 0;
1183
 
  }
1184
1114
  /* Accept this device */
1185
1115
  if(debug){
1186
1116
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1189
1119
  return 1;
1190
1120
}
1191
1121
 
1192
 
int notdotentries(const struct dirent *direntry){
1193
 
  /* Skip "." and ".." */
1194
 
  if(direntry->d_name[0] == '.'
1195
 
     and (direntry->d_name[1] == '\0'
1196
 
          or (direntry->d_name[1] == '.'
1197
 
              and direntry->d_name[2] == '\0'))){
1198
 
    return 0;
1199
 
  }
1200
 
  return 1;
1201
 
}
1202
 
 
1203
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
 
  int ret;
1205
 
  struct timespec now;
1206
 
  struct timespec waited_time;
1207
 
  intmax_t block_time;
1208
 
 
1209
 
  while(true){
1210
 
    if(mc.current_server == NULL){
1211
 
      if (debug){
1212
 
        fprintf(stderr,
1213
 
                "Wait until first server is found. No timeout!\n");
1214
 
      }
1215
 
      ret = avahi_simple_poll_iterate(s, -1);
1216
 
    } else {
1217
 
      if (debug){
1218
 
        fprintf(stderr, "Check current_server if we should run it,"
1219
 
                " or wait\n");
1220
 
      }
1221
 
      /* the current time */
1222
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1223
 
      if(ret == -1){
1224
 
        perror_plus("clock_gettime");
1225
 
        return -1;
1226
 
      }
1227
 
      /* Calculating in ms how long time between now and server
1228
 
         who we visted longest time ago. Now - last seen.  */
1229
 
      waited_time.tv_sec = (now.tv_sec
1230
 
                            - mc.current_server->last_seen.tv_sec);
1231
 
      waited_time.tv_nsec = (now.tv_nsec
1232
 
                             - mc.current_server->last_seen.tv_nsec);
1233
 
      /* total time is 10s/10,000ms.
1234
 
         Converting to s from ms by dividing by 1,000,
1235
 
         and ns to ms by dividing by 1,000,000. */
1236
 
      block_time = ((retry_interval
1237
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1239
 
 
1240
 
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
1242
 
      }
1243
 
 
1244
 
      if(block_time <= 0){
1245
 
        ret = start_mandos_communication(mc.current_server->ip,
1246
 
                                         mc.current_server->port,
1247
 
                                         mc.current_server->if_index,
1248
 
                                         mc.current_server->af);
1249
 
        if(ret == 0){
1250
 
          avahi_simple_poll_quit(mc.simple_poll);
1251
 
          return 0;
1252
 
        }
1253
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1254
 
                            &mc.current_server->last_seen);
1255
 
        if(ret == -1){
1256
 
          perror_plus("clock_gettime");
1257
 
          return -1;
1258
 
        }
1259
 
        mc.current_server = mc.current_server->next;
1260
 
        block_time = 0;         /* Call avahi to find new Mandos
1261
 
                                   servers, but don't block */
1262
 
      }
1263
 
      
1264
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1265
 
    }
1266
 
    if(ret != 0){
1267
 
      if (ret > 0 or errno != EINTR) {
1268
 
        return (ret != 1) ? ret : 0;
1269
 
      }
1270
 
    }
1271
 
  }
1272
 
}
1273
 
 
1274
1122
int main(int argc, char *argv[]){
1275
1123
  AvahiSServiceBrowser *sb = NULL;
1276
1124
  int error;
1293
1141
  bool gnutls_initialized = false;
1294
1142
  bool gpgme_initialized = false;
1295
1143
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
1298
1144
  
1299
1145
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1146
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1306
1152
  errno = 0;
1307
1153
  ret = setgid(gid);
1308
1154
  if(ret == -1){
1309
 
    perror_plus("setgid");
 
1155
    perror("setgid");
1310
1156
  }
1311
1157
  
1312
1158
  /* Lower user privileges (temporarily) */
1313
1159
  errno = 0;
1314
1160
  ret = seteuid(uid);
1315
1161
  if(ret == -1){
1316
 
    perror_plus("seteuid");
 
1162
    perror("seteuid");
1317
1163
  }
1318
1164
  
1319
1165
  if(quit_now){
1354
1200
        .arg = "SECONDS",
1355
1201
        .doc = "Maximum delay to wait for interface startup",
1356
1202
        .group = 2 },
1357
 
      { .name = "retry", .key = 132,
1358
 
        .arg = "SECONDS",
1359
 
        .doc = "Retry interval used when denied by the mandos server",
1360
 
        .group = 2 },
1361
1203
      /*
1362
1204
       * These reproduce what we would get without ARGP_NO_HELP
1363
1205
       */
1407
1249
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
1250
          argp_error(state, "Bad delay");
1409
1251
        }
1410
 
      case 132:                 /* --retry */
1411
 
        errno = 0;
1412
 
        retry_interval = strtod(arg, &tmp);
1413
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1414
 
           or (retry_interval * 1000) > INT_MAX){
1415
 
          argp_error(state, "Bad retry interval");
1416
 
        }
1417
1252
        break;
1418
1253
        /*
1419
1254
         * These reproduce what we would get without ARGP_NO_HELP
1447
1282
    case ENOMEM:
1448
1283
    default:
1449
1284
      errno = ret;
1450
 
      perror_plus("argp_parse");
 
1285
      perror("argp_parse");
1451
1286
      exitcode = EX_OSERR;
1452
1287
      goto end;
1453
1288
    case EINVAL:
1455
1290
      goto end;
1456
1291
    }
1457
1292
  }
1458
 
    
1459
 
  {
1460
 
    /* Work around Debian bug #633582:
1461
 
       <http://bugs.debian.org/633582> */
1462
 
    struct stat st;
1463
 
    
1464
 
    /* Re-raise priviliges */
1465
 
    errno = 0;
1466
 
    ret = seteuid(0);
1467
 
    if(ret == -1){
1468
 
      perror_plus("seteuid");
1469
 
    }
1470
 
    
1471
 
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
 
    if(seckey_fd == -1){
1473
 
      perror_plus("open");
1474
 
    } else {
1475
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1476
 
      if(ret == -1){
1477
 
        perror_plus("fstat");
1478
 
      } else {
1479
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
 
          ret = fchown(seckey_fd, uid, gid);
1481
 
          if(ret == -1){
1482
 
            perror_plus("fchown");
1483
 
          }
1484
 
        }
1485
 
      }
1486
 
      TEMP_FAILURE_RETRY(close(seckey_fd));
1487
 
    }
1488
 
    
1489
 
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
 
    if(pubkey_fd == -1){
1491
 
      perror_plus("open");
1492
 
    } else {
1493
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1494
 
      if(ret == -1){
1495
 
        perror_plus("fstat");
1496
 
      } else {
1497
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
 
          ret = fchown(pubkey_fd, uid, gid);
1499
 
          if(ret == -1){
1500
 
            perror_plus("fchown");
1501
 
          }
1502
 
        }
1503
 
      }
1504
 
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1505
 
    }
1506
 
    
1507
 
    /* Lower privileges */
1508
 
    errno = 0;
1509
 
    ret = seteuid(uid);
1510
 
    if(ret == -1){
1511
 
      perror_plus("seteuid");
1512
 
    }
1513
 
  }
1514
1293
  
1515
1294
  if(not debug){
1516
1295
    avahi_set_log_function(empty_log);
1527
1306
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1528
1307
      }
1529
1308
      if(interface == NULL){
1530
 
        perror_plus("malloc");
 
1309
        perror("malloc");
1531
1310
        free(direntries);
1532
1311
        exitcode = EXIT_FAILURE;
1533
1312
        goto end;
1555
1334
  sigemptyset(&sigterm_action.sa_mask);
1556
1335
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1557
1336
  if(ret == -1){
1558
 
    perror_plus("sigaddset");
 
1337
    perror("sigaddset");
1559
1338
    exitcode = EX_OSERR;
1560
1339
    goto end;
1561
1340
  }
1562
1341
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1563
1342
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
 
1343
    perror("sigaddset");
1565
1344
    exitcode = EX_OSERR;
1566
1345
    goto end;
1567
1346
  }
1568
1347
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1569
1348
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
 
1349
    perror("sigaddset");
1571
1350
    exitcode = EX_OSERR;
1572
1351
    goto end;
1573
1352
  }
1577
1356
  */
1578
1357
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1579
1358
  if(ret == -1){
1580
 
    perror_plus("sigaction");
 
1359
    perror("sigaction");
1581
1360
    return EX_OSERR;
1582
1361
  }
1583
1362
  if(old_sigterm_action.sa_handler != SIG_IGN){
1584
1363
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1585
1364
    if(ret == -1){
1586
 
      perror_plus("sigaction");
 
1365
      perror("sigaction");
1587
1366
      exitcode = EX_OSERR;
1588
1367
      goto end;
1589
1368
    }
1590
1369
  }
1591
1370
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1592
1371
  if(ret == -1){
1593
 
    perror_plus("sigaction");
 
1372
    perror("sigaction");
1594
1373
    return EX_OSERR;
1595
1374
  }
1596
1375
  if(old_sigterm_action.sa_handler != SIG_IGN){
1597
1376
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1598
1377
    if(ret == -1){
1599
 
      perror_plus("sigaction");
 
1378
      perror("sigaction");
1600
1379
      exitcode = EX_OSERR;
1601
1380
      goto end;
1602
1381
    }
1603
1382
  }
1604
1383
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1605
1384
  if(ret == -1){
1606
 
    perror_plus("sigaction");
 
1385
    perror("sigaction");
1607
1386
    return EX_OSERR;
1608
1387
  }
1609
1388
  if(old_sigterm_action.sa_handler != SIG_IGN){
1610
1389
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1611
1390
    if(ret == -1){
1612
 
      perror_plus("sigaction");
 
1391
      perror("sigaction");
1613
1392
      exitcode = EX_OSERR;
1614
1393
      goto end;
1615
1394
    }
1632
1411
    errno = 0;
1633
1412
    ret = seteuid(0);
1634
1413
    if(ret == -1){
1635
 
      perror_plus("seteuid");
 
1414
      perror("seteuid");
1636
1415
    }
1637
1416
    
1638
1417
#ifdef __linux__
1642
1421
    bool restore_loglevel = true;
1643
1422
    if(ret == -1){
1644
1423
      restore_loglevel = false;
1645
 
      perror_plus("klogctl");
 
1424
      perror("klogctl");
1646
1425
    }
1647
1426
#endif  /* __linux__ */
1648
1427
    
1649
1428
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1650
1429
    if(sd < 0){
1651
 
      perror_plus("socket");
 
1430
      perror("socket");
1652
1431
      exitcode = EX_OSERR;
1653
1432
#ifdef __linux__
1654
1433
      if(restore_loglevel){
1655
1434
        ret = klogctl(7, NULL, 0);
1656
1435
        if(ret == -1){
1657
 
          perror_plus("klogctl");
 
1436
          perror("klogctl");
1658
1437
        }
1659
1438
      }
1660
1439
#endif  /* __linux__ */
1662
1441
      errno = 0;
1663
1442
      ret = seteuid(uid);
1664
1443
      if(ret == -1){
1665
 
        perror_plus("seteuid");
 
1444
        perror("seteuid");
1666
1445
      }
1667
1446
      goto end;
1668
1447
    }
1669
1448
    strcpy(network.ifr_name, interface);
1670
1449
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1671
1450
    if(ret == -1){
1672
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1451
      perror("ioctl SIOCGIFFLAGS");
1673
1452
#ifdef __linux__
1674
1453
      if(restore_loglevel){
1675
1454
        ret = klogctl(7, NULL, 0);
1676
1455
        if(ret == -1){
1677
 
          perror_plus("klogctl");
 
1456
          perror("klogctl");
1678
1457
        }
1679
1458
      }
1680
1459
#endif  /* __linux__ */
1683
1462
      errno = 0;
1684
1463
      ret = seteuid(uid);
1685
1464
      if(ret == -1){
1686
 
        perror_plus("seteuid");
 
1465
        perror("seteuid");
1687
1466
      }
1688
1467
      goto end;
1689
1468
    }
1693
1472
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1694
1473
      if(ret == -1){
1695
1474
        take_down_interface = false;
1696
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1475
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1697
1476
        exitcode = EX_OSERR;
1698
1477
#ifdef __linux__
1699
1478
        if(restore_loglevel){
1700
1479
          ret = klogctl(7, NULL, 0);
1701
1480
          if(ret == -1){
1702
 
            perror_plus("klogctl");
 
1481
            perror("klogctl");
1703
1482
          }
1704
1483
        }
1705
1484
#endif  /* __linux__ */
1707
1486
        errno = 0;
1708
1487
        ret = seteuid(uid);
1709
1488
        if(ret == -1){
1710
 
          perror_plus("seteuid");
 
1489
          perror("seteuid");
1711
1490
        }
1712
1491
        goto end;
1713
1492
      }
1714
1493
    }
1715
 
    /* Sleep checking until interface is running.
1716
 
       Check every 0.25s, up to total time of delay */
 
1494
    /* sleep checking until interface is running */
1717
1495
    for(int i=0; i < delay * 4; i++){
1718
1496
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1719
1497
      if(ret == -1){
1720
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1498
        perror("ioctl SIOCGIFFLAGS");
1721
1499
      } else if(network.ifr_flags & IFF_RUNNING){
1722
1500
        break;
1723
1501
      }
1724
1502
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1725
1503
      ret = nanosleep(&sleeptime, NULL);
1726
1504
      if(ret == -1 and errno != EINTR){
1727
 
        perror_plus("nanosleep");
 
1505
        perror("nanosleep");
1728
1506
      }
1729
1507
    }
1730
1508
    if(not take_down_interface){
1731
1509
      /* We won't need the socket anymore */
1732
1510
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1733
1511
      if(ret == -1){
1734
 
        perror_plus("close");
 
1512
        perror("close");
1735
1513
      }
1736
1514
    }
1737
1515
#ifdef __linux__
1739
1517
      /* Restores kernel loglevel to default */
1740
1518
      ret = klogctl(7, NULL, 0);
1741
1519
      if(ret == -1){
1742
 
        perror_plus("klogctl");
 
1520
        perror("klogctl");
1743
1521
      }
1744
1522
    }
1745
1523
#endif  /* __linux__ */
1749
1527
      /* Lower privileges */
1750
1528
      ret = seteuid(uid);
1751
1529
      if(ret == -1){
1752
 
        perror_plus("seteuid");
 
1530
        perror("seteuid");
1753
1531
      }
1754
1532
    } else {
1755
1533
      /* Lower privileges permanently */
1756
1534
      ret = setuid(uid);
1757
1535
      if(ret == -1){
1758
 
        perror_plus("setuid");
 
1536
        perror("setuid");
1759
1537
      }
1760
1538
    }
1761
1539
  }
1777
1555
    goto end;
1778
1556
  }
1779
1557
  
 
1558
  tempdir_created = true;
1780
1559
  if(mkdtemp(tempdir) == NULL){
1781
 
    perror_plus("mkdtemp");
 
1560
    tempdir_created = false;
 
1561
    perror("mkdtemp");
1782
1562
    goto end;
1783
1563
  }
1784
 
  tempdir_created = true;
1785
1564
  
1786
1565
  if(quit_now){
1787
1566
    goto end;
1847
1626
      if(quit_now or ret == 0){
1848
1627
        break;
1849
1628
      }
1850
 
      sleep((int)retry_interval or 1);
 
1629
      sleep(15);
1851
1630
    };
1852
1631
 
1853
1632
    if (not quit_now){
1911
1690
  if(debug){
1912
1691
    fprintf(stderr, "Starting Avahi loop search\n");
1913
1692
  }
1914
 
 
1915
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1916
 
                                (int)(retry_interval * 1000));
1917
 
  if(debug){
1918
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1919
 
            (ret == 0) ? "successfully" : "with error");
1920
 
  }
 
1693
  
 
1694
  avahi_simple_poll_loop(mc.simple_poll);
1921
1695
  
1922
1696
 end:
1923
1697
  
1944
1718
  if(gpgme_initialized){
1945
1719
    gpgme_release(mc.ctx);
1946
1720
  }
1947
 
 
1948
 
  /* Cleans up the circular linked list of Mandos servers the client
1949
 
     has seen */
1950
 
  if(mc.current_server != NULL){
1951
 
    mc.current_server->prev->next = NULL;
1952
 
    while(mc.current_server != NULL){
1953
 
      server *next = mc.current_server->next;
1954
 
      free(mc.current_server);
1955
 
      mc.current_server = next;
1956
 
    }
1957
 
  }
1958
1721
  
1959
1722
  /* Take down the network interface */
1960
1723
  if(take_down_interface){
1962
1725
    errno = 0;
1963
1726
    ret = seteuid(0);
1964
1727
    if(ret == -1){
1965
 
      perror_plus("seteuid");
 
1728
      perror("seteuid");
1966
1729
    }
1967
1730
    if(geteuid() == 0){
1968
1731
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1969
1732
      if(ret == -1){
1970
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1733
        perror("ioctl SIOCGIFFLAGS");
1971
1734
      } else if(network.ifr_flags & IFF_UP) {
1972
1735
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1973
1736
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
1737
        if(ret == -1){
1975
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1738
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1976
1739
        }
1977
1740
      }
1978
1741
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1979
1742
      if(ret == -1){
1980
 
        perror_plus("close");
 
1743
        perror("close");
1981
1744
      }
1982
1745
      /* Lower privileges permanently */
1983
1746
      errno = 0;
1984
1747
      ret = setuid(uid);
1985
1748
      if(ret == -1){
1986
 
        perror_plus("setuid");
 
1749
        perror("setuid");
1987
1750
      }
1988
1751
    }
1989
1752
  }
1990
1753
  
1991
 
  /* Removes the GPGME temp directory and all files inside */
 
1754
  /* Removes the temp directory used by GPGME */
1992
1755
  if(tempdir_created){
1993
 
    struct dirent **direntries = NULL;
1994
 
    struct dirent *direntry = NULL;
1995
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1996
 
    if (ret > 0){
1997
 
      for(int i = 0; i < ret; i++){
1998
 
        direntry = direntries[i];
 
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
        }
1999
1776
        char *fullname = NULL;
2000
1777
        ret = asprintf(&fullname, "%s/%s", tempdir,
2001
1778
                       direntry->d_name);
2002
1779
        if(ret < 0){
2003
 
          perror_plus("asprintf");
 
1780
          perror("asprintf");
2004
1781
          continue;
2005
1782
        }
2006
1783
        ret = remove(fullname);
2010
1787
        }
2011
1788
        free(fullname);
2012
1789
      }
2013
 
    }
2014
 
 
2015
 
    /* need to be cleaned even if ret == 0 because man page doesn't
2016
 
       specify */
2017
 
    free(direntries);
2018
 
    if (ret == -1){
2019
 
      perror_plus("scandir");
 
1790
      closedir(d);
2020
1791
    }
2021
1792
    ret = rmdir(tempdir);
2022
1793
    if(ret == -1 and errno != ENOENT){
2023
 
      perror_plus("rmdir");
 
1794
      perror("rmdir");
2024
1795
    }
2025
1796
  }
2026
1797
  
2031
1802
                                            &old_sigterm_action,
2032
1803
                                            NULL));
2033
1804
    if(ret == -1){
2034
 
      perror_plus("sigaction");
 
1805
      perror("sigaction");
2035
1806
    }
2036
1807
    do {
2037
1808
      ret = raise(signal_received);
2038
1809
    } while(ret != 0 and errno == EINTR);
2039
1810
    if(ret != 0){
2040
 
      perror_plus("raise");
 
1811
      perror("raise");
2041
1812
      abort();
2042
1813
    }
2043
1814
    TEMP_FAILURE_RETRY(pause());