/mandos/release

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

« back to all changes in this revision

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

Merge from Björn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
130
131
static const char sys_class_net[] = "/sys/class/net";
131
132
char *connect_to = NULL;
132
133
 
 
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
 
133
145
/* Used for passing in values through the Avahi callback functions */
134
146
typedef struct {
135
147
  AvahiSimplePoll *simple_poll;
139
151
  gnutls_dh_params_t dh_params;
140
152
  const char *priority;
141
153
  gpgme_ctx_t ctx;
 
154
  server *current_server;
142
155
} mandos_context;
143
156
 
144
157
/* global context so signal handler can reach it*/
145
158
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
159
                      .dh_bits = 1024, .priority = "SECURE256"
147
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
160
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
161
                      .current_server = NULL };
148
162
 
149
163
sig_atomic_t quit_now = 0;
150
164
int signal_received = 0;
151
165
 
 
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
 
152
173
/*
153
174
 * Make additional room in "buffer" for at least BUFFER_SIZE more
154
175
 * bytes. "buffer_capacity" is how much is currently allocated,
166
187
  return buffer_capacity;
167
188
}
168
189
 
 
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
 
169
227
/* 
170
228
 * Initialize GPGME.
171
229
 */
185
243
    
186
244
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
187
245
    if(fd == -1){
188
 
      perror("open");
 
246
      perror_plus("open");
189
247
      return false;
190
248
    }
191
249
    
205
263
    
206
264
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
207
265
    if(ret == -1){
208
 
      perror("close");
 
266
      perror_plus("close");
209
267
    }
210
268
    gpgme_data_release(pgp_data);
211
269
    return true;
336
394
  
337
395
  /* Seek back to the beginning of the GPGME plaintext data buffer */
338
396
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
339
 
    perror("gpgme_data_seek");
 
397
    perror_plus("gpgme_data_seek");
340
398
    plaintext_length = -1;
341
399
    goto decrypt_end;
342
400
  }
347
405
                                      (size_t)plaintext_length,
348
406
                                      plaintext_capacity);
349
407
    if(plaintext_capacity == 0){
350
 
        perror("incbuffer");
 
408
        perror_plus("incbuffer");
351
409
        plaintext_length = -1;
352
410
        goto decrypt_end;
353
411
    }
360
418
      break;
361
419
    }
362
420
    if(ret < 0){
363
 
      perror("gpgme_data_read");
 
421
      perror_plus("gpgme_data_read");
364
422
      plaintext_length = -1;
365
423
      goto decrypt_end;
366
424
    }
423
481
  }
424
482
  
425
483
  /* OpenPGP credentials */
426
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
484
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
485
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
429
 
                                                    from
430
 
                                                    -Wunreachable-code
431
 
                                                 */
 
486
    fprintf(stderr, "GnuTLS memory error: %s\n",
432
487
            safer_gnutls_strerror(ret));
433
488
    gnutls_global_deinit();
434
489
    return -1;
589
644
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
645
  if(tcp_sd < 0){
591
646
    int e = errno;
592
 
    perror("socket");
 
647
    perror_plus("socket");
593
648
    errno = e;
594
649
    goto mandos_end;
595
650
  }
609
664
  }
610
665
  if(ret < 0 ){
611
666
    int e = errno;
612
 
    perror("inet_pton");
 
667
    perror_plus("inet_pton");
613
668
    errno = e;
614
669
    goto mandos_end;
615
670
  }
651
706
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
707
      char interface[IF_NAMESIZE];
653
708
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
 
        perror("if_indextoname");
 
709
        perror_plus("if_indextoname");
655
710
      } else {
656
711
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
712
                ip, interface, port);
671
726
                        sizeof(addrstr));
672
727
    }
673
728
    if(pcret == NULL){
674
 
      perror("inet_ntop");
 
729
      perror_plus("inet_ntop");
675
730
    } else {
676
731
      if(strcmp(addrstr, ip) != 0){
677
732
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
692
747
  if(ret < 0){
693
748
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
749
      int e = errno;
695
 
      perror("connect");
 
750
      perror_plus("connect");
696
751
      errno = e;
697
752
    }
698
753
    goto mandos_end;
711
766
                                   out_size - written));
712
767
    if(ret == -1){
713
768
      int e = errno;
714
 
      perror("write");
 
769
      perror_plus("write");
715
770
      errno = e;
716
771
      goto mandos_end;
717
772
    }
741
796
    errno = EINTR;
742
797
    goto mandos_end;
743
798
  }
744
 
 
745
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
799
  
 
800
  /* Spurious warning from -Wint-to-pointer-cast */
746
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
747
802
  
748
803
  if(quit_now){
785
840
                                   buffer_capacity);
786
841
    if(buffer_capacity == 0){
787
842
      int e = errno;
788
 
      perror("incbuffer");
 
843
      perror_plus("incbuffer");
789
844
      errno = e;
790
845
      goto mandos_end;
791
846
    }
896
951
      if(e == 0){
897
952
        e = errno;
898
953
      }
899
 
      perror("close");
 
954
      perror_plus("close");
900
955
    }
901
956
    gnutls_deinit(session);
 
957
    errno = e;
902
958
    if(quit_now){
903
 
      e = EINTR;
 
959
      errno = EINTR;
904
960
      retval = -1;
905
961
    }
906
 
    errno = e;
907
962
  }
908
963
  return retval;
909
964
}
952
1007
                                           avahi_proto_to_af(proto));
953
1008
      if(ret == 0){
954
1009
        avahi_simple_poll_quit(mc.simple_poll);
 
1010
      } else {
 
1011
        ret = add_server(ip, port, interface,
 
1012
                         avahi_proto_to_af(proto));
955
1013
      }
956
1014
    }
957
1015
  }
1011
1069
  }
1012
1070
}
1013
1071
 
1014
 
/* stop main loop after sigterm has been called */
 
1072
/* Signal handler that stops main loop after SIGTERM */
1015
1073
static void handle_sigterm(int sig){
1016
1074
  if(quit_now){
1017
1075
    return;
1019
1077
  quit_now = 1;
1020
1078
  signal_received = sig;
1021
1079
  int old_errno = errno;
 
1080
  /* set main loop to exit */
1022
1081
  if(mc.simple_poll != NULL){
1023
1082
    avahi_simple_poll_quit(mc.simple_poll);
1024
1083
  }
1039
1098
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1040
1099
                     if_entry->d_name);
1041
1100
  if(ret < 0){
1042
 
    perror("asprintf");
 
1101
    perror_plus("asprintf");
1043
1102
    return 0;
1044
1103
  }
1045
1104
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1046
1105
  if(flags_fd == -1){
1047
 
    perror("open");
 
1106
    perror_plus("open");
1048
1107
    free(flagname);
1049
1108
    return 0;
1050
1109
  }
1051
1110
  free(flagname);
1052
1111
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1053
1112
  /* read line from flags_fd */
1054
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1113
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1055
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1056
1115
  flagstring[(size_t)to_read] = '\0';
1057
1116
  if(flagstring == NULL){
1058
 
    perror("malloc");
 
1117
    perror_plus("malloc");
1059
1118
    close(flags_fd);
1060
1119
    return 0;
1061
1120
  }
1063
1122
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1064
1123
                                             (size_t)to_read));
1065
1124
    if(ssret == -1){
1066
 
      perror("read");
 
1125
      perror_plus("read");
1067
1126
      free(flagstring);
1068
1127
      close(flags_fd);
1069
1128
      return 0;
1141
1200
  return 1;
1142
1201
}
1143
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
 
1144
1274
int main(int argc, char *argv[]){
1145
1275
  AvahiSServiceBrowser *sb = NULL;
1146
1276
  int error;
1163
1293
  bool gnutls_initialized = false;
1164
1294
  bool gpgme_initialized = false;
1165
1295
  float delay = 2.5f;
 
1296
  double retry_interval = 10; /* 10s between trying a server and
 
1297
                                 retrying the same server again */
1166
1298
  
1167
1299
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1168
1300
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1174
1306
  errno = 0;
1175
1307
  ret = setgid(gid);
1176
1308
  if(ret == -1){
1177
 
    perror("setgid");
 
1309
    perror_plus("setgid");
1178
1310
  }
1179
1311
  
1180
1312
  /* Lower user privileges (temporarily) */
1181
1313
  errno = 0;
1182
1314
  ret = seteuid(uid);
1183
1315
  if(ret == -1){
1184
 
    perror("seteuid");
 
1316
    perror_plus("seteuid");
1185
1317
  }
1186
1318
  
1187
1319
  if(quit_now){
1222
1354
        .arg = "SECONDS",
1223
1355
        .doc = "Maximum delay to wait for interface startup",
1224
1356
        .group = 2 },
 
1357
      { .name = "retry", .key = 132,
 
1358
        .arg = "SECONDS",
 
1359
        .doc = "Retry interval used when denied by the mandos server",
 
1360
        .group = 2 },
1225
1361
      /*
1226
1362
       * These reproduce what we would get without ARGP_NO_HELP
1227
1363
       */
1271
1407
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1272
1408
          argp_error(state, "Bad delay");
1273
1409
        }
 
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
        }
1274
1417
        break;
1275
1418
        /*
1276
1419
         * These reproduce what we would get without ARGP_NO_HELP
1304
1447
    case ENOMEM:
1305
1448
    default:
1306
1449
      errno = ret;
1307
 
      perror("argp_parse");
 
1450
      perror_plus("argp_parse");
1308
1451
      exitcode = EX_OSERR;
1309
1452
      goto end;
1310
1453
    case EINVAL:
1328
1471
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1329
1472
      }
1330
1473
      if(interface == NULL){
1331
 
        perror("malloc");
 
1474
        perror_plus("malloc");
1332
1475
        free(direntries);
1333
1476
        exitcode = EXIT_FAILURE;
1334
1477
        goto end;
1356
1499
  sigemptyset(&sigterm_action.sa_mask);
1357
1500
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1358
1501
  if(ret == -1){
1359
 
    perror("sigaddset");
 
1502
    perror_plus("sigaddset");
1360
1503
    exitcode = EX_OSERR;
1361
1504
    goto end;
1362
1505
  }
1363
1506
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1364
1507
  if(ret == -1){
1365
 
    perror("sigaddset");
 
1508
    perror_plus("sigaddset");
1366
1509
    exitcode = EX_OSERR;
1367
1510
    goto end;
1368
1511
  }
1369
1512
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1370
1513
  if(ret == -1){
1371
 
    perror("sigaddset");
 
1514
    perror_plus("sigaddset");
1372
1515
    exitcode = EX_OSERR;
1373
1516
    goto end;
1374
1517
  }
1378
1521
  */
1379
1522
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1380
1523
  if(ret == -1){
1381
 
    perror("sigaction");
 
1524
    perror_plus("sigaction");
1382
1525
    return EX_OSERR;
1383
1526
  }
1384
1527
  if(old_sigterm_action.sa_handler != SIG_IGN){
1385
1528
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1386
1529
    if(ret == -1){
1387
 
      perror("sigaction");
 
1530
      perror_plus("sigaction");
1388
1531
      exitcode = EX_OSERR;
1389
1532
      goto end;
1390
1533
    }
1391
1534
  }
1392
1535
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1393
1536
  if(ret == -1){
1394
 
    perror("sigaction");
 
1537
    perror_plus("sigaction");
1395
1538
    return EX_OSERR;
1396
1539
  }
1397
1540
  if(old_sigterm_action.sa_handler != SIG_IGN){
1398
1541
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1399
1542
    if(ret == -1){
1400
 
      perror("sigaction");
 
1543
      perror_plus("sigaction");
1401
1544
      exitcode = EX_OSERR;
1402
1545
      goto end;
1403
1546
    }
1404
1547
  }
1405
1548
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1406
1549
  if(ret == -1){
1407
 
    perror("sigaction");
 
1550
    perror_plus("sigaction");
1408
1551
    return EX_OSERR;
1409
1552
  }
1410
1553
  if(old_sigterm_action.sa_handler != SIG_IGN){
1411
1554
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1412
1555
    if(ret == -1){
1413
 
      perror("sigaction");
 
1556
      perror_plus("sigaction");
1414
1557
      exitcode = EX_OSERR;
1415
1558
      goto end;
1416
1559
    }
1433
1576
    errno = 0;
1434
1577
    ret = seteuid(0);
1435
1578
    if(ret == -1){
1436
 
      perror("seteuid");
 
1579
      perror_plus("seteuid");
1437
1580
    }
1438
1581
    
1439
1582
#ifdef __linux__
1443
1586
    bool restore_loglevel = true;
1444
1587
    if(ret == -1){
1445
1588
      restore_loglevel = false;
1446
 
      perror("klogctl");
 
1589
      perror_plus("klogctl");
1447
1590
    }
1448
1591
#endif  /* __linux__ */
1449
1592
    
1450
1593
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1451
1594
    if(sd < 0){
1452
 
      perror("socket");
 
1595
      perror_plus("socket");
1453
1596
      exitcode = EX_OSERR;
1454
1597
#ifdef __linux__
1455
1598
      if(restore_loglevel){
1456
1599
        ret = klogctl(7, NULL, 0);
1457
1600
        if(ret == -1){
1458
 
          perror("klogctl");
 
1601
          perror_plus("klogctl");
1459
1602
        }
1460
1603
      }
1461
1604
#endif  /* __linux__ */
1463
1606
      errno = 0;
1464
1607
      ret = seteuid(uid);
1465
1608
      if(ret == -1){
1466
 
        perror("seteuid");
 
1609
        perror_plus("seteuid");
1467
1610
      }
1468
1611
      goto end;
1469
1612
    }
1470
1613
    strcpy(network.ifr_name, interface);
1471
1614
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1472
1615
    if(ret == -1){
1473
 
      perror("ioctl SIOCGIFFLAGS");
 
1616
      perror_plus("ioctl SIOCGIFFLAGS");
1474
1617
#ifdef __linux__
1475
1618
      if(restore_loglevel){
1476
1619
        ret = klogctl(7, NULL, 0);
1477
1620
        if(ret == -1){
1478
 
          perror("klogctl");
 
1621
          perror_plus("klogctl");
1479
1622
        }
1480
1623
      }
1481
1624
#endif  /* __linux__ */
1484
1627
      errno = 0;
1485
1628
      ret = seteuid(uid);
1486
1629
      if(ret == -1){
1487
 
        perror("seteuid");
 
1630
        perror_plus("seteuid");
1488
1631
      }
1489
1632
      goto end;
1490
1633
    }
1494
1637
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1495
1638
      if(ret == -1){
1496
1639
        take_down_interface = false;
1497
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1640
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1498
1641
        exitcode = EX_OSERR;
1499
1642
#ifdef __linux__
1500
1643
        if(restore_loglevel){
1501
1644
          ret = klogctl(7, NULL, 0);
1502
1645
          if(ret == -1){
1503
 
            perror("klogctl");
 
1646
            perror_plus("klogctl");
1504
1647
          }
1505
1648
        }
1506
1649
#endif  /* __linux__ */
1508
1651
        errno = 0;
1509
1652
        ret = seteuid(uid);
1510
1653
        if(ret == -1){
1511
 
          perror("seteuid");
 
1654
          perror_plus("seteuid");
1512
1655
        }
1513
1656
        goto end;
1514
1657
      }
1515
1658
    }
1516
 
    /* sleep checking until interface is running */
 
1659
    /* Sleep checking until interface is running.
 
1660
       Check every 0.25s, up to total time of delay */
1517
1661
    for(int i=0; i < delay * 4; i++){
1518
1662
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1519
1663
      if(ret == -1){
1520
 
        perror("ioctl SIOCGIFFLAGS");
 
1664
        perror_plus("ioctl SIOCGIFFLAGS");
1521
1665
      } else if(network.ifr_flags & IFF_RUNNING){
1522
1666
        break;
1523
1667
      }
1524
1668
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1525
1669
      ret = nanosleep(&sleeptime, NULL);
1526
1670
      if(ret == -1 and errno != EINTR){
1527
 
        perror("nanosleep");
 
1671
        perror_plus("nanosleep");
1528
1672
      }
1529
1673
    }
1530
1674
    if(not take_down_interface){
1531
1675
      /* We won't need the socket anymore */
1532
1676
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1533
1677
      if(ret == -1){
1534
 
        perror("close");
 
1678
        perror_plus("close");
1535
1679
      }
1536
1680
    }
1537
1681
#ifdef __linux__
1539
1683
      /* Restores kernel loglevel to default */
1540
1684
      ret = klogctl(7, NULL, 0);
1541
1685
      if(ret == -1){
1542
 
        perror("klogctl");
 
1686
        perror_plus("klogctl");
1543
1687
      }
1544
1688
    }
1545
1689
#endif  /* __linux__ */
1549
1693
      /* Lower privileges */
1550
1694
      ret = seteuid(uid);
1551
1695
      if(ret == -1){
1552
 
        perror("seteuid");
 
1696
        perror_plus("seteuid");
1553
1697
      }
1554
1698
    } else {
1555
1699
      /* Lower privileges permanently */
1556
1700
      ret = setuid(uid);
1557
1701
      if(ret == -1){
1558
 
        perror("setuid");
 
1702
        perror_plus("setuid");
1559
1703
      }
1560
1704
    }
1561
1705
  }
1578
1722
  }
1579
1723
  
1580
1724
  if(mkdtemp(tempdir) == NULL){
1581
 
    perror("mkdtemp");
 
1725
    perror_plus("mkdtemp");
1582
1726
    goto end;
1583
1727
  }
1584
1728
  tempdir_created = true;
1647
1791
      if(quit_now or ret == 0){
1648
1792
        break;
1649
1793
      }
1650
 
      sleep(15);
 
1794
      sleep((int)retry_interval or 1);
1651
1795
    };
1652
1796
 
1653
1797
    if (not quit_now){
1711
1855
  if(debug){
1712
1856
    fprintf(stderr, "Starting Avahi loop search\n");
1713
1857
  }
1714
 
  
1715
 
  avahi_simple_poll_loop(mc.simple_poll);
 
1858
 
 
1859
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
1860
                                (int)(retry_interval * 1000));
 
1861
  if(debug){
 
1862
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
1863
            (ret == 0) ? "successfully" : "with error");
 
1864
  }
1716
1865
  
1717
1866
 end:
1718
1867
  
1739
1888
  if(gpgme_initialized){
1740
1889
    gpgme_release(mc.ctx);
1741
1890
  }
 
1891
 
 
1892
  /* Cleans up the circular linked list of Mandos servers the client
 
1893
     has seen */
 
1894
  if(mc.current_server != NULL){
 
1895
    mc.current_server->prev->next = NULL;
 
1896
    while(mc.current_server != NULL){
 
1897
      server *next = mc.current_server->next;
 
1898
      free(mc.current_server);
 
1899
      mc.current_server = next;
 
1900
    }
 
1901
  }
1742
1902
  
1743
1903
  /* Take down the network interface */
1744
1904
  if(take_down_interface){
1746
1906
    errno = 0;
1747
1907
    ret = seteuid(0);
1748
1908
    if(ret == -1){
1749
 
      perror("seteuid");
 
1909
      perror_plus("seteuid");
1750
1910
    }
1751
1911
    if(geteuid() == 0){
1752
1912
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1753
1913
      if(ret == -1){
1754
 
        perror("ioctl SIOCGIFFLAGS");
 
1914
        perror_plus("ioctl SIOCGIFFLAGS");
1755
1915
      } else if(network.ifr_flags & IFF_UP) {
1756
1916
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1757
1917
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1758
1918
        if(ret == -1){
1759
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1919
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1760
1920
        }
1761
1921
      }
1762
1922
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1763
1923
      if(ret == -1){
1764
 
        perror("close");
 
1924
        perror_plus("close");
1765
1925
      }
1766
1926
      /* Lower privileges permanently */
1767
1927
      errno = 0;
1768
1928
      ret = setuid(uid);
1769
1929
      if(ret == -1){
1770
 
        perror("setuid");
 
1930
        perror_plus("setuid");
1771
1931
      }
1772
1932
    }
1773
1933
  }
1784
1944
        ret = asprintf(&fullname, "%s/%s", tempdir,
1785
1945
                       direntry->d_name);
1786
1946
        if(ret < 0){
1787
 
          perror("asprintf");
 
1947
          perror_plus("asprintf");
1788
1948
          continue;
1789
1949
        }
1790
1950
        ret = remove(fullname);
1796
1956
      }
1797
1957
    }
1798
1958
 
1799
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
 
1959
    /* need to be cleaned even if ret == 0 because man page doesn't
 
1960
       specify */
1800
1961
    free(direntries);
1801
1962
    if (ret == -1){
1802
 
      perror("scandir");
 
1963
      perror_plus("scandir");
1803
1964
    }
1804
1965
    ret = rmdir(tempdir);
1805
1966
    if(ret == -1 and errno != ENOENT){
1806
 
      perror("rmdir");
 
1967
      perror_plus("rmdir");
1807
1968
    }
1808
1969
  }
1809
1970
  
1814
1975
                                            &old_sigterm_action,
1815
1976
                                            NULL));
1816
1977
    if(ret == -1){
1817
 
      perror("sigaction");
 
1978
      perror_plus("sigaction");
1818
1979
    }
1819
1980
    do {
1820
1981
      ret = raise(signal_received);
1821
1982
    } while(ret != 0 and errno == EINTR);
1822
1983
    if(ret != 0){
1823
 
      perror("raise");
 
1984
      perror_plus("raise");
1824
1985
      abort();
1825
1986
    }
1826
1987
    TEMP_FAILURE_RETRY(pause());