/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

Changed ForkingMixIn in favor of multiprocessing
Added approval functionallity

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-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 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
 
                                   program_invocation_short_name */
67
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
70
69
                                   if_nametoindex(), IF_NAMESIZE */
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
128
127
static const char mandos_protocol_version[] = "1";
129
128
const char *argp_program_version = "mandos-client " VERSION;
130
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
 
static const char sys_class_net[] = "/sys/class/net";
132
 
char *connect_to = NULL;
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
130
 
145
131
/* Used for passing in values through the Avahi callback functions */
146
132
typedef struct {
151
137
  gnutls_dh_params_t dh_params;
152
138
  const char *priority;
153
139
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
140
} mandos_context;
156
141
 
157
142
/* global context so signal handler can reach it*/
158
143
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
144
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
145
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
146
 
163
147
sig_atomic_t quit_now = 0;
164
148
int signal_received = 0;
165
149
 
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
150
/*
174
151
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
152
 * bytes. "buffer_capacity" is how much is currently allocated,
187
164
  return buffer_capacity;
188
165
}
189
166
 
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
 
 
227
167
/* 
228
168
 * Initialize GPGME.
229
169
 */
243
183
    
244
184
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
245
185
    if(fd == -1){
246
 
      perror_plus("open");
 
186
      perror("open");
247
187
      return false;
248
188
    }
249
189
    
263
203
    
264
204
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
265
205
    if(ret == -1){
266
 
      perror_plus("close");
 
206
      perror("close");
267
207
    }
268
208
    gpgme_data_release(pgp_data);
269
209
    return true;
394
334
  
395
335
  /* Seek back to the beginning of the GPGME plaintext data buffer */
396
336
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
397
 
    perror_plus("gpgme_data_seek");
 
337
    perror("gpgme_data_seek");
398
338
    plaintext_length = -1;
399
339
    goto decrypt_end;
400
340
  }
405
345
                                      (size_t)plaintext_length,
406
346
                                      plaintext_capacity);
407
347
    if(plaintext_capacity == 0){
408
 
        perror_plus("incbuffer");
 
348
        perror("incbuffer");
409
349
        plaintext_length = -1;
410
350
        goto decrypt_end;
411
351
    }
418
358
      break;
419
359
    }
420
360
    if(ret < 0){
421
 
      perror_plus("gpgme_data_read");
 
361
      perror("gpgme_data_read");
422
362
      plaintext_length = -1;
423
363
      goto decrypt_end;
424
364
    }
481
421
  }
482
422
  
483
423
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
485
425
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
487
430
            safer_gnutls_strerror(ret));
488
431
    gnutls_global_deinit();
489
432
    return -1;
644
587
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
588
  if(tcp_sd < 0){
646
589
    int e = errno;
647
 
    perror_plus("socket");
 
590
    perror("socket");
648
591
    errno = e;
649
592
    goto mandos_end;
650
593
  }
664
607
  }
665
608
  if(ret < 0 ){
666
609
    int e = errno;
667
 
    perror_plus("inet_pton");
 
610
    perror("inet_pton");
668
611
    errno = e;
669
612
    goto mandos_end;
670
613
  }
706
649
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
650
      char interface[IF_NAMESIZE];
708
651
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
652
        perror("if_indextoname");
710
653
      } else {
711
654
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
655
                ip, interface, port);
726
669
                        sizeof(addrstr));
727
670
    }
728
671
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
672
      perror("inet_ntop");
730
673
    } else {
731
674
      if(strcmp(addrstr, ip) != 0){
732
675
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
745
688
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
746
689
  }
747
690
  if(ret < 0){
748
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
 
      int e = errno;
750
 
      perror_plus("connect");
751
 
      errno = e;
752
 
    }
 
691
    int e = errno;
 
692
    perror("connect");
 
693
    errno = e;
753
694
    goto mandos_end;
754
695
  }
755
696
  
766
707
                                   out_size - written));
767
708
    if(ret == -1){
768
709
      int e = errno;
769
 
      perror_plus("write");
 
710
      perror("write");
770
711
      errno = e;
771
712
      goto mandos_end;
772
713
    }
797
738
    goto mandos_end;
798
739
  }
799
740
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
801
741
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
742
  
803
743
  if(quit_now){
840
780
                                   buffer_capacity);
841
781
    if(buffer_capacity == 0){
842
782
      int e = errno;
843
 
      perror_plus("incbuffer");
 
783
      perror("incbuffer");
844
784
      errno = e;
845
785
      goto mandos_end;
846
786
    }
951
891
      if(e == 0){
952
892
        e = errno;
953
893
      }
954
 
      perror_plus("close");
 
894
      perror("close");
955
895
    }
956
896
    gnutls_deinit(session);
957
 
    errno = e;
958
897
    if(quit_now){
959
 
      errno = EINTR;
 
898
      e = EINTR;
960
899
      retval = -1;
961
900
    }
 
901
    errno = e;
962
902
  }
963
903
  return retval;
964
904
}
1007
947
                                           avahi_proto_to_af(proto));
1008
948
      if(ret == 0){
1009
949
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
950
      }
1014
951
    }
1015
952
  }
1069
1006
  }
1070
1007
}
1071
1008
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
1009
/* stop main loop after sigterm has been called */
1073
1010
static void handle_sigterm(int sig){
1074
1011
  if(quit_now){
1075
1012
    return;
1077
1014
  quit_now = 1;
1078
1015
  signal_received = sig;
1079
1016
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
1017
  if(mc.simple_poll != NULL){
1082
1018
    avahi_simple_poll_quit(mc.simple_poll);
1083
1019
  }
1084
1020
  errno = old_errno;
1085
1021
}
1086
1022
 
1087
 
/* 
1088
 
 * This function determines if a directory entry in /sys/class/net
1089
 
 * corresponds to an acceptable network device.
1090
 
 * (This function is passed to scandir(3) as a filter function.)
1091
 
 */
1092
 
int good_interface(const struct dirent *if_entry){
1093
 
  ssize_t ssret;
1094
 
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
 
                     if_entry->d_name);
1100
 
  if(ret < 0){
1101
 
    perror_plus("asprintf");
1102
 
    return 0;
1103
 
  }
1104
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
 
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
1108
 
    return 0;
1109
 
  }
1110
 
  free(flagname);
1111
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
 
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
 
  flagstring[(size_t)to_read] = '\0';
1116
 
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
1118
 
    close(flags_fd);
1119
 
    return 0;
1120
 
  }
1121
 
  while(to_read > 0){
1122
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
 
                                             (size_t)to_read));
1124
 
    if(ssret == -1){
1125
 
      perror_plus("read");
1126
 
      free(flagstring);
1127
 
      close(flags_fd);
1128
 
      return 0;
1129
 
    }
1130
 
    to_read -= ssret;
1131
 
    if(ssret == 0){
1132
 
      break;
1133
 
    }
1134
 
  }
1135
 
  close(flags_fd);
1136
 
  intmax_t tmpmax;
1137
 
  char *tmp;
1138
 
  errno = 0;
1139
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1140
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
 
                                         and not (isspace(*tmp)))
1142
 
     or tmpmax != (ifreq_flags)tmpmax){
1143
 
    if(debug){
1144
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
 
              flagstring, if_entry->d_name);
1146
 
    }
1147
 
    free(flagstring);
1148
 
    return 0;
1149
 
  }
1150
 
  free(flagstring);
1151
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1152
 
  /* Reject the loopback device */
1153
 
  if(flags & IFF_LOOPBACK){
1154
 
    if(debug){
1155
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1156
 
              if_entry->d_name);
1157
 
    }
1158
 
    return 0;
1159
 
  }
1160
 
  /* Accept point-to-point devices only if connect_to is specified */
1161
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1162
 
    if(debug){
1163
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1164
 
              if_entry->d_name);
1165
 
    }
1166
 
    return 1;
1167
 
  }
1168
 
  /* Otherwise, reject non-broadcast-capable devices */
1169
 
  if(not (flags & IFF_BROADCAST)){
1170
 
    if(debug){
1171
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1172
 
              if_entry->d_name);
1173
 
    }
1174
 
    return 0;
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
 
  }
1184
 
  /* Accept this device */
1185
 
  if(debug){
1186
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1187
 
            if_entry->d_name);
1188
 
  }
1189
 
  return 1;
1190
 
}
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
 
 
1274
1023
int main(int argc, char *argv[]){
1275
1024
  AvahiSServiceBrowser *sb = NULL;
1276
1025
  int error;
1278
1027
  intmax_t tmpmax;
1279
1028
  char *tmp;
1280
1029
  int exitcode = EXIT_SUCCESS;
1281
 
  const char *interface = "";
 
1030
  const char *interface = "eth0";
1282
1031
  struct ifreq network;
1283
1032
  int sd = -1;
1284
1033
  bool take_down_interface = false;
1285
1034
  uid_t uid;
1286
1035
  gid_t gid;
 
1036
  char *connect_to = NULL;
1287
1037
  char tempdir[] = "/tmp/mandosXXXXXX";
1288
1038
  bool tempdir_created = false;
1289
1039
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1293
1043
  bool gnutls_initialized = false;
1294
1044
  bool gpgme_initialized = false;
1295
1045
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
1298
1046
  
1299
1047
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1048
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1306
1054
  errno = 0;
1307
1055
  ret = setgid(gid);
1308
1056
  if(ret == -1){
1309
 
    perror_plus("setgid");
 
1057
    perror("setgid");
1310
1058
  }
1311
1059
  
1312
1060
  /* Lower user privileges (temporarily) */
1313
1061
  errno = 0;
1314
1062
  ret = seteuid(uid);
1315
1063
  if(ret == -1){
1316
 
    perror_plus("seteuid");
 
1064
    perror("seteuid");
1317
1065
  }
1318
1066
  
1319
1067
  if(quit_now){
1354
1102
        .arg = "SECONDS",
1355
1103
        .doc = "Maximum delay to wait for interface startup",
1356
1104
        .group = 2 },
1357
 
      { .name = "retry", .key = 132,
1358
 
        .arg = "SECONDS",
1359
 
        .doc = "Retry interval used when denied by the mandos server",
1360
 
        .group = 2 },
1361
1105
      /*
1362
1106
       * These reproduce what we would get without ARGP_NO_HELP
1363
1107
       */
1407
1151
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
1152
          argp_error(state, "Bad delay");
1409
1153
        }
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
 
        }
1417
1154
        break;
1418
1155
        /*
1419
1156
         * These reproduce what we would get without ARGP_NO_HELP
1447
1184
    case ENOMEM:
1448
1185
    default:
1449
1186
      errno = ret;
1450
 
      perror_plus("argp_parse");
 
1187
      perror("argp_parse");
1451
1188
      exitcode = EX_OSERR;
1452
1189
      goto end;
1453
1190
    case EINVAL:
1455
1192
      goto end;
1456
1193
    }
1457
1194
  }
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
 
  }
1514
1195
  
1515
1196
  if(not debug){
1516
1197
    avahi_set_log_function(empty_log);
1517
1198
  }
1518
 
 
1519
 
  if(interface[0] == '\0'){
1520
 
    struct dirent **direntries;
1521
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1522
 
                  alphasort);
1523
 
    if(ret >= 1){
1524
 
      /* Pick the first good interface */
1525
 
      interface = strdup(direntries[0]->d_name);
1526
 
      if(debug){
1527
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1528
 
      }
1529
 
      if(interface == NULL){
1530
 
        perror_plus("malloc");
1531
 
        free(direntries);
1532
 
        exitcode = EXIT_FAILURE;
1533
 
        goto end;
1534
 
      }
1535
 
      free(direntries);
1536
 
    } else {
1537
 
      free(direntries);
1538
 
      fprintf(stderr, "Could not find a network interface\n");
1539
 
      exitcode = EXIT_FAILURE;
1540
 
      goto end;
1541
 
    }
1542
 
  }
1543
1199
  
1544
1200
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1545
1201
     from the signal handler */
1555
1211
  sigemptyset(&sigterm_action.sa_mask);
1556
1212
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1557
1213
  if(ret == -1){
1558
 
    perror_plus("sigaddset");
 
1214
    perror("sigaddset");
1559
1215
    exitcode = EX_OSERR;
1560
1216
    goto end;
1561
1217
  }
1562
1218
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1563
1219
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
 
1220
    perror("sigaddset");
1565
1221
    exitcode = EX_OSERR;
1566
1222
    goto end;
1567
1223
  }
1568
1224
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1569
1225
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
 
1226
    perror("sigaddset");
1571
1227
    exitcode = EX_OSERR;
1572
1228
    goto end;
1573
1229
  }
1577
1233
  */
1578
1234
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1579
1235
  if(ret == -1){
1580
 
    perror_plus("sigaction");
 
1236
    perror("sigaction");
1581
1237
    return EX_OSERR;
1582
1238
  }
1583
1239
  if(old_sigterm_action.sa_handler != SIG_IGN){
1584
1240
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1585
1241
    if(ret == -1){
1586
 
      perror_plus("sigaction");
 
1242
      perror("sigaction");
1587
1243
      exitcode = EX_OSERR;
1588
1244
      goto end;
1589
1245
    }
1590
1246
  }
1591
1247
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1592
1248
  if(ret == -1){
1593
 
    perror_plus("sigaction");
 
1249
    perror("sigaction");
1594
1250
    return EX_OSERR;
1595
1251
  }
1596
1252
  if(old_sigterm_action.sa_handler != SIG_IGN){
1597
1253
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1598
1254
    if(ret == -1){
1599
 
      perror_plus("sigaction");
 
1255
      perror("sigaction");
1600
1256
      exitcode = EX_OSERR;
1601
1257
      goto end;
1602
1258
    }
1603
1259
  }
1604
1260
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1605
1261
  if(ret == -1){
1606
 
    perror_plus("sigaction");
 
1262
    perror("sigaction");
1607
1263
    return EX_OSERR;
1608
1264
  }
1609
1265
  if(old_sigterm_action.sa_handler != SIG_IGN){
1610
1266
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1611
1267
    if(ret == -1){
1612
 
      perror_plus("sigaction");
 
1268
      perror("sigaction");
1613
1269
      exitcode = EX_OSERR;
1614
1270
      goto end;
1615
1271
    }
1616
1272
  }
1617
1273
  
1618
1274
  /* If the interface is down, bring it up */
1619
 
  if(strcmp(interface, "none") != 0){
 
1275
  if(interface[0] != '\0'){
1620
1276
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1621
1277
    if(if_index == 0){
1622
1278
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1632
1288
    errno = 0;
1633
1289
    ret = seteuid(0);
1634
1290
    if(ret == -1){
1635
 
      perror_plus("seteuid");
 
1291
      perror("seteuid");
1636
1292
    }
1637
1293
    
1638
1294
#ifdef __linux__
1642
1298
    bool restore_loglevel = true;
1643
1299
    if(ret == -1){
1644
1300
      restore_loglevel = false;
1645
 
      perror_plus("klogctl");
 
1301
      perror("klogctl");
1646
1302
    }
1647
1303
#endif  /* __linux__ */
1648
1304
    
1649
1305
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1650
1306
    if(sd < 0){
1651
 
      perror_plus("socket");
 
1307
      perror("socket");
1652
1308
      exitcode = EX_OSERR;
1653
1309
#ifdef __linux__
1654
1310
      if(restore_loglevel){
1655
1311
        ret = klogctl(7, NULL, 0);
1656
1312
        if(ret == -1){
1657
 
          perror_plus("klogctl");
 
1313
          perror("klogctl");
1658
1314
        }
1659
1315
      }
1660
1316
#endif  /* __linux__ */
1662
1318
      errno = 0;
1663
1319
      ret = seteuid(uid);
1664
1320
      if(ret == -1){
1665
 
        perror_plus("seteuid");
 
1321
        perror("seteuid");
1666
1322
      }
1667
1323
      goto end;
1668
1324
    }
1669
1325
    strcpy(network.ifr_name, interface);
1670
1326
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1671
1327
    if(ret == -1){
1672
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1328
      perror("ioctl SIOCGIFFLAGS");
1673
1329
#ifdef __linux__
1674
1330
      if(restore_loglevel){
1675
1331
        ret = klogctl(7, NULL, 0);
1676
1332
        if(ret == -1){
1677
 
          perror_plus("klogctl");
 
1333
          perror("klogctl");
1678
1334
        }
1679
1335
      }
1680
1336
#endif  /* __linux__ */
1683
1339
      errno = 0;
1684
1340
      ret = seteuid(uid);
1685
1341
      if(ret == -1){
1686
 
        perror_plus("seteuid");
 
1342
        perror("seteuid");
1687
1343
      }
1688
1344
      goto end;
1689
1345
    }
1693
1349
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1694
1350
      if(ret == -1){
1695
1351
        take_down_interface = false;
1696
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1352
        perror("ioctl SIOCSIFFLAGS");
1697
1353
        exitcode = EX_OSERR;
1698
1354
#ifdef __linux__
1699
1355
        if(restore_loglevel){
1700
1356
          ret = klogctl(7, NULL, 0);
1701
1357
          if(ret == -1){
1702
 
            perror_plus("klogctl");
 
1358
            perror("klogctl");
1703
1359
          }
1704
1360
        }
1705
1361
#endif  /* __linux__ */
1707
1363
        errno = 0;
1708
1364
        ret = seteuid(uid);
1709
1365
        if(ret == -1){
1710
 
          perror_plus("seteuid");
 
1366
          perror("seteuid");
1711
1367
        }
1712
1368
        goto end;
1713
1369
      }
1714
1370
    }
1715
 
    /* Sleep checking until interface is running.
1716
 
       Check every 0.25s, up to total time of delay */
 
1371
    /* sleep checking until interface is running */
1717
1372
    for(int i=0; i < delay * 4; i++){
1718
1373
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1719
1374
      if(ret == -1){
1720
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1375
        perror("ioctl SIOCGIFFLAGS");
1721
1376
      } else if(network.ifr_flags & IFF_RUNNING){
1722
1377
        break;
1723
1378
      }
1724
1379
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1725
1380
      ret = nanosleep(&sleeptime, NULL);
1726
1381
      if(ret == -1 and errno != EINTR){
1727
 
        perror_plus("nanosleep");
 
1382
        perror("nanosleep");
1728
1383
      }
1729
1384
    }
1730
1385
    if(not take_down_interface){
1731
1386
      /* We won't need the socket anymore */
1732
1387
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1733
1388
      if(ret == -1){
1734
 
        perror_plus("close");
 
1389
        perror("close");
1735
1390
      }
1736
1391
    }
1737
1392
#ifdef __linux__
1739
1394
      /* Restores kernel loglevel to default */
1740
1395
      ret = klogctl(7, NULL, 0);
1741
1396
      if(ret == -1){
1742
 
        perror_plus("klogctl");
 
1397
        perror("klogctl");
1743
1398
      }
1744
1399
    }
1745
1400
#endif  /* __linux__ */
1749
1404
      /* Lower privileges */
1750
1405
      ret = seteuid(uid);
1751
1406
      if(ret == -1){
1752
 
        perror_plus("seteuid");
 
1407
        perror("seteuid");
1753
1408
      }
1754
1409
    } else {
1755
1410
      /* Lower privileges permanently */
1756
1411
      ret = setuid(uid);
1757
1412
      if(ret == -1){
1758
 
        perror_plus("setuid");
 
1413
        perror("setuid");
1759
1414
      }
1760
1415
    }
1761
1416
  }
1777
1432
    goto end;
1778
1433
  }
1779
1434
  
 
1435
  tempdir_created = true;
1780
1436
  if(mkdtemp(tempdir) == NULL){
1781
 
    perror_plus("mkdtemp");
 
1437
    tempdir_created = false;
 
1438
    perror("mkdtemp");
1782
1439
    goto end;
1783
1440
  }
1784
 
  tempdir_created = true;
1785
1441
  
1786
1442
  if(quit_now){
1787
1443
    goto end;
1841
1497
    if(quit_now){
1842
1498
      goto end;
1843
1499
    }
1844
 
 
1845
 
    while(not quit_now){
1846
 
      ret = start_mandos_communication(address, port, if_index, af);
1847
 
      if(quit_now or ret == 0){
 
1500
    
 
1501
    ret = start_mandos_communication(address, port, if_index, af);
 
1502
    if(ret < 0){
 
1503
      switch(errno){
 
1504
      case ENETUNREACH:
 
1505
      case EHOSTDOWN:
 
1506
      case EHOSTUNREACH:
 
1507
        exitcode = EX_NOHOST;
 
1508
        break;
 
1509
      case EINVAL:
 
1510
        exitcode = EX_USAGE;
 
1511
        break;
 
1512
      case EIO:
 
1513
        exitcode = EX_IOERR;
 
1514
        break;
 
1515
      case EPROTO:
 
1516
        exitcode = EX_PROTOCOL;
 
1517
        break;
 
1518
      default:
 
1519
        exitcode = EX_OSERR;
1848
1520
        break;
1849
1521
      }
1850
 
      sleep((int)retry_interval or 1);
1851
 
    };
1852
 
 
1853
 
    if (not quit_now){
 
1522
    } else {
1854
1523
      exitcode = EXIT_SUCCESS;
1855
1524
    }
1856
 
 
1857
1525
    goto end;
1858
1526
  }
1859
1527
  
1911
1579
  if(debug){
1912
1580
    fprintf(stderr, "Starting Avahi loop search\n");
1913
1581
  }
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
 
  }
 
1582
  
 
1583
  avahi_simple_poll_loop(mc.simple_poll);
1921
1584
  
1922
1585
 end:
1923
1586
  
1944
1607
  if(gpgme_initialized){
1945
1608
    gpgme_release(mc.ctx);
1946
1609
  }
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
 
  }
1958
1610
  
1959
1611
  /* Take down the network interface */
1960
1612
  if(take_down_interface){
1962
1614
    errno = 0;
1963
1615
    ret = seteuid(0);
1964
1616
    if(ret == -1){
1965
 
      perror_plus("seteuid");
 
1617
      perror("seteuid");
1966
1618
    }
1967
1619
    if(geteuid() == 0){
1968
1620
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1969
1621
      if(ret == -1){
1970
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1622
        perror("ioctl SIOCGIFFLAGS");
1971
1623
      } else if(network.ifr_flags & IFF_UP) {
1972
1624
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1973
1625
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
1626
        if(ret == -1){
1975
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1627
          perror("ioctl SIOCSIFFLAGS");
1976
1628
        }
1977
1629
      }
1978
1630
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1979
1631
      if(ret == -1){
1980
 
        perror_plus("close");
 
1632
        perror("close");
1981
1633
      }
1982
1634
      /* Lower privileges permanently */
1983
1635
      errno = 0;
1984
1636
      ret = setuid(uid);
1985
1637
      if(ret == -1){
1986
 
        perror_plus("setuid");
 
1638
        perror("setuid");
1987
1639
      }
1988
1640
    }
1989
1641
  }
1990
1642
  
1991
 
  /* Removes the GPGME temp directory and all files inside */
 
1643
  /* Removes the temp directory used by GPGME */
1992
1644
  if(tempdir_created){
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];
 
1645
    DIR *d;
 
1646
    struct dirent *direntry;
 
1647
    d = opendir(tempdir);
 
1648
    if(d == NULL){
 
1649
      if(errno != ENOENT){
 
1650
        perror("opendir");
 
1651
      }
 
1652
    } else {
 
1653
      while(true){
 
1654
        direntry = readdir(d);
 
1655
        if(direntry == NULL){
 
1656
          break;
 
1657
        }
 
1658
        /* Skip "." and ".." */
 
1659
        if(direntry->d_name[0] == '.'
 
1660
           and (direntry->d_name[1] == '\0'
 
1661
                or (direntry->d_name[1] == '.'
 
1662
                    and direntry->d_name[2] == '\0'))){
 
1663
          continue;
 
1664
        }
1999
1665
        char *fullname = NULL;
2000
1666
        ret = asprintf(&fullname, "%s/%s", tempdir,
2001
1667
                       direntry->d_name);
2002
1668
        if(ret < 0){
2003
 
          perror_plus("asprintf");
 
1669
          perror("asprintf");
2004
1670
          continue;
2005
1671
        }
2006
1672
        ret = remove(fullname);
2010
1676
        }
2011
1677
        free(fullname);
2012
1678
      }
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");
 
1679
      closedir(d);
2020
1680
    }
2021
1681
    ret = rmdir(tempdir);
2022
1682
    if(ret == -1 and errno != ENOENT){
2023
 
      perror_plus("rmdir");
 
1683
      perror("rmdir");
2024
1684
    }
2025
1685
  }
2026
1686
  
2031
1691
                                            &old_sigterm_action,
2032
1692
                                            NULL));
2033
1693
    if(ret == -1){
2034
 
      perror_plus("sigaction");
 
1694
      perror("sigaction");
2035
1695
    }
2036
1696
    do {
2037
1697
      ret = raise(signal_received);
2038
1698
    } while(ret != 0 and errno == EINTR);
2039
1699
    if(ret != 0){
2040
 
      perror_plus("raise");
 
1700
      perror("raise");
2041
1701
      abort();
2042
1702
    }
2043
1703
    TEMP_FAILURE_RETRY(pause());