/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: Björn Påhlsson
  • Date: 2011-06-18 20:21:33 UTC
  • mfrom: (483 trunk)
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110618202133-j9f1ibmcplwjkbce
merge

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
 
/* Add server to set of servers to retry periodically */
191
 
int add_server(const char *ip, uint16_t port,
192
 
                 AvahiIfIndex if_index,
193
 
                 int af){
194
 
  int ret;
195
 
  server *new_server = malloc(sizeof(server));
196
 
  if(new_server == NULL){
197
 
    perror_plus("malloc");
198
 
    return -1;
199
 
  }
200
 
  *new_server = (server){ .ip = strdup(ip),
201
 
                         .port = port,
202
 
                         .if_index = if_index,
203
 
                         .af = af };
204
 
  if(new_server->ip == NULL){
205
 
    perror_plus("strdup");
206
 
    return -1;
207
 
  }
208
 
  /* Special case of first server */
209
 
  if (mc.current_server == NULL){
210
 
    new_server->next = new_server;
211
 
    new_server->prev = new_server;
212
 
    mc.current_server = new_server;
213
 
  /* Place the new server last in the list */
214
 
  } else {
215
 
    new_server->next = mc.current_server;
216
 
    new_server->prev = mc.current_server->prev;
217
 
    new_server->prev->next = new_server;
218
 
    mc.current_server->prev = new_server;
219
 
  }
220
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
221
 
  if(ret == -1){
222
 
    perror_plus("clock_gettime");
223
 
    return -1;
224
 
  }
225
 
  return 0;
226
 
}
227
 
 
228
169
/* 
229
170
 * Initialize GPGME.
230
171
 */
244
185
    
245
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
246
187
    if(fd == -1){
247
 
      perror_plus("open");
 
188
      perror("open");
248
189
      return false;
249
190
    }
250
191
    
264
205
    
265
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
266
207
    if(ret == -1){
267
 
      perror_plus("close");
 
208
      perror("close");
268
209
    }
269
210
    gpgme_data_release(pgp_data);
270
211
    return true;
395
336
  
396
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
397
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
398
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
399
340
    plaintext_length = -1;
400
341
    goto decrypt_end;
401
342
  }
406
347
                                      (size_t)plaintext_length,
407
348
                                      plaintext_capacity);
408
349
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
410
351
        plaintext_length = -1;
411
352
        goto decrypt_end;
412
353
    }
419
360
      break;
420
361
    }
421
362
    if(ret < 0){
422
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
423
364
      plaintext_length = -1;
424
365
      goto decrypt_end;
425
366
    }
482
423
  }
483
424
  
484
425
  /* OpenPGP credentials */
485
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  gnutls_certificate_allocate_credentials(&mc.cred);
486
427
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
429
                                                    from
 
430
                                                    -Wunreachable-code
 
431
                                                 */
488
432
            safer_gnutls_strerror(ret));
489
433
    gnutls_global_deinit();
490
434
    return -1;
645
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
646
590
  if(tcp_sd < 0){
647
591
    int e = errno;
648
 
    perror_plus("socket");
 
592
    perror("socket");
649
593
    errno = e;
650
594
    goto mandos_end;
651
595
  }
665
609
  }
666
610
  if(ret < 0 ){
667
611
    int e = errno;
668
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
669
613
    errno = e;
670
614
    goto mandos_end;
671
615
  }
707
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
708
652
      char interface[IF_NAMESIZE];
709
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
711
655
      } else {
712
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
657
                ip, interface, port);
727
671
                        sizeof(addrstr));
728
672
    }
729
673
    if(pcret == NULL){
730
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
731
675
    } else {
732
676
      if(strcmp(addrstr, ip) != 0){
733
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
748
692
  if(ret < 0){
749
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
750
694
      int e = errno;
751
 
      perror_plus("connect");
 
695
      perror("connect");
752
696
      errno = e;
753
697
    }
754
698
    goto mandos_end;
767
711
                                   out_size - written));
768
712
    if(ret == -1){
769
713
      int e = errno;
770
 
      perror_plus("write");
 
714
      perror("write");
771
715
      errno = e;
772
716
      goto mandos_end;
773
717
    }
798
742
    goto mandos_end;
799
743
  }
800
744
  
801
 
  /* Spurious warning from -Wint-to-pointer-cast */
802
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
803
746
  
804
747
  if(quit_now){
841
784
                                   buffer_capacity);
842
785
    if(buffer_capacity == 0){
843
786
      int e = errno;
844
 
      perror_plus("incbuffer");
 
787
      perror("incbuffer");
845
788
      errno = e;
846
789
      goto mandos_end;
847
790
    }
952
895
      if(e == 0){
953
896
        e = errno;
954
897
      }
955
 
      perror_plus("close");
 
898
      perror("close");
956
899
    }
957
900
    gnutls_deinit(session);
958
 
    errno = e;
959
901
    if(quit_now){
960
 
      errno = EINTR;
 
902
      e = EINTR;
961
903
      retval = -1;
962
904
    }
 
905
    errno = e;
963
906
  }
964
907
  return retval;
965
908
}
1008
951
                                           avahi_proto_to_af(proto));
1009
952
      if(ret == 0){
1010
953
        avahi_simple_poll_quit(mc.simple_poll);
1011
 
      } else {
1012
 
        ret = add_server(ip, port, interface,
1013
 
                         avahi_proto_to_af(proto));
1014
954
      }
1015
955
    }
1016
956
  }
1070
1010
  }
1071
1011
}
1072
1012
 
1073
 
/* Signal handler that stops main loop after SIGTERM */
 
1013
/* stop main loop after sigterm has been called */
1074
1014
static void handle_sigterm(int sig){
1075
1015
  if(quit_now){
1076
1016
    return;
1078
1018
  quit_now = 1;
1079
1019
  signal_received = sig;
1080
1020
  int old_errno = errno;
1081
 
  /* set main loop to exit */
1082
1021
  if(mc.simple_poll != NULL){
1083
1022
    avahi_simple_poll_quit(mc.simple_poll);
1084
1023
  }
1099
1038
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1100
1039
                     if_entry->d_name);
1101
1040
  if(ret < 0){
1102
 
    perror_plus("asprintf");
 
1041
    perror("asprintf");
1103
1042
    return 0;
1104
1043
  }
1105
1044
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
1045
  if(flags_fd == -1){
1107
 
    perror_plus("open");
 
1046
    perror("open");
1108
1047
    free(flagname);
1109
1048
    return 0;
1110
1049
  }
1111
1050
  free(flagname);
1112
1051
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1113
1052
  /* read line from flags_fd */
1114
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1053
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1115
1054
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
1055
  flagstring[(size_t)to_read] = '\0';
1117
1056
  if(flagstring == NULL){
1118
 
    perror_plus("malloc");
 
1057
    perror("malloc");
1119
1058
    close(flags_fd);
1120
1059
    return 0;
1121
1060
  }
1123
1062
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1124
1063
                                             (size_t)to_read));
1125
1064
    if(ssret == -1){
1126
 
      perror_plus("read");
 
1065
      perror("read");
1127
1066
      free(flagstring);
1128
1067
      close(flags_fd);
1129
1068
      return 0;
1190
1129
  return 1;
1191
1130
}
1192
1131
 
1193
 
int notdotentries(const struct dirent *direntry){
1194
 
  /* Skip "." and ".." */
1195
 
  if(direntry->d_name[0] == '.'
1196
 
     and (direntry->d_name[1] == '\0'
1197
 
          or (direntry->d_name[1] == '.'
1198
 
              and direntry->d_name[2] == '\0'))){
1199
 
    return 0;
1200
 
  }
1201
 
  return 1;
1202
 
}
1203
 
 
1204
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
 
  int ret;
1206
 
  struct timespec now;
1207
 
  struct timespec waited_time;
1208
 
  intmax_t block_time;
1209
 
  
1210
 
  while(true){
1211
 
    if(mc.current_server == NULL){
1212
 
      if (debug){
1213
 
        fprintf(stderr,
1214
 
                "Wait until first server is found. No timeout!\n");
1215
 
      }
1216
 
      ret = avahi_simple_poll_iterate(s, -1);
1217
 
    } else {
1218
 
      if (debug){
1219
 
        fprintf(stderr, "Check current_server if we should run it,"
1220
 
                " or wait\n");
1221
 
      }
1222
 
      /* the current time */
1223
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1224
 
      if(ret == -1){
1225
 
        perror_plus("clock_gettime");
1226
 
        return -1;
1227
 
      }
1228
 
      /* Calculating in ms how long time between now and server
1229
 
         who we visted longest time ago. Now - last seen.  */
1230
 
      waited_time.tv_sec = (now.tv_sec
1231
 
                            - mc.current_server->last_seen.tv_sec);
1232
 
      waited_time.tv_nsec = (now.tv_nsec
1233
 
                             - mc.current_server->last_seen.tv_nsec);
1234
 
      /* total time is 10s/10,000ms.
1235
 
         Converting to s from ms by dividing by 1,000,
1236
 
         and ns to ms by dividing by 1,000,000. */
1237
 
      block_time = ((retry_interval
1238
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1239
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1240
 
      
1241
 
      if (debug){
1242
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1243
 
      }
1244
 
      
1245
 
      if(block_time <= 0){
1246
 
        ret = start_mandos_communication(mc.current_server->ip,
1247
 
                                         mc.current_server->port,
1248
 
                                         mc.current_server->if_index,
1249
 
                                         mc.current_server->af);
1250
 
        if(ret == 0){
1251
 
          avahi_simple_poll_quit(mc.simple_poll);
1252
 
          return 0;
1253
 
        }
1254
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1255
 
                            &mc.current_server->last_seen);
1256
 
        if(ret == -1){
1257
 
          perror_plus("clock_gettime");
1258
 
          return -1;
1259
 
        }
1260
 
        mc.current_server = mc.current_server->next;
1261
 
        block_time = 0;         /* Call avahi to find new Mandos
1262
 
                                   servers, but don't block */
1263
 
      }
1264
 
      
1265
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1266
 
    }
1267
 
    if(ret != 0){
1268
 
      if (ret > 0 or errno != EINTR) {
1269
 
        return (ret != 1) ? ret : 0;
1270
 
      }
1271
 
    }
1272
 
  }
1273
 
}
1274
 
 
1275
1132
int main(int argc, char *argv[]){
1276
1133
  AvahiSServiceBrowser *sb = NULL;
1277
1134
  int error;
1294
1151
  bool gnutls_initialized = false;
1295
1152
  bool gpgme_initialized = false;
1296
1153
  float delay = 2.5f;
1297
 
  double retry_interval = 10; /* 10s between trying a server and
1298
 
                                 retrying the same server again */
1299
1154
  
1300
1155
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1301
1156
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1307
1162
  errno = 0;
1308
1163
  ret = setgid(gid);
1309
1164
  if(ret == -1){
1310
 
    perror_plus("setgid");
 
1165
    perror("setgid");
1311
1166
  }
1312
1167
  
1313
1168
  /* Lower user privileges (temporarily) */
1314
1169
  errno = 0;
1315
1170
  ret = seteuid(uid);
1316
1171
  if(ret == -1){
1317
 
    perror_plus("seteuid");
 
1172
    perror("seteuid");
1318
1173
  }
1319
1174
  
1320
1175
  if(quit_now){
1355
1210
        .arg = "SECONDS",
1356
1211
        .doc = "Maximum delay to wait for interface startup",
1357
1212
        .group = 2 },
1358
 
      { .name = "retry", .key = 132,
1359
 
        .arg = "SECONDS",
1360
 
        .doc = "Retry interval used when denied by the mandos server",
1361
 
        .group = 2 },
1362
1213
      /*
1363
1214
       * These reproduce what we would get without ARGP_NO_HELP
1364
1215
       */
1408
1259
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1409
1260
          argp_error(state, "Bad delay");
1410
1261
        }
1411
 
      case 132:                 /* --retry */
1412
 
        errno = 0;
1413
 
        retry_interval = strtod(arg, &tmp);
1414
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1415
 
           or (retry_interval * 1000) > INT_MAX
1416
 
           or retry_interval < 0){
1417
 
          argp_error(state, "Bad retry interval");
1418
 
        }
1419
1262
        break;
1420
1263
        /*
1421
1264
         * These reproduce what we would get without ARGP_NO_HELP
1449
1292
    case ENOMEM:
1450
1293
    default:
1451
1294
      errno = ret;
1452
 
      perror_plus("argp_parse");
 
1295
      perror("argp_parse");
1453
1296
      exitcode = EX_OSERR;
1454
1297
      goto end;
1455
1298
    case EINVAL:
1457
1300
      goto end;
1458
1301
    }
1459
1302
  }
1460
 
    
1461
 
  {
1462
 
    /* Work around Debian bug #633582:
1463
 
       <http://bugs.debian.org/633582> */
1464
 
    struct stat st;
1465
 
    
1466
 
    /* Re-raise priviliges */
1467
 
    errno = 0;
1468
 
    ret = seteuid(0);
1469
 
    if(ret == -1){
1470
 
      perror_plus("seteuid");
1471
 
    }
1472
 
    
1473
 
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1474
 
    if(seckey_fd == -1){
1475
 
      perror_plus("open");
1476
 
    } else {
1477
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1478
 
      if(ret == -1){
1479
 
        perror_plus("fstat");
1480
 
      } else {
1481
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1482
 
          ret = fchown(seckey_fd, uid, gid);
1483
 
          if(ret == -1){
1484
 
            perror_plus("fchown");
1485
 
          }
1486
 
        }
1487
 
      }
1488
 
      TEMP_FAILURE_RETRY(close(seckey_fd));
1489
 
    }
1490
 
    
1491
 
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1492
 
    if(pubkey_fd == -1){
1493
 
      perror_plus("open");
1494
 
    } else {
1495
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1496
 
      if(ret == -1){
1497
 
        perror_plus("fstat");
1498
 
      } else {
1499
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1500
 
          ret = fchown(pubkey_fd, uid, gid);
1501
 
          if(ret == -1){
1502
 
            perror_plus("fchown");
1503
 
          }
1504
 
        }
1505
 
      }
1506
 
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1507
 
    }
1508
 
    
1509
 
    /* Lower privileges */
1510
 
    errno = 0;
1511
 
    ret = seteuid(uid);
1512
 
    if(ret == -1){
1513
 
      perror_plus("seteuid");
1514
 
    }
1515
 
  }
1516
1303
  
1517
1304
  if(not debug){
1518
1305
    avahi_set_log_function(empty_log);
1529
1316
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1530
1317
      }
1531
1318
      if(interface == NULL){
1532
 
        perror_plus("malloc");
 
1319
        perror("malloc");
1533
1320
        free(direntries);
1534
1321
        exitcode = EXIT_FAILURE;
1535
1322
        goto end;
1557
1344
  sigemptyset(&sigterm_action.sa_mask);
1558
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1559
1346
  if(ret == -1){
1560
 
    perror_plus("sigaddset");
 
1347
    perror("sigaddset");
1561
1348
    exitcode = EX_OSERR;
1562
1349
    goto end;
1563
1350
  }
1564
1351
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1565
1352
  if(ret == -1){
1566
 
    perror_plus("sigaddset");
 
1353
    perror("sigaddset");
1567
1354
    exitcode = EX_OSERR;
1568
1355
    goto end;
1569
1356
  }
1570
1357
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1571
1358
  if(ret == -1){
1572
 
    perror_plus("sigaddset");
 
1359
    perror("sigaddset");
1573
1360
    exitcode = EX_OSERR;
1574
1361
    goto end;
1575
1362
  }
1579
1366
  */
1580
1367
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1581
1368
  if(ret == -1){
1582
 
    perror_plus("sigaction");
 
1369
    perror("sigaction");
1583
1370
    return EX_OSERR;
1584
1371
  }
1585
1372
  if(old_sigterm_action.sa_handler != SIG_IGN){
1586
1373
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1587
1374
    if(ret == -1){
1588
 
      perror_plus("sigaction");
 
1375
      perror("sigaction");
1589
1376
      exitcode = EX_OSERR;
1590
1377
      goto end;
1591
1378
    }
1592
1379
  }
1593
1380
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1594
1381
  if(ret == -1){
1595
 
    perror_plus("sigaction");
 
1382
    perror("sigaction");
1596
1383
    return EX_OSERR;
1597
1384
  }
1598
1385
  if(old_sigterm_action.sa_handler != SIG_IGN){
1599
1386
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1600
1387
    if(ret == -1){
1601
 
      perror_plus("sigaction");
 
1388
      perror("sigaction");
1602
1389
      exitcode = EX_OSERR;
1603
1390
      goto end;
1604
1391
    }
1605
1392
  }
1606
1393
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1607
1394
  if(ret == -1){
1608
 
    perror_plus("sigaction");
 
1395
    perror("sigaction");
1609
1396
    return EX_OSERR;
1610
1397
  }
1611
1398
  if(old_sigterm_action.sa_handler != SIG_IGN){
1612
1399
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1613
1400
    if(ret == -1){
1614
 
      perror_plus("sigaction");
 
1401
      perror("sigaction");
1615
1402
      exitcode = EX_OSERR;
1616
1403
      goto end;
1617
1404
    }
1634
1421
    errno = 0;
1635
1422
    ret = seteuid(0);
1636
1423
    if(ret == -1){
1637
 
      perror_plus("seteuid");
 
1424
      perror("seteuid");
1638
1425
    }
1639
1426
    
1640
1427
#ifdef __linux__
1644
1431
    bool restore_loglevel = true;
1645
1432
    if(ret == -1){
1646
1433
      restore_loglevel = false;
1647
 
      perror_plus("klogctl");
 
1434
      perror("klogctl");
1648
1435
    }
1649
1436
#endif  /* __linux__ */
1650
1437
    
1651
1438
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1652
1439
    if(sd < 0){
1653
 
      perror_plus("socket");
 
1440
      perror("socket");
1654
1441
      exitcode = EX_OSERR;
1655
1442
#ifdef __linux__
1656
1443
      if(restore_loglevel){
1657
1444
        ret = klogctl(7, NULL, 0);
1658
1445
        if(ret == -1){
1659
 
          perror_plus("klogctl");
 
1446
          perror("klogctl");
1660
1447
        }
1661
1448
      }
1662
1449
#endif  /* __linux__ */
1664
1451
      errno = 0;
1665
1452
      ret = seteuid(uid);
1666
1453
      if(ret == -1){
1667
 
        perror_plus("seteuid");
 
1454
        perror("seteuid");
1668
1455
      }
1669
1456
      goto end;
1670
1457
    }
1671
1458
    strcpy(network.ifr_name, interface);
1672
1459
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1673
1460
    if(ret == -1){
1674
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1461
      perror("ioctl SIOCGIFFLAGS");
1675
1462
#ifdef __linux__
1676
1463
      if(restore_loglevel){
1677
1464
        ret = klogctl(7, NULL, 0);
1678
1465
        if(ret == -1){
1679
 
          perror_plus("klogctl");
 
1466
          perror("klogctl");
1680
1467
        }
1681
1468
      }
1682
1469
#endif  /* __linux__ */
1685
1472
      errno = 0;
1686
1473
      ret = seteuid(uid);
1687
1474
      if(ret == -1){
1688
 
        perror_plus("seteuid");
 
1475
        perror("seteuid");
1689
1476
      }
1690
1477
      goto end;
1691
1478
    }
1695
1482
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1696
1483
      if(ret == -1){
1697
1484
        take_down_interface = false;
1698
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1485
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1699
1486
        exitcode = EX_OSERR;
1700
1487
#ifdef __linux__
1701
1488
        if(restore_loglevel){
1702
1489
          ret = klogctl(7, NULL, 0);
1703
1490
          if(ret == -1){
1704
 
            perror_plus("klogctl");
 
1491
            perror("klogctl");
1705
1492
          }
1706
1493
        }
1707
1494
#endif  /* __linux__ */
1709
1496
        errno = 0;
1710
1497
        ret = seteuid(uid);
1711
1498
        if(ret == -1){
1712
 
          perror_plus("seteuid");
 
1499
          perror("seteuid");
1713
1500
        }
1714
1501
        goto end;
1715
1502
      }
1716
1503
    }
1717
 
    /* Sleep checking until interface is running.
1718
 
       Check every 0.25s, up to total time of delay */
 
1504
    /* sleep checking until interface is running */
1719
1505
    for(int i=0; i < delay * 4; i++){
1720
1506
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1721
1507
      if(ret == -1){
1722
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1508
        perror("ioctl SIOCGIFFLAGS");
1723
1509
      } else if(network.ifr_flags & IFF_RUNNING){
1724
1510
        break;
1725
1511
      }
1726
1512
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1727
1513
      ret = nanosleep(&sleeptime, NULL);
1728
1514
      if(ret == -1 and errno != EINTR){
1729
 
        perror_plus("nanosleep");
 
1515
        perror("nanosleep");
1730
1516
      }
1731
1517
    }
1732
1518
    if(not take_down_interface){
1733
1519
      /* We won't need the socket anymore */
1734
1520
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1735
1521
      if(ret == -1){
1736
 
        perror_plus("close");
 
1522
        perror("close");
1737
1523
      }
1738
1524
    }
1739
1525
#ifdef __linux__
1741
1527
      /* Restores kernel loglevel to default */
1742
1528
      ret = klogctl(7, NULL, 0);
1743
1529
      if(ret == -1){
1744
 
        perror_plus("klogctl");
 
1530
        perror("klogctl");
1745
1531
      }
1746
1532
    }
1747
1533
#endif  /* __linux__ */
1751
1537
      /* Lower privileges */
1752
1538
      ret = seteuid(uid);
1753
1539
      if(ret == -1){
1754
 
        perror_plus("seteuid");
 
1540
        perror("seteuid");
1755
1541
      }
1756
1542
    } else {
1757
1543
      /* Lower privileges permanently */
1758
1544
      ret = setuid(uid);
1759
1545
      if(ret == -1){
1760
 
        perror_plus("setuid");
 
1546
        perror("setuid");
1761
1547
      }
1762
1548
    }
1763
1549
  }
1779
1565
    goto end;
1780
1566
  }
1781
1567
  
 
1568
  tempdir_created = true;
1782
1569
  if(mkdtemp(tempdir) == NULL){
1783
 
    perror_plus("mkdtemp");
 
1570
    tempdir_created = false;
 
1571
    perror("mkdtemp");
1784
1572
    goto end;
1785
1573
  }
1786
 
  tempdir_created = true;
1787
1574
  
1788
1575
  if(quit_now){
1789
1576
    goto end;
1831
1618
    
1832
1619
    port = (uint16_t)tmpmax;
1833
1620
    *address = '\0';
 
1621
    address = connect_to;
1834
1622
    /* Colon in address indicates IPv6 */
1835
1623
    int af;
1836
 
    if(strchr(connect_to, ':') != NULL){
 
1624
    if(strchr(address, ':') != NULL){
1837
1625
      af = AF_INET6;
1838
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1839
 
      if(connect_to[0] == '[' and address[-1] == ']')
1840
 
        {
1841
 
          connect_to++;
1842
 
          address[-1] = '\0';
1843
 
        }
1844
1626
    } else {
1845
1627
      af = AF_INET;
1846
1628
    }
1847
 
    address = connect_to;
1848
1629
    
1849
1630
    if(quit_now){
1850
1631
      goto end;
1851
1632
    }
1852
 
    
 
1633
 
1853
1634
    while(not quit_now){
1854
1635
      ret = start_mandos_communication(address, port, if_index, af);
1855
1636
      if(quit_now or ret == 0){
1856
1637
        break;
1857
1638
      }
1858
 
      if(debug){
1859
 
        fprintf(stderr, "Retrying in %d seconds\n",
1860
 
                (int)retry_interval);
1861
 
      }
1862
 
      sleep((int)retry_interval);
 
1639
      sleep(15);
1863
1640
    };
1864
1641
 
1865
1642
    if (not quit_now){
1923
1700
  if(debug){
1924
1701
    fprintf(stderr, "Starting Avahi loop search\n");
1925
1702
  }
1926
 
 
1927
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1928
 
                                (int)(retry_interval * 1000));
1929
 
  if(debug){
1930
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1931
 
            (ret == 0) ? "successfully" : "with error");
1932
 
  }
 
1703
  
 
1704
  avahi_simple_poll_loop(mc.simple_poll);
1933
1705
  
1934
1706
 end:
1935
1707
  
1956
1728
  if(gpgme_initialized){
1957
1729
    gpgme_release(mc.ctx);
1958
1730
  }
1959
 
 
1960
 
  /* Cleans up the circular linked list of Mandos servers the client
1961
 
     has seen */
1962
 
  if(mc.current_server != NULL){
1963
 
    mc.current_server->prev->next = NULL;
1964
 
    while(mc.current_server != NULL){
1965
 
      server *next = mc.current_server->next;
1966
 
      free(mc.current_server);
1967
 
      mc.current_server = next;
1968
 
    }
1969
 
  }
1970
1731
  
1971
1732
  /* Take down the network interface */
1972
1733
  if(take_down_interface){
1974
1735
    errno = 0;
1975
1736
    ret = seteuid(0);
1976
1737
    if(ret == -1){
1977
 
      perror_plus("seteuid");
 
1738
      perror("seteuid");
1978
1739
    }
1979
1740
    if(geteuid() == 0){
1980
1741
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1981
1742
      if(ret == -1){
1982
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1743
        perror("ioctl SIOCGIFFLAGS");
1983
1744
      } else if(network.ifr_flags & IFF_UP) {
1984
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1985
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1986
1747
        if(ret == -1){
1987
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1988
1749
        }
1989
1750
      }
1990
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1991
1752
      if(ret == -1){
1992
 
        perror_plus("close");
 
1753
        perror("close");
1993
1754
      }
1994
1755
      /* Lower privileges permanently */
1995
1756
      errno = 0;
1996
1757
      ret = setuid(uid);
1997
1758
      if(ret == -1){
1998
 
        perror_plus("setuid");
 
1759
        perror("setuid");
1999
1760
      }
2000
1761
    }
2001
1762
  }
2002
1763
  
2003
 
  /* Removes the GPGME temp directory and all files inside */
 
1764
  /* Removes the temp directory used by GPGME */
2004
1765
  if(tempdir_created){
2005
 
    struct dirent **direntries = NULL;
2006
 
    struct dirent *direntry = NULL;
2007
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
2008
 
    if (ret > 0){
2009
 
      for(int i = 0; i < ret; i++){
2010
 
        direntry = direntries[i];
 
1766
    DIR *d;
 
1767
    struct dirent *direntry;
 
1768
    d = opendir(tempdir);
 
1769
    if(d == NULL){
 
1770
      if(errno != ENOENT){
 
1771
        perror("opendir");
 
1772
      }
 
1773
    } else {
 
1774
      while(true){
 
1775
        direntry = readdir(d);
 
1776
        if(direntry == NULL){
 
1777
          break;
 
1778
        }
 
1779
        /* Skip "." and ".." */
 
1780
        if(direntry->d_name[0] == '.'
 
1781
           and (direntry->d_name[1] == '\0'
 
1782
                or (direntry->d_name[1] == '.'
 
1783
                    and direntry->d_name[2] == '\0'))){
 
1784
          continue;
 
1785
        }
2011
1786
        char *fullname = NULL;
2012
1787
        ret = asprintf(&fullname, "%s/%s", tempdir,
2013
1788
                       direntry->d_name);
2014
1789
        if(ret < 0){
2015
 
          perror_plus("asprintf");
 
1790
          perror("asprintf");
2016
1791
          continue;
2017
1792
        }
2018
1793
        ret = remove(fullname);
2022
1797
        }
2023
1798
        free(fullname);
2024
1799
      }
2025
 
    }
2026
 
 
2027
 
    /* need to be cleaned even if ret == 0 because man page doesn't
2028
 
       specify */
2029
 
    free(direntries);
2030
 
    if (ret == -1){
2031
 
      perror_plus("scandir");
 
1800
      closedir(d);
2032
1801
    }
2033
1802
    ret = rmdir(tempdir);
2034
1803
    if(ret == -1 and errno != ENOENT){
2035
 
      perror_plus("rmdir");
 
1804
      perror("rmdir");
2036
1805
    }
2037
1806
  }
2038
1807
  
2043
1812
                                            &old_sigterm_action,
2044
1813
                                            NULL));
2045
1814
    if(ret == -1){
2046
 
      perror_plus("sigaction");
 
1815
      perror("sigaction");
2047
1816
    }
2048
1817
    do {
2049
1818
      ret = raise(signal_received);
2050
1819
    } while(ret != 0 and errno == EINTR);
2051
1820
    if(ret != 0){
2052
 
      perror_plus("raise");
 
1821
      perror("raise");
2053
1822
      abort();
2054
1823
    }
2055
1824
    TEMP_FAILURE_RETRY(pause());