/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-27 18:27:02 UTC
  • mfrom: (237.4.16 release)
  • Revision ID: teddy@fukt.bsnet.se-20110727182702-lmoxs1l571yikkl0
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
66
67
#include <time.h>               /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
                                   SIOCSIFFLAGS, if_indextoname(),
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
130
131
static const char sys_class_net[] = "/sys/class/net";
131
132
char *connect_to = NULL;
132
133
 
 
134
/* Doubly linked list that need to be circularly linked when used */
 
135
typedef struct server{
 
136
  const char *ip;
 
137
  uint16_t port;
 
138
  AvahiIfIndex if_index;
 
139
  int af;
 
140
  struct timespec last_seen;
 
141
  struct server *next;
 
142
  struct server *prev;
 
143
} server;
 
144
 
133
145
/* Used for passing in values through the Avahi callback functions */
134
146
typedef struct {
135
147
  AvahiSimplePoll *simple_poll;
139
151
  gnutls_dh_params_t dh_params;
140
152
  const char *priority;
141
153
  gpgme_ctx_t ctx;
 
154
  server *current_server;
142
155
} mandos_context;
143
156
 
144
157
/* global context so signal handler can reach it*/
145
158
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
159
                      .dh_bits = 1024, .priority = "SECURE256"
147
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
160
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
161
                      .current_server = NULL };
148
162
 
149
163
sig_atomic_t quit_now = 0;
150
164
int signal_received = 0;
151
165
 
 
166
/* Function to use when printing errors */
 
167
void perror_plus(const char *print_text){
 
168
  fprintf(stderr, "Mandos plugin %s: ",
 
169
          program_invocation_short_name);
 
170
  perror(print_text);
 
171
}
 
172
 
152
173
/*
153
174
 * Make additional room in "buffer" for at least BUFFER_SIZE more
154
175
 * bytes. "buffer_capacity" is how much is currently allocated,
166
187
  return buffer_capacity;
167
188
}
168
189
 
 
190
int add_server(const char *ip, uint16_t port,
 
191
                 AvahiIfIndex if_index,
 
192
                 int af){
 
193
  int ret;
 
194
  server *new_server = malloc(sizeof(server));
 
195
  if(new_server == NULL){
 
196
    perror_plus("malloc");
 
197
    return -1;
 
198
  }
 
199
  *new_server = (server){ .ip = strdup(ip),
 
200
                         .port = port,
 
201
                         .if_index = if_index,
 
202
                         .af = af };
 
203
  if(new_server->ip == NULL){
 
204
    perror_plus("strdup");
 
205
    return -1;
 
206
  }
 
207
  /* unique case of first server */
 
208
  if (mc.current_server == NULL){
 
209
    new_server->next = new_server;
 
210
    new_server->prev = new_server;
 
211
    mc.current_server = new_server;
 
212
  /* Placing the new server last in the list */
 
213
  } else {
 
214
    new_server->next = mc.current_server;
 
215
    new_server->prev = mc.current_server->prev;
 
216
    new_server->prev->next = new_server;
 
217
    mc.current_server->prev = new_server;
 
218
  }
 
219
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
 
220
  if(ret == -1){
 
221
    perror_plus("clock_gettime");
 
222
    return -1;
 
223
  }
 
224
  return 0;
 
225
}
 
226
 
169
227
/* 
170
228
 * Initialize GPGME.
171
229
 */
185
243
    
186
244
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
187
245
    if(fd == -1){
188
 
      perror("open");
 
246
      perror_plus("open");
189
247
      return false;
190
248
    }
191
249
    
205
263
    
206
264
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
207
265
    if(ret == -1){
208
 
      perror("close");
 
266
      perror_plus("close");
209
267
    }
210
268
    gpgme_data_release(pgp_data);
211
269
    return true;
336
394
  
337
395
  /* Seek back to the beginning of the GPGME plaintext data buffer */
338
396
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
339
 
    perror("gpgme_data_seek");
 
397
    perror_plus("gpgme_data_seek");
340
398
    plaintext_length = -1;
341
399
    goto decrypt_end;
342
400
  }
347
405
                                      (size_t)plaintext_length,
348
406
                                      plaintext_capacity);
349
407
    if(plaintext_capacity == 0){
350
 
        perror("incbuffer");
 
408
        perror_plus("incbuffer");
351
409
        plaintext_length = -1;
352
410
        goto decrypt_end;
353
411
    }
360
418
      break;
361
419
    }
362
420
    if(ret < 0){
363
 
      perror("gpgme_data_read");
 
421
      perror_plus("gpgme_data_read");
364
422
      plaintext_length = -1;
365
423
      goto decrypt_end;
366
424
    }
423
481
  }
424
482
  
425
483
  /* OpenPGP credentials */
426
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
484
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
485
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
429
 
                                                    from
430
 
                                                    -Wunreachable-code
431
 
                                                 */
 
486
    fprintf(stderr, "GnuTLS memory error: %s\n",
432
487
            safer_gnutls_strerror(ret));
433
488
    gnutls_global_deinit();
434
489
    return -1;
589
644
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
645
  if(tcp_sd < 0){
591
646
    int e = errno;
592
 
    perror("socket");
 
647
    perror_plus("socket");
593
648
    errno = e;
594
649
    goto mandos_end;
595
650
  }
609
664
  }
610
665
  if(ret < 0 ){
611
666
    int e = errno;
612
 
    perror("inet_pton");
 
667
    perror_plus("inet_pton");
613
668
    errno = e;
614
669
    goto mandos_end;
615
670
  }
651
706
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
707
      char interface[IF_NAMESIZE];
653
708
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
 
        perror("if_indextoname");
 
709
        perror_plus("if_indextoname");
655
710
      } else {
656
711
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
712
                ip, interface, port);
671
726
                        sizeof(addrstr));
672
727
    }
673
728
    if(pcret == NULL){
674
 
      perror("inet_ntop");
 
729
      perror_plus("inet_ntop");
675
730
    } else {
676
731
      if(strcmp(addrstr, ip) != 0){
677
732
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
692
747
  if(ret < 0){
693
748
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
694
749
      int e = errno;
695
 
      perror("connect");
 
750
      perror_plus("connect");
696
751
      errno = e;
697
752
    }
698
753
    goto mandos_end;
711
766
                                   out_size - written));
712
767
    if(ret == -1){
713
768
      int e = errno;
714
 
      perror("write");
 
769
      perror_plus("write");
715
770
      errno = e;
716
771
      goto mandos_end;
717
772
    }
741
796
    errno = EINTR;
742
797
    goto mandos_end;
743
798
  }
744
 
 
745
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
799
  
 
800
  /* Spurious warning from -Wint-to-pointer-cast */
746
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
747
802
  
748
803
  if(quit_now){
785
840
                                   buffer_capacity);
786
841
    if(buffer_capacity == 0){
787
842
      int e = errno;
788
 
      perror("incbuffer");
 
843
      perror_plus("incbuffer");
789
844
      errno = e;
790
845
      goto mandos_end;
791
846
    }
896
951
      if(e == 0){
897
952
        e = errno;
898
953
      }
899
 
      perror("close");
 
954
      perror_plus("close");
900
955
    }
901
956
    gnutls_deinit(session);
 
957
    errno = e;
902
958
    if(quit_now){
903
 
      e = EINTR;
 
959
      errno = EINTR;
904
960
      retval = -1;
905
961
    }
906
 
    errno = e;
907
962
  }
908
963
  return retval;
909
964
}
952
1007
                                           avahi_proto_to_af(proto));
953
1008
      if(ret == 0){
954
1009
        avahi_simple_poll_quit(mc.simple_poll);
 
1010
      } else {
 
1011
        ret = add_server(ip, port, interface,
 
1012
                         avahi_proto_to_af(proto));
955
1013
      }
956
1014
    }
957
1015
  }
1011
1069
  }
1012
1070
}
1013
1071
 
1014
 
/* stop main loop after sigterm has been called */
 
1072
/* Signal handler that stops main loop after SIGTERM */
1015
1073
static void handle_sigterm(int sig){
1016
1074
  if(quit_now){
1017
1075
    return;
1019
1077
  quit_now = 1;
1020
1078
  signal_received = sig;
1021
1079
  int old_errno = errno;
 
1080
  /* set main loop to exit */
1022
1081
  if(mc.simple_poll != NULL){
1023
1082
    avahi_simple_poll_quit(mc.simple_poll);
1024
1083
  }
1039
1098
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1040
1099
                     if_entry->d_name);
1041
1100
  if(ret < 0){
1042
 
    perror("asprintf");
 
1101
    perror_plus("asprintf");
1043
1102
    return 0;
1044
1103
  }
1045
1104
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1046
1105
  if(flags_fd == -1){
1047
 
    perror("open");
 
1106
    perror_plus("open");
1048
1107
    free(flagname);
1049
1108
    return 0;
1050
1109
  }
1051
1110
  free(flagname);
1052
1111
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1053
1112
  /* read line from flags_fd */
1054
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1113
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1055
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1056
1115
  flagstring[(size_t)to_read] = '\0';
1057
1116
  if(flagstring == NULL){
1058
 
    perror("malloc");
 
1117
    perror_plus("malloc");
1059
1118
    close(flags_fd);
1060
1119
    return 0;
1061
1120
  }
1063
1122
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1064
1123
                                             (size_t)to_read));
1065
1124
    if(ssret == -1){
1066
 
      perror("read");
 
1125
      perror_plus("read");
1067
1126
      free(flagstring);
1068
1127
      close(flags_fd);
1069
1128
      return 0;
1141
1200
  return 1;
1142
1201
}
1143
1202
 
 
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1204
  int ret;
 
1205
  struct timespec now;
 
1206
  struct timespec waited_time;
 
1207
  intmax_t block_time;
 
1208
 
 
1209
  while(true){
 
1210
    if(mc.current_server == NULL){
 
1211
      if (debug){
 
1212
        fprintf(stderr,
 
1213
                "Wait until first server is found. No timeout!\n");
 
1214
      }
 
1215
      ret = avahi_simple_poll_iterate(s, -1);
 
1216
    } else {
 
1217
      if (debug){
 
1218
        fprintf(stderr, "Check current_server if we should run it,"
 
1219
                " or wait\n");
 
1220
      }
 
1221
      /* the current time */
 
1222
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
 
1223
      if(ret == -1){
 
1224
        perror_plus("clock_gettime");
 
1225
        return -1;
 
1226
      }
 
1227
      /* Calculating in ms how long time between now and server
 
1228
         who we visted longest time ago. Now - last seen.  */
 
1229
      waited_time.tv_sec = (now.tv_sec
 
1230
                            - mc.current_server->last_seen.tv_sec);
 
1231
      waited_time.tv_nsec = (now.tv_nsec
 
1232
                             - mc.current_server->last_seen.tv_nsec);
 
1233
      /* total time is 10s/10,000ms.
 
1234
         Converting to s from ms by dividing by 1,000,
 
1235
         and ns to ms by dividing by 1,000,000. */
 
1236
      block_time = ((retry_interval
 
1237
                     - ((intmax_t)waited_time.tv_sec * 1000))
 
1238
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1239
 
 
1240
      if (debug){
 
1241
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1242
      }
 
1243
 
 
1244
      if(block_time <= 0){
 
1245
        ret = start_mandos_communication(mc.current_server->ip,
 
1246
                                         mc.current_server->port,
 
1247
                                         mc.current_server->if_index,
 
1248
                                         mc.current_server->af);
 
1249
        if(ret == 0){
 
1250
          avahi_simple_poll_quit(mc.simple_poll);
 
1251
          return 0;
 
1252
        }
 
1253
        ret = clock_gettime(CLOCK_MONOTONIC,
 
1254
                            &mc.current_server->last_seen);
 
1255
        if(ret == -1){
 
1256
          perror_plus("clock_gettime");
 
1257
          return -1;
 
1258
        }
 
1259
        mc.current_server = mc.current_server->next;
 
1260
        block_time = 0;         /* Call avahi to find new Mandos
 
1261
                                   servers, but don't block */
 
1262
      }
 
1263
      
 
1264
      ret = avahi_simple_poll_iterate(s, (int)block_time);
 
1265
    }
 
1266
    if(ret != 0){
 
1267
      if (ret > 0 or errno != EINTR) {
 
1268
        return (ret != 1) ? ret : 0;
 
1269
      }
 
1270
    }
 
1271
  }
 
1272
}
 
1273
 
1144
1274
int main(int argc, char *argv[]){
1145
1275
  AvahiSServiceBrowser *sb = NULL;
1146
1276
  int error;
1163
1293
  bool gnutls_initialized = false;
1164
1294
  bool gpgme_initialized = false;
1165
1295
  float delay = 2.5f;
 
1296
  double retry_interval = 10; /* 10s between trying a server and
 
1297
                                 retrying the same server again */
1166
1298
  
1167
1299
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1168
1300
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1174
1306
  errno = 0;
1175
1307
  ret = setgid(gid);
1176
1308
  if(ret == -1){
1177
 
    perror("setgid");
 
1309
    perror_plus("setgid");
1178
1310
  }
1179
1311
  
1180
1312
  /* Lower user privileges (temporarily) */
1181
1313
  errno = 0;
1182
1314
  ret = seteuid(uid);
1183
1315
  if(ret == -1){
1184
 
    perror("seteuid");
 
1316
    perror_plus("seteuid");
1185
1317
  }
1186
1318
  
1187
1319
  if(quit_now){
1222
1354
        .arg = "SECONDS",
1223
1355
        .doc = "Maximum delay to wait for interface startup",
1224
1356
        .group = 2 },
 
1357
      { .name = "retry", .key = 132,
 
1358
        .arg = "SECONDS",
 
1359
        .doc = "Retry interval used when denied by the mandos server",
 
1360
        .group = 2 },
1225
1361
      /*
1226
1362
       * These reproduce what we would get without ARGP_NO_HELP
1227
1363
       */
1271
1407
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1272
1408
          argp_error(state, "Bad delay");
1273
1409
        }
 
1410
      case 132:                 /* --retry */
 
1411
        errno = 0;
 
1412
        retry_interval = strtod(arg, &tmp);
 
1413
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1414
           or (retry_interval * 1000) > INT_MAX){
 
1415
          argp_error(state, "Bad retry interval");
 
1416
        }
1274
1417
        break;
1275
1418
        /*
1276
1419
         * These reproduce what we would get without ARGP_NO_HELP
1304
1447
    case ENOMEM:
1305
1448
    default:
1306
1449
      errno = ret;
1307
 
      perror("argp_parse");
 
1450
      perror_plus("argp_parse");
1308
1451
      exitcode = EX_OSERR;
1309
1452
      goto end;
1310
1453
    case EINVAL:
1312
1455
      goto end;
1313
1456
    }
1314
1457
  }
 
1458
    
 
1459
  {
 
1460
    /* Work around Debian bug #633582:
 
1461
       <http://bugs.debian.org/633582> */
 
1462
    struct stat st;
 
1463
    
 
1464
    /* Re-raise priviliges */
 
1465
    errno = 0;
 
1466
    ret = seteuid(0);
 
1467
    if(ret == -1){
 
1468
      perror_plus("seteuid");
 
1469
    }
 
1470
    
 
1471
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
 
1472
    if(seckey_fd == -1){
 
1473
      perror_plus("open");
 
1474
    } else {
 
1475
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1476
      if(ret == -1){
 
1477
        perror_plus("fstat");
 
1478
      } else {
 
1479
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1480
          ret = fchown(seckey_fd, uid, gid);
 
1481
          if(ret == -1){
 
1482
            perror_plus("fchown");
 
1483
          }
 
1484
        }
 
1485
      }
 
1486
      TEMP_FAILURE_RETRY(close(seckey_fd));
 
1487
    }
 
1488
    
 
1489
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
 
1490
    if(pubkey_fd == -1){
 
1491
      perror_plus("open");
 
1492
    } else {
 
1493
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1494
      if(ret == -1){
 
1495
        perror_plus("fstat");
 
1496
      } else {
 
1497
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1498
          ret = fchown(pubkey_fd, uid, gid);
 
1499
          if(ret == -1){
 
1500
            perror_plus("fchown");
 
1501
          }
 
1502
        }
 
1503
      }
 
1504
      TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1505
    }
 
1506
    
 
1507
    /* Lower privileges */
 
1508
    errno = 0;
 
1509
    ret = seteuid(uid);
 
1510
    if(ret == -1){
 
1511
      perror_plus("seteuid");
 
1512
    }
 
1513
  }
1315
1514
  
1316
1515
  if(not debug){
1317
1516
    avahi_set_log_function(empty_log);
1328
1527
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1329
1528
      }
1330
1529
      if(interface == NULL){
1331
 
        perror("malloc");
 
1530
        perror_plus("malloc");
1332
1531
        free(direntries);
1333
1532
        exitcode = EXIT_FAILURE;
1334
1533
        goto end;
1356
1555
  sigemptyset(&sigterm_action.sa_mask);
1357
1556
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1358
1557
  if(ret == -1){
1359
 
    perror("sigaddset");
 
1558
    perror_plus("sigaddset");
1360
1559
    exitcode = EX_OSERR;
1361
1560
    goto end;
1362
1561
  }
1363
1562
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1364
1563
  if(ret == -1){
1365
 
    perror("sigaddset");
 
1564
    perror_plus("sigaddset");
1366
1565
    exitcode = EX_OSERR;
1367
1566
    goto end;
1368
1567
  }
1369
1568
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1370
1569
  if(ret == -1){
1371
 
    perror("sigaddset");
 
1570
    perror_plus("sigaddset");
1372
1571
    exitcode = EX_OSERR;
1373
1572
    goto end;
1374
1573
  }
1378
1577
  */
1379
1578
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1380
1579
  if(ret == -1){
1381
 
    perror("sigaction");
 
1580
    perror_plus("sigaction");
1382
1581
    return EX_OSERR;
1383
1582
  }
1384
1583
  if(old_sigterm_action.sa_handler != SIG_IGN){
1385
1584
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1386
1585
    if(ret == -1){
1387
 
      perror("sigaction");
 
1586
      perror_plus("sigaction");
1388
1587
      exitcode = EX_OSERR;
1389
1588
      goto end;
1390
1589
    }
1391
1590
  }
1392
1591
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1393
1592
  if(ret == -1){
1394
 
    perror("sigaction");
 
1593
    perror_plus("sigaction");
1395
1594
    return EX_OSERR;
1396
1595
  }
1397
1596
  if(old_sigterm_action.sa_handler != SIG_IGN){
1398
1597
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1399
1598
    if(ret == -1){
1400
 
      perror("sigaction");
 
1599
      perror_plus("sigaction");
1401
1600
      exitcode = EX_OSERR;
1402
1601
      goto end;
1403
1602
    }
1404
1603
  }
1405
1604
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1406
1605
  if(ret == -1){
1407
 
    perror("sigaction");
 
1606
    perror_plus("sigaction");
1408
1607
    return EX_OSERR;
1409
1608
  }
1410
1609
  if(old_sigterm_action.sa_handler != SIG_IGN){
1411
1610
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1412
1611
    if(ret == -1){
1413
 
      perror("sigaction");
 
1612
      perror_plus("sigaction");
1414
1613
      exitcode = EX_OSERR;
1415
1614
      goto end;
1416
1615
    }
1433
1632
    errno = 0;
1434
1633
    ret = seteuid(0);
1435
1634
    if(ret == -1){
1436
 
      perror("seteuid");
 
1635
      perror_plus("seteuid");
1437
1636
    }
1438
1637
    
1439
1638
#ifdef __linux__
1443
1642
    bool restore_loglevel = true;
1444
1643
    if(ret == -1){
1445
1644
      restore_loglevel = false;
1446
 
      perror("klogctl");
 
1645
      perror_plus("klogctl");
1447
1646
    }
1448
1647
#endif  /* __linux__ */
1449
1648
    
1450
1649
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1451
1650
    if(sd < 0){
1452
 
      perror("socket");
 
1651
      perror_plus("socket");
1453
1652
      exitcode = EX_OSERR;
1454
1653
#ifdef __linux__
1455
1654
      if(restore_loglevel){
1456
1655
        ret = klogctl(7, NULL, 0);
1457
1656
        if(ret == -1){
1458
 
          perror("klogctl");
 
1657
          perror_plus("klogctl");
1459
1658
        }
1460
1659
      }
1461
1660
#endif  /* __linux__ */
1463
1662
      errno = 0;
1464
1663
      ret = seteuid(uid);
1465
1664
      if(ret == -1){
1466
 
        perror("seteuid");
 
1665
        perror_plus("seteuid");
1467
1666
      }
1468
1667
      goto end;
1469
1668
    }
1470
1669
    strcpy(network.ifr_name, interface);
1471
1670
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1472
1671
    if(ret == -1){
1473
 
      perror("ioctl SIOCGIFFLAGS");
 
1672
      perror_plus("ioctl SIOCGIFFLAGS");
1474
1673
#ifdef __linux__
1475
1674
      if(restore_loglevel){
1476
1675
        ret = klogctl(7, NULL, 0);
1477
1676
        if(ret == -1){
1478
 
          perror("klogctl");
 
1677
          perror_plus("klogctl");
1479
1678
        }
1480
1679
      }
1481
1680
#endif  /* __linux__ */
1484
1683
      errno = 0;
1485
1684
      ret = seteuid(uid);
1486
1685
      if(ret == -1){
1487
 
        perror("seteuid");
 
1686
        perror_plus("seteuid");
1488
1687
      }
1489
1688
      goto end;
1490
1689
    }
1494
1693
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1495
1694
      if(ret == -1){
1496
1695
        take_down_interface = false;
1497
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1696
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1498
1697
        exitcode = EX_OSERR;
1499
1698
#ifdef __linux__
1500
1699
        if(restore_loglevel){
1501
1700
          ret = klogctl(7, NULL, 0);
1502
1701
          if(ret == -1){
1503
 
            perror("klogctl");
 
1702
            perror_plus("klogctl");
1504
1703
          }
1505
1704
        }
1506
1705
#endif  /* __linux__ */
1508
1707
        errno = 0;
1509
1708
        ret = seteuid(uid);
1510
1709
        if(ret == -1){
1511
 
          perror("seteuid");
 
1710
          perror_plus("seteuid");
1512
1711
        }
1513
1712
        goto end;
1514
1713
      }
1515
1714
    }
1516
 
    /* sleep checking until interface is running */
 
1715
    /* Sleep checking until interface is running.
 
1716
       Check every 0.25s, up to total time of delay */
1517
1717
    for(int i=0; i < delay * 4; i++){
1518
1718
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1519
1719
      if(ret == -1){
1520
 
        perror("ioctl SIOCGIFFLAGS");
 
1720
        perror_plus("ioctl SIOCGIFFLAGS");
1521
1721
      } else if(network.ifr_flags & IFF_RUNNING){
1522
1722
        break;
1523
1723
      }
1524
1724
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1525
1725
      ret = nanosleep(&sleeptime, NULL);
1526
1726
      if(ret == -1 and errno != EINTR){
1527
 
        perror("nanosleep");
 
1727
        perror_plus("nanosleep");
1528
1728
      }
1529
1729
    }
1530
1730
    if(not take_down_interface){
1531
1731
      /* We won't need the socket anymore */
1532
1732
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1533
1733
      if(ret == -1){
1534
 
        perror("close");
 
1734
        perror_plus("close");
1535
1735
      }
1536
1736
    }
1537
1737
#ifdef __linux__
1539
1739
      /* Restores kernel loglevel to default */
1540
1740
      ret = klogctl(7, NULL, 0);
1541
1741
      if(ret == -1){
1542
 
        perror("klogctl");
 
1742
        perror_plus("klogctl");
1543
1743
      }
1544
1744
    }
1545
1745
#endif  /* __linux__ */
1549
1749
      /* Lower privileges */
1550
1750
      ret = seteuid(uid);
1551
1751
      if(ret == -1){
1552
 
        perror("seteuid");
 
1752
        perror_plus("seteuid");
1553
1753
      }
1554
1754
    } else {
1555
1755
      /* Lower privileges permanently */
1556
1756
      ret = setuid(uid);
1557
1757
      if(ret == -1){
1558
 
        perror("setuid");
 
1758
        perror_plus("setuid");
1559
1759
      }
1560
1760
    }
1561
1761
  }
1578
1778
  }
1579
1779
  
1580
1780
  if(mkdtemp(tempdir) == NULL){
1581
 
    perror("mkdtemp");
 
1781
    perror_plus("mkdtemp");
1582
1782
    goto end;
1583
1783
  }
1584
1784
  tempdir_created = true;
1647
1847
      if(quit_now or ret == 0){
1648
1848
        break;
1649
1849
      }
1650
 
      sleep(15);
 
1850
      sleep((int)retry_interval or 1);
1651
1851
    };
1652
1852
 
1653
1853
    if (not quit_now){
1711
1911
  if(debug){
1712
1912
    fprintf(stderr, "Starting Avahi loop search\n");
1713
1913
  }
1714
 
  
1715
 
  avahi_simple_poll_loop(mc.simple_poll);
 
1914
 
 
1915
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
1916
                                (int)(retry_interval * 1000));
 
1917
  if(debug){
 
1918
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
1919
            (ret == 0) ? "successfully" : "with error");
 
1920
  }
1716
1921
  
1717
1922
 end:
1718
1923
  
1739
1944
  if(gpgme_initialized){
1740
1945
    gpgme_release(mc.ctx);
1741
1946
  }
 
1947
 
 
1948
  /* Cleans up the circular linked list of Mandos servers the client
 
1949
     has seen */
 
1950
  if(mc.current_server != NULL){
 
1951
    mc.current_server->prev->next = NULL;
 
1952
    while(mc.current_server != NULL){
 
1953
      server *next = mc.current_server->next;
 
1954
      free(mc.current_server);
 
1955
      mc.current_server = next;
 
1956
    }
 
1957
  }
1742
1958
  
1743
1959
  /* Take down the network interface */
1744
1960
  if(take_down_interface){
1746
1962
    errno = 0;
1747
1963
    ret = seteuid(0);
1748
1964
    if(ret == -1){
1749
 
      perror("seteuid");
 
1965
      perror_plus("seteuid");
1750
1966
    }
1751
1967
    if(geteuid() == 0){
1752
1968
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1753
1969
      if(ret == -1){
1754
 
        perror("ioctl SIOCGIFFLAGS");
 
1970
        perror_plus("ioctl SIOCGIFFLAGS");
1755
1971
      } else if(network.ifr_flags & IFF_UP) {
1756
1972
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1757
1973
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1758
1974
        if(ret == -1){
1759
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1975
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1760
1976
        }
1761
1977
      }
1762
1978
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1763
1979
      if(ret == -1){
1764
 
        perror("close");
 
1980
        perror_plus("close");
1765
1981
      }
1766
1982
      /* Lower privileges permanently */
1767
1983
      errno = 0;
1768
1984
      ret = setuid(uid);
1769
1985
      if(ret == -1){
1770
 
        perror("setuid");
 
1986
        perror_plus("setuid");
1771
1987
      }
1772
1988
    }
1773
1989
  }
1784
2000
        ret = asprintf(&fullname, "%s/%s", tempdir,
1785
2001
                       direntry->d_name);
1786
2002
        if(ret < 0){
1787
 
          perror("asprintf");
 
2003
          perror_plus("asprintf");
1788
2004
          continue;
1789
2005
        }
1790
2006
        ret = remove(fullname);
1796
2012
      }
1797
2013
    }
1798
2014
 
1799
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
 
2015
    /* need to be cleaned even if ret == 0 because man page doesn't
 
2016
       specify */
1800
2017
    free(direntries);
1801
2018
    if (ret == -1){
1802
 
      perror("scandir");
 
2019
      perror_plus("scandir");
1803
2020
    }
1804
2021
    ret = rmdir(tempdir);
1805
2022
    if(ret == -1 and errno != ENOENT){
1806
 
      perror("rmdir");
 
2023
      perror_plus("rmdir");
1807
2024
    }
1808
2025
  }
1809
2026
  
1814
2031
                                            &old_sigterm_action,
1815
2032
                                            NULL));
1816
2033
    if(ret == -1){
1817
 
      perror("sigaction");
 
2034
      perror_plus("sigaction");
1818
2035
    }
1819
2036
    do {
1820
2037
      ret = raise(signal_received);
1821
2038
    } while(ret != 0 and errno == EINTR);
1822
2039
    if(ret != 0){
1823
 
      perror("raise");
 
2040
      perror_plus("raise");
1824
2041
      abort();
1825
2042
    }
1826
2043
    TEMP_FAILURE_RETRY(pause());