/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

* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
                                              of reading
                                              /sys/class/net/*/flags.

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
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
29
 * Contact the authors at <mandos@recompile.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
 
#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,
126
127
bool debug = false;
127
128
static const char mandos_protocol_version[] = "1";
128
129
const char *argp_program_version = "mandos-client " VERSION;
129
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
130
const char *argp_program_bug_address = "<mandos@recompile.se>";
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
/* 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
 
169
228
/* 
170
229
 * Initialize GPGME.
171
230
 */
185
244
    
186
245
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
187
246
    if(fd == -1){
188
 
      perror("open");
 
247
      perror_plus("open");
189
248
      return false;
190
249
    }
191
250
    
205
264
    
206
265
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
207
266
    if(ret == -1){
208
 
      perror("close");
 
267
      perror_plus("close");
209
268
    }
210
269
    gpgme_data_release(pgp_data);
211
270
    return true;
224
283
    return false;
225
284
  }
226
285
  
227
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
286
  /* Set GPGME home directory for the OpenPGP engine only */
228
287
  rc = gpgme_get_engine_info(&engine_info);
229
288
  if(rc != GPG_ERR_NO_ERROR){
230
289
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
336
395
  
337
396
  /* Seek back to the beginning of the GPGME plaintext data buffer */
338
397
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
339
 
    perror("gpgme_data_seek");
 
398
    perror_plus("gpgme_data_seek");
340
399
    plaintext_length = -1;
341
400
    goto decrypt_end;
342
401
  }
347
406
                                      (size_t)plaintext_length,
348
407
                                      plaintext_capacity);
349
408
    if(plaintext_capacity == 0){
350
 
        perror("incbuffer");
 
409
        perror_plus("incbuffer");
351
410
        plaintext_length = -1;
352
411
        goto decrypt_end;
353
412
    }
360
419
      break;
361
420
    }
362
421
    if(ret < 0){
363
 
      perror("gpgme_data_read");
 
422
      perror_plus("gpgme_data_read");
364
423
      plaintext_length = -1;
365
424
      goto decrypt_end;
366
425
    }
423
482
  }
424
483
  
425
484
  /* OpenPGP credentials */
426
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
485
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
427
486
  if(ret != GNUTLS_E_SUCCESS){
428
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
429
 
                                                    from
430
 
                                                    -Wunreachable-code
431
 
                                                 */
 
487
    fprintf(stderr, "GnuTLS memory error: %s\n",
432
488
            safer_gnutls_strerror(ret));
433
489
    gnutls_global_deinit();
434
490
    return -1;
589
645
  tcp_sd = socket(pf, SOCK_STREAM, 0);
590
646
  if(tcp_sd < 0){
591
647
    int e = errno;
592
 
    perror("socket");
 
648
    perror_plus("socket");
593
649
    errno = e;
594
650
    goto mandos_end;
595
651
  }
609
665
  }
610
666
  if(ret < 0 ){
611
667
    int e = errno;
612
 
    perror("inet_pton");
 
668
    perror_plus("inet_pton");
613
669
    errno = e;
614
670
    goto mandos_end;
615
671
  }
651
707
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
652
708
      char interface[IF_NAMESIZE];
653
709
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
654
 
        perror("if_indextoname");
 
710
        perror_plus("if_indextoname");
655
711
      } else {
656
712
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
657
713
                ip, interface, port);
671
727
                        sizeof(addrstr));
672
728
    }
673
729
    if(pcret == NULL){
674
 
      perror("inet_ntop");
 
730
      perror_plus("inet_ntop");
675
731
    } else {
676
732
      if(strcmp(addrstr, ip) != 0){
677
733
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
690
746
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
691
747
  }
692
748
  if(ret < 0){
693
 
    int e = errno;
694
 
    perror("connect");
695
 
    errno = e;
 
749
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
 
750
      int e = errno;
 
751
      perror_plus("connect");
 
752
      errno = e;
 
753
    }
696
754
    goto mandos_end;
697
755
  }
698
756
  
709
767
                                   out_size - written));
710
768
    if(ret == -1){
711
769
      int e = errno;
712
 
      perror("write");
 
770
      perror_plus("write");
713
771
      errno = e;
714
772
      goto mandos_end;
715
773
    }
740
798
    goto mandos_end;
741
799
  }
742
800
  
 
801
  /* Spurious warning from -Wint-to-pointer-cast */
743
802
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
744
803
  
745
804
  if(quit_now){
782
841
                                   buffer_capacity);
783
842
    if(buffer_capacity == 0){
784
843
      int e = errno;
785
 
      perror("incbuffer");
 
844
      perror_plus("incbuffer");
786
845
      errno = e;
787
846
      goto mandos_end;
788
847
    }
893
952
      if(e == 0){
894
953
        e = errno;
895
954
      }
896
 
      perror("close");
 
955
      perror_plus("close");
897
956
    }
898
957
    gnutls_deinit(session);
 
958
    errno = e;
899
959
    if(quit_now){
900
 
      e = EINTR;
 
960
      errno = EINTR;
901
961
      retval = -1;
902
962
    }
903
 
    errno = e;
904
963
  }
905
964
  return retval;
906
965
}
949
1008
                                           avahi_proto_to_af(proto));
950
1009
      if(ret == 0){
951
1010
        avahi_simple_poll_quit(mc.simple_poll);
 
1011
      } else {
 
1012
        ret = add_server(ip, port, interface,
 
1013
                         avahi_proto_to_af(proto));
952
1014
      }
953
1015
    }
954
1016
  }
1008
1070
  }
1009
1071
}
1010
1072
 
1011
 
/* stop main loop after sigterm has been called */
 
1073
/* Signal handler that stops main loop after SIGTERM */
1012
1074
static void handle_sigterm(int sig){
1013
1075
  if(quit_now){
1014
1076
    return;
1016
1078
  quit_now = 1;
1017
1079
  signal_received = sig;
1018
1080
  int old_errno = errno;
 
1081
  /* set main loop to exit */
1019
1082
  if(mc.simple_poll != NULL){
1020
1083
    avahi_simple_poll_quit(mc.simple_poll);
1021
1084
  }
1022
1085
  errno = old_errno;
1023
1086
}
1024
1087
 
1025
 
/* 
1026
 
 * This function determines if a directory entry in /sys/class/net
1027
 
 * corresponds to an acceptable network device.
1028
 
 * (This function is passed to scandir(3) as a filter function.)
1029
 
 */
1030
 
int good_interface(const struct dirent *if_entry){
1031
 
  ssize_t ssret;
1032
 
  char *flagname = NULL;
1033
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1034
 
                     if_entry->d_name);
1035
 
  if(ret < 0){
1036
 
    perror("asprintf");
1037
 
    return 0;
1038
 
  }
1039
 
  if(if_entry->d_name[0] == '.'){
1040
 
    return 0;
1041
 
  }
1042
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1043
 
  if(flags_fd == -1){
1044
 
    perror("open");
1045
 
    return 0;
1046
 
  }
1047
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1048
 
  /* read line from flags_fd */
1049
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1050
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1051
 
  flagstring[(size_t)to_read] = '\0';
1052
 
  if(flagstring == NULL){
1053
 
    perror("malloc");
1054
 
    close(flags_fd);
1055
 
    return 0;
1056
 
  }
1057
 
  while(to_read > 0){
1058
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1059
 
                                             (size_t)to_read));
1060
 
    if(ssret == -1){
1061
 
      perror("read");
1062
 
      free(flagstring);
1063
 
      close(flags_fd);
1064
 
      return 0;
1065
 
    }
1066
 
    to_read -= ssret;
1067
 
    if(ssret == 0){
1068
 
      break;
1069
 
    }
1070
 
  }
1071
 
  close(flags_fd);
1072
 
  intmax_t tmpmax;
1073
 
  char *tmp;
1074
 
  errno = 0;
1075
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1076
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1077
 
                                         and not (isspace(*tmp)))
1078
 
     or tmpmax != (ifreq_flags)tmpmax){
 
1088
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1089
  int ret;
 
1090
  
 
1091
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1092
  if(s < 0){
 
1093
    perror_plus("socket");
 
1094
    return false;
 
1095
  }
 
1096
  strcpy(ifr->ifr_name, ifname);
 
1097
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1098
  if(ret == -1){
1079
1099
    if(debug){
1080
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1081
 
              flagstring, if_entry->d_name);
 
1100
      perror_plus("ioctl SIOCGIFFLAGS");
1082
1101
    }
1083
 
    free(flagstring);
1084
 
    return 0;
 
1102
    return false;
1085
1103
  }
1086
 
  free(flagstring);
1087
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
 
1104
  return true;
 
1105
}
 
1106
 
 
1107
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1108
  
1088
1109
  /* Reject the loopback device */
1089
 
  if(flags & IFF_LOOPBACK){
 
1110
  if(ifr->ifr_flags & IFF_LOOPBACK){
1090
1111
    if(debug){
1091
1112
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1092
 
              if_entry->d_name);
 
1113
              ifname);
1093
1114
    }
1094
 
    return 0;
 
1115
    return false;
1095
1116
  }
1096
1117
  /* Accept point-to-point devices only if connect_to is specified */
1097
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
 
1118
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1098
1119
    if(debug){
1099
1120
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1100
 
              if_entry->d_name);
 
1121
              ifname);
1101
1122
    }
1102
 
    return 1;
 
1123
    return true;
1103
1124
  }
1104
1125
  /* Otherwise, reject non-broadcast-capable devices */
1105
 
  if(not (flags & IFF_BROADCAST)){
 
1126
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1106
1127
    if(debug){
1107
1128
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1108
 
              if_entry->d_name);
1109
 
    }
1110
 
    return 0;
1111
 
  }
 
1129
              ifname);
 
1130
    }
 
1131
    return false;
 
1132
  }
 
1133
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1134
  if(ifr->ifr_flags & IFF_NOARP){
 
1135
    if(debug){
 
1136
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1137
    }
 
1138
    return false;
 
1139
  }
 
1140
  
1112
1141
  /* Accept this device */
1113
1142
  if(debug){
1114
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1115
 
            if_entry->d_name);
1116
 
  }
1117
 
  return 1;
 
1143
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1144
  }
 
1145
  return true;
 
1146
}
 
1147
 
 
1148
/* 
 
1149
 * This function determines if a directory entry in /sys/class/net
 
1150
 * corresponds to an acceptable network device.
 
1151
 * (This function is passed to scandir(3) as a filter function.)
 
1152
 */
 
1153
int good_interface(const struct dirent *if_entry){
 
1154
  int ret;
 
1155
  if(if_entry->d_name[0] == '.'){
 
1156
    return 0;
 
1157
  }
 
1158
  struct ifreq ifr;
 
1159
 
 
1160
  if(not get_flags(if_entry->d_name, &ifr)){
 
1161
    return 0;
 
1162
  }
 
1163
  
 
1164
  if(not good_flags(if_entry->d_name, &ifr)){
 
1165
    return 0;
 
1166
  }
 
1167
  return 1;
 
1168
}
 
1169
 
 
1170
int notdotentries(const struct dirent *direntry){
 
1171
  /* Skip "." and ".." */
 
1172
  if(direntry->d_name[0] == '.'
 
1173
     and (direntry->d_name[1] == '\0'
 
1174
          or (direntry->d_name[1] == '.'
 
1175
              and direntry->d_name[2] == '\0'))){
 
1176
    return 0;
 
1177
  }
 
1178
  return 1;
 
1179
}
 
1180
 
 
1181
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
 
1182
  int ret;
 
1183
  struct timespec now;
 
1184
  struct timespec waited_time;
 
1185
  intmax_t block_time;
 
1186
  
 
1187
  while(true){
 
1188
    if(mc.current_server == NULL){
 
1189
      if (debug){
 
1190
        fprintf(stderr,
 
1191
                "Wait until first server is found. No timeout!\n");
 
1192
      }
 
1193
      ret = avahi_simple_poll_iterate(s, -1);
 
1194
    } else {
 
1195
      if (debug){
 
1196
        fprintf(stderr, "Check current_server if we should run it,"
 
1197
                " or wait\n");
 
1198
      }
 
1199
      /* the current time */
 
1200
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
 
1201
      if(ret == -1){
 
1202
        perror_plus("clock_gettime");
 
1203
        return -1;
 
1204
      }
 
1205
      /* Calculating in ms how long time between now and server
 
1206
         who we visted longest time ago. Now - last seen.  */
 
1207
      waited_time.tv_sec = (now.tv_sec
 
1208
                            - mc.current_server->last_seen.tv_sec);
 
1209
      waited_time.tv_nsec = (now.tv_nsec
 
1210
                             - mc.current_server->last_seen.tv_nsec);
 
1211
      /* total time is 10s/10,000ms.
 
1212
         Converting to s from ms by dividing by 1,000,
 
1213
         and ns to ms by dividing by 1,000,000. */
 
1214
      block_time = ((retry_interval
 
1215
                     - ((intmax_t)waited_time.tv_sec * 1000))
 
1216
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1217
      
 
1218
      if (debug){
 
1219
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1220
      }
 
1221
      
 
1222
      if(block_time <= 0){
 
1223
        ret = start_mandos_communication(mc.current_server->ip,
 
1224
                                         mc.current_server->port,
 
1225
                                         mc.current_server->if_index,
 
1226
                                         mc.current_server->af);
 
1227
        if(ret == 0){
 
1228
          avahi_simple_poll_quit(mc.simple_poll);
 
1229
          return 0;
 
1230
        }
 
1231
        ret = clock_gettime(CLOCK_MONOTONIC,
 
1232
                            &mc.current_server->last_seen);
 
1233
        if(ret == -1){
 
1234
          perror_plus("clock_gettime");
 
1235
          return -1;
 
1236
        }
 
1237
        mc.current_server = mc.current_server->next;
 
1238
        block_time = 0;         /* Call avahi to find new Mandos
 
1239
                                   servers, but don't block */
 
1240
      }
 
1241
      
 
1242
      ret = avahi_simple_poll_iterate(s, (int)block_time);
 
1243
    }
 
1244
    if(ret != 0){
 
1245
      if (ret > 0 or errno != EINTR) {
 
1246
        return (ret != 1) ? ret : 0;
 
1247
      }
 
1248
    }
 
1249
  }
1118
1250
}
1119
1251
 
1120
1252
int main(int argc, char *argv[]){
1139
1271
  bool gnutls_initialized = false;
1140
1272
  bool gpgme_initialized = false;
1141
1273
  float delay = 2.5f;
 
1274
  double retry_interval = 10; /* 10s between trying a server and
 
1275
                                 retrying the same server again */
1142
1276
  
1143
1277
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1144
1278
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1150
1284
  errno = 0;
1151
1285
  ret = setgid(gid);
1152
1286
  if(ret == -1){
1153
 
    perror("setgid");
 
1287
    perror_plus("setgid");
1154
1288
  }
1155
1289
  
1156
1290
  /* Lower user privileges (temporarily) */
1157
1291
  errno = 0;
1158
1292
  ret = seteuid(uid);
1159
1293
  if(ret == -1){
1160
 
    perror("seteuid");
 
1294
    perror_plus("seteuid");
1161
1295
  }
1162
1296
  
1163
1297
  if(quit_now){
1198
1332
        .arg = "SECONDS",
1199
1333
        .doc = "Maximum delay to wait for interface startup",
1200
1334
        .group = 2 },
 
1335
      { .name = "retry", .key = 132,
 
1336
        .arg = "SECONDS",
 
1337
        .doc = "Retry interval used when denied by the mandos server",
 
1338
        .group = 2 },
1201
1339
      /*
1202
1340
       * These reproduce what we would get without ARGP_NO_HELP
1203
1341
       */
1247
1385
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1248
1386
          argp_error(state, "Bad delay");
1249
1387
        }
 
1388
      case 132:                 /* --retry */
 
1389
        errno = 0;
 
1390
        retry_interval = strtod(arg, &tmp);
 
1391
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
1392
           or (retry_interval * 1000) > INT_MAX
 
1393
           or retry_interval < 0){
 
1394
          argp_error(state, "Bad retry interval");
 
1395
        }
1250
1396
        break;
1251
1397
        /*
1252
1398
         * These reproduce what we would get without ARGP_NO_HELP
1280
1426
    case ENOMEM:
1281
1427
    default:
1282
1428
      errno = ret;
1283
 
      perror("argp_parse");
 
1429
      perror_plus("argp_parse");
1284
1430
      exitcode = EX_OSERR;
1285
1431
      goto end;
1286
1432
    case EINVAL:
1288
1434
      goto end;
1289
1435
    }
1290
1436
  }
 
1437
    
 
1438
  {
 
1439
    /* Work around Debian bug #633582:
 
1440
       <http://bugs.debian.org/633582> */
 
1441
    struct stat st;
 
1442
    
 
1443
    /* Re-raise priviliges */
 
1444
    errno = 0;
 
1445
    ret = seteuid(0);
 
1446
    if(ret == -1){
 
1447
      perror_plus("seteuid");
 
1448
    }
 
1449
    
 
1450
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1451
      int seckey_fd = open(seckey, O_RDONLY);
 
1452
      if(seckey_fd == -1){
 
1453
        perror_plus("open");
 
1454
      } else {
 
1455
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1456
        if(ret == -1){
 
1457
          perror_plus("fstat");
 
1458
        } else {
 
1459
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1460
            ret = fchown(seckey_fd, uid, gid);
 
1461
            if(ret == -1){
 
1462
              perror_plus("fchown");
 
1463
            }
 
1464
          }
 
1465
        }
 
1466
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1467
      }
 
1468
    }
 
1469
    
 
1470
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1471
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1472
      if(pubkey_fd == -1){
 
1473
        perror_plus("open");
 
1474
      } else {
 
1475
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_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(pubkey_fd, uid, gid);
 
1481
            if(ret == -1){
 
1482
              perror_plus("fchown");
 
1483
            }
 
1484
          }
 
1485
        }
 
1486
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1487
      }
 
1488
    }
 
1489
    
 
1490
    /* Lower privileges */
 
1491
    errno = 0;
 
1492
    ret = seteuid(uid);
 
1493
    if(ret == -1){
 
1494
      perror_plus("seteuid");
 
1495
    }
 
1496
  }
1291
1497
  
1292
1498
  if(not debug){
1293
1499
    avahi_set_log_function(empty_log);
1304
1510
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1305
1511
      }
1306
1512
      if(interface == NULL){
1307
 
        perror("malloc");
 
1513
        perror_plus("malloc");
1308
1514
        free(direntries);
1309
1515
        exitcode = EXIT_FAILURE;
1310
1516
        goto end;
1332
1538
  sigemptyset(&sigterm_action.sa_mask);
1333
1539
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1334
1540
  if(ret == -1){
1335
 
    perror("sigaddset");
 
1541
    perror_plus("sigaddset");
1336
1542
    exitcode = EX_OSERR;
1337
1543
    goto end;
1338
1544
  }
1339
1545
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1340
1546
  if(ret == -1){
1341
 
    perror("sigaddset");
 
1547
    perror_plus("sigaddset");
1342
1548
    exitcode = EX_OSERR;
1343
1549
    goto end;
1344
1550
  }
1345
1551
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1346
1552
  if(ret == -1){
1347
 
    perror("sigaddset");
 
1553
    perror_plus("sigaddset");
1348
1554
    exitcode = EX_OSERR;
1349
1555
    goto end;
1350
1556
  }
1354
1560
  */
1355
1561
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1356
1562
  if(ret == -1){
1357
 
    perror("sigaction");
 
1563
    perror_plus("sigaction");
1358
1564
    return EX_OSERR;
1359
1565
  }
1360
1566
  if(old_sigterm_action.sa_handler != SIG_IGN){
1361
1567
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1362
1568
    if(ret == -1){
1363
 
      perror("sigaction");
 
1569
      perror_plus("sigaction");
1364
1570
      exitcode = EX_OSERR;
1365
1571
      goto end;
1366
1572
    }
1367
1573
  }
1368
1574
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1369
1575
  if(ret == -1){
1370
 
    perror("sigaction");
 
1576
    perror_plus("sigaction");
1371
1577
    return EX_OSERR;
1372
1578
  }
1373
1579
  if(old_sigterm_action.sa_handler != SIG_IGN){
1374
1580
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1375
1581
    if(ret == -1){
1376
 
      perror("sigaction");
 
1582
      perror_plus("sigaction");
1377
1583
      exitcode = EX_OSERR;
1378
1584
      goto end;
1379
1585
    }
1380
1586
  }
1381
1587
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1382
1588
  if(ret == -1){
1383
 
    perror("sigaction");
 
1589
    perror_plus("sigaction");
1384
1590
    return EX_OSERR;
1385
1591
  }
1386
1592
  if(old_sigterm_action.sa_handler != SIG_IGN){
1387
1593
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1388
1594
    if(ret == -1){
1389
 
      perror("sigaction");
 
1595
      perror_plus("sigaction");
1390
1596
      exitcode = EX_OSERR;
1391
1597
      goto end;
1392
1598
    }
1409
1615
    errno = 0;
1410
1616
    ret = seteuid(0);
1411
1617
    if(ret == -1){
1412
 
      perror("seteuid");
 
1618
      perror_plus("seteuid");
1413
1619
    }
1414
1620
    
1415
1621
#ifdef __linux__
1419
1625
    bool restore_loglevel = true;
1420
1626
    if(ret == -1){
1421
1627
      restore_loglevel = false;
1422
 
      perror("klogctl");
 
1628
      perror_plus("klogctl");
1423
1629
    }
1424
1630
#endif  /* __linux__ */
1425
1631
    
1426
1632
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1427
1633
    if(sd < 0){
1428
 
      perror("socket");
 
1634
      perror_plus("socket");
1429
1635
      exitcode = EX_OSERR;
1430
1636
#ifdef __linux__
1431
1637
      if(restore_loglevel){
1432
1638
        ret = klogctl(7, NULL, 0);
1433
1639
        if(ret == -1){
1434
 
          perror("klogctl");
 
1640
          perror_plus("klogctl");
1435
1641
        }
1436
1642
      }
1437
1643
#endif  /* __linux__ */
1439
1645
      errno = 0;
1440
1646
      ret = seteuid(uid);
1441
1647
      if(ret == -1){
1442
 
        perror("seteuid");
 
1648
        perror_plus("seteuid");
1443
1649
      }
1444
1650
      goto end;
1445
1651
    }
1446
1652
    strcpy(network.ifr_name, interface);
1447
1653
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1448
1654
    if(ret == -1){
1449
 
      perror("ioctl SIOCGIFFLAGS");
 
1655
      perror_plus("ioctl SIOCGIFFLAGS");
1450
1656
#ifdef __linux__
1451
1657
      if(restore_loglevel){
1452
1658
        ret = klogctl(7, NULL, 0);
1453
1659
        if(ret == -1){
1454
 
          perror("klogctl");
 
1660
          perror_plus("klogctl");
1455
1661
        }
1456
1662
      }
1457
1663
#endif  /* __linux__ */
1460
1666
      errno = 0;
1461
1667
      ret = seteuid(uid);
1462
1668
      if(ret == -1){
1463
 
        perror("seteuid");
 
1669
        perror_plus("seteuid");
1464
1670
      }
1465
1671
      goto end;
1466
1672
    }
1470
1676
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1471
1677
      if(ret == -1){
1472
1678
        take_down_interface = false;
1473
 
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1679
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1474
1680
        exitcode = EX_OSERR;
1475
1681
#ifdef __linux__
1476
1682
        if(restore_loglevel){
1477
1683
          ret = klogctl(7, NULL, 0);
1478
1684
          if(ret == -1){
1479
 
            perror("klogctl");
 
1685
            perror_plus("klogctl");
1480
1686
          }
1481
1687
        }
1482
1688
#endif  /* __linux__ */
1484
1690
        errno = 0;
1485
1691
        ret = seteuid(uid);
1486
1692
        if(ret == -1){
1487
 
          perror("seteuid");
 
1693
          perror_plus("seteuid");
1488
1694
        }
1489
1695
        goto end;
1490
1696
      }
1491
1697
    }
1492
 
    /* sleep checking until interface is running */
 
1698
    /* Sleep checking until interface is running.
 
1699
       Check every 0.25s, up to total time of delay */
1493
1700
    for(int i=0; i < delay * 4; i++){
1494
1701
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1495
1702
      if(ret == -1){
1496
 
        perror("ioctl SIOCGIFFLAGS");
 
1703
        perror_plus("ioctl SIOCGIFFLAGS");
1497
1704
      } else if(network.ifr_flags & IFF_RUNNING){
1498
1705
        break;
1499
1706
      }
1500
1707
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1501
1708
      ret = nanosleep(&sleeptime, NULL);
1502
1709
      if(ret == -1 and errno != EINTR){
1503
 
        perror("nanosleep");
 
1710
        perror_plus("nanosleep");
1504
1711
      }
1505
1712
    }
1506
1713
    if(not take_down_interface){
1507
1714
      /* We won't need the socket anymore */
1508
1715
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1509
1716
      if(ret == -1){
1510
 
        perror("close");
 
1717
        perror_plus("close");
1511
1718
      }
1512
1719
    }
1513
1720
#ifdef __linux__
1515
1722
      /* Restores kernel loglevel to default */
1516
1723
      ret = klogctl(7, NULL, 0);
1517
1724
      if(ret == -1){
1518
 
        perror("klogctl");
 
1725
        perror_plus("klogctl");
1519
1726
      }
1520
1727
    }
1521
1728
#endif  /* __linux__ */
1525
1732
      /* Lower privileges */
1526
1733
      ret = seteuid(uid);
1527
1734
      if(ret == -1){
1528
 
        perror("seteuid");
 
1735
        perror_plus("seteuid");
1529
1736
      }
1530
1737
    } else {
1531
1738
      /* Lower privileges permanently */
1532
1739
      ret = setuid(uid);
1533
1740
      if(ret == -1){
1534
 
        perror("setuid");
 
1741
        perror_plus("setuid");
1535
1742
      }
1536
1743
    }
1537
1744
  }
1553
1760
    goto end;
1554
1761
  }
1555
1762
  
 
1763
  if(mkdtemp(tempdir) == NULL){
 
1764
    perror_plus("mkdtemp");
 
1765
    goto end;
 
1766
  }
1556
1767
  tempdir_created = true;
1557
 
  if(mkdtemp(tempdir) == NULL){
1558
 
    tempdir_created = false;
1559
 
    perror("mkdtemp");
1560
 
    goto end;
1561
 
  }
1562
1768
  
1563
1769
  if(quit_now){
1564
1770
    goto end;
1606
1812
    
1607
1813
    port = (uint16_t)tmpmax;
1608
1814
    *address = '\0';
1609
 
    address = connect_to;
1610
1815
    /* Colon in address indicates IPv6 */
1611
1816
    int af;
1612
 
    if(strchr(address, ':') != NULL){
 
1817
    if(strchr(connect_to, ':') != NULL){
1613
1818
      af = AF_INET6;
 
1819
      /* Accept [] around IPv6 address - see RFC 5952 */
 
1820
      if(connect_to[0] == '[' and address[-1] == ']')
 
1821
        {
 
1822
          connect_to++;
 
1823
          address[-1] = '\0';
 
1824
        }
1614
1825
    } else {
1615
1826
      af = AF_INET;
1616
1827
    }
 
1828
    address = connect_to;
1617
1829
    
1618
1830
    if(quit_now){
1619
1831
      goto end;
1620
1832
    }
1621
1833
    
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;
1641
 
        break;
1642
 
      }
1643
 
    } else {
 
1834
    while(not quit_now){
 
1835
      ret = start_mandos_communication(address, port, if_index, af);
 
1836
      if(quit_now or ret == 0){
 
1837
        break;
 
1838
      }
 
1839
      if(debug){
 
1840
        fprintf(stderr, "Retrying in %d seconds\n",
 
1841
                (int)retry_interval);
 
1842
      }
 
1843
      sleep((int)retry_interval);
 
1844
    }
 
1845
    
 
1846
    if (not quit_now){
1644
1847
      exitcode = EXIT_SUCCESS;
1645
1848
    }
 
1849
 
1646
1850
    goto end;
1647
1851
  }
1648
1852
  
1700
1904
  if(debug){
1701
1905
    fprintf(stderr, "Starting Avahi loop search\n");
1702
1906
  }
1703
 
  
1704
 
  avahi_simple_poll_loop(mc.simple_poll);
 
1907
 
 
1908
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
1909
                                (int)(retry_interval * 1000));
 
1910
  if(debug){
 
1911
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
 
1912
            (ret == 0) ? "successfully" : "with error");
 
1913
  }
1705
1914
  
1706
1915
 end:
1707
1916
  
1728
1937
  if(gpgme_initialized){
1729
1938
    gpgme_release(mc.ctx);
1730
1939
  }
 
1940
 
 
1941
  /* Cleans up the circular linked list of Mandos servers the client
 
1942
     has seen */
 
1943
  if(mc.current_server != NULL){
 
1944
    mc.current_server->prev->next = NULL;
 
1945
    while(mc.current_server != NULL){
 
1946
      server *next = mc.current_server->next;
 
1947
      free(mc.current_server);
 
1948
      mc.current_server = next;
 
1949
    }
 
1950
  }
1731
1951
  
1732
1952
  /* Take down the network interface */
1733
1953
  if(take_down_interface){
1735
1955
    errno = 0;
1736
1956
    ret = seteuid(0);
1737
1957
    if(ret == -1){
1738
 
      perror("seteuid");
 
1958
      perror_plus("seteuid");
1739
1959
    }
1740
1960
    if(geteuid() == 0){
1741
1961
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1742
1962
      if(ret == -1){
1743
 
        perror("ioctl SIOCGIFFLAGS");
 
1963
        perror_plus("ioctl SIOCGIFFLAGS");
1744
1964
      } else if(network.ifr_flags & IFF_UP) {
1745
1965
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
1966
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1747
1967
        if(ret == -1){
1748
 
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
 
1968
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1749
1969
        }
1750
1970
      }
1751
1971
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1752
1972
      if(ret == -1){
1753
 
        perror("close");
 
1973
        perror_plus("close");
1754
1974
      }
1755
1975
      /* Lower privileges permanently */
1756
1976
      errno = 0;
1757
1977
      ret = setuid(uid);
1758
1978
      if(ret == -1){
1759
 
        perror("setuid");
 
1979
        perror_plus("setuid");
1760
1980
      }
1761
1981
    }
1762
1982
  }
1763
1983
  
1764
 
  /* Removes the temp directory used by GPGME */
 
1984
  /* Removes the GPGME temp directory and all files inside */
1765
1985
  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
 
        }
 
1986
    struct dirent **direntries = NULL;
 
1987
    struct dirent *direntry = NULL;
 
1988
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
1989
                             alphasort);
 
1990
    if (numentries > 0){
 
1991
      for(int i = 0; i < numentries; i++){
 
1992
        direntry = direntries[i];
1786
1993
        char *fullname = NULL;
1787
1994
        ret = asprintf(&fullname, "%s/%s", tempdir,
1788
1995
                       direntry->d_name);
1789
1996
        if(ret < 0){
1790
 
          perror("asprintf");
 
1997
          perror_plus("asprintf");
1791
1998
          continue;
1792
1999
        }
1793
2000
        ret = remove(fullname);
1797
2004
        }
1798
2005
        free(fullname);
1799
2006
      }
1800
 
      closedir(d);
 
2007
    }
 
2008
 
 
2009
    /* need to clean even if 0 because man page doesn't specify */
 
2010
    free(direntries);
 
2011
    if (numentries == -1){
 
2012
      perror_plus("scandir");
1801
2013
    }
1802
2014
    ret = rmdir(tempdir);
1803
2015
    if(ret == -1 and errno != ENOENT){
1804
 
      perror("rmdir");
 
2016
      perror_plus("rmdir");
1805
2017
    }
1806
2018
  }
1807
2019
  
1812
2024
                                            &old_sigterm_action,
1813
2025
                                            NULL));
1814
2026
    if(ret == -1){
1815
 
      perror("sigaction");
 
2027
      perror_plus("sigaction");
1816
2028
    }
1817
2029
    do {
1818
2030
      ret = raise(signal_received);
1819
2031
    } while(ret != 0 and errno == EINTR);
1820
2032
    if(ret != 0){
1821
 
      perror("raise");
 
2033
      perror_plus("raise");
1822
2034
      abort();
1823
2035
    }
1824
2036
    TEMP_FAILURE_RETRY(pause());