/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2011-07-27 17:58:27 UTC
  • mto: (237.4.29 release)
  • mto: This revision was merged to the branch mainline in revision 490.
  • Revision ID: teddy@fukt.bsnet.se-20110727175827-nrd1ysjl4jrh6qw1
Tags: version-1.3.1-1
* Makefile (version): Changed to "1.3.1".
* NEWS (Version 1.3.1): New entry.
* debian/changelog (1.3.1-1): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
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
 
#include <time.h>               /* nanosleep(), time() */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
 
67
#include <time.h>               /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
                                   SIOCSIFFLAGS, if_indextoname(),
69
70
                                   if_nametoindex(), IF_NAMESIZE */
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);
690
745
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
746
  }
692
747
  if(ret < 0){
693
 
    int e = errno;
694
 
    perror("connect");
695
 
    errno = e;
 
748
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
749
      int e = errno;
 
750
      perror_plus("connect");
 
751
      errno = e;
 
752
    }
696
753
    goto mandos_end;
697
754
  }
698
755
  
709
766
                                   out_size - written));
710
767
    if(ret == -1){
711
768
      int e = errno;
712
 
      perror("write");
 
769
      perror_plus("write");
713
770
      errno = e;
714
771
      goto mandos_end;
715
772
    }
740
797
    goto mandos_end;
741
798
  }
742
799
  
 
800
  /* Spurious warning from -Wint-to-pointer-cast */
743
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
744
802
  
745
803
  if(quit_now){
782
840
                                   buffer_capacity);
783
841
    if(buffer_capacity == 0){
784
842
      int e = errno;
785
 
      perror("incbuffer");
 
843
      perror_plus("incbuffer");
786
844
      errno = e;
787
845
      goto mandos_end;
788
846
    }
893
951
      if(e == 0){
894
952
        e = errno;
895
953
      }
896
 
      perror("close");
 
954
      perror_plus("close");
897
955
    }
898
956
    gnutls_deinit(session);
 
957
    errno = e;
899
958
    if(quit_now){
900
 
      e = EINTR;
 
959
      errno = EINTR;
901
960
      retval = -1;
902
961
    }
903
 
    errno = e;
904
962
  }
905
963
  return retval;
906
964
}
949
1007
                                           avahi_proto_to_af(proto));
950
1008
      if(ret == 0){
951
1009
        avahi_simple_poll_quit(mc.simple_poll);
 
1010
      } else {
 
1011
        ret = add_server(ip, port, interface,
 
1012
                         avahi_proto_to_af(proto));
952
1013
      }
953
1014
    }
954
1015
  }
1008
1069
  }
1009
1070
}
1010
1071
 
1011
 
/* stop main loop after sigterm has been called */
 
1072
/* Signal handler that stops main loop after SIGTERM */
1012
1073
static void handle_sigterm(int sig){
1013
1074
  if(quit_now){
1014
1075
    return;
1016
1077
  quit_now = 1;
1017
1078
  signal_received = sig;
1018
1079
  int old_errno = errno;
 
1080
  /* set main loop to exit */
1019
1081
  if(mc.simple_poll != NULL){
1020
1082
    avahi_simple_poll_quit(mc.simple_poll);
1021
1083
  }
1030
1092
int good_interface(const struct dirent *if_entry){
1031
1093
  ssize_t ssret;
1032
1094
  char *flagname = NULL;
 
1095
  if(if_entry->d_name[0] == '.'){
 
1096
    return 0;
 
1097
  }
1033
1098
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1034
1099
                     if_entry->d_name);
1035
1100
  if(ret < 0){
1036
 
    perror("asprintf");
1037
 
    return 0;
1038
 
  }
1039
 
  if(if_entry->d_name[0] == '.'){
 
1101
    perror_plus("asprintf");
1040
1102
    return 0;
1041
1103
  }
1042
1104
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1043
1105
  if(flags_fd == -1){
1044
 
    perror("open");
 
1106
    perror_plus("open");
 
1107
    free(flagname);
1045
1108
    return 0;
1046
1109
  }
 
1110
  free(flagname);
1047
1111
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1048
1112
  /* read line from flags_fd */
1049
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1113
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1050
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1051
1115
  flagstring[(size_t)to_read] = '\0';
1052
1116
  if(flagstring == NULL){
1053
 
    perror("malloc");
 
1117
    perror_plus("malloc");
1054
1118
    close(flags_fd);
1055
1119
    return 0;
1056
1120
  }
1058
1122
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1059
1123
                                             (size_t)to_read));
1060
1124
    if(ssret == -1){
1061
 
      perror("read");
 
1125
      perror_plus("read");
1062
1126
      free(flagstring);
1063
1127
      close(flags_fd);
1064
1128
      return 0;
1109
1173
    }
1110
1174
    return 0;
1111
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
  }
1112
1184
  /* Accept this device */
1113
1185
  if(debug){
1114
1186
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1117
1189
  return 1;
1118
1190
}
1119
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
 
1120
1274
int main(int argc, char *argv[]){
1121
1275
  AvahiSServiceBrowser *sb = NULL;
1122
1276
  int error;
1139
1293
  bool gnutls_initialized = false;
1140
1294
  bool gpgme_initialized = false;
1141
1295
  float delay = 2.5f;
 
1296
  double retry_interval = 10; /* 10s between trying a server and
 
1297
                                 retrying the same server again */
1142
1298
  
1143
1299
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1144
1300
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1150
1306
  errno = 0;
1151
1307
  ret = setgid(gid);
1152
1308
  if(ret == -1){
1153
 
    perror("setgid");
 
1309
    perror_plus("setgid");
1154
1310
  }
1155
1311
  
1156
1312
  /* Lower user privileges (temporarily) */
1157
1313
  errno = 0;
1158
1314
  ret = seteuid(uid);
1159
1315
  if(ret == -1){
1160
 
    perror("seteuid");
 
1316
    perror_plus("seteuid");
1161
1317
  }
1162
1318
  
1163
1319
  if(quit_now){
1198
1354
        .arg = "SECONDS",
1199
1355
        .doc = "Maximum delay to wait for interface startup",
1200
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 },
1201
1361
      /*
1202
1362
       * These reproduce what we would get without ARGP_NO_HELP
1203
1363
       */
1247
1407
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1248
1408
          argp_error(state, "Bad delay");
1249
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
        }
1250
1417
        break;
1251
1418
        /*
1252
1419
         * These reproduce what we would get without ARGP_NO_HELP
1280
1447
    case ENOMEM:
1281
1448
    default:
1282
1449
      errno = ret;
1283
 
      perror("argp_parse");
 
1450
      perror_plus("argp_parse");
1284
1451
      exitcode = EX_OSERR;
1285
1452
      goto end;
1286
1453
    case EINVAL:
1288
1455
      goto end;
1289
1456
    }
1290
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
  }
1291
1514
  
1292
1515
  if(not debug){
1293
1516
    avahi_set_log_function(empty_log);
1304
1527
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1305
1528
      }
1306
1529
      if(interface == NULL){
1307
 
        perror("malloc");
 
1530
        perror_plus("malloc");
1308
1531
        free(direntries);
1309
1532
        exitcode = EXIT_FAILURE;
1310
1533
        goto end;
1332
1555
  sigemptyset(&sigterm_action.sa_mask);
1333
1556
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1334
1557
  if(ret == -1){
1335
 
    perror("sigaddset");
 
1558
    perror_plus("sigaddset");
1336
1559
    exitcode = EX_OSERR;
1337
1560
    goto end;
1338
1561
  }
1339
1562
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1340
1563
  if(ret == -1){
1341
 
    perror("sigaddset");
 
1564
    perror_plus("sigaddset");
1342
1565
    exitcode = EX_OSERR;
1343
1566
    goto end;
1344
1567
  }
1345
1568
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1346
1569
  if(ret == -1){
1347
 
    perror("sigaddset");
 
1570
    perror_plus("sigaddset");
1348
1571
    exitcode = EX_OSERR;
1349
1572
    goto end;
1350
1573
  }
1354
1577
  */
1355
1578
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1356
1579
  if(ret == -1){
1357
 
    perror("sigaction");
 
1580
    perror_plus("sigaction");
1358
1581
    return EX_OSERR;
1359
1582
  }
1360
1583
  if(old_sigterm_action.sa_handler != SIG_IGN){
1361
1584
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1362
1585
    if(ret == -1){
1363
 
      perror("sigaction");
 
1586
      perror_plus("sigaction");
1364
1587
      exitcode = EX_OSERR;
1365
1588
      goto end;
1366
1589
    }
1367
1590
  }
1368
1591
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1369
1592
  if(ret == -1){
1370
 
    perror("sigaction");
 
1593
    perror_plus("sigaction");
1371
1594
    return EX_OSERR;
1372
1595
  }
1373
1596
  if(old_sigterm_action.sa_handler != SIG_IGN){
1374
1597
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1375
1598
    if(ret == -1){
1376
 
      perror("sigaction");
 
1599
      perror_plus("sigaction");
1377
1600
      exitcode = EX_OSERR;
1378
1601
      goto end;
1379
1602
    }
1380
1603
  }
1381
1604
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1382
1605
  if(ret == -1){
1383
 
    perror("sigaction");
 
1606
    perror_plus("sigaction");
1384
1607
    return EX_OSERR;
1385
1608
  }
1386
1609
  if(old_sigterm_action.sa_handler != SIG_IGN){
1387
1610
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1388
1611
    if(ret == -1){
1389
 
      perror("sigaction");
 
1612
      perror_plus("sigaction");
1390
1613
      exitcode = EX_OSERR;
1391
1614
      goto end;
1392
1615
    }
1409
1632
    errno = 0;
1410
1633
    ret = seteuid(0);
1411
1634
    if(ret == -1){
1412
 
      perror("seteuid");
 
1635
      perror_plus("seteuid");
1413
1636
    }
1414
1637
    
1415
1638
#ifdef __linux__
1419
1642
    bool restore_loglevel = true;
1420
1643
    if(ret == -1){
1421
1644
      restore_loglevel = false;
1422
 
      perror("klogctl");
 
1645
      perror_plus("klogctl");
1423
1646
    }
1424
1647
#endif  /* __linux__ */
1425
1648
    
1426
1649
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1427
1650
    if(sd < 0){
1428
 
      perror("socket");
 
1651
      perror_plus("socket");
1429
1652
      exitcode = EX_OSERR;
1430
1653
#ifdef __linux__
1431
1654
      if(restore_loglevel){
1432
1655
        ret = klogctl(7, NULL, 0);
1433
1656
        if(ret == -1){
1434
 
          perror("klogctl");
 
1657
          perror_plus("klogctl");
1435
1658
        }
1436
1659
      }
1437
1660
#endif  /* __linux__ */
1439
1662
      errno = 0;
1440
1663
      ret = seteuid(uid);
1441
1664
      if(ret == -1){
1442
 
        perror("seteuid");
 
1665
        perror_plus("seteuid");
1443
1666
      }
1444
1667
      goto end;
1445
1668
    }
1446
1669
    strcpy(network.ifr_name, interface);
1447
1670
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1448
1671
    if(ret == -1){
1449
 
      perror("ioctl SIOCGIFFLAGS");
 
1672
      perror_plus("ioctl SIOCGIFFLAGS");
1450
1673
#ifdef __linux__
1451
1674
      if(restore_loglevel){
1452
1675
        ret = klogctl(7, NULL, 0);
1453
1676
        if(ret == -1){
1454
 
          perror("klogctl");
 
1677
          perror_plus("klogctl");
1455
1678
        }
1456
1679
      }
1457
1680
#endif  /* __linux__ */
1460
1683
      errno = 0;
1461
1684
      ret = seteuid(uid);
1462
1685
      if(ret == -1){
1463
 
        perror("seteuid");
 
1686
        perror_plus("seteuid");
1464
1687
      }
1465
1688
      goto end;
1466
1689
    }
1470
1693
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1471
1694
      if(ret == -1){
1472
1695
        take_down_interface = false;
1473
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1696
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1474
1697
        exitcode = EX_OSERR;
1475
1698
#ifdef __linux__
1476
1699
        if(restore_loglevel){
1477
1700
          ret = klogctl(7, NULL, 0);
1478
1701
          if(ret == -1){
1479
 
            perror("klogctl");
 
1702
            perror_plus("klogctl");
1480
1703
          }
1481
1704
        }
1482
1705
#endif  /* __linux__ */
1484
1707
        errno = 0;
1485
1708
        ret = seteuid(uid);
1486
1709
        if(ret == -1){
1487
 
          perror("seteuid");
 
1710
          perror_plus("seteuid");
1488
1711
        }
1489
1712
        goto end;
1490
1713
      }
1491
1714
    }
1492
 
    /* sleep checking until interface is running */
 
1715
    /* Sleep checking until interface is running.
 
1716
       Check every 0.25s, up to total time of delay */
1493
1717
    for(int i=0; i < delay * 4; i++){
1494
1718
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1495
1719
      if(ret == -1){
1496
 
        perror("ioctl SIOCGIFFLAGS");
 
1720
        perror_plus("ioctl SIOCGIFFLAGS");
1497
1721
      } else if(network.ifr_flags & IFF_RUNNING){
1498
1722
        break;
1499
1723
      }
1500
1724
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1501
1725
      ret = nanosleep(&sleeptime, NULL);
1502
1726
      if(ret == -1 and errno != EINTR){
1503
 
        perror("nanosleep");
 
1727
        perror_plus("nanosleep");
1504
1728
      }
1505
1729
    }
1506
1730
    if(not take_down_interface){
1507
1731
      /* We won't need the socket anymore */
1508
1732
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1509
1733
      if(ret == -1){
1510
 
        perror("close");
 
1734
        perror_plus("close");
1511
1735
      }
1512
1736
    }
1513
1737
#ifdef __linux__
1515
1739
      /* Restores kernel loglevel to default */
1516
1740
      ret = klogctl(7, NULL, 0);
1517
1741
      if(ret == -1){
1518
 
        perror("klogctl");
 
1742
        perror_plus("klogctl");
1519
1743
      }
1520
1744
    }
1521
1745
#endif  /* __linux__ */
1525
1749
      /* Lower privileges */
1526
1750
      ret = seteuid(uid);
1527
1751
      if(ret == -1){
1528
 
        perror("seteuid");
 
1752
        perror_plus("seteuid");
1529
1753
      }
1530
1754
    } else {
1531
1755
      /* Lower privileges permanently */
1532
1756
      ret = setuid(uid);
1533
1757
      if(ret == -1){
1534
 
        perror("setuid");
 
1758
        perror_plus("setuid");
1535
1759
      }
1536
1760
    }
1537
1761
  }
1553
1777
    goto end;
1554
1778
  }
1555
1779
  
 
1780
  if(mkdtemp(tempdir) == NULL){
 
1781
    perror_plus("mkdtemp");
 
1782
    goto end;
 
1783
  }
1556
1784
  tempdir_created = true;
1557
 
  if(mkdtemp(tempdir) == NULL){
1558
 
    tempdir_created = false;
1559
 
    perror("mkdtemp");
1560
 
    goto end;
1561
 
  }
1562
1785
  
1563
1786
  if(quit_now){
1564
1787
    goto end;
1618
1841
    if(quit_now){
1619
1842
      goto end;
1620
1843
    }
1621
 
    
1622
 
    ret = start_mandos_communication(address, port, if_index, af);
1623
 
    if(ret < 0){
1624
 
      switch(errno){
1625
 
      case ENETUNREACH:
1626
 
      case EHOSTDOWN:
1627
 
      case EHOSTUNREACH:
1628
 
        exitcode = EX_NOHOST;
1629
 
        break;
1630
 
      case EINVAL:
1631
 
        exitcode = EX_USAGE;
1632
 
        break;
1633
 
      case EIO:
1634
 
        exitcode = EX_IOERR;
1635
 
        break;
1636
 
      case EPROTO:
1637
 
        exitcode = EX_PROTOCOL;
1638
 
        break;
1639
 
      default:
1640
 
        exitcode = EX_OSERR;
 
1844
 
 
1845
    while(not quit_now){
 
1846
      ret = start_mandos_communication(address, port, if_index, af);
 
1847
      if(quit_now or ret == 0){
1641
1848
        break;
1642
1849
      }
1643
 
    } else {
 
1850
      sleep((int)retry_interval or 1);
 
1851
    };
 
1852
 
 
1853
    if (not quit_now){
1644
1854
      exitcode = EXIT_SUCCESS;
1645
1855
    }
 
1856
 
1646
1857
    goto end;
1647
1858
  }
1648
1859
  
1700
1911
  if(debug){
1701
1912
    fprintf(stderr, "Starting Avahi loop search\n");
1702
1913
  }
1703
 
  
1704
 
  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
  }
1705
1921
  
1706
1922
 end:
1707
1923
  
1728
1944
  if(gpgme_initialized){
1729
1945
    gpgme_release(mc.ctx);
1730
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
  }
1731
1958
  
1732
1959
  /* Take down the network interface */
1733
1960
  if(take_down_interface){
1735
1962
    errno = 0;
1736
1963
    ret = seteuid(0);
1737
1964
    if(ret == -1){
1738
 
      perror("seteuid");
 
1965
      perror_plus("seteuid");
1739
1966
    }
1740
1967
    if(geteuid() == 0){
1741
1968
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1742
1969
      if(ret == -1){
1743
 
        perror("ioctl SIOCGIFFLAGS");
 
1970
        perror_plus("ioctl SIOCGIFFLAGS");
1744
1971
      } else if(network.ifr_flags & IFF_UP) {
1745
1972
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
1973
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1747
1974
        if(ret == -1){
1748
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1975
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1749
1976
        }
1750
1977
      }
1751
1978
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1752
1979
      if(ret == -1){
1753
 
        perror("close");
 
1980
        perror_plus("close");
1754
1981
      }
1755
1982
      /* Lower privileges permanently */
1756
1983
      errno = 0;
1757
1984
      ret = setuid(uid);
1758
1985
      if(ret == -1){
1759
 
        perror("setuid");
 
1986
        perror_plus("setuid");
1760
1987
      }
1761
1988
    }
1762
1989
  }
1763
1990
  
1764
 
  /* Removes the temp directory used by GPGME */
 
1991
  /* Removes the GPGME temp directory and all files inside */
1765
1992
  if(tempdir_created){
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
 
        }
 
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];
1786
1999
        char *fullname = NULL;
1787
2000
        ret = asprintf(&fullname, "%s/%s", tempdir,
1788
2001
                       direntry->d_name);
1789
2002
        if(ret < 0){
1790
 
          perror("asprintf");
 
2003
          perror_plus("asprintf");
1791
2004
          continue;
1792
2005
        }
1793
2006
        ret = remove(fullname);
1797
2010
        }
1798
2011
        free(fullname);
1799
2012
      }
1800
 
      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");
1801
2020
    }
1802
2021
    ret = rmdir(tempdir);
1803
2022
    if(ret == -1 and errno != ENOENT){
1804
 
      perror("rmdir");
 
2023
      perror_plus("rmdir");
1805
2024
    }
1806
2025
  }
1807
2026
  
1812
2031
                                            &old_sigterm_action,
1813
2032
                                            NULL));
1814
2033
    if(ret == -1){
1815
 
      perror("sigaction");
 
2034
      perror_plus("sigaction");
1816
2035
    }
1817
2036
    do {
1818
2037
      ret = raise(signal_received);
1819
2038
    } while(ret != 0 and errno == EINTR);
1820
2039
    if(ret != 0){
1821
 
      perror("raise");
 
2040
      perror_plus("raise");
1822
2041
      abort();
1823
2042
    }
1824
2043
    TEMP_FAILURE_RETRY(pause());