/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 at bsnet
  • Date: 2011-03-21 19:22:27 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110321192227-y74ngvvcy9eq6shi
* plugins.d/mandos-client.c (good_interface): Check if the interface
                                              name starts with "."
                                              before anything else.
                                              Bug fix: Fix memory
                                              leak.

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(),
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
131
130
static const char sys_class_net[] = "/sys/class/net";
132
131
char *connect_to = NULL;
133
132
 
134
 
/* Doubly linked list that need to be circularly linked when used */
135
 
typedef struct server{
136
 
  const char *ip;
137
 
  uint16_t port;
138
 
  AvahiIfIndex if_index;
139
 
  int af;
140
 
  struct timespec last_seen;
141
 
  struct server *next;
142
 
  struct server *prev;
143
 
} server;
144
 
 
145
133
/* Used for passing in values through the Avahi callback functions */
146
134
typedef struct {
147
135
  AvahiSimplePoll *simple_poll;
151
139
  gnutls_dh_params_t dh_params;
152
140
  const char *priority;
153
141
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
142
} mandos_context;
156
143
 
157
144
/* global context so signal handler can reach it*/
158
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
146
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
148
 
163
149
sig_atomic_t quit_now = 0;
164
150
int signal_received = 0;
165
151
 
166
 
/* Function to use when printing errors */
167
 
void perror_plus(const char *print_text){
168
 
  fprintf(stderr, "Mandos plugin %s: ",
169
 
          program_invocation_short_name);
170
 
  perror(print_text);
171
 
}
172
 
 
173
152
/*
174
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
154
 * bytes. "buffer_capacity" is how much is currently allocated,
187
166
  return buffer_capacity;
188
167
}
189
168
 
190
 
int add_server(const char *ip, uint16_t port,
191
 
                 AvahiIfIndex if_index,
192
 
                 int af){
193
 
  int ret;
194
 
  server *new_server = malloc(sizeof(server));
195
 
  if(new_server == NULL){
196
 
    perror_plus("malloc");
197
 
    return -1;
198
 
  }
199
 
  *new_server = (server){ .ip = strdup(ip),
200
 
                         .port = port,
201
 
                         .if_index = if_index,
202
 
                         .af = af };
203
 
  if(new_server->ip == NULL){
204
 
    perror_plus("strdup");
205
 
    return -1;
206
 
  }
207
 
  /* unique case of first server */
208
 
  if (mc.current_server == NULL){
209
 
    new_server->next = new_server;
210
 
    new_server->prev = new_server;
211
 
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
213
 
  } else {
214
 
    new_server->next = mc.current_server;
215
 
    new_server->prev = mc.current_server->prev;
216
 
    new_server->prev->next = new_server;
217
 
    mc.current_server->prev = new_server;
218
 
  }
219
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
220
 
  if(ret == -1){
221
 
    perror_plus("clock_gettime");
222
 
    return -1;
223
 
  }
224
 
  return 0;
225
 
}
226
 
 
227
169
/* 
228
170
 * Initialize GPGME.
229
171
 */
243
185
    
244
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
245
187
    if(fd == -1){
246
 
      perror_plus("open");
 
188
      perror("open");
247
189
      return false;
248
190
    }
249
191
    
263
205
    
264
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
265
207
    if(ret == -1){
266
 
      perror_plus("close");
 
208
      perror("close");
267
209
    }
268
210
    gpgme_data_release(pgp_data);
269
211
    return true;
394
336
  
395
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
396
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
397
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
398
340
    plaintext_length = -1;
399
341
    goto decrypt_end;
400
342
  }
405
347
                                      (size_t)plaintext_length,
406
348
                                      plaintext_capacity);
407
349
    if(plaintext_capacity == 0){
408
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
409
351
        plaintext_length = -1;
410
352
        goto decrypt_end;
411
353
    }
418
360
      break;
419
361
    }
420
362
    if(ret < 0){
421
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
422
364
      plaintext_length = -1;
423
365
      goto decrypt_end;
424
366
    }
481
423
  }
482
424
  
483
425
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  gnutls_certificate_allocate_credentials(&mc.cred);
485
427
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
429
                                                    from
 
430
                                                    -Wunreachable-code
 
431
                                                 */
487
432
            safer_gnutls_strerror(ret));
488
433
    gnutls_global_deinit();
489
434
    return -1;
644
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
590
  if(tcp_sd < 0){
646
591
    int e = errno;
647
 
    perror_plus("socket");
 
592
    perror("socket");
648
593
    errno = e;
649
594
    goto mandos_end;
650
595
  }
664
609
  }
665
610
  if(ret < 0 ){
666
611
    int e = errno;
667
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
668
613
    errno = e;
669
614
    goto mandos_end;
670
615
  }
706
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
652
      char interface[IF_NAMESIZE];
708
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
710
655
      } else {
711
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
657
                ip, interface, port);
726
671
                        sizeof(addrstr));
727
672
    }
728
673
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
730
675
    } else {
731
676
      if(strcmp(addrstr, ip) != 0){
732
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
747
692
  if(ret < 0){
748
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
694
      int e = errno;
750
 
      perror_plus("connect");
 
695
      perror("connect");
751
696
      errno = e;
752
697
    }
753
698
    goto mandos_end;
766
711
                                   out_size - written));
767
712
    if(ret == -1){
768
713
      int e = errno;
769
 
      perror_plus("write");
 
714
      perror("write");
770
715
      errno = e;
771
716
      goto mandos_end;
772
717
    }
797
742
    goto mandos_end;
798
743
  }
799
744
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
801
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
746
  
803
747
  if(quit_now){
840
784
                                   buffer_capacity);
841
785
    if(buffer_capacity == 0){
842
786
      int e = errno;
843
 
      perror_plus("incbuffer");
 
787
      perror("incbuffer");
844
788
      errno = e;
845
789
      goto mandos_end;
846
790
    }
951
895
      if(e == 0){
952
896
        e = errno;
953
897
      }
954
 
      perror_plus("close");
 
898
      perror("close");
955
899
    }
956
900
    gnutls_deinit(session);
957
 
    errno = e;
958
901
    if(quit_now){
959
 
      errno = EINTR;
 
902
      e = EINTR;
960
903
      retval = -1;
961
904
    }
 
905
    errno = e;
962
906
  }
963
907
  return retval;
964
908
}
1007
951
                                           avahi_proto_to_af(proto));
1008
952
      if(ret == 0){
1009
953
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
954
      }
1014
955
    }
1015
956
  }
1069
1010
  }
1070
1011
}
1071
1012
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
1013
/* stop main loop after sigterm has been called */
1073
1014
static void handle_sigterm(int sig){
1074
1015
  if(quit_now){
1075
1016
    return;
1077
1018
  quit_now = 1;
1078
1019
  signal_received = sig;
1079
1020
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
1021
  if(mc.simple_poll != NULL){
1082
1022
    avahi_simple_poll_quit(mc.simple_poll);
1083
1023
  }
1098
1038
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
1039
                     if_entry->d_name);
1100
1040
  if(ret < 0){
1101
 
    perror_plus("asprintf");
 
1041
    perror("asprintf");
1102
1042
    return 0;
1103
1043
  }
1104
1044
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
1045
  if(flags_fd == -1){
1106
 
    perror_plus("open");
 
1046
    perror("open");
1107
1047
    free(flagname);
1108
1048
    return 0;
1109
1049
  }
1110
1050
  free(flagname);
1111
1051
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1052
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1053
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1054
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1055
  flagstring[(size_t)to_read] = '\0';
1116
1056
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
 
1057
    perror("malloc");
1118
1058
    close(flags_fd);
1119
1059
    return 0;
1120
1060
  }
1122
1062
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
1063
                                             (size_t)to_read));
1124
1064
    if(ssret == -1){
1125
 
      perror_plus("read");
 
1065
      perror("read");
1126
1066
      free(flagstring);
1127
1067
      close(flags_fd);
1128
1068
      return 0;
1173
1113
    }
1174
1114
    return 0;
1175
1115
  }
1176
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1177
 
  if(flags & IFF_NOARP){
1178
 
    if(debug){
1179
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1180
 
              if_entry->d_name);
1181
 
    }
1182
 
    return 0;
1183
 
  }
1184
1116
  /* Accept this device */
1185
1117
  if(debug){
1186
1118
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1189
1121
  return 1;
1190
1122
}
1191
1123
 
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
1124
int main(int argc, char *argv[]){
1275
1125
  AvahiSServiceBrowser *sb = NULL;
1276
1126
  int error;
1293
1143
  bool gnutls_initialized = false;
1294
1144
  bool gpgme_initialized = false;
1295
1145
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
1298
1146
  
1299
1147
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1148
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1306
1154
  errno = 0;
1307
1155
  ret = setgid(gid);
1308
1156
  if(ret == -1){
1309
 
    perror_plus("setgid");
 
1157
    perror("setgid");
1310
1158
  }
1311
1159
  
1312
1160
  /* Lower user privileges (temporarily) */
1313
1161
  errno = 0;
1314
1162
  ret = seteuid(uid);
1315
1163
  if(ret == -1){
1316
 
    perror_plus("seteuid");
 
1164
    perror("seteuid");
1317
1165
  }
1318
1166
  
1319
1167
  if(quit_now){
1354
1202
        .arg = "SECONDS",
1355
1203
        .doc = "Maximum delay to wait for interface startup",
1356
1204
        .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
1205
      /*
1362
1206
       * These reproduce what we would get without ARGP_NO_HELP
1363
1207
       */
1407
1251
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
1252
          argp_error(state, "Bad delay");
1409
1253
        }
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
1254
        break;
1418
1255
        /*
1419
1256
         * These reproduce what we would get without ARGP_NO_HELP
1447
1284
    case ENOMEM:
1448
1285
    default:
1449
1286
      errno = ret;
1450
 
      perror_plus("argp_parse");
 
1287
      perror("argp_parse");
1451
1288
      exitcode = EX_OSERR;
1452
1289
      goto end;
1453
1290
    case EINVAL:
1471
1308
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1472
1309
      }
1473
1310
      if(interface == NULL){
1474
 
        perror_plus("malloc");
 
1311
        perror("malloc");
1475
1312
        free(direntries);
1476
1313
        exitcode = EXIT_FAILURE;
1477
1314
        goto end;
1499
1336
  sigemptyset(&sigterm_action.sa_mask);
1500
1337
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1501
1338
  if(ret == -1){
1502
 
    perror_plus("sigaddset");
 
1339
    perror("sigaddset");
1503
1340
    exitcode = EX_OSERR;
1504
1341
    goto end;
1505
1342
  }
1506
1343
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1507
1344
  if(ret == -1){
1508
 
    perror_plus("sigaddset");
 
1345
    perror("sigaddset");
1509
1346
    exitcode = EX_OSERR;
1510
1347
    goto end;
1511
1348
  }
1512
1349
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1513
1350
  if(ret == -1){
1514
 
    perror_plus("sigaddset");
 
1351
    perror("sigaddset");
1515
1352
    exitcode = EX_OSERR;
1516
1353
    goto end;
1517
1354
  }
1521
1358
  */
1522
1359
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1523
1360
  if(ret == -1){
1524
 
    perror_plus("sigaction");
 
1361
    perror("sigaction");
1525
1362
    return EX_OSERR;
1526
1363
  }
1527
1364
  if(old_sigterm_action.sa_handler != SIG_IGN){
1528
1365
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1529
1366
    if(ret == -1){
1530
 
      perror_plus("sigaction");
 
1367
      perror("sigaction");
1531
1368
      exitcode = EX_OSERR;
1532
1369
      goto end;
1533
1370
    }
1534
1371
  }
1535
1372
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1536
1373
  if(ret == -1){
1537
 
    perror_plus("sigaction");
 
1374
    perror("sigaction");
1538
1375
    return EX_OSERR;
1539
1376
  }
1540
1377
  if(old_sigterm_action.sa_handler != SIG_IGN){
1541
1378
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1542
1379
    if(ret == -1){
1543
 
      perror_plus("sigaction");
 
1380
      perror("sigaction");
1544
1381
      exitcode = EX_OSERR;
1545
1382
      goto end;
1546
1383
    }
1547
1384
  }
1548
1385
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1549
1386
  if(ret == -1){
1550
 
    perror_plus("sigaction");
 
1387
    perror("sigaction");
1551
1388
    return EX_OSERR;
1552
1389
  }
1553
1390
  if(old_sigterm_action.sa_handler != SIG_IGN){
1554
1391
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1555
1392
    if(ret == -1){
1556
 
      perror_plus("sigaction");
 
1393
      perror("sigaction");
1557
1394
      exitcode = EX_OSERR;
1558
1395
      goto end;
1559
1396
    }
1576
1413
    errno = 0;
1577
1414
    ret = seteuid(0);
1578
1415
    if(ret == -1){
1579
 
      perror_plus("seteuid");
 
1416
      perror("seteuid");
1580
1417
    }
1581
1418
    
1582
1419
#ifdef __linux__
1586
1423
    bool restore_loglevel = true;
1587
1424
    if(ret == -1){
1588
1425
      restore_loglevel = false;
1589
 
      perror_plus("klogctl");
 
1426
      perror("klogctl");
1590
1427
    }
1591
1428
#endif  /* __linux__ */
1592
1429
    
1593
1430
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1594
1431
    if(sd < 0){
1595
 
      perror_plus("socket");
 
1432
      perror("socket");
1596
1433
      exitcode = EX_OSERR;
1597
1434
#ifdef __linux__
1598
1435
      if(restore_loglevel){
1599
1436
        ret = klogctl(7, NULL, 0);
1600
1437
        if(ret == -1){
1601
 
          perror_plus("klogctl");
 
1438
          perror("klogctl");
1602
1439
        }
1603
1440
      }
1604
1441
#endif  /* __linux__ */
1606
1443
      errno = 0;
1607
1444
      ret = seteuid(uid);
1608
1445
      if(ret == -1){
1609
 
        perror_plus("seteuid");
 
1446
        perror("seteuid");
1610
1447
      }
1611
1448
      goto end;
1612
1449
    }
1613
1450
    strcpy(network.ifr_name, interface);
1614
1451
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1615
1452
    if(ret == -1){
1616
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1453
      perror("ioctl SIOCGIFFLAGS");
1617
1454
#ifdef __linux__
1618
1455
      if(restore_loglevel){
1619
1456
        ret = klogctl(7, NULL, 0);
1620
1457
        if(ret == -1){
1621
 
          perror_plus("klogctl");
 
1458
          perror("klogctl");
1622
1459
        }
1623
1460
      }
1624
1461
#endif  /* __linux__ */
1627
1464
      errno = 0;
1628
1465
      ret = seteuid(uid);
1629
1466
      if(ret == -1){
1630
 
        perror_plus("seteuid");
 
1467
        perror("seteuid");
1631
1468
      }
1632
1469
      goto end;
1633
1470
    }
1637
1474
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1638
1475
      if(ret == -1){
1639
1476
        take_down_interface = false;
1640
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1477
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1641
1478
        exitcode = EX_OSERR;
1642
1479
#ifdef __linux__
1643
1480
        if(restore_loglevel){
1644
1481
          ret = klogctl(7, NULL, 0);
1645
1482
          if(ret == -1){
1646
 
            perror_plus("klogctl");
 
1483
            perror("klogctl");
1647
1484
          }
1648
1485
        }
1649
1486
#endif  /* __linux__ */
1651
1488
        errno = 0;
1652
1489
        ret = seteuid(uid);
1653
1490
        if(ret == -1){
1654
 
          perror_plus("seteuid");
 
1491
          perror("seteuid");
1655
1492
        }
1656
1493
        goto end;
1657
1494
      }
1658
1495
    }
1659
 
    /* Sleep checking until interface is running.
1660
 
       Check every 0.25s, up to total time of delay */
 
1496
    /* sleep checking until interface is running */
1661
1497
    for(int i=0; i < delay * 4; i++){
1662
1498
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1663
1499
      if(ret == -1){
1664
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1500
        perror("ioctl SIOCGIFFLAGS");
1665
1501
      } else if(network.ifr_flags & IFF_RUNNING){
1666
1502
        break;
1667
1503
      }
1668
1504
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1669
1505
      ret = nanosleep(&sleeptime, NULL);
1670
1506
      if(ret == -1 and errno != EINTR){
1671
 
        perror_plus("nanosleep");
 
1507
        perror("nanosleep");
1672
1508
      }
1673
1509
    }
1674
1510
    if(not take_down_interface){
1675
1511
      /* We won't need the socket anymore */
1676
1512
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1677
1513
      if(ret == -1){
1678
 
        perror_plus("close");
 
1514
        perror("close");
1679
1515
      }
1680
1516
    }
1681
1517
#ifdef __linux__
1683
1519
      /* Restores kernel loglevel to default */
1684
1520
      ret = klogctl(7, NULL, 0);
1685
1521
      if(ret == -1){
1686
 
        perror_plus("klogctl");
 
1522
        perror("klogctl");
1687
1523
      }
1688
1524
    }
1689
1525
#endif  /* __linux__ */
1693
1529
      /* Lower privileges */
1694
1530
      ret = seteuid(uid);
1695
1531
      if(ret == -1){
1696
 
        perror_plus("seteuid");
 
1532
        perror("seteuid");
1697
1533
      }
1698
1534
    } else {
1699
1535
      /* Lower privileges permanently */
1700
1536
      ret = setuid(uid);
1701
1537
      if(ret == -1){
1702
 
        perror_plus("setuid");
 
1538
        perror("setuid");
1703
1539
      }
1704
1540
    }
1705
1541
  }
1721
1557
    goto end;
1722
1558
  }
1723
1559
  
 
1560
  tempdir_created = true;
1724
1561
  if(mkdtemp(tempdir) == NULL){
1725
 
    perror_plus("mkdtemp");
 
1562
    tempdir_created = false;
 
1563
    perror("mkdtemp");
1726
1564
    goto end;
1727
1565
  }
1728
 
  tempdir_created = true;
1729
1566
  
1730
1567
  if(quit_now){
1731
1568
    goto end;
1791
1628
      if(quit_now or ret == 0){
1792
1629
        break;
1793
1630
      }
1794
 
      sleep((int)retry_interval or 1);
 
1631
      sleep(15);
1795
1632
    };
1796
1633
 
1797
1634
    if (not quit_now){
1855
1692
  if(debug){
1856
1693
    fprintf(stderr, "Starting Avahi loop search\n");
1857
1694
  }
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
 
  }
 
1695
  
 
1696
  avahi_simple_poll_loop(mc.simple_poll);
1865
1697
  
1866
1698
 end:
1867
1699
  
1888
1720
  if(gpgme_initialized){
1889
1721
    gpgme_release(mc.ctx);
1890
1722
  }
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
1723
  
1903
1724
  /* Take down the network interface */
1904
1725
  if(take_down_interface){
1906
1727
    errno = 0;
1907
1728
    ret = seteuid(0);
1908
1729
    if(ret == -1){
1909
 
      perror_plus("seteuid");
 
1730
      perror("seteuid");
1910
1731
    }
1911
1732
    if(geteuid() == 0){
1912
1733
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1913
1734
      if(ret == -1){
1914
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1735
        perror("ioctl SIOCGIFFLAGS");
1915
1736
      } else if(network.ifr_flags & IFF_UP) {
1916
1737
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1917
1738
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1918
1739
        if(ret == -1){
1919
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1740
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1920
1741
        }
1921
1742
      }
1922
1743
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1923
1744
      if(ret == -1){
1924
 
        perror_plus("close");
 
1745
        perror("close");
1925
1746
      }
1926
1747
      /* Lower privileges permanently */
1927
1748
      errno = 0;
1928
1749
      ret = setuid(uid);
1929
1750
      if(ret == -1){
1930
 
        perror_plus("setuid");
 
1751
        perror("setuid");
1931
1752
      }
1932
1753
    }
1933
1754
  }
1934
1755
  
1935
 
  /* Removes the GPGME temp directory and all files inside */
 
1756
  /* Removes the temp directory used by GPGME */
1936
1757
  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];
 
1758
    DIR *d;
 
1759
    struct dirent *direntry;
 
1760
    d = opendir(tempdir);
 
1761
    if(d == NULL){
 
1762
      if(errno != ENOENT){
 
1763
        perror("opendir");
 
1764
      }
 
1765
    } else {
 
1766
      while(true){
 
1767
        direntry = readdir(d);
 
1768
        if(direntry == NULL){
 
1769
          break;
 
1770
        }
 
1771
        /* Skip "." and ".." */
 
1772
        if(direntry->d_name[0] == '.'
 
1773
           and (direntry->d_name[1] == '\0'
 
1774
                or (direntry->d_name[1] == '.'
 
1775
                    and direntry->d_name[2] == '\0'))){
 
1776
          continue;
 
1777
        }
1943
1778
        char *fullname = NULL;
1944
1779
        ret = asprintf(&fullname, "%s/%s", tempdir,
1945
1780
                       direntry->d_name);
1946
1781
        if(ret < 0){
1947
 
          perror_plus("asprintf");
 
1782
          perror("asprintf");
1948
1783
          continue;
1949
1784
        }
1950
1785
        ret = remove(fullname);
1954
1789
        }
1955
1790
        free(fullname);
1956
1791
      }
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");
 
1792
      closedir(d);
1964
1793
    }
1965
1794
    ret = rmdir(tempdir);
1966
1795
    if(ret == -1 and errno != ENOENT){
1967
 
      perror_plus("rmdir");
 
1796
      perror("rmdir");
1968
1797
    }
1969
1798
  }
1970
1799
  
1975
1804
                                            &old_sigterm_action,
1976
1805
                                            NULL));
1977
1806
    if(ret == -1){
1978
 
      perror_plus("sigaction");
 
1807
      perror("sigaction");
1979
1808
    }
1980
1809
    do {
1981
1810
      ret = raise(signal_received);
1982
1811
    } while(ret != 0 and errno == EINTR);
1983
1812
    if(ret != 0){
1984
 
      perror_plus("raise");
 
1813
      perror("raise");
1985
1814
      abort();
1986
1815
    }
1987
1816
    TEMP_FAILURE_RETRY(pause());