/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

  • Committer: Teddy Hogeborn
  • Date: 2011-07-25 18:52:11 UTC
  • mfrom: (237.7.37 trunk)
  • Revision ID: teddy@fukt.bsnet.se-20110725185211-wlitr9jvs70e1xh8
MergeĀ fromĀ trunk.

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
    }
742
797
    goto mandos_end;
743
798
  }
744
799
  
 
800
  /* Spurious warning from -Wint-to-pointer-cast */
745
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
746
802
  
747
803
  if(quit_now){
784
840
                                   buffer_capacity);
785
841
    if(buffer_capacity == 0){
786
842
      int e = errno;
787
 
      perror("incbuffer");
 
843
      perror_plus("incbuffer");
788
844
      errno = e;
789
845
      goto mandos_end;
790
846
    }
895
951
      if(e == 0){
896
952
        e = errno;
897
953
      }
898
 
      perror("close");
 
954
      perror_plus("close");
899
955
    }
900
956
    gnutls_deinit(session);
 
957
    errno = e;
901
958
    if(quit_now){
902
 
      e = EINTR;
 
959
      errno = EINTR;
903
960
      retval = -1;
904
961
    }
905
 
    errno = e;
906
962
  }
907
963
  return retval;
908
964
}
951
1007
                                           avahi_proto_to_af(proto));
952
1008
      if(ret == 0){
953
1009
        avahi_simple_poll_quit(mc.simple_poll);
 
1010
      } else {
 
1011
        ret = add_server(ip, port, interface,
 
1012
                         avahi_proto_to_af(proto));
954
1013
      }
955
1014
    }
956
1015
  }
1010
1069
  }
1011
1070
}
1012
1071
 
1013
 
/* stop main loop after sigterm has been called */
 
1072
/* Signal handler that stops main loop after SIGTERM */
1014
1073
static void handle_sigterm(int sig){
1015
1074
  if(quit_now){
1016
1075
    return;
1018
1077
  quit_now = 1;
1019
1078
  signal_received = sig;
1020
1079
  int old_errno = errno;
 
1080
  /* set main loop to exit */
1021
1081
  if(mc.simple_poll != NULL){
1022
1082
    avahi_simple_poll_quit(mc.simple_poll);
1023
1083
  }
1032
1092
int good_interface(const struct dirent *if_entry){
1033
1093
  ssize_t ssret;
1034
1094
  char *flagname = NULL;
 
1095
  if(if_entry->d_name[0] == '.'){
 
1096
    return 0;
 
1097
  }
1035
1098
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
1099
                     if_entry->d_name);
1037
1100
  if(ret < 0){
1038
 
    perror("asprintf");
1039
 
    return 0;
1040
 
  }
1041
 
  if(if_entry->d_name[0] == '.'){
 
1101
    perror_plus("asprintf");
1042
1102
    return 0;
1043
1103
  }
1044
1104
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1045
1105
  if(flags_fd == -1){
1046
 
    perror("open");
 
1106
    perror_plus("open");
 
1107
    free(flagname);
1047
1108
    return 0;
1048
1109
  }
 
1110
  free(flagname);
1049
1111
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1050
1112
  /* read line from flags_fd */
1051
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1113
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1052
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
1115
  flagstring[(size_t)to_read] = '\0';
1054
1116
  if(flagstring == NULL){
1055
 
    perror("malloc");
 
1117
    perror_plus("malloc");
1056
1118
    close(flags_fd);
1057
1119
    return 0;
1058
1120
  }
1060
1122
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1061
1123
                                             (size_t)to_read));
1062
1124
    if(ssret == -1){
1063
 
      perror("read");
 
1125
      perror_plus("read");
1064
1126
      free(flagstring);
1065
1127
      close(flags_fd);
1066
1128
      return 0;
1111
1173
    }
1112
1174
    return 0;
1113
1175
  }
 
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
  }
1114
1184
  /* Accept this device */
1115
1185
  if(debug){
1116
1186
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1119
1189
  return 1;
1120
1190
}
1121
1191
 
 
1192
int notdotentries(const struct dirent *direntry){
 
1193
  /* Skip "." and ".." */
 
1194
  if(direntry->d_name[0] == '.'
 
1195
     and (direntry->d_name[1] == '\0'
 
1196
          or (direntry->d_name[1] == '.'
 
1197
              and direntry->d_name[2] == '\0'))){
 
1198
    return 0;
 
1199
  }
 
1200
  return 1;
 
1201
}
 
1202
 
 
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1204
  int ret;
 
1205
  struct timespec now;
 
1206
  struct timespec waited_time;
 
1207
  intmax_t block_time;
 
1208
 
 
1209
  while(true){
 
1210
    if(mc.current_server == NULL){
 
1211
      if (debug){
 
1212
        fprintf(stderr,
 
1213
                "Wait until first server is found. No timeout!\n");
 
1214
      }
 
1215
      ret = avahi_simple_poll_iterate(s, -1);
 
1216
    } else {
 
1217
      if (debug){
 
1218
        fprintf(stderr, "Check current_server if we should run it,"
 
1219
                " or wait\n");
 
1220
      }
 
1221
      /* the current time */
 
1222
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
 
1223
      if(ret == -1){
 
1224
        perror_plus("clock_gettime");
 
1225
        return -1;
 
1226
      }
 
1227
      /* Calculating in ms how long time between now and server
 
1228
         who we visted longest time ago. Now - last seen.  */
 
1229
      waited_time.tv_sec = (now.tv_sec
 
1230
                            - mc.current_server->last_seen.tv_sec);
 
1231
      waited_time.tv_nsec = (now.tv_nsec
 
1232
                             - mc.current_server->last_seen.tv_nsec);
 
1233
      /* total time is 10s/10,000ms.
 
1234
         Converting to s from ms by dividing by 1,000,
 
1235
         and ns to ms by dividing by 1,000,000. */
 
1236
      block_time = ((retry_interval
 
1237
                     - ((intmax_t)waited_time.tv_sec * 1000))
 
1238
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1239
 
 
1240
      if (debug){
 
1241
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1242
      }
 
1243
 
 
1244
      if(block_time <= 0){
 
1245
        ret = start_mandos_communication(mc.current_server->ip,
 
1246
                                         mc.current_server->port,
 
1247
                                         mc.current_server->if_index,
 
1248
                                         mc.current_server->af);
 
1249
        if(ret == 0){
 
1250
          avahi_simple_poll_quit(mc.simple_poll);
 
1251
          return 0;
 
1252
        }
 
1253
        ret = clock_gettime(CLOCK_MONOTONIC,
 
1254
                            &mc.current_server->last_seen);
 
1255
        if(ret == -1){
 
1256
          perror_plus("clock_gettime");
 
1257
          return -1;
 
1258
        }
 
1259
        mc.current_server = mc.current_server->next;
 
1260
        block_time = 0;         /* Call avahi to find new Mandos
 
1261
                                   servers, but don't block */
 
1262
      }
 
1263
      
 
1264
      ret = avahi_simple_poll_iterate(s, (int)block_time);
 
1265
    }
 
1266
    if(ret != 0){
 
1267
      if (ret > 0 or errno != EINTR) {
 
1268
        return (ret != 1) ? ret : 0;
 
1269
      }
 
1270
    }
 
1271
  }
 
1272
}
 
1273
 
1122
1274
int main(int argc, char *argv[]){
1123
1275
  AvahiSServiceBrowser *sb = NULL;
1124
1276
  int error;
1141
1293
  bool gnutls_initialized = false;
1142
1294
  bool gpgme_initialized = false;
1143
1295
  float delay = 2.5f;
 
1296
  double retry_interval = 10; /* 10s between trying a server and
 
1297
                                 retrying the same server again */
1144
1298
  
1145
1299
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1146
1300
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1152
1306
  errno = 0;
1153
1307
  ret = setgid(gid);
1154
1308
  if(ret == -1){
1155
 
    perror("setgid");
 
1309
    perror_plus("setgid");
1156
1310
  }
1157
1311
  
1158
1312
  /* Lower user privileges (temporarily) */
1159
1313
  errno = 0;
1160
1314
  ret = seteuid(uid);
1161
1315
  if(ret == -1){
1162
 
    perror("seteuid");
 
1316
    perror_plus("seteuid");
1163
1317
  }
1164
1318
  
1165
1319
  if(quit_now){
1200
1354
        .arg = "SECONDS",
1201
1355
        .doc = "Maximum delay to wait for interface startup",
1202
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 },
1203
1361
      /*
1204
1362
       * These reproduce what we would get without ARGP_NO_HELP
1205
1363
       */
1249
1407
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1250
1408
          argp_error(state, "Bad delay");
1251
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
        }
1252
1417
        break;
1253
1418
        /*
1254
1419
         * These reproduce what we would get without ARGP_NO_HELP
1282
1447
    case ENOMEM:
1283
1448
    default:
1284
1449
      errno = ret;
1285
 
      perror("argp_parse");
 
1450
      perror_plus("argp_parse");
1286
1451
      exitcode = EX_OSERR;
1287
1452
      goto end;
1288
1453
    case EINVAL:
1290
1455
      goto end;
1291
1456
    }
1292
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
  }
1293
1514
  
1294
1515
  if(not debug){
1295
1516
    avahi_set_log_function(empty_log);
1306
1527
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1307
1528
      }
1308
1529
      if(interface == NULL){
1309
 
        perror("malloc");
 
1530
        perror_plus("malloc");
1310
1531
        free(direntries);
1311
1532
        exitcode = EXIT_FAILURE;
1312
1533
        goto end;
1334
1555
  sigemptyset(&sigterm_action.sa_mask);
1335
1556
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1336
1557
  if(ret == -1){
1337
 
    perror("sigaddset");
 
1558
    perror_plus("sigaddset");
1338
1559
    exitcode = EX_OSERR;
1339
1560
    goto end;
1340
1561
  }
1341
1562
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1342
1563
  if(ret == -1){
1343
 
    perror("sigaddset");
 
1564
    perror_plus("sigaddset");
1344
1565
    exitcode = EX_OSERR;
1345
1566
    goto end;
1346
1567
  }
1347
1568
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1348
1569
  if(ret == -1){
1349
 
    perror("sigaddset");
 
1570
    perror_plus("sigaddset");
1350
1571
    exitcode = EX_OSERR;
1351
1572
    goto end;
1352
1573
  }
1356
1577
  */
1357
1578
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1358
1579
  if(ret == -1){
1359
 
    perror("sigaction");
 
1580
    perror_plus("sigaction");
1360
1581
    return EX_OSERR;
1361
1582
  }
1362
1583
  if(old_sigterm_action.sa_handler != SIG_IGN){
1363
1584
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1364
1585
    if(ret == -1){
1365
 
      perror("sigaction");
 
1586
      perror_plus("sigaction");
1366
1587
      exitcode = EX_OSERR;
1367
1588
      goto end;
1368
1589
    }
1369
1590
  }
1370
1591
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1371
1592
  if(ret == -1){
1372
 
    perror("sigaction");
 
1593
    perror_plus("sigaction");
1373
1594
    return EX_OSERR;
1374
1595
  }
1375
1596
  if(old_sigterm_action.sa_handler != SIG_IGN){
1376
1597
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1377
1598
    if(ret == -1){
1378
 
      perror("sigaction");
 
1599
      perror_plus("sigaction");
1379
1600
      exitcode = EX_OSERR;
1380
1601
      goto end;
1381
1602
    }
1382
1603
  }
1383
1604
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1384
1605
  if(ret == -1){
1385
 
    perror("sigaction");
 
1606
    perror_plus("sigaction");
1386
1607
    return EX_OSERR;
1387
1608
  }
1388
1609
  if(old_sigterm_action.sa_handler != SIG_IGN){
1389
1610
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1390
1611
    if(ret == -1){
1391
 
      perror("sigaction");
 
1612
      perror_plus("sigaction");
1392
1613
      exitcode = EX_OSERR;
1393
1614
      goto end;
1394
1615
    }
1411
1632
    errno = 0;
1412
1633
    ret = seteuid(0);
1413
1634
    if(ret == -1){
1414
 
      perror("seteuid");
 
1635
      perror_plus("seteuid");
1415
1636
    }
1416
1637
    
1417
1638
#ifdef __linux__
1421
1642
    bool restore_loglevel = true;
1422
1643
    if(ret == -1){
1423
1644
      restore_loglevel = false;
1424
 
      perror("klogctl");
 
1645
      perror_plus("klogctl");
1425
1646
    }
1426
1647
#endif  /* __linux__ */
1427
1648
    
1428
1649
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1429
1650
    if(sd < 0){
1430
 
      perror("socket");
 
1651
      perror_plus("socket");
1431
1652
      exitcode = EX_OSERR;
1432
1653
#ifdef __linux__
1433
1654
      if(restore_loglevel){
1434
1655
        ret = klogctl(7, NULL, 0);
1435
1656
        if(ret == -1){
1436
 
          perror("klogctl");
 
1657
          perror_plus("klogctl");
1437
1658
        }
1438
1659
      }
1439
1660
#endif  /* __linux__ */
1441
1662
      errno = 0;
1442
1663
      ret = seteuid(uid);
1443
1664
      if(ret == -1){
1444
 
        perror("seteuid");
 
1665
        perror_plus("seteuid");
1445
1666
      }
1446
1667
      goto end;
1447
1668
    }
1448
1669
    strcpy(network.ifr_name, interface);
1449
1670
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1450
1671
    if(ret == -1){
1451
 
      perror("ioctl SIOCGIFFLAGS");
 
1672
      perror_plus("ioctl SIOCGIFFLAGS");
1452
1673
#ifdef __linux__
1453
1674
      if(restore_loglevel){
1454
1675
        ret = klogctl(7, NULL, 0);
1455
1676
        if(ret == -1){
1456
 
          perror("klogctl");
 
1677
          perror_plus("klogctl");
1457
1678
        }
1458
1679
      }
1459
1680
#endif  /* __linux__ */
1462
1683
      errno = 0;
1463
1684
      ret = seteuid(uid);
1464
1685
      if(ret == -1){
1465
 
        perror("seteuid");
 
1686
        perror_plus("seteuid");
1466
1687
      }
1467
1688
      goto end;
1468
1689
    }
1472
1693
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1473
1694
      if(ret == -1){
1474
1695
        take_down_interface = false;
1475
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1696
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1476
1697
        exitcode = EX_OSERR;
1477
1698
#ifdef __linux__
1478
1699
        if(restore_loglevel){
1479
1700
          ret = klogctl(7, NULL, 0);
1480
1701
          if(ret == -1){
1481
 
            perror("klogctl");
 
1702
            perror_plus("klogctl");
1482
1703
          }
1483
1704
        }
1484
1705
#endif  /* __linux__ */
1486
1707
        errno = 0;
1487
1708
        ret = seteuid(uid);
1488
1709
        if(ret == -1){
1489
 
          perror("seteuid");
 
1710
          perror_plus("seteuid");
1490
1711
        }
1491
1712
        goto end;
1492
1713
      }
1493
1714
    }
1494
 
    /* sleep checking until interface is running */
 
1715
    /* Sleep checking until interface is running.
 
1716
       Check every 0.25s, up to total time of delay */
1495
1717
    for(int i=0; i < delay * 4; i++){
1496
1718
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1497
1719
      if(ret == -1){
1498
 
        perror("ioctl SIOCGIFFLAGS");
 
1720
        perror_plus("ioctl SIOCGIFFLAGS");
1499
1721
      } else if(network.ifr_flags & IFF_RUNNING){
1500
1722
        break;
1501
1723
      }
1502
1724
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1503
1725
      ret = nanosleep(&sleeptime, NULL);
1504
1726
      if(ret == -1 and errno != EINTR){
1505
 
        perror("nanosleep");
 
1727
        perror_plus("nanosleep");
1506
1728
      }
1507
1729
    }
1508
1730
    if(not take_down_interface){
1509
1731
      /* We won't need the socket anymore */
1510
1732
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1511
1733
      if(ret == -1){
1512
 
        perror("close");
 
1734
        perror_plus("close");
1513
1735
      }
1514
1736
    }
1515
1737
#ifdef __linux__
1517
1739
      /* Restores kernel loglevel to default */
1518
1740
      ret = klogctl(7, NULL, 0);
1519
1741
      if(ret == -1){
1520
 
        perror("klogctl");
 
1742
        perror_plus("klogctl");
1521
1743
      }
1522
1744
    }
1523
1745
#endif  /* __linux__ */
1527
1749
      /* Lower privileges */
1528
1750
      ret = seteuid(uid);
1529
1751
      if(ret == -1){
1530
 
        perror("seteuid");
 
1752
        perror_plus("seteuid");
1531
1753
      }
1532
1754
    } else {
1533
1755
      /* Lower privileges permanently */
1534
1756
      ret = setuid(uid);
1535
1757
      if(ret == -1){
1536
 
        perror("setuid");
 
1758
        perror_plus("setuid");
1537
1759
      }
1538
1760
    }
1539
1761
  }
1555
1777
    goto end;
1556
1778
  }
1557
1779
  
 
1780
  if(mkdtemp(tempdir) == NULL){
 
1781
    perror_plus("mkdtemp");
 
1782
    goto end;
 
1783
  }
1558
1784
  tempdir_created = true;
1559
 
  if(mkdtemp(tempdir) == NULL){
1560
 
    tempdir_created = false;
1561
 
    perror("mkdtemp");
1562
 
    goto end;
1563
 
  }
1564
1785
  
1565
1786
  if(quit_now){
1566
1787
    goto end;
1626
1847
      if(quit_now or ret == 0){
1627
1848
        break;
1628
1849
      }
1629
 
      sleep(15);
 
1850
      sleep((int)retry_interval or 1);
1630
1851
    };
1631
1852
 
1632
1853
    if (not quit_now){
1690
1911
  if(debug){
1691
1912
    fprintf(stderr, "Starting Avahi loop search\n");
1692
1913
  }
1693
 
  
1694
 
  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
  }
1695
1921
  
1696
1922
 end:
1697
1923
  
1718
1944
  if(gpgme_initialized){
1719
1945
    gpgme_release(mc.ctx);
1720
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
  }
1721
1958
  
1722
1959
  /* Take down the network interface */
1723
1960
  if(take_down_interface){
1725
1962
    errno = 0;
1726
1963
    ret = seteuid(0);
1727
1964
    if(ret == -1){
1728
 
      perror("seteuid");
 
1965
      perror_plus("seteuid");
1729
1966
    }
1730
1967
    if(geteuid() == 0){
1731
1968
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1732
1969
      if(ret == -1){
1733
 
        perror("ioctl SIOCGIFFLAGS");
 
1970
        perror_plus("ioctl SIOCGIFFLAGS");
1734
1971
      } else if(network.ifr_flags & IFF_UP) {
1735
1972
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1736
1973
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1737
1974
        if(ret == -1){
1738
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1975
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1739
1976
        }
1740
1977
      }
1741
1978
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1742
1979
      if(ret == -1){
1743
 
        perror("close");
 
1980
        perror_plus("close");
1744
1981
      }
1745
1982
      /* Lower privileges permanently */
1746
1983
      errno = 0;
1747
1984
      ret = setuid(uid);
1748
1985
      if(ret == -1){
1749
 
        perror("setuid");
 
1986
        perror_plus("setuid");
1750
1987
      }
1751
1988
    }
1752
1989
  }
1753
1990
  
1754
 
  /* Removes the temp directory used by GPGME */
 
1991
  /* Removes the GPGME temp directory and all files inside */
1755
1992
  if(tempdir_created){
1756
 
    DIR *d;
1757
 
    struct dirent *direntry;
1758
 
    d = opendir(tempdir);
1759
 
    if(d == NULL){
1760
 
      if(errno != ENOENT){
1761
 
        perror("opendir");
1762
 
      }
1763
 
    } else {
1764
 
      while(true){
1765
 
        direntry = readdir(d);
1766
 
        if(direntry == NULL){
1767
 
          break;
1768
 
        }
1769
 
        /* Skip "." and ".." */
1770
 
        if(direntry->d_name[0] == '.'
1771
 
           and (direntry->d_name[1] == '\0'
1772
 
                or (direntry->d_name[1] == '.'
1773
 
                    and direntry->d_name[2] == '\0'))){
1774
 
          continue;
1775
 
        }
 
1993
    struct dirent **direntries = NULL;
 
1994
    struct dirent *direntry = NULL;
 
1995
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
 
1996
    if (ret > 0){
 
1997
      for(int i = 0; i < ret; i++){
 
1998
        direntry = direntries[i];
1776
1999
        char *fullname = NULL;
1777
2000
        ret = asprintf(&fullname, "%s/%s", tempdir,
1778
2001
                       direntry->d_name);
1779
2002
        if(ret < 0){
1780
 
          perror("asprintf");
 
2003
          perror_plus("asprintf");
1781
2004
          continue;
1782
2005
        }
1783
2006
        ret = remove(fullname);
1787
2010
        }
1788
2011
        free(fullname);
1789
2012
      }
1790
 
      closedir(d);
 
2013
    }
 
2014
 
 
2015
    /* need to be cleaned even if ret == 0 because man page doesn't
 
2016
       specify */
 
2017
    free(direntries);
 
2018
    if (ret == -1){
 
2019
      perror_plus("scandir");
1791
2020
    }
1792
2021
    ret = rmdir(tempdir);
1793
2022
    if(ret == -1 and errno != ENOENT){
1794
 
      perror("rmdir");
 
2023
      perror_plus("rmdir");
1795
2024
    }
1796
2025
  }
1797
2026
  
1802
2031
                                            &old_sigterm_action,
1803
2032
                                            NULL));
1804
2033
    if(ret == -1){
1805
 
      perror("sigaction");
 
2034
      perror_plus("sigaction");
1806
2035
    }
1807
2036
    do {
1808
2037
      ret = raise(signal_received);
1809
2038
    } while(ret != 0 and errno == EINTR);
1810
2039
    if(ret != 0){
1811
 
      perror("raise");
 
2040
      perror_plus("raise");
1812
2041
      abort();
1813
2042
    }
1814
2043
    TEMP_FAILURE_RETRY(pause());