/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

* 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, program_invocation_short_name */
 
65
#include <errno.h>              /* perror(), errno */
66
66
#include <time.h>               /* nanosleep(), time(), sleep() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
74
                                   getuid(), getgid(), seteuid(),
75
75
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
77
#include <iso646.h>             /* not, or, and */
78
78
#include <argp.h>               /* struct argp_option, error_t, struct
79
79
                                   argp_state, struct argp,
130
130
static const char sys_class_net[] = "/sys/class/net";
131
131
char *connect_to = NULL;
132
132
 
133
 
/* Doubly linked list that need to be circular linked when ever used */
134
 
typedef struct server{
135
 
  const char *ip;
136
 
  uint16_t port;
137
 
  AvahiIfIndex if_index;
138
 
  int af;
139
 
  struct timespec last_seen;
140
 
  struct server *next;
141
 
  struct server *prev;
142
 
} server;
143
 
 
144
133
/* Used for passing in values through the Avahi callback functions */
145
134
typedef struct {
146
135
  AvahiSimplePoll *simple_poll;
150
139
  gnutls_dh_params_t dh_params;
151
140
  const char *priority;
152
141
  gpgme_ctx_t ctx;
153
 
  server *current_server;
154
142
} mandos_context;
155
143
 
156
144
/* global context so signal handler can reach it*/
157
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
158
146
                      .dh_bits = 1024, .priority = "SECURE256"
159
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
160
148
 
161
149
sig_atomic_t quit_now = 0;
162
150
int signal_received = 0;
163
151
 
164
 
/* Function to use when printing errors */
165
 
void perror_plus(const char *print_text){
166
 
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
167
 
  perror(print_text);
168
 
}
169
 
 
170
152
/*
171
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
172
154
 * bytes. "buffer_capacity" is how much is currently allocated,
184
166
  return buffer_capacity;
185
167
}
186
168
 
187
 
int add_server(const char *ip, uint16_t port,
188
 
                 AvahiIfIndex if_index,
189
 
                 int af){
190
 
  int ret;
191
 
  server *new_server = malloc(sizeof(server));
192
 
  if(new_server == NULL){
193
 
    perror_plus("malloc");
194
 
    return -1;
195
 
  }
196
 
  *new_server = (server){ .ip = strdup(ip),
197
 
                         .port = port,
198
 
                         .if_index = if_index,
199
 
                         .af = af };
200
 
  if(new_server->ip == NULL){
201
 
    perror_plus("strdup");
202
 
    return -1;    
203
 
  }
204
 
  /* uniqe case of first server */
205
 
  if (mc.current_server == NULL){
206
 
    new_server->next = new_server;
207
 
    new_server->prev = new_server;
208
 
    mc.current_server = new_server;
209
 
  /* Placing the new server last in the list */
210
 
  } else {
211
 
    new_server->next = mc.current_server;
212
 
    new_server->prev = mc.current_server->prev;
213
 
    new_server->prev->next = new_server;
214
 
    mc.current_server->prev = new_server;
215
 
  }
216
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
217
 
  if(ret == -1){
218
 
    perror_plus("clock_gettime");
219
 
    return -1;
220
 
  }
221
 
  return 0;
222
 
}
223
 
 
224
169
/* 
225
170
 * Initialize GPGME.
226
171
 */
240
185
    
241
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
242
187
    if(fd == -1){
243
 
      perror_plus("open");
 
188
      perror("open");
244
189
      return false;
245
190
    }
246
191
    
260
205
    
261
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
262
207
    if(ret == -1){
263
 
      perror_plus("close");
 
208
      perror("close");
264
209
    }
265
210
    gpgme_data_release(pgp_data);
266
211
    return true;
391
336
  
392
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
393
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
394
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
395
340
    plaintext_length = -1;
396
341
    goto decrypt_end;
397
342
  }
402
347
                                      (size_t)plaintext_length,
403
348
                                      plaintext_capacity);
404
349
    if(plaintext_capacity == 0){
405
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
406
351
        plaintext_length = -1;
407
352
        goto decrypt_end;
408
353
    }
415
360
      break;
416
361
    }
417
362
    if(ret < 0){
418
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
419
364
      plaintext_length = -1;
420
365
      goto decrypt_end;
421
366
    }
478
423
  }
479
424
  
480
425
  /* OpenPGP credentials */
481
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
482
427
  if(ret != GNUTLS_E_SUCCESS){
483
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
484
 
                                                    from
485
 
                                                    -Wunreachable-code
486
 
                                                 */
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n",
487
429
            safer_gnutls_strerror(ret));
488
430
    gnutls_global_deinit();
489
431
    return -1;
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
    }
796
738
    errno = EINTR;
797
739
    goto mandos_end;
798
740
  }
799
 
 
800
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
741
  
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 has been called */
 
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
  }
1110
1047
  free(flagname);
1111
1048
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1049
  /* read line from flags_fd */
1113
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1050
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
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, "Wait until first server is found. No timeout!\n");     
1213
 
      }
1214
 
      ret = avahi_simple_poll_iterate(s, -1);
1215
 
    } else {
1216
 
      if (debug){
1217
 
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
1218
 
      }
1219
 
      /* the current time */
1220
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1221
 
      if(ret == -1){
1222
 
        perror_plus("clock_gettime");
1223
 
        return -1;
1224
 
      }
1225
 
      /* Calculating in ms how long time between now and server
1226
 
         who we visted longest time ago. Now - last seen.  */
1227
 
      waited_time.tv_sec = now.tv_sec - mc.current_server->last_seen.tv_sec;
1228
 
      waited_time.tv_nsec = now.tv_nsec - mc.current_server->last_seen.tv_nsec;
1229
 
      /* total time is 10s/10000ms. Converting to s to ms by 1000/s, and ns to ms by divind by 1000000. */
1230
 
      block_time = (retry_interval - ((intmax_t)waited_time.tv_sec * 1000)) - ((intmax_t)waited_time.tv_nsec / 1000000);
1231
 
 
1232
 
      if (debug){
1233
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
1234
 
      }
1235
 
 
1236
 
      if(block_time <= 0){
1237
 
        ret = start_mandos_communication(mc.current_server->ip,
1238
 
                                   mc.current_server->port,
1239
 
                                   mc.current_server->if_index,
1240
 
                                   mc.current_server->af);
1241
 
        if(ret == 0){
1242
 
          avahi_simple_poll_quit(mc.simple_poll);
1243
 
          return 0;
1244
 
        }
1245
 
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1246
 
        if(ret == -1){
1247
 
          perror_plus("clock_gettime");
1248
 
          return -1;
1249
 
        }
1250
 
        mc.current_server = mc.current_server->next;
1251
 
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
1252
 
      }
1253
 
      
1254
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1255
 
    }
1256
 
    if(ret != 0){
1257
 
      if (ret > 0 or errno != EINTR) {
1258
 
        return (ret != 1) ? ret : 0;
1259
 
      }
1260
 
    }
1261
 
  }
1262
 
}
1263
 
 
1264
1129
int main(int argc, char *argv[]){
1265
1130
  AvahiSServiceBrowser *sb = NULL;
1266
1131
  int error;
1283
1148
  bool gnutls_initialized = false;
1284
1149
  bool gpgme_initialized = false;
1285
1150
  float delay = 2.5f;
1286
 
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
1287
1151
  
1288
1152
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1289
1153
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1295
1159
  errno = 0;
1296
1160
  ret = setgid(gid);
1297
1161
  if(ret == -1){
1298
 
    perror_plus("setgid");
 
1162
    perror("setgid");
1299
1163
  }
1300
1164
  
1301
1165
  /* Lower user privileges (temporarily) */
1302
1166
  errno = 0;
1303
1167
  ret = seteuid(uid);
1304
1168
  if(ret == -1){
1305
 
    perror_plus("seteuid");
 
1169
    perror("seteuid");
1306
1170
  }
1307
1171
  
1308
1172
  if(quit_now){
1343
1207
        .arg = "SECONDS",
1344
1208
        .doc = "Maximum delay to wait for interface startup",
1345
1209
        .group = 2 },
1346
 
      { .name = "retry", .key = 132,
1347
 
        .arg = "SECONDS",
1348
 
        .doc = "Retry interval used when denied by the mandos server",
1349
 
        .group = 2 },
1350
1210
      /*
1351
1211
       * These reproduce what we would get without ARGP_NO_HELP
1352
1212
       */
1396
1256
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1397
1257
          argp_error(state, "Bad delay");
1398
1258
        }
1399
 
      case 132:                 /* --retry */
1400
 
        errno = 0;
1401
 
        retry_interval = strtod(arg, &tmp);
1402
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1403
 
           or (retry_interval * 1000) > INT_MAX){
1404
 
          argp_error(state, "Bad retry interval");
1405
 
        }
1406
1259
        break;
1407
1260
        /*
1408
1261
         * These reproduce what we would get without ARGP_NO_HELP
1436
1289
    case ENOMEM:
1437
1290
    default:
1438
1291
      errno = ret;
1439
 
      perror_plus("argp_parse");
 
1292
      perror("argp_parse");
1440
1293
      exitcode = EX_OSERR;
1441
1294
      goto end;
1442
1295
    case EINVAL:
1460
1313
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1461
1314
      }
1462
1315
      if(interface == NULL){
1463
 
        perror_plus("malloc");
 
1316
        perror("malloc");
1464
1317
        free(direntries);
1465
1318
        exitcode = EXIT_FAILURE;
1466
1319
        goto end;
1488
1341
  sigemptyset(&sigterm_action.sa_mask);
1489
1342
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1490
1343
  if(ret == -1){
1491
 
    perror_plus("sigaddset");
 
1344
    perror("sigaddset");
1492
1345
    exitcode = EX_OSERR;
1493
1346
    goto end;
1494
1347
  }
1495
1348
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1496
1349
  if(ret == -1){
1497
 
    perror_plus("sigaddset");
 
1350
    perror("sigaddset");
1498
1351
    exitcode = EX_OSERR;
1499
1352
    goto end;
1500
1353
  }
1501
1354
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1502
1355
  if(ret == -1){
1503
 
    perror_plus("sigaddset");
 
1356
    perror("sigaddset");
1504
1357
    exitcode = EX_OSERR;
1505
1358
    goto end;
1506
1359
  }
1510
1363
  */
1511
1364
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1512
1365
  if(ret == -1){
1513
 
    perror_plus("sigaction");
 
1366
    perror("sigaction");
1514
1367
    return EX_OSERR;
1515
1368
  }
1516
1369
  if(old_sigterm_action.sa_handler != SIG_IGN){
1517
1370
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1518
1371
    if(ret == -1){
1519
 
      perror_plus("sigaction");
 
1372
      perror("sigaction");
1520
1373
      exitcode = EX_OSERR;
1521
1374
      goto end;
1522
1375
    }
1523
1376
  }
1524
1377
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1525
1378
  if(ret == -1){
1526
 
    perror_plus("sigaction");
 
1379
    perror("sigaction");
1527
1380
    return EX_OSERR;
1528
1381
  }
1529
1382
  if(old_sigterm_action.sa_handler != SIG_IGN){
1530
1383
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1531
1384
    if(ret == -1){
1532
 
      perror_plus("sigaction");
 
1385
      perror("sigaction");
1533
1386
      exitcode = EX_OSERR;
1534
1387
      goto end;
1535
1388
    }
1536
1389
  }
1537
1390
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1538
1391
  if(ret == -1){
1539
 
    perror_plus("sigaction");
 
1392
    perror("sigaction");
1540
1393
    return EX_OSERR;
1541
1394
  }
1542
1395
  if(old_sigterm_action.sa_handler != SIG_IGN){
1543
1396
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1544
1397
    if(ret == -1){
1545
 
      perror_plus("sigaction");
 
1398
      perror("sigaction");
1546
1399
      exitcode = EX_OSERR;
1547
1400
      goto end;
1548
1401
    }
1565
1418
    errno = 0;
1566
1419
    ret = seteuid(0);
1567
1420
    if(ret == -1){
1568
 
      perror_plus("seteuid");
 
1421
      perror("seteuid");
1569
1422
    }
1570
1423
    
1571
1424
#ifdef __linux__
1575
1428
    bool restore_loglevel = true;
1576
1429
    if(ret == -1){
1577
1430
      restore_loglevel = false;
1578
 
      perror_plus("klogctl");
 
1431
      perror("klogctl");
1579
1432
    }
1580
1433
#endif  /* __linux__ */
1581
1434
    
1582
1435
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1583
1436
    if(sd < 0){
1584
 
      perror_plus("socket");
 
1437
      perror("socket");
1585
1438
      exitcode = EX_OSERR;
1586
1439
#ifdef __linux__
1587
1440
      if(restore_loglevel){
1588
1441
        ret = klogctl(7, NULL, 0);
1589
1442
        if(ret == -1){
1590
 
          perror_plus("klogctl");
 
1443
          perror("klogctl");
1591
1444
        }
1592
1445
      }
1593
1446
#endif  /* __linux__ */
1595
1448
      errno = 0;
1596
1449
      ret = seteuid(uid);
1597
1450
      if(ret == -1){
1598
 
        perror_plus("seteuid");
 
1451
        perror("seteuid");
1599
1452
      }
1600
1453
      goto end;
1601
1454
    }
1602
1455
    strcpy(network.ifr_name, interface);
1603
1456
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1604
1457
    if(ret == -1){
1605
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1458
      perror("ioctl SIOCGIFFLAGS");
1606
1459
#ifdef __linux__
1607
1460
      if(restore_loglevel){
1608
1461
        ret = klogctl(7, NULL, 0);
1609
1462
        if(ret == -1){
1610
 
          perror_plus("klogctl");
 
1463
          perror("klogctl");
1611
1464
        }
1612
1465
      }
1613
1466
#endif  /* __linux__ */
1616
1469
      errno = 0;
1617
1470
      ret = seteuid(uid);
1618
1471
      if(ret == -1){
1619
 
        perror_plus("seteuid");
 
1472
        perror("seteuid");
1620
1473
      }
1621
1474
      goto end;
1622
1475
    }
1626
1479
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1627
1480
      if(ret == -1){
1628
1481
        take_down_interface = false;
1629
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1482
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1630
1483
        exitcode = EX_OSERR;
1631
1484
#ifdef __linux__
1632
1485
        if(restore_loglevel){
1633
1486
          ret = klogctl(7, NULL, 0);
1634
1487
          if(ret == -1){
1635
 
            perror_plus("klogctl");
 
1488
            perror("klogctl");
1636
1489
          }
1637
1490
        }
1638
1491
#endif  /* __linux__ */
1640
1493
        errno = 0;
1641
1494
        ret = seteuid(uid);
1642
1495
        if(ret == -1){
1643
 
          perror_plus("seteuid");
 
1496
          perror("seteuid");
1644
1497
        }
1645
1498
        goto end;
1646
1499
      }
1647
1500
    }
1648
 
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
 
1501
    /* sleep checking until interface is running */
1649
1502
    for(int i=0; i < delay * 4; i++){
1650
1503
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1651
1504
      if(ret == -1){
1652
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1505
        perror("ioctl SIOCGIFFLAGS");
1653
1506
      } else if(network.ifr_flags & IFF_RUNNING){
1654
1507
        break;
1655
1508
      }
1656
1509
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1657
1510
      ret = nanosleep(&sleeptime, NULL);
1658
1511
      if(ret == -1 and errno != EINTR){
1659
 
        perror_plus("nanosleep");
 
1512
        perror("nanosleep");
1660
1513
      }
1661
1514
    }
1662
1515
    if(not take_down_interface){
1663
1516
      /* We won't need the socket anymore */
1664
1517
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1665
1518
      if(ret == -1){
1666
 
        perror_plus("close");
 
1519
        perror("close");
1667
1520
      }
1668
1521
    }
1669
1522
#ifdef __linux__
1671
1524
      /* Restores kernel loglevel to default */
1672
1525
      ret = klogctl(7, NULL, 0);
1673
1526
      if(ret == -1){
1674
 
        perror_plus("klogctl");
 
1527
        perror("klogctl");
1675
1528
      }
1676
1529
    }
1677
1530
#endif  /* __linux__ */
1681
1534
      /* Lower privileges */
1682
1535
      ret = seteuid(uid);
1683
1536
      if(ret == -1){
1684
 
        perror_plus("seteuid");
 
1537
        perror("seteuid");
1685
1538
      }
1686
1539
    } else {
1687
1540
      /* Lower privileges permanently */
1688
1541
      ret = setuid(uid);
1689
1542
      if(ret == -1){
1690
 
        perror_plus("setuid");
 
1543
        perror("setuid");
1691
1544
      }
1692
1545
    }
1693
1546
  }
1709
1562
    goto end;
1710
1563
  }
1711
1564
  
 
1565
  tempdir_created = true;
1712
1566
  if(mkdtemp(tempdir) == NULL){
1713
 
    perror_plus("mkdtemp");
 
1567
    tempdir_created = false;
 
1568
    perror("mkdtemp");
1714
1569
    goto end;
1715
1570
  }
1716
 
  tempdir_created = true;
1717
1571
  
1718
1572
  if(quit_now){
1719
1573
    goto end;
1779
1633
      if(quit_now or ret == 0){
1780
1634
        break;
1781
1635
      }
1782
 
      sleep((int)retry_interval or 1);
 
1636
      sleep(15);
1783
1637
    };
1784
1638
 
1785
1639
    if (not quit_now){
1843
1697
  if(debug){
1844
1698
    fprintf(stderr, "Starting Avahi loop search\n");
1845
1699
  }
1846
 
 
1847
 
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1848
 
  if(debug){
1849
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
 
            (ret == 0) ? "successfully" : "with error");
1851
 
  }
 
1700
  
 
1701
  avahi_simple_poll_loop(mc.simple_poll);
1852
1702
  
1853
1703
 end:
1854
1704
  
1875
1725
  if(gpgme_initialized){
1876
1726
    gpgme_release(mc.ctx);
1877
1727
  }
1878
 
 
1879
 
  /* cleans up the circular linked list of mandos servers the client has seen */
1880
 
  if(mc.current_server != NULL){
1881
 
    mc.current_server->prev->next = NULL;
1882
 
    while(mc.current_server != NULL){
1883
 
      server *next = mc.current_server->next;
1884
 
      free(mc.current_server);
1885
 
      mc.current_server = next;
1886
 
    }
1887
 
  }
1888
1728
  
1889
1729
  /* Take down the network interface */
1890
1730
  if(take_down_interface){
1892
1732
    errno = 0;
1893
1733
    ret = seteuid(0);
1894
1734
    if(ret == -1){
1895
 
      perror_plus("seteuid");
 
1735
      perror("seteuid");
1896
1736
    }
1897
1737
    if(geteuid() == 0){
1898
1738
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1899
1739
      if(ret == -1){
1900
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1740
        perror("ioctl SIOCGIFFLAGS");
1901
1741
      } else if(network.ifr_flags & IFF_UP) {
1902
1742
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1903
1743
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1904
1744
        if(ret == -1){
1905
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1745
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1906
1746
        }
1907
1747
      }
1908
1748
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1909
1749
      if(ret == -1){
1910
 
        perror_plus("close");
 
1750
        perror("close");
1911
1751
      }
1912
1752
      /* Lower privileges permanently */
1913
1753
      errno = 0;
1914
1754
      ret = setuid(uid);
1915
1755
      if(ret == -1){
1916
 
        perror_plus("setuid");
 
1756
        perror("setuid");
1917
1757
      }
1918
1758
    }
1919
1759
  }
1920
1760
  
1921
 
  /* Removes the GPGME temp directory and all files inside */
 
1761
  /* Removes the temp directory used by GPGME */
1922
1762
  if(tempdir_created){
1923
 
    struct dirent **direntries = NULL;
1924
 
    struct dirent *direntry = NULL;
1925
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1926
 
    if (ret > 0){
1927
 
      for(int i = 0; i < ret; i++){
1928
 
        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
        }
1929
1783
        char *fullname = NULL;
1930
1784
        ret = asprintf(&fullname, "%s/%s", tempdir,
1931
1785
                       direntry->d_name);
1932
1786
        if(ret < 0){
1933
 
          perror_plus("asprintf");
 
1787
          perror("asprintf");
1934
1788
          continue;
1935
1789
        }
1936
1790
        ret = remove(fullname);
1940
1794
        }
1941
1795
        free(fullname);
1942
1796
      }
1943
 
    }
1944
 
 
1945
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
1946
 
    free(direntries);
1947
 
    if (ret == -1){
1948
 
      perror_plus("scandir");
 
1797
      closedir(d);
1949
1798
    }
1950
1799
    ret = rmdir(tempdir);
1951
1800
    if(ret == -1 and errno != ENOENT){
1952
 
      perror_plus("rmdir");
 
1801
      perror("rmdir");
1953
1802
    }
1954
1803
  }
1955
1804
  
1960
1809
                                            &old_sigterm_action,
1961
1810
                                            NULL));
1962
1811
    if(ret == -1){
1963
 
      perror_plus("sigaction");
 
1812
      perror("sigaction");
1964
1813
    }
1965
1814
    do {
1966
1815
      ret = raise(signal_received);
1967
1816
    } while(ret != 0 and errno == EINTR);
1968
1817
    if(ret != 0){
1969
 
      perror_plus("raise");
 
1818
      perror("raise");
1970
1819
      abort();
1971
1820
    }
1972
1821
    TEMP_FAILURE_RETRY(pause());