/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2011-07-13 02:24:39 UTC
  • mfrom: (24.1.174 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20110713022439-wbv6kghshdsc2x24
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(),
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
    }
586
644
  tcp_sd = socket(pf, SOCK_STREAM, 0);
587
645
  if(tcp_sd < 0){
588
646
    int e = errno;
589
 
    perror("socket");
 
647
    perror_plus("socket");
590
648
    errno = e;
591
649
    goto mandos_end;
592
650
  }
606
664
  }
607
665
  if(ret < 0 ){
608
666
    int e = errno;
609
 
    perror("inet_pton");
 
667
    perror_plus("inet_pton");
610
668
    errno = e;
611
669
    goto mandos_end;
612
670
  }
648
706
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
649
707
      char interface[IF_NAMESIZE];
650
708
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
651
 
        perror("if_indextoname");
 
709
        perror_plus("if_indextoname");
652
710
      } else {
653
711
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
654
712
                ip, interface, port);
668
726
                        sizeof(addrstr));
669
727
    }
670
728
    if(pcret == NULL){
671
 
      perror("inet_ntop");
 
729
      perror_plus("inet_ntop");
672
730
    } else {
673
731
      if(strcmp(addrstr, ip) != 0){
674
732
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
689
747
  if(ret < 0){
690
748
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
749
      int e = errno;
692
 
      perror("connect");
 
750
      perror_plus("connect");
693
751
      errno = e;
694
752
    }
695
753
    goto mandos_end;
708
766
                                   out_size - written));
709
767
    if(ret == -1){
710
768
      int e = errno;
711
 
      perror("write");
 
769
      perror_plus("write");
712
770
      errno = e;
713
771
      goto mandos_end;
714
772
    }
739
797
    goto mandos_end;
740
798
  }
741
799
  
 
800
  /* Spurious warning from -Wint-to-pointer-cast */
742
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
743
802
  
744
803
  if(quit_now){
781
840
                                   buffer_capacity);
782
841
    if(buffer_capacity == 0){
783
842
      int e = errno;
784
 
      perror("incbuffer");
 
843
      perror_plus("incbuffer");
785
844
      errno = e;
786
845
      goto mandos_end;
787
846
    }
892
951
      if(e == 0){
893
952
        e = errno;
894
953
      }
895
 
      perror("close");
 
954
      perror_plus("close");
896
955
    }
897
956
    gnutls_deinit(session);
 
957
    errno = e;
898
958
    if(quit_now){
899
 
      e = EINTR;
 
959
      errno = EINTR;
900
960
      retval = -1;
901
961
    }
902
 
    errno = e;
903
962
  }
904
963
  return retval;
905
964
}
948
1007
                                           avahi_proto_to_af(proto));
949
1008
      if(ret == 0){
950
1009
        avahi_simple_poll_quit(mc.simple_poll);
 
1010
      } else {
 
1011
        ret = add_server(ip, port, interface,
 
1012
                         avahi_proto_to_af(proto));
951
1013
      }
952
1014
    }
953
1015
  }
1007
1069
  }
1008
1070
}
1009
1071
 
1010
 
/* stop main loop after sigterm has been called */
 
1072
/* Signal handler that stops main loop after SIGTERM */
1011
1073
static void handle_sigterm(int sig){
1012
1074
  if(quit_now){
1013
1075
    return;
1015
1077
  quit_now = 1;
1016
1078
  signal_received = sig;
1017
1079
  int old_errno = errno;
 
1080
  /* set main loop to exit */
1018
1081
  if(mc.simple_poll != NULL){
1019
1082
    avahi_simple_poll_quit(mc.simple_poll);
1020
1083
  }
1035
1098
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
1099
                     if_entry->d_name);
1037
1100
  if(ret < 0){
1038
 
    perror("asprintf");
 
1101
    perror_plus("asprintf");
1039
1102
    return 0;
1040
1103
  }
1041
1104
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
1105
  if(flags_fd == -1){
1043
 
    perror("open");
 
1106
    perror_plus("open");
1044
1107
    free(flagname);
1045
1108
    return 0;
1046
1109
  }
1051
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
1115
  flagstring[(size_t)to_read] = '\0';
1053
1116
  if(flagstring == NULL){
1054
 
    perror("malloc");
 
1117
    perror_plus("malloc");
1055
1118
    close(flags_fd);
1056
1119
    return 0;
1057
1120
  }
1059
1122
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1060
1123
                                             (size_t)to_read));
1061
1124
    if(ssret == -1){
1062
 
      perror("read");
 
1125
      perror_plus("read");
1063
1126
      free(flagstring);
1064
1127
      close(flags_fd);
1065
1128
      return 0;
1126
1189
  return 1;
1127
1190
}
1128
1191
 
 
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
 
1129
1274
int main(int argc, char *argv[]){
1130
1275
  AvahiSServiceBrowser *sb = NULL;
1131
1276
  int error;
1148
1293
  bool gnutls_initialized = false;
1149
1294
  bool gpgme_initialized = false;
1150
1295
  float delay = 2.5f;
 
1296
  double retry_interval = 10; /* 10s between trying a server and
 
1297
                                 retrying the same server again */
1151
1298
  
1152
1299
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1153
1300
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1159
1306
  errno = 0;
1160
1307
  ret = setgid(gid);
1161
1308
  if(ret == -1){
1162
 
    perror("setgid");
 
1309
    perror_plus("setgid");
1163
1310
  }
1164
1311
  
1165
1312
  /* Lower user privileges (temporarily) */
1166
1313
  errno = 0;
1167
1314
  ret = seteuid(uid);
1168
1315
  if(ret == -1){
1169
 
    perror("seteuid");
 
1316
    perror_plus("seteuid");
1170
1317
  }
1171
1318
  
1172
1319
  if(quit_now){
1207
1354
        .arg = "SECONDS",
1208
1355
        .doc = "Maximum delay to wait for interface startup",
1209
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 },
1210
1361
      /*
1211
1362
       * These reproduce what we would get without ARGP_NO_HELP
1212
1363
       */
1256
1407
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1257
1408
          argp_error(state, "Bad delay");
1258
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
        }
1259
1417
        break;
1260
1418
        /*
1261
1419
         * These reproduce what we would get without ARGP_NO_HELP
1289
1447
    case ENOMEM:
1290
1448
    default:
1291
1449
      errno = ret;
1292
 
      perror("argp_parse");
 
1450
      perror_plus("argp_parse");
1293
1451
      exitcode = EX_OSERR;
1294
1452
      goto end;
1295
1453
    case EINVAL:
1313
1471
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
1472
      }
1315
1473
      if(interface == NULL){
1316
 
        perror("malloc");
 
1474
        perror_plus("malloc");
1317
1475
        free(direntries);
1318
1476
        exitcode = EXIT_FAILURE;
1319
1477
        goto end;
1341
1499
  sigemptyset(&sigterm_action.sa_mask);
1342
1500
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1343
1501
  if(ret == -1){
1344
 
    perror("sigaddset");
 
1502
    perror_plus("sigaddset");
1345
1503
    exitcode = EX_OSERR;
1346
1504
    goto end;
1347
1505
  }
1348
1506
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1349
1507
  if(ret == -1){
1350
 
    perror("sigaddset");
 
1508
    perror_plus("sigaddset");
1351
1509
    exitcode = EX_OSERR;
1352
1510
    goto end;
1353
1511
  }
1354
1512
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1355
1513
  if(ret == -1){
1356
 
    perror("sigaddset");
 
1514
    perror_plus("sigaddset");
1357
1515
    exitcode = EX_OSERR;
1358
1516
    goto end;
1359
1517
  }
1363
1521
  */
1364
1522
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1365
1523
  if(ret == -1){
1366
 
    perror("sigaction");
 
1524
    perror_plus("sigaction");
1367
1525
    return EX_OSERR;
1368
1526
  }
1369
1527
  if(old_sigterm_action.sa_handler != SIG_IGN){
1370
1528
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1371
1529
    if(ret == -1){
1372
 
      perror("sigaction");
 
1530
      perror_plus("sigaction");
1373
1531
      exitcode = EX_OSERR;
1374
1532
      goto end;
1375
1533
    }
1376
1534
  }
1377
1535
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1378
1536
  if(ret == -1){
1379
 
    perror("sigaction");
 
1537
    perror_plus("sigaction");
1380
1538
    return EX_OSERR;
1381
1539
  }
1382
1540
  if(old_sigterm_action.sa_handler != SIG_IGN){
1383
1541
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1384
1542
    if(ret == -1){
1385
 
      perror("sigaction");
 
1543
      perror_plus("sigaction");
1386
1544
      exitcode = EX_OSERR;
1387
1545
      goto end;
1388
1546
    }
1389
1547
  }
1390
1548
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1391
1549
  if(ret == -1){
1392
 
    perror("sigaction");
 
1550
    perror_plus("sigaction");
1393
1551
    return EX_OSERR;
1394
1552
  }
1395
1553
  if(old_sigterm_action.sa_handler != SIG_IGN){
1396
1554
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1397
1555
    if(ret == -1){
1398
 
      perror("sigaction");
 
1556
      perror_plus("sigaction");
1399
1557
      exitcode = EX_OSERR;
1400
1558
      goto end;
1401
1559
    }
1418
1576
    errno = 0;
1419
1577
    ret = seteuid(0);
1420
1578
    if(ret == -1){
1421
 
      perror("seteuid");
 
1579
      perror_plus("seteuid");
1422
1580
    }
1423
1581
    
1424
1582
#ifdef __linux__
1428
1586
    bool restore_loglevel = true;
1429
1587
    if(ret == -1){
1430
1588
      restore_loglevel = false;
1431
 
      perror("klogctl");
 
1589
      perror_plus("klogctl");
1432
1590
    }
1433
1591
#endif  /* __linux__ */
1434
1592
    
1435
1593
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1436
1594
    if(sd < 0){
1437
 
      perror("socket");
 
1595
      perror_plus("socket");
1438
1596
      exitcode = EX_OSERR;
1439
1597
#ifdef __linux__
1440
1598
      if(restore_loglevel){
1441
1599
        ret = klogctl(7, NULL, 0);
1442
1600
        if(ret == -1){
1443
 
          perror("klogctl");
 
1601
          perror_plus("klogctl");
1444
1602
        }
1445
1603
      }
1446
1604
#endif  /* __linux__ */
1448
1606
      errno = 0;
1449
1607
      ret = seteuid(uid);
1450
1608
      if(ret == -1){
1451
 
        perror("seteuid");
 
1609
        perror_plus("seteuid");
1452
1610
      }
1453
1611
      goto end;
1454
1612
    }
1455
1613
    strcpy(network.ifr_name, interface);
1456
1614
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1457
1615
    if(ret == -1){
1458
 
      perror("ioctl SIOCGIFFLAGS");
 
1616
      perror_plus("ioctl SIOCGIFFLAGS");
1459
1617
#ifdef __linux__
1460
1618
      if(restore_loglevel){
1461
1619
        ret = klogctl(7, NULL, 0);
1462
1620
        if(ret == -1){
1463
 
          perror("klogctl");
 
1621
          perror_plus("klogctl");
1464
1622
        }
1465
1623
      }
1466
1624
#endif  /* __linux__ */
1469
1627
      errno = 0;
1470
1628
      ret = seteuid(uid);
1471
1629
      if(ret == -1){
1472
 
        perror("seteuid");
 
1630
        perror_plus("seteuid");
1473
1631
      }
1474
1632
      goto end;
1475
1633
    }
1479
1637
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
1638
      if(ret == -1){
1481
1639
        take_down_interface = false;
1482
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1640
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1483
1641
        exitcode = EX_OSERR;
1484
1642
#ifdef __linux__
1485
1643
        if(restore_loglevel){
1486
1644
          ret = klogctl(7, NULL, 0);
1487
1645
          if(ret == -1){
1488
 
            perror("klogctl");
 
1646
            perror_plus("klogctl");
1489
1647
          }
1490
1648
        }
1491
1649
#endif  /* __linux__ */
1493
1651
        errno = 0;
1494
1652
        ret = seteuid(uid);
1495
1653
        if(ret == -1){
1496
 
          perror("seteuid");
 
1654
          perror_plus("seteuid");
1497
1655
        }
1498
1656
        goto end;
1499
1657
      }
1500
1658
    }
1501
 
    /* sleep checking until interface is running */
 
1659
    /* Sleep checking until interface is running.
 
1660
       Check every 0.25s, up to total time of delay */
1502
1661
    for(int i=0; i < delay * 4; i++){
1503
1662
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1504
1663
      if(ret == -1){
1505
 
        perror("ioctl SIOCGIFFLAGS");
 
1664
        perror_plus("ioctl SIOCGIFFLAGS");
1506
1665
      } else if(network.ifr_flags & IFF_RUNNING){
1507
1666
        break;
1508
1667
      }
1509
1668
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1510
1669
      ret = nanosleep(&sleeptime, NULL);
1511
1670
      if(ret == -1 and errno != EINTR){
1512
 
        perror("nanosleep");
 
1671
        perror_plus("nanosleep");
1513
1672
      }
1514
1673
    }
1515
1674
    if(not take_down_interface){
1516
1675
      /* We won't need the socket anymore */
1517
1676
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1518
1677
      if(ret == -1){
1519
 
        perror("close");
 
1678
        perror_plus("close");
1520
1679
      }
1521
1680
    }
1522
1681
#ifdef __linux__
1524
1683
      /* Restores kernel loglevel to default */
1525
1684
      ret = klogctl(7, NULL, 0);
1526
1685
      if(ret == -1){
1527
 
        perror("klogctl");
 
1686
        perror_plus("klogctl");
1528
1687
      }
1529
1688
    }
1530
1689
#endif  /* __linux__ */
1534
1693
      /* Lower privileges */
1535
1694
      ret = seteuid(uid);
1536
1695
      if(ret == -1){
1537
 
        perror("seteuid");
 
1696
        perror_plus("seteuid");
1538
1697
      }
1539
1698
    } else {
1540
1699
      /* Lower privileges permanently */
1541
1700
      ret = setuid(uid);
1542
1701
      if(ret == -1){
1543
 
        perror("setuid");
 
1702
        perror_plus("setuid");
1544
1703
      }
1545
1704
    }
1546
1705
  }
1562
1721
    goto end;
1563
1722
  }
1564
1723
  
 
1724
  if(mkdtemp(tempdir) == NULL){
 
1725
    perror_plus("mkdtemp");
 
1726
    goto end;
 
1727
  }
1565
1728
  tempdir_created = true;
1566
 
  if(mkdtemp(tempdir) == NULL){
1567
 
    tempdir_created = false;
1568
 
    perror("mkdtemp");
1569
 
    goto end;
1570
 
  }
1571
1729
  
1572
1730
  if(quit_now){
1573
1731
    goto end;
1633
1791
      if(quit_now or ret == 0){
1634
1792
        break;
1635
1793
      }
1636
 
      sleep(15);
 
1794
      sleep((int)retry_interval or 1);
1637
1795
    };
1638
1796
 
1639
1797
    if (not quit_now){
1697
1855
  if(debug){
1698
1856
    fprintf(stderr, "Starting Avahi loop search\n");
1699
1857
  }
1700
 
  
1701
 
  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
  }
1702
1865
  
1703
1866
 end:
1704
1867
  
1725
1888
  if(gpgme_initialized){
1726
1889
    gpgme_release(mc.ctx);
1727
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
  }
1728
1902
  
1729
1903
  /* Take down the network interface */
1730
1904
  if(take_down_interface){
1732
1906
    errno = 0;
1733
1907
    ret = seteuid(0);
1734
1908
    if(ret == -1){
1735
 
      perror("seteuid");
 
1909
      perror_plus("seteuid");
1736
1910
    }
1737
1911
    if(geteuid() == 0){
1738
1912
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1739
1913
      if(ret == -1){
1740
 
        perror("ioctl SIOCGIFFLAGS");
 
1914
        perror_plus("ioctl SIOCGIFFLAGS");
1741
1915
      } else if(network.ifr_flags & IFF_UP) {
1742
1916
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1743
1917
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
1918
        if(ret == -1){
1745
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1919
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1746
1920
        }
1747
1921
      }
1748
1922
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1749
1923
      if(ret == -1){
1750
 
        perror("close");
 
1924
        perror_plus("close");
1751
1925
      }
1752
1926
      /* Lower privileges permanently */
1753
1927
      errno = 0;
1754
1928
      ret = setuid(uid);
1755
1929
      if(ret == -1){
1756
 
        perror("setuid");
 
1930
        perror_plus("setuid");
1757
1931
      }
1758
1932
    }
1759
1933
  }
1760
1934
  
1761
 
  /* Removes the temp directory used by GPGME */
 
1935
  /* Removes the GPGME temp directory and all files inside */
1762
1936
  if(tempdir_created){
1763
 
    DIR *d;
1764
 
    struct dirent *direntry;
1765
 
    d = opendir(tempdir);
1766
 
    if(d == NULL){
1767
 
      if(errno != ENOENT){
1768
 
        perror("opendir");
1769
 
      }
1770
 
    } else {
1771
 
      while(true){
1772
 
        direntry = readdir(d);
1773
 
        if(direntry == NULL){
1774
 
          break;
1775
 
        }
1776
 
        /* Skip "." and ".." */
1777
 
        if(direntry->d_name[0] == '.'
1778
 
           and (direntry->d_name[1] == '\0'
1779
 
                or (direntry->d_name[1] == '.'
1780
 
                    and direntry->d_name[2] == '\0'))){
1781
 
          continue;
1782
 
        }
 
1937
    struct dirent **direntries = NULL;
 
1938
    struct dirent *direntry = NULL;
 
1939
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
 
1940
    if (ret > 0){
 
1941
      for(int i = 0; i < ret; i++){
 
1942
        direntry = direntries[i];
1783
1943
        char *fullname = NULL;
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);
1794
1954
        }
1795
1955
        free(fullname);
1796
1956
      }
1797
 
      closedir(d);
 
1957
    }
 
1958
 
 
1959
    /* need to be cleaned even if ret == 0 because man page doesn't
 
1960
       specify */
 
1961
    free(direntries);
 
1962
    if (ret == -1){
 
1963
      perror_plus("scandir");
1798
1964
    }
1799
1965
    ret = rmdir(tempdir);
1800
1966
    if(ret == -1 and errno != ENOENT){
1801
 
      perror("rmdir");
 
1967
      perror_plus("rmdir");
1802
1968
    }
1803
1969
  }
1804
1970
  
1809
1975
                                            &old_sigterm_action,
1810
1976
                                            NULL));
1811
1977
    if(ret == -1){
1812
 
      perror("sigaction");
 
1978
      perror_plus("sigaction");
1813
1979
    }
1814
1980
    do {
1815
1981
      ret = raise(signal_received);
1816
1982
    } while(ret != 0 and errno == EINTR);
1817
1983
    if(ret != 0){
1818
 
      perror("raise");
 
1984
      perror_plus("raise");
1819
1985
      abort();
1820
1986
    }
1821
1987
    TEMP_FAILURE_RETRY(pause());