/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 01:11:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110713011112-bgf3tenqq60uoyyb
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
                                      libraries.  Remove "$(COMMON)".
                                      Thanks to Angel Abad
                                      <angelabad@ubuntu.com>.
* initramfs-tools-script: Work around change in initramfs-tools,
                          Debian bug #633582.
* plugins.d/mandos-client.c (init_gnutls_global): Bug fix: check for
                errors from gnutls_certificate_allocate_credentials().

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