/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
                                      libraries.  Remove "$(COMMON)".
                                      Thanks to Angel Abad
                                      <angelabad@ubuntu.com>.
* initramfs-tools-script: Work around change in initramfs-tools,
                          Debian bug #633582.
* plugins.d/mandos-client.c (init_gnutls_global): Bug fix: check for
                errors from gnutls_certificate_allocate_credentials().

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@recompile.se>.
 
29
 * Contact the authors at <mandos@fukt.bsnet.se>.
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
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(),
127
126
bool debug = false;
128
127
static const char mandos_protocol_version[] = "1";
129
128
const char *argp_program_version = "mandos-client " VERSION;
130
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
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;
283
224
    return false;
284
225
  }
285
226
  
286
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
227
    /* Set GPGME home directory for the OpenPGP engine only */
287
228
  rc = gpgme_get_engine_info(&engine_info);
288
229
  if(rc != GPG_ERR_NO_ERROR){
289
230
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
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
    }
645
586
  tcp_sd = socket(pf, SOCK_STREAM, 0);
646
587
  if(tcp_sd < 0){
647
588
    int e = errno;
648
 
    perror_plus("socket");
 
589
    perror("socket");
649
590
    errno = e;
650
591
    goto mandos_end;
651
592
  }
665
606
  }
666
607
  if(ret < 0 ){
667
608
    int e = errno;
668
 
    perror_plus("inet_pton");
 
609
    perror("inet_pton");
669
610
    errno = e;
670
611
    goto mandos_end;
671
612
  }
707
648
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
708
649
      char interface[IF_NAMESIZE];
709
650
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
 
        perror_plus("if_indextoname");
 
651
        perror("if_indextoname");
711
652
      } else {
712
653
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
654
                ip, interface, port);
727
668
                        sizeof(addrstr));
728
669
    }
729
670
    if(pcret == NULL){
730
 
      perror_plus("inet_ntop");
 
671
      perror("inet_ntop");
731
672
    } else {
732
673
      if(strcmp(addrstr, ip) != 0){
733
674
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
748
689
  if(ret < 0){
749
690
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
750
691
      int e = errno;
751
 
      perror_plus("connect");
 
692
      perror("connect");
752
693
      errno = e;
753
694
    }
754
695
    goto mandos_end;
767
708
                                   out_size - written));
768
709
    if(ret == -1){
769
710
      int e = errno;
770
 
      perror_plus("write");
 
711
      perror("write");
771
712
      errno = e;
772
713
      goto mandos_end;
773
714
    }
798
739
    goto mandos_end;
799
740
  }
800
741
  
801
 
  /* Spurious warning from -Wint-to-pointer-cast */
802
742
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
803
743
  
804
744
  if(quit_now){
841
781
                                   buffer_capacity);
842
782
    if(buffer_capacity == 0){
843
783
      int e = errno;
844
 
      perror_plus("incbuffer");
 
784
      perror("incbuffer");
845
785
      errno = e;
846
786
      goto mandos_end;
847
787
    }
952
892
      if(e == 0){
953
893
        e = errno;
954
894
      }
955
 
      perror_plus("close");
 
895
      perror("close");
956
896
    }
957
897
    gnutls_deinit(session);
958
 
    errno = e;
959
898
    if(quit_now){
960
 
      errno = EINTR;
 
899
      e = EINTR;
961
900
      retval = -1;
962
901
    }
 
902
    errno = e;
963
903
  }
964
904
  return retval;
965
905
}
1008
948
                                           avahi_proto_to_af(proto));
1009
949
      if(ret == 0){
1010
950
        avahi_simple_poll_quit(mc.simple_poll);
1011
 
      } else {
1012
 
        ret = add_server(ip, port, interface,
1013
 
                         avahi_proto_to_af(proto));
1014
951
      }
1015
952
    }
1016
953
  }
1070
1007
  }
1071
1008
}
1072
1009
 
1073
 
/* Signal handler that stops main loop after SIGTERM */
 
1010
/* stop main loop after sigterm has been called */
1074
1011
static void handle_sigterm(int sig){
1075
1012
  if(quit_now){
1076
1013
    return;
1078
1015
  quit_now = 1;
1079
1016
  signal_received = sig;
1080
1017
  int old_errno = errno;
1081
 
  /* set main loop to exit */
1082
1018
  if(mc.simple_poll != NULL){
1083
1019
    avahi_simple_poll_quit(mc.simple_poll);
1084
1020
  }
1099
1035
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1100
1036
                     if_entry->d_name);
1101
1037
  if(ret < 0){
1102
 
    perror_plus("asprintf");
 
1038
    perror("asprintf");
1103
1039
    return 0;
1104
1040
  }
1105
1041
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
1042
  if(flags_fd == -1){
1107
 
    perror_plus("open");
 
1043
    perror("open");
1108
1044
    free(flagname);
1109
1045
    return 0;
1110
1046
  }
1115
1051
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
1052
  flagstring[(size_t)to_read] = '\0';
1117
1053
  if(flagstring == NULL){
1118
 
    perror_plus("malloc");
 
1054
    perror("malloc");
1119
1055
    close(flags_fd);
1120
1056
    return 0;
1121
1057
  }
1123
1059
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1124
1060
                                             (size_t)to_read));
1125
1061
    if(ssret == -1){
1126
 
      perror_plus("read");
 
1062
      perror("read");
1127
1063
      free(flagstring);
1128
1064
      close(flags_fd);
1129
1065
      return 0;
1190
1126
  return 1;
1191
1127
}
1192
1128
 
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
1129
int main(int argc, char *argv[]){
1276
1130
  AvahiSServiceBrowser *sb = NULL;
1277
1131
  int error;
1294
1148
  bool gnutls_initialized = false;
1295
1149
  bool gpgme_initialized = false;
1296
1150
  float delay = 2.5f;
1297
 
  double retry_interval = 10; /* 10s between trying a server and
1298
 
                                 retrying the same server again */
1299
1151
  
1300
1152
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1301
1153
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1307
1159
  errno = 0;
1308
1160
  ret = setgid(gid);
1309
1161
  if(ret == -1){
1310
 
    perror_plus("setgid");
 
1162
    perror("setgid");
1311
1163
  }
1312
1164
  
1313
1165
  /* Lower user privileges (temporarily) */
1314
1166
  errno = 0;
1315
1167
  ret = seteuid(uid);
1316
1168
  if(ret == -1){
1317
 
    perror_plus("seteuid");
 
1169
    perror("seteuid");
1318
1170
  }
1319
1171
  
1320
1172
  if(quit_now){
1355
1207
        .arg = "SECONDS",
1356
1208
        .doc = "Maximum delay to wait for interface startup",
1357
1209
        .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
1210
      /*
1363
1211
       * These reproduce what we would get without ARGP_NO_HELP
1364
1212
       */
1408
1256
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1409
1257
          argp_error(state, "Bad delay");
1410
1258
        }
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
1259
        break;
1420
1260
        /*
1421
1261
         * These reproduce what we would get without ARGP_NO_HELP
1449
1289
    case ENOMEM:
1450
1290
    default:
1451
1291
      errno = ret;
1452
 
      perror_plus("argp_parse");
 
1292
      perror("argp_parse");
1453
1293
      exitcode = EX_OSERR;
1454
1294
      goto end;
1455
1295
    case EINVAL:
1457
1297
      goto end;
1458
1298
    }
1459
1299
  }
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
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1474
 
      int seckey_fd = open(seckey, O_RDONLY);
1475
 
      if(seckey_fd == -1){
1476
 
        perror_plus("open");
1477
 
      } else {
1478
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1479
 
        if(ret == -1){
1480
 
          perror_plus("fstat");
1481
 
        } else {
1482
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1483
 
            ret = fchown(seckey_fd, uid, gid);
1484
 
            if(ret == -1){
1485
 
              perror_plus("fchown");
1486
 
            }
1487
 
          }
1488
 
        }
1489
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1490
 
      }
1491
 
    }
1492
 
    
1493
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1494
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1495
 
      if(pubkey_fd == -1){
1496
 
        perror_plus("open");
1497
 
      } else {
1498
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1499
 
        if(ret == -1){
1500
 
          perror_plus("fstat");
1501
 
        } else {
1502
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1503
 
            ret = fchown(pubkey_fd, uid, gid);
1504
 
            if(ret == -1){
1505
 
              perror_plus("fchown");
1506
 
            }
1507
 
          }
1508
 
        }
1509
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1510
 
      }
1511
 
    }
1512
 
    
1513
 
    /* Lower privileges */
1514
 
    errno = 0;
1515
 
    ret = seteuid(uid);
1516
 
    if(ret == -1){
1517
 
      perror_plus("seteuid");
1518
 
    }
1519
 
  }
1520
1300
  
1521
1301
  if(not debug){
1522
1302
    avahi_set_log_function(empty_log);
1533
1313
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1534
1314
      }
1535
1315
      if(interface == NULL){
1536
 
        perror_plus("malloc");
 
1316
        perror("malloc");
1537
1317
        free(direntries);
1538
1318
        exitcode = EXIT_FAILURE;
1539
1319
        goto end;
1561
1341
  sigemptyset(&sigterm_action.sa_mask);
1562
1342
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1563
1343
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
 
1344
    perror("sigaddset");
1565
1345
    exitcode = EX_OSERR;
1566
1346
    goto end;
1567
1347
  }
1568
1348
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1569
1349
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
 
1350
    perror("sigaddset");
1571
1351
    exitcode = EX_OSERR;
1572
1352
    goto end;
1573
1353
  }
1574
1354
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1575
1355
  if(ret == -1){
1576
 
    perror_plus("sigaddset");
 
1356
    perror("sigaddset");
1577
1357
    exitcode = EX_OSERR;
1578
1358
    goto end;
1579
1359
  }
1583
1363
  */
1584
1364
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1585
1365
  if(ret == -1){
1586
 
    perror_plus("sigaction");
 
1366
    perror("sigaction");
1587
1367
    return EX_OSERR;
1588
1368
  }
1589
1369
  if(old_sigterm_action.sa_handler != SIG_IGN){
1590
1370
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1591
1371
    if(ret == -1){
1592
 
      perror_plus("sigaction");
 
1372
      perror("sigaction");
1593
1373
      exitcode = EX_OSERR;
1594
1374
      goto end;
1595
1375
    }
1596
1376
  }
1597
1377
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1598
1378
  if(ret == -1){
1599
 
    perror_plus("sigaction");
 
1379
    perror("sigaction");
1600
1380
    return EX_OSERR;
1601
1381
  }
1602
1382
  if(old_sigterm_action.sa_handler != SIG_IGN){
1603
1383
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1604
1384
    if(ret == -1){
1605
 
      perror_plus("sigaction");
 
1385
      perror("sigaction");
1606
1386
      exitcode = EX_OSERR;
1607
1387
      goto end;
1608
1388
    }
1609
1389
  }
1610
1390
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1611
1391
  if(ret == -1){
1612
 
    perror_plus("sigaction");
 
1392
    perror("sigaction");
1613
1393
    return EX_OSERR;
1614
1394
  }
1615
1395
  if(old_sigterm_action.sa_handler != SIG_IGN){
1616
1396
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1617
1397
    if(ret == -1){
1618
 
      perror_plus("sigaction");
 
1398
      perror("sigaction");
1619
1399
      exitcode = EX_OSERR;
1620
1400
      goto end;
1621
1401
    }
1638
1418
    errno = 0;
1639
1419
    ret = seteuid(0);
1640
1420
    if(ret == -1){
1641
 
      perror_plus("seteuid");
 
1421
      perror("seteuid");
1642
1422
    }
1643
1423
    
1644
1424
#ifdef __linux__
1648
1428
    bool restore_loglevel = true;
1649
1429
    if(ret == -1){
1650
1430
      restore_loglevel = false;
1651
 
      perror_plus("klogctl");
 
1431
      perror("klogctl");
1652
1432
    }
1653
1433
#endif  /* __linux__ */
1654
1434
    
1655
1435
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1656
1436
    if(sd < 0){
1657
 
      perror_plus("socket");
 
1437
      perror("socket");
1658
1438
      exitcode = EX_OSERR;
1659
1439
#ifdef __linux__
1660
1440
      if(restore_loglevel){
1661
1441
        ret = klogctl(7, NULL, 0);
1662
1442
        if(ret == -1){
1663
 
          perror_plus("klogctl");
 
1443
          perror("klogctl");
1664
1444
        }
1665
1445
      }
1666
1446
#endif  /* __linux__ */
1668
1448
      errno = 0;
1669
1449
      ret = seteuid(uid);
1670
1450
      if(ret == -1){
1671
 
        perror_plus("seteuid");
 
1451
        perror("seteuid");
1672
1452
      }
1673
1453
      goto end;
1674
1454
    }
1675
1455
    strcpy(network.ifr_name, interface);
1676
1456
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1677
1457
    if(ret == -1){
1678
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1458
      perror("ioctl SIOCGIFFLAGS");
1679
1459
#ifdef __linux__
1680
1460
      if(restore_loglevel){
1681
1461
        ret = klogctl(7, NULL, 0);
1682
1462
        if(ret == -1){
1683
 
          perror_plus("klogctl");
 
1463
          perror("klogctl");
1684
1464
        }
1685
1465
      }
1686
1466
#endif  /* __linux__ */
1689
1469
      errno = 0;
1690
1470
      ret = seteuid(uid);
1691
1471
      if(ret == -1){
1692
 
        perror_plus("seteuid");
 
1472
        perror("seteuid");
1693
1473
      }
1694
1474
      goto end;
1695
1475
    }
1699
1479
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1700
1480
      if(ret == -1){
1701
1481
        take_down_interface = false;
1702
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1482
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1703
1483
        exitcode = EX_OSERR;
1704
1484
#ifdef __linux__
1705
1485
        if(restore_loglevel){
1706
1486
          ret = klogctl(7, NULL, 0);
1707
1487
          if(ret == -1){
1708
 
            perror_plus("klogctl");
 
1488
            perror("klogctl");
1709
1489
          }
1710
1490
        }
1711
1491
#endif  /* __linux__ */
1713
1493
        errno = 0;
1714
1494
        ret = seteuid(uid);
1715
1495
        if(ret == -1){
1716
 
          perror_plus("seteuid");
 
1496
          perror("seteuid");
1717
1497
        }
1718
1498
        goto end;
1719
1499
      }
1720
1500
    }
1721
 
    /* Sleep checking until interface is running.
1722
 
       Check every 0.25s, up to total time of delay */
 
1501
    /* sleep checking until interface is running */
1723
1502
    for(int i=0; i < delay * 4; i++){
1724
1503
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1725
1504
      if(ret == -1){
1726
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1505
        perror("ioctl SIOCGIFFLAGS");
1727
1506
      } else if(network.ifr_flags & IFF_RUNNING){
1728
1507
        break;
1729
1508
      }
1730
1509
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1731
1510
      ret = nanosleep(&sleeptime, NULL);
1732
1511
      if(ret == -1 and errno != EINTR){
1733
 
        perror_plus("nanosleep");
 
1512
        perror("nanosleep");
1734
1513
      }
1735
1514
    }
1736
1515
    if(not take_down_interface){
1737
1516
      /* We won't need the socket anymore */
1738
1517
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1739
1518
      if(ret == -1){
1740
 
        perror_plus("close");
 
1519
        perror("close");
1741
1520
      }
1742
1521
    }
1743
1522
#ifdef __linux__
1745
1524
      /* Restores kernel loglevel to default */
1746
1525
      ret = klogctl(7, NULL, 0);
1747
1526
      if(ret == -1){
1748
 
        perror_plus("klogctl");
 
1527
        perror("klogctl");
1749
1528
      }
1750
1529
    }
1751
1530
#endif  /* __linux__ */
1755
1534
      /* Lower privileges */
1756
1535
      ret = seteuid(uid);
1757
1536
      if(ret == -1){
1758
 
        perror_plus("seteuid");
 
1537
        perror("seteuid");
1759
1538
      }
1760
1539
    } else {
1761
1540
      /* Lower privileges permanently */
1762
1541
      ret = setuid(uid);
1763
1542
      if(ret == -1){
1764
 
        perror_plus("setuid");
 
1543
        perror("setuid");
1765
1544
      }
1766
1545
    }
1767
1546
  }
1783
1562
    goto end;
1784
1563
  }
1785
1564
  
 
1565
  tempdir_created = true;
1786
1566
  if(mkdtemp(tempdir) == NULL){
1787
 
    perror_plus("mkdtemp");
 
1567
    tempdir_created = false;
 
1568
    perror("mkdtemp");
1788
1569
    goto end;
1789
1570
  }
1790
 
  tempdir_created = true;
1791
1571
  
1792
1572
  if(quit_now){
1793
1573
    goto end;
1835
1615
    
1836
1616
    port = (uint16_t)tmpmax;
1837
1617
    *address = '\0';
 
1618
    address = connect_to;
1838
1619
    /* Colon in address indicates IPv6 */
1839
1620
    int af;
1840
 
    if(strchr(connect_to, ':') != NULL){
 
1621
    if(strchr(address, ':') != NULL){
1841
1622
      af = AF_INET6;
1842
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1843
 
      if(connect_to[0] == '[' and address[-1] == ']')
1844
 
        {
1845
 
          connect_to++;
1846
 
          address[-1] = '\0';
1847
 
        }
1848
1623
    } else {
1849
1624
      af = AF_INET;
1850
1625
    }
1851
 
    address = connect_to;
1852
1626
    
1853
1627
    if(quit_now){
1854
1628
      goto end;
1855
1629
    }
1856
 
    
 
1630
 
1857
1631
    while(not quit_now){
1858
1632
      ret = start_mandos_communication(address, port, if_index, af);
1859
1633
      if(quit_now or ret == 0){
1860
1634
        break;
1861
1635
      }
1862
 
      if(debug){
1863
 
        fprintf(stderr, "Retrying in %d seconds\n",
1864
 
                (int)retry_interval);
1865
 
      }
1866
 
      sleep((int)retry_interval);
1867
 
    }
1868
 
    
 
1636
      sleep(15);
 
1637
    };
 
1638
 
1869
1639
    if (not quit_now){
1870
1640
      exitcode = EXIT_SUCCESS;
1871
1641
    }
1927
1697
  if(debug){
1928
1698
    fprintf(stderr, "Starting Avahi loop search\n");
1929
1699
  }
1930
 
 
1931
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1932
 
                                (int)(retry_interval * 1000));
1933
 
  if(debug){
1934
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1935
 
            (ret == 0) ? "successfully" : "with error");
1936
 
  }
 
1700
  
 
1701
  avahi_simple_poll_loop(mc.simple_poll);
1937
1702
  
1938
1703
 end:
1939
1704
  
1960
1725
  if(gpgme_initialized){
1961
1726
    gpgme_release(mc.ctx);
1962
1727
  }
1963
 
 
1964
 
  /* Cleans up the circular linked list of Mandos servers the client
1965
 
     has seen */
1966
 
  if(mc.current_server != NULL){
1967
 
    mc.current_server->prev->next = NULL;
1968
 
    while(mc.current_server != NULL){
1969
 
      server *next = mc.current_server->next;
1970
 
      free(mc.current_server);
1971
 
      mc.current_server = next;
1972
 
    }
1973
 
  }
1974
1728
  
1975
1729
  /* Take down the network interface */
1976
1730
  if(take_down_interface){
1978
1732
    errno = 0;
1979
1733
    ret = seteuid(0);
1980
1734
    if(ret == -1){
1981
 
      perror_plus("seteuid");
 
1735
      perror("seteuid");
1982
1736
    }
1983
1737
    if(geteuid() == 0){
1984
1738
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1985
1739
      if(ret == -1){
1986
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1740
        perror("ioctl SIOCGIFFLAGS");
1987
1741
      } else if(network.ifr_flags & IFF_UP) {
1988
1742
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1989
1743
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1990
1744
        if(ret == -1){
1991
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1745
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1992
1746
        }
1993
1747
      }
1994
1748
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1995
1749
      if(ret == -1){
1996
 
        perror_plus("close");
 
1750
        perror("close");
1997
1751
      }
1998
1752
      /* Lower privileges permanently */
1999
1753
      errno = 0;
2000
1754
      ret = setuid(uid);
2001
1755
      if(ret == -1){
2002
 
        perror_plus("setuid");
 
1756
        perror("setuid");
2003
1757
      }
2004
1758
    }
2005
1759
  }
2006
1760
  
2007
 
  /* Removes the GPGME temp directory and all files inside */
 
1761
  /* Removes the temp directory used by GPGME */
2008
1762
  if(tempdir_created){
2009
 
    struct dirent **direntries = NULL;
2010
 
    struct dirent *direntry = NULL;
2011
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2012
 
                             alphasort);
2013
 
    if (numentries > 0){
2014
 
      for(int i = 0; i < numentries; i++){
2015
 
        direntry = direntries[i];
 
1763
    DIR *d;
 
1764
    struct dirent *direntry;
 
1765
    d = opendir(tempdir);
 
1766
    if(d == NULL){
 
1767
      if(errno != ENOENT){
 
1768
        perror("opendir");
 
1769
      }
 
1770
    } else {
 
1771
      while(true){
 
1772
        direntry = readdir(d);
 
1773
        if(direntry == NULL){
 
1774
          break;
 
1775
        }
 
1776
        /* Skip "." and ".." */
 
1777
        if(direntry->d_name[0] == '.'
 
1778
           and (direntry->d_name[1] == '\0'
 
1779
                or (direntry->d_name[1] == '.'
 
1780
                    and direntry->d_name[2] == '\0'))){
 
1781
          continue;
 
1782
        }
2016
1783
        char *fullname = NULL;
2017
1784
        ret = asprintf(&fullname, "%s/%s", tempdir,
2018
1785
                       direntry->d_name);
2019
1786
        if(ret < 0){
2020
 
          perror_plus("asprintf");
 
1787
          perror("asprintf");
2021
1788
          continue;
2022
1789
        }
2023
1790
        ret = remove(fullname);
2027
1794
        }
2028
1795
        free(fullname);
2029
1796
      }
2030
 
    }
2031
 
 
2032
 
    /* need to clean even if 0 because man page doesn't specify */
2033
 
    free(direntries);
2034
 
    if (numentries == -1){
2035
 
      perror_plus("scandir");
 
1797
      closedir(d);
2036
1798
    }
2037
1799
    ret = rmdir(tempdir);
2038
1800
    if(ret == -1 and errno != ENOENT){
2039
 
      perror_plus("rmdir");
 
1801
      perror("rmdir");
2040
1802
    }
2041
1803
  }
2042
1804
  
2047
1809
                                            &old_sigterm_action,
2048
1810
                                            NULL));
2049
1811
    if(ret == -1){
2050
 
      perror_plus("sigaction");
 
1812
      perror("sigaction");
2051
1813
    }
2052
1814
    do {
2053
1815
      ret = raise(signal_received);
2054
1816
    } while(ret != 0 and errno == EINTR);
2055
1817
    if(ret != 0){
2056
 
      perror_plus("raise");
 
1818
      perror("raise");
2057
1819
      abort();
2058
1820
    }
2059
1821
    TEMP_FAILURE_RETRY(pause());