/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: 2010-09-30 06:24:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100930062420-nm7nlb160myyzj82
* mandos-monitor.xml (KEYS): Bug fix: Changed "r" to "R".
* .bzrignore: Updated.
  (debian/po/messages.mo, debian/po/templates.pot): Removed.
  (plugins.d/plymouth): Added.

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-2010 Teddy Hogeborn
 
13
 * Copyright © 2008-2010 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@recompile.se>.
 
29
 * Contact the authors at <mandos@fukt.bsnet.se>.
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno,
66
 
                                   program_invocation_short_name */
 
65
#include <errno.h>              /* perror(), errno */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
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,
123
122
#define PATHDIR "/conf/conf.d/mandos"
124
123
#define SECKEY "seckey.txt"
125
124
#define PUBKEY "pubkey.txt"
126
 
#define HOOKDIR "/lib/mandos/network-hooks.d"
127
125
 
128
126
bool debug = false;
129
127
static const char mandos_protocol_version[] = "1";
130
128
const char *argp_program_version = "mandos-client " VERSION;
131
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
132
130
static const char sys_class_net[] = "/sys/class/net";
133
131
char *connect_to = NULL;
134
132
 
135
 
/* Doubly linked list that need to be circularly linked when used */
136
 
typedef struct server{
137
 
  const char *ip;
138
 
  uint16_t port;
139
 
  AvahiIfIndex if_index;
140
 
  int af;
141
 
  struct timespec last_seen;
142
 
  struct server *next;
143
 
  struct server *prev;
144
 
} server;
145
 
 
146
133
/* Used for passing in values through the Avahi callback functions */
147
134
typedef struct {
148
135
  AvahiSimplePoll *simple_poll;
152
139
  gnutls_dh_params_t dh_params;
153
140
  const char *priority;
154
141
  gpgme_ctx_t ctx;
155
 
  server *current_server;
156
142
} mandos_context;
157
143
 
158
144
/* global context so signal handler can reach it*/
159
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
160
146
                      .dh_bits = 1024, .priority = "SECURE256"
161
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
162
 
                      .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
163
148
 
164
149
sig_atomic_t quit_now = 0;
165
150
int signal_received = 0;
166
151
 
167
 
/* Function to use when printing errors */
168
 
void perror_plus(const char *print_text){
169
 
  fprintf(stderr, "Mandos plugin %s: ",
170
 
          program_invocation_short_name);
171
 
  perror(print_text);
172
 
}
173
 
 
174
152
/*
175
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
176
154
 * bytes. "buffer_capacity" is how much is currently allocated,
188
166
  return buffer_capacity;
189
167
}
190
168
 
191
 
/* Add server to set of servers to retry periodically */
192
 
int add_server(const char *ip, uint16_t port,
193
 
                 AvahiIfIndex if_index,
194
 
                 int af){
195
 
  int ret;
196
 
  server *new_server = malloc(sizeof(server));
197
 
  if(new_server == NULL){
198
 
    perror_plus("malloc");
199
 
    return -1;
200
 
  }
201
 
  *new_server = (server){ .ip = strdup(ip),
202
 
                         .port = port,
203
 
                         .if_index = if_index,
204
 
                         .af = af };
205
 
  if(new_server->ip == NULL){
206
 
    perror_plus("strdup");
207
 
    return -1;
208
 
  }
209
 
  /* Special case of first server */
210
 
  if (mc.current_server == NULL){
211
 
    new_server->next = new_server;
212
 
    new_server->prev = new_server;
213
 
    mc.current_server = new_server;
214
 
  /* Place the new server last in the list */
215
 
  } else {
216
 
    new_server->next = mc.current_server;
217
 
    new_server->prev = mc.current_server->prev;
218
 
    new_server->prev->next = new_server;
219
 
    mc.current_server->prev = new_server;
220
 
  }
221
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
222
 
  if(ret == -1){
223
 
    perror_plus("clock_gettime");
224
 
    return -1;
225
 
  }
226
 
  return 0;
227
 
}
228
 
 
229
169
/* 
230
170
 * Initialize GPGME.
231
171
 */
245
185
    
246
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
247
187
    if(fd == -1){
248
 
      perror_plus("open");
 
188
      perror("open");
249
189
      return false;
250
190
    }
251
191
    
265
205
    
266
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
267
207
    if(ret == -1){
268
 
      perror_plus("close");
 
208
      perror("close");
269
209
    }
270
210
    gpgme_data_release(pgp_data);
271
211
    return true;
284
224
    return false;
285
225
  }
286
226
  
287
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
227
    /* Set GPGME home directory for the OpenPGP engine only */
288
228
  rc = gpgme_get_engine_info(&engine_info);
289
229
  if(rc != GPG_ERR_NO_ERROR){
290
230
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
396
336
  
397
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
398
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
399
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
400
340
    plaintext_length = -1;
401
341
    goto decrypt_end;
402
342
  }
407
347
                                      (size_t)plaintext_length,
408
348
                                      plaintext_capacity);
409
349
    if(plaintext_capacity == 0){
410
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
411
351
        plaintext_length = -1;
412
352
        goto decrypt_end;
413
353
    }
420
360
      break;
421
361
    }
422
362
    if(ret < 0){
423
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
424
364
      plaintext_length = -1;
425
365
      goto decrypt_end;
426
366
    }
483
423
  }
484
424
  
485
425
  /* OpenPGP credentials */
486
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  gnutls_certificate_allocate_credentials(&mc.cred);
487
427
  if(ret != GNUTLS_E_SUCCESS){
488
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
429
                                                    from
 
430
                                                    -Wunreachable-code
 
431
                                                 */
489
432
            safer_gnutls_strerror(ret));
490
433
    gnutls_global_deinit();
491
434
    return -1;
646
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
647
590
  if(tcp_sd < 0){
648
591
    int e = errno;
649
 
    perror_plus("socket");
 
592
    perror("socket");
650
593
    errno = e;
651
594
    goto mandos_end;
652
595
  }
666
609
  }
667
610
  if(ret < 0 ){
668
611
    int e = errno;
669
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
670
613
    errno = e;
671
614
    goto mandos_end;
672
615
  }
708
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
709
652
      char interface[IF_NAMESIZE];
710
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
711
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
712
655
      } else {
713
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
714
657
                ip, interface, port);
728
671
                        sizeof(addrstr));
729
672
    }
730
673
    if(pcret == NULL){
731
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
732
675
    } else {
733
676
      if(strcmp(addrstr, ip) != 0){
734
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
749
692
  if(ret < 0){
750
693
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
751
694
      int e = errno;
752
 
      perror_plus("connect");
 
695
      perror("connect");
753
696
      errno = e;
754
697
    }
755
698
    goto mandos_end;
768
711
                                   out_size - written));
769
712
    if(ret == -1){
770
713
      int e = errno;
771
 
      perror_plus("write");
 
714
      perror("write");
772
715
      errno = e;
773
716
      goto mandos_end;
774
717
    }
799
742
    goto mandos_end;
800
743
  }
801
744
  
802
 
  /* Spurious warning from -Wint-to-pointer-cast */
803
745
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
804
746
  
805
747
  if(quit_now){
842
784
                                   buffer_capacity);
843
785
    if(buffer_capacity == 0){
844
786
      int e = errno;
845
 
      perror_plus("incbuffer");
 
787
      perror("incbuffer");
846
788
      errno = e;
847
789
      goto mandos_end;
848
790
    }
953
895
      if(e == 0){
954
896
        e = errno;
955
897
      }
956
 
      perror_plus("close");
 
898
      perror("close");
957
899
    }
958
900
    gnutls_deinit(session);
959
 
    errno = e;
960
901
    if(quit_now){
961
 
      errno = EINTR;
 
902
      e = EINTR;
962
903
      retval = -1;
963
904
    }
 
905
    errno = e;
964
906
  }
965
907
  return retval;
966
908
}
1009
951
                                           avahi_proto_to_af(proto));
1010
952
      if(ret == 0){
1011
953
        avahi_simple_poll_quit(mc.simple_poll);
1012
 
      } else {
1013
 
        ret = add_server(ip, port, interface,
1014
 
                         avahi_proto_to_af(proto));
1015
954
      }
1016
955
    }
1017
956
  }
1071
1010
  }
1072
1011
}
1073
1012
 
1074
 
/* Signal handler that stops main loop after SIGTERM */
 
1013
/* stop main loop after sigterm has been called */
1075
1014
static void handle_sigterm(int sig){
1076
1015
  if(quit_now){
1077
1016
    return;
1079
1018
  quit_now = 1;
1080
1019
  signal_received = sig;
1081
1020
  int old_errno = errno;
1082
 
  /* set main loop to exit */
1083
1021
  if(mc.simple_poll != NULL){
1084
1022
    avahi_simple_poll_quit(mc.simple_poll);
1085
1023
  }
1086
1024
  errno = old_errno;
1087
1025
}
1088
1026
 
1089
 
bool get_flags(const char *ifname, struct ifreq *ifr){
1090
 
  int ret;
1091
 
  
1092
 
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1093
 
  if(s < 0){
1094
 
    perror_plus("socket");
1095
 
    return false;
1096
 
  }
1097
 
  strcpy(ifr->ifr_name, ifname);
1098
 
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1099
 
  if(ret == -1){
1100
 
    if(debug){
1101
 
      perror_plus("ioctl SIOCGIFFLAGS");
1102
 
    }
1103
 
    return false;
1104
 
  }
1105
 
  return true;
1106
 
}
1107
 
 
1108
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1109
 
  
1110
 
  /* Reject the loopback device */
1111
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
1112
 
    if(debug){
1113
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1114
 
              ifname);
1115
 
    }
1116
 
    return false;
1117
 
  }
1118
 
  /* Accept point-to-point devices only if connect_to is specified */
1119
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1120
 
    if(debug){
1121
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1122
 
              ifname);
1123
 
    }
1124
 
    return true;
1125
 
  }
1126
 
  /* Otherwise, reject non-broadcast-capable devices */
1127
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
1128
 
    if(debug){
1129
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1130
 
              ifname);
1131
 
    }
1132
 
    return false;
1133
 
  }
1134
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1135
 
  if(ifr->ifr_flags & IFF_NOARP){
1136
 
    if(debug){
1137
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1138
 
    }
1139
 
    return false;
1140
 
  }
1141
 
  
1142
 
  /* Accept this device */
1143
 
  if(debug){
1144
 
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1145
 
  }
1146
 
  return true;
1147
 
}
1148
 
 
1149
1027
/* 
1150
1028
 * This function determines if a directory entry in /sys/class/net
1151
1029
 * corresponds to an acceptable network device.
1152
1030
 * (This function is passed to scandir(3) as a filter function.)
1153
1031
 */
1154
1032
int good_interface(const struct dirent *if_entry){
1155
 
  int ret;
1156
 
  if(if_entry->d_name[0] == '.'){
1157
 
    return 0;
1158
 
  }
1159
 
  struct ifreq ifr;
1160
 
 
1161
 
  if(not get_flags(if_entry->d_name, &ifr)){
1162
 
    return 0;
1163
 
  }
1164
 
  
1165
 
  if(not good_flags(if_entry->d_name, &ifr)){
1166
 
    return 0;
1167
 
  }
1168
 
  return 1;
1169
 
}
1170
 
 
1171
 
/* 
1172
 
 * This function determines if a directory entry in /sys/class/net
1173
 
 * corresponds to an acceptable network device which is up.
1174
 
 * (This function is passed to scandir(3) as a filter function.)
1175
 
 */
1176
 
int up_interface(const struct dirent *if_entry){
1177
1033
  ssize_t ssret;
1178
1034
  char *flagname = NULL;
1179
 
  if(if_entry->d_name[0] == '.'){
1180
 
    return 0;
1181
 
  }
1182
1035
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1183
1036
                     if_entry->d_name);
1184
1037
  if(ret < 0){
1185
 
    perror_plus("asprintf");
 
1038
    perror("asprintf");
 
1039
    return 0;
 
1040
  }
 
1041
  if(if_entry->d_name[0] == '.'){
1186
1042
    return 0;
1187
1043
  }
1188
1044
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1189
1045
  if(flags_fd == -1){
1190
 
    perror_plus("open");
1191
 
    free(flagname);
 
1046
    perror("open");
1192
1047
    return 0;
1193
1048
  }
1194
 
  free(flagname);
1195
1049
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1196
1050
  /* read line from flags_fd */
1197
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1051
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1198
1052
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1199
1053
  flagstring[(size_t)to_read] = '\0';
1200
1054
  if(flagstring == NULL){
1201
 
    perror_plus("malloc");
 
1055
    perror("malloc");
1202
1056
    close(flags_fd);
1203
1057
    return 0;
1204
1058
  }
1206
1060
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1207
1061
                                             (size_t)to_read));
1208
1062
    if(ssret == -1){
1209
 
      perror_plus("read");
 
1063
      perror("read");
1210
1064
      free(flagstring);
1211
1065
      close(flags_fd);
1212
1066
      return 0;
1241
1095
    }
1242
1096
    return 0;
1243
1097
  }
1244
 
 
1245
 
  /* Reject down interfaces */
1246
 
  if(not (flags & IFF_UP)){
1247
 
    return 0;
1248
 
  }
1249
 
  
1250
1098
  /* Accept point-to-point devices only if connect_to is specified */
1251
1099
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1252
1100
    if(debug){
1263
1111
    }
1264
1112
    return 0;
1265
1113
  }
1266
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1267
 
  if(flags & IFF_NOARP){
1268
 
    if(debug){
1269
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1270
 
              if_entry->d_name);
1271
 
    }
1272
 
    return 0;
1273
 
  }
1274
1114
  /* Accept this device */
1275
1115
  if(debug){
1276
1116
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1279
1119
  return 1;
1280
1120
}
1281
1121
 
1282
 
int notdotentries(const struct dirent *direntry){
1283
 
  /* Skip "." and ".." */
1284
 
  if(direntry->d_name[0] == '.'
1285
 
     and (direntry->d_name[1] == '\0'
1286
 
          or (direntry->d_name[1] == '.'
1287
 
              and direntry->d_name[2] == '\0'))){
1288
 
    return 0;
1289
 
  }
1290
 
  return 1;
1291
 
}
1292
 
 
1293
 
/* Is this directory entry a runnable program? */
1294
 
int runnable_hook(const struct dirent *direntry){
1295
 
  int ret;
1296
 
  struct stat st;
1297
 
  
1298
 
  if((direntry->d_name)[0] == '\0'){
1299
 
    /* Empty name? */
1300
 
    return 0;
1301
 
  }
1302
 
  
1303
 
  /* Save pointer to last character */
1304
 
  char *end = strchr(direntry->d_name, '\0')-1;
1305
 
  
1306
 
  if(*end == '~'){
1307
 
    /* Backup name~ */
1308
 
    return 0;
1309
 
  }
1310
 
  
1311
 
  if(((direntry->d_name)[0] == '#')
1312
 
     and (*end == '#')){
1313
 
    /* Temporary #name# */
1314
 
    return 0;
1315
 
  }
1316
 
  
1317
 
  /* XXX more rules here */
1318
 
  
1319
 
  ret = stat(direntry->d_name, &st);
1320
 
  if(ret == -1){
1321
 
    if(debug){
1322
 
      perror_plus("Could not stat plugin");
1323
 
    }
1324
 
    return 0;
1325
 
  }
1326
 
  if(not (st.st_mode & S_ISREG)){
1327
 
    /* Not a regular file */
1328
 
    return 0;
1329
 
  }
1330
 
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1331
 
    /* Not executable */
1332
 
    return 0;
1333
 
  }
1334
 
  return 1;
1335
 
}
1336
 
 
1337
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1338
 
  int ret;
1339
 
  struct timespec now;
1340
 
  struct timespec waited_time;
1341
 
  intmax_t block_time;
1342
 
  
1343
 
  while(true){
1344
 
    if(mc.current_server == NULL){
1345
 
      if (debug){
1346
 
        fprintf(stderr,
1347
 
                "Wait until first server is found. No timeout!\n");
1348
 
      }
1349
 
      ret = avahi_simple_poll_iterate(s, -1);
1350
 
    } else {
1351
 
      if (debug){
1352
 
        fprintf(stderr, "Check current_server if we should run it,"
1353
 
                " or wait\n");
1354
 
      }
1355
 
      /* the current time */
1356
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1357
 
      if(ret == -1){
1358
 
        perror_plus("clock_gettime");
1359
 
        return -1;
1360
 
      }
1361
 
      /* Calculating in ms how long time between now and server
1362
 
         who we visted longest time ago. Now - last seen.  */
1363
 
      waited_time.tv_sec = (now.tv_sec
1364
 
                            - mc.current_server->last_seen.tv_sec);
1365
 
      waited_time.tv_nsec = (now.tv_nsec
1366
 
                             - mc.current_server->last_seen.tv_nsec);
1367
 
      /* total time is 10s/10,000ms.
1368
 
         Converting to s from ms by dividing by 1,000,
1369
 
         and ns to ms by dividing by 1,000,000. */
1370
 
      block_time = ((retry_interval
1371
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1372
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1373
 
      
1374
 
      if (debug){
1375
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1376
 
      }
1377
 
      
1378
 
      if(block_time <= 0){
1379
 
        ret = start_mandos_communication(mc.current_server->ip,
1380
 
                                         mc.current_server->port,
1381
 
                                         mc.current_server->if_index,
1382
 
                                         mc.current_server->af);
1383
 
        if(ret == 0){
1384
 
          avahi_simple_poll_quit(mc.simple_poll);
1385
 
          return 0;
1386
 
        }
1387
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1388
 
                            &mc.current_server->last_seen);
1389
 
        if(ret == -1){
1390
 
          perror_plus("clock_gettime");
1391
 
          return -1;
1392
 
        }
1393
 
        mc.current_server = mc.current_server->next;
1394
 
        block_time = 0;         /* Call avahi to find new Mandos
1395
 
                                   servers, but don't block */
1396
 
      }
1397
 
      
1398
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1399
 
    }
1400
 
    if(ret != 0){
1401
 
      if (ret > 0 or errno != EINTR) {
1402
 
        return (ret != 1) ? ret : 0;
1403
 
      }
1404
 
    }
1405
 
  }
1406
 
}
1407
 
 
1408
1122
int main(int argc, char *argv[]){
1409
1123
  AvahiSServiceBrowser *sb = NULL;
1410
1124
  int error;
1427
1141
  bool gnutls_initialized = false;
1428
1142
  bool gpgme_initialized = false;
1429
1143
  float delay = 2.5f;
1430
 
  double retry_interval = 10; /* 10s between trying a server and
1431
 
                                 retrying the same server again */
1432
1144
  
1433
1145
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1434
1146
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1440
1152
  errno = 0;
1441
1153
  ret = setgid(gid);
1442
1154
  if(ret == -1){
1443
 
    perror_plus("setgid");
 
1155
    perror("setgid");
1444
1156
  }
1445
1157
  
1446
1158
  /* Lower user privileges (temporarily) */
1447
1159
  errno = 0;
1448
1160
  ret = seteuid(uid);
1449
1161
  if(ret == -1){
1450
 
    perror_plus("seteuid");
 
1162
    perror("seteuid");
1451
1163
  }
1452
1164
  
1453
1165
  if(quit_now){
1488
1200
        .arg = "SECONDS",
1489
1201
        .doc = "Maximum delay to wait for interface startup",
1490
1202
        .group = 2 },
1491
 
      { .name = "retry", .key = 132,
1492
 
        .arg = "SECONDS",
1493
 
        .doc = "Retry interval used when denied by the mandos server",
1494
 
        .group = 2 },
1495
1203
      /*
1496
1204
       * These reproduce what we would get without ARGP_NO_HELP
1497
1205
       */
1541
1249
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1542
1250
          argp_error(state, "Bad delay");
1543
1251
        }
1544
 
      case 132:                 /* --retry */
1545
 
        errno = 0;
1546
 
        retry_interval = strtod(arg, &tmp);
1547
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1548
 
           or (retry_interval * 1000) > INT_MAX
1549
 
           or retry_interval < 0){
1550
 
          argp_error(state, "Bad retry interval");
1551
 
        }
1552
1252
        break;
1553
1253
        /*
1554
1254
         * These reproduce what we would get without ARGP_NO_HELP
1582
1282
    case ENOMEM:
1583
1283
    default:
1584
1284
      errno = ret;
1585
 
      perror_plus("argp_parse");
 
1285
      perror("argp_parse");
1586
1286
      exitcode = EX_OSERR;
1587
1287
      goto end;
1588
1288
    case EINVAL:
1590
1290
      goto end;
1591
1291
    }
1592
1292
  }
1593
 
    
1594
 
  {
1595
 
    /* Work around Debian bug #633582:
1596
 
       <http://bugs.debian.org/633582> */
1597
 
    struct stat st;
1598
 
    
1599
 
    /* Re-raise priviliges */
1600
 
    errno = 0;
1601
 
    ret = seteuid(0);
1602
 
    if(ret == -1){
1603
 
      perror_plus("seteuid");
1604
 
    }
1605
 
    
1606
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1607
 
      int seckey_fd = open(seckey, O_RDONLY);
1608
 
      if(seckey_fd == -1){
1609
 
        perror_plus("open");
1610
 
      } else {
1611
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1612
 
        if(ret == -1){
1613
 
          perror_plus("fstat");
1614
 
        } else {
1615
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1616
 
            ret = fchown(seckey_fd, uid, gid);
1617
 
            if(ret == -1){
1618
 
              perror_plus("fchown");
1619
 
            }
1620
 
          }
1621
 
        }
1622
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1623
 
      }
1624
 
    }
1625
 
    
1626
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1627
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1628
 
      if(pubkey_fd == -1){
1629
 
        perror_plus("open");
1630
 
      } else {
1631
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1632
 
        if(ret == -1){
1633
 
          perror_plus("fstat");
1634
 
        } else {
1635
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1636
 
            ret = fchown(pubkey_fd, uid, gid);
1637
 
            if(ret == -1){
1638
 
              perror_plus("fchown");
1639
 
            }
1640
 
          }
1641
 
        }
1642
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1643
 
      }
1644
 
    }
1645
 
    
1646
 
    /* Lower privileges */
1647
 
    errno = 0;
1648
 
    ret = seteuid(uid);
1649
 
    if(ret == -1){
1650
 
      perror_plus("seteuid");
1651
 
    }
1652
 
  }
1653
 
  
1654
 
  /* Find network hooks and run them */
1655
 
  {
1656
 
    struct dirent **direntries;
1657
 
    struct dirent *direntry;
1658
 
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1659
 
                           alphasort);
1660
 
    int devnull = open("/dev/null", O_RDONLY);
1661
 
    for(int i = 0; i < numhooks; i++){
1662
 
      direntry = direntries[0];
1663
 
      char *fullname = NULL;
1664
 
      ret = asprintf(&fullname, "%s/%s", tempdir,
1665
 
                     direntry->d_name);
1666
 
      if(ret < 0){
1667
 
        perror_plus("asprintf");
1668
 
        continue;
1669
 
      }
1670
 
      pid_t hook_pid = fork();
1671
 
      if(hook_pid == 0){
1672
 
        /* Child */
1673
 
        dup2(devnull, STDIN_FILENO);
1674
 
        close(devnull);
1675
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1676
 
        setenv("DEVICE", interface, 1);
1677
 
        setenv("VERBOSE", debug ? "1" : "0", 1);
1678
 
        setenv("MODE", "start", 1);
1679
 
        /* setenv( XXX more here */
1680
 
        ret = execl(fullname, direntry->d_name, "start");
1681
 
        perror_plus("execl");
1682
 
      }
1683
 
      free(fullname);
1684
 
      if(quit_now){
1685
 
        goto end;
1686
 
      }
1687
 
    }
1688
 
    close(devnull);
1689
 
  }
1690
1293
  
1691
1294
  if(not debug){
1692
1295
    avahi_set_log_function(empty_log);
1693
1296
  }
1694
 
  
 
1297
 
1695
1298
  if(interface[0] == '\0'){
1696
1299
    struct dirent **direntries;
1697
1300
    ret = scandir(sys_class_net, &direntries, good_interface,
1703
1306
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1704
1307
      }
1705
1308
      if(interface == NULL){
1706
 
        perror_plus("malloc");
 
1309
        perror("malloc");
1707
1310
        free(direntries);
1708
1311
        exitcode = EXIT_FAILURE;
1709
1312
        goto end;
1731
1334
  sigemptyset(&sigterm_action.sa_mask);
1732
1335
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1733
1336
  if(ret == -1){
1734
 
    perror_plus("sigaddset");
 
1337
    perror("sigaddset");
1735
1338
    exitcode = EX_OSERR;
1736
1339
    goto end;
1737
1340
  }
1738
1341
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1739
1342
  if(ret == -1){
1740
 
    perror_plus("sigaddset");
 
1343
    perror("sigaddset");
1741
1344
    exitcode = EX_OSERR;
1742
1345
    goto end;
1743
1346
  }
1744
1347
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1745
1348
  if(ret == -1){
1746
 
    perror_plus("sigaddset");
 
1349
    perror("sigaddset");
1747
1350
    exitcode = EX_OSERR;
1748
1351
    goto end;
1749
1352
  }
1753
1356
  */
1754
1357
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1755
1358
  if(ret == -1){
1756
 
    perror_plus("sigaction");
 
1359
    perror("sigaction");
1757
1360
    return EX_OSERR;
1758
1361
  }
1759
1362
  if(old_sigterm_action.sa_handler != SIG_IGN){
1760
1363
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1761
1364
    if(ret == -1){
1762
 
      perror_plus("sigaction");
 
1365
      perror("sigaction");
1763
1366
      exitcode = EX_OSERR;
1764
1367
      goto end;
1765
1368
    }
1766
1369
  }
1767
1370
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1768
1371
  if(ret == -1){
1769
 
    perror_plus("sigaction");
 
1372
    perror("sigaction");
1770
1373
    return EX_OSERR;
1771
1374
  }
1772
1375
  if(old_sigterm_action.sa_handler != SIG_IGN){
1773
1376
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1774
1377
    if(ret == -1){
1775
 
      perror_plus("sigaction");
 
1378
      perror("sigaction");
1776
1379
      exitcode = EX_OSERR;
1777
1380
      goto end;
1778
1381
    }
1779
1382
  }
1780
1383
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1781
1384
  if(ret == -1){
1782
 
    perror_plus("sigaction");
 
1385
    perror("sigaction");
1783
1386
    return EX_OSERR;
1784
1387
  }
1785
1388
  if(old_sigterm_action.sa_handler != SIG_IGN){
1786
1389
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1787
1390
    if(ret == -1){
1788
 
      perror_plus("sigaction");
 
1391
      perror("sigaction");
1789
1392
      exitcode = EX_OSERR;
1790
1393
      goto end;
1791
1394
    }
1808
1411
    errno = 0;
1809
1412
    ret = seteuid(0);
1810
1413
    if(ret == -1){
1811
 
      perror_plus("seteuid");
 
1414
      perror("seteuid");
1812
1415
    }
1813
1416
    
1814
1417
#ifdef __linux__
1818
1421
    bool restore_loglevel = true;
1819
1422
    if(ret == -1){
1820
1423
      restore_loglevel = false;
1821
 
      perror_plus("klogctl");
 
1424
      perror("klogctl");
1822
1425
    }
1823
1426
#endif  /* __linux__ */
1824
1427
    
1825
1428
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1826
1429
    if(sd < 0){
1827
 
      perror_plus("socket");
 
1430
      perror("socket");
1828
1431
      exitcode = EX_OSERR;
1829
1432
#ifdef __linux__
1830
1433
      if(restore_loglevel){
1831
1434
        ret = klogctl(7, NULL, 0);
1832
1435
        if(ret == -1){
1833
 
          perror_plus("klogctl");
 
1436
          perror("klogctl");
1834
1437
        }
1835
1438
      }
1836
1439
#endif  /* __linux__ */
1838
1441
      errno = 0;
1839
1442
      ret = seteuid(uid);
1840
1443
      if(ret == -1){
1841
 
        perror_plus("seteuid");
 
1444
        perror("seteuid");
1842
1445
      }
1843
1446
      goto end;
1844
1447
    }
1845
1448
    strcpy(network.ifr_name, interface);
1846
1449
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1847
1450
    if(ret == -1){
1848
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1451
      perror("ioctl SIOCGIFFLAGS");
1849
1452
#ifdef __linux__
1850
1453
      if(restore_loglevel){
1851
1454
        ret = klogctl(7, NULL, 0);
1852
1455
        if(ret == -1){
1853
 
          perror_plus("klogctl");
 
1456
          perror("klogctl");
1854
1457
        }
1855
1458
      }
1856
1459
#endif  /* __linux__ */
1859
1462
      errno = 0;
1860
1463
      ret = seteuid(uid);
1861
1464
      if(ret == -1){
1862
 
        perror_plus("seteuid");
 
1465
        perror("seteuid");
1863
1466
      }
1864
1467
      goto end;
1865
1468
    }
1869
1472
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1870
1473
      if(ret == -1){
1871
1474
        take_down_interface = false;
1872
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1475
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1873
1476
        exitcode = EX_OSERR;
1874
1477
#ifdef __linux__
1875
1478
        if(restore_loglevel){
1876
1479
          ret = klogctl(7, NULL, 0);
1877
1480
          if(ret == -1){
1878
 
            perror_plus("klogctl");
 
1481
            perror("klogctl");
1879
1482
          }
1880
1483
        }
1881
1484
#endif  /* __linux__ */
1883
1486
        errno = 0;
1884
1487
        ret = seteuid(uid);
1885
1488
        if(ret == -1){
1886
 
          perror_plus("seteuid");
 
1489
          perror("seteuid");
1887
1490
        }
1888
1491
        goto end;
1889
1492
      }
1890
1493
    }
1891
 
    /* Sleep checking until interface is running.
1892
 
       Check every 0.25s, up to total time of delay */
 
1494
    /* sleep checking until interface is running */
1893
1495
    for(int i=0; i < delay * 4; i++){
1894
1496
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1895
1497
      if(ret == -1){
1896
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1498
        perror("ioctl SIOCGIFFLAGS");
1897
1499
      } else if(network.ifr_flags & IFF_RUNNING){
1898
1500
        break;
1899
1501
      }
1900
1502
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1901
1503
      ret = nanosleep(&sleeptime, NULL);
1902
1504
      if(ret == -1 and errno != EINTR){
1903
 
        perror_plus("nanosleep");
 
1505
        perror("nanosleep");
1904
1506
      }
1905
1507
    }
1906
1508
    if(not take_down_interface){
1907
1509
      /* We won't need the socket anymore */
1908
1510
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1909
1511
      if(ret == -1){
1910
 
        perror_plus("close");
 
1512
        perror("close");
1911
1513
      }
1912
1514
    }
1913
1515
#ifdef __linux__
1915
1517
      /* Restores kernel loglevel to default */
1916
1518
      ret = klogctl(7, NULL, 0);
1917
1519
      if(ret == -1){
1918
 
        perror_plus("klogctl");
 
1520
        perror("klogctl");
1919
1521
      }
1920
1522
    }
1921
1523
#endif  /* __linux__ */
1925
1527
      /* Lower privileges */
1926
1528
      ret = seteuid(uid);
1927
1529
      if(ret == -1){
1928
 
        perror_plus("seteuid");
 
1530
        perror("seteuid");
1929
1531
      }
1930
1532
    } else {
1931
1533
      /* Lower privileges permanently */
1932
1534
      ret = setuid(uid);
1933
1535
      if(ret == -1){
1934
 
        perror_plus("setuid");
 
1536
        perror("setuid");
1935
1537
      }
1936
1538
    }
1937
1539
  }
1953
1555
    goto end;
1954
1556
  }
1955
1557
  
 
1558
  tempdir_created = true;
1956
1559
  if(mkdtemp(tempdir) == NULL){
1957
 
    perror_plus("mkdtemp");
 
1560
    tempdir_created = false;
 
1561
    perror("mkdtemp");
1958
1562
    goto end;
1959
1563
  }
1960
 
  tempdir_created = true;
1961
1564
  
1962
1565
  if(quit_now){
1963
1566
    goto end;
2005
1608
    
2006
1609
    port = (uint16_t)tmpmax;
2007
1610
    *address = '\0';
 
1611
    address = connect_to;
2008
1612
    /* Colon in address indicates IPv6 */
2009
1613
    int af;
2010
 
    if(strchr(connect_to, ':') != NULL){
 
1614
    if(strchr(address, ':') != NULL){
2011
1615
      af = AF_INET6;
2012
 
      /* Accept [] around IPv6 address - see RFC 5952 */
2013
 
      if(connect_to[0] == '[' and address[-1] == ']')
2014
 
        {
2015
 
          connect_to++;
2016
 
          address[-1] = '\0';
2017
 
        }
2018
1616
    } else {
2019
1617
      af = AF_INET;
2020
1618
    }
2021
 
    address = connect_to;
2022
1619
    
2023
1620
    if(quit_now){
2024
1621
      goto end;
2025
1622
    }
2026
 
    
 
1623
 
2027
1624
    while(not quit_now){
2028
1625
      ret = start_mandos_communication(address, port, if_index, af);
2029
1626
      if(quit_now or ret == 0){
2030
1627
        break;
2031
1628
      }
2032
 
      if(debug){
2033
 
        fprintf(stderr, "Retrying in %d seconds\n",
2034
 
                (int)retry_interval);
2035
 
      }
2036
 
      sleep((int)retry_interval);
2037
 
    }
2038
 
    
 
1629
      sleep(15);
 
1630
    };
 
1631
 
2039
1632
    if (not quit_now){
2040
1633
      exitcode = EXIT_SUCCESS;
2041
1634
    }
2042
 
    
 
1635
 
2043
1636
    goto end;
2044
1637
  }
2045
1638
  
2097
1690
  if(debug){
2098
1691
    fprintf(stderr, "Starting Avahi loop search\n");
2099
1692
  }
2100
 
 
2101
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2102
 
                                (int)(retry_interval * 1000));
2103
 
  if(debug){
2104
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2105
 
            (ret == 0) ? "successfully" : "with error");
2106
 
  }
 
1693
  
 
1694
  avahi_simple_poll_loop(mc.simple_poll);
2107
1695
  
2108
1696
 end:
2109
1697
  
2130
1718
  if(gpgme_initialized){
2131
1719
    gpgme_release(mc.ctx);
2132
1720
  }
2133
 
 
2134
 
  /* Cleans up the circular linked list of Mandos servers the client
2135
 
     has seen */
2136
 
  if(mc.current_server != NULL){
2137
 
    mc.current_server->prev->next = NULL;
2138
 
    while(mc.current_server != NULL){
2139
 
      server *next = mc.current_server->next;
2140
 
      free(mc.current_server);
2141
 
      mc.current_server = next;
2142
 
    }
2143
 
  }
2144
 
  
2145
 
  /* XXX run network hooks "stop" here  */
2146
1721
  
2147
1722
  /* Take down the network interface */
2148
1723
  if(take_down_interface){
2150
1725
    errno = 0;
2151
1726
    ret = seteuid(0);
2152
1727
    if(ret == -1){
2153
 
      perror_plus("seteuid");
 
1728
      perror("seteuid");
2154
1729
    }
2155
1730
    if(geteuid() == 0){
2156
1731
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2157
1732
      if(ret == -1){
2158
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1733
        perror("ioctl SIOCGIFFLAGS");
2159
1734
      } else if(network.ifr_flags & IFF_UP) {
2160
1735
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2161
1736
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2162
1737
        if(ret == -1){
2163
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1738
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
2164
1739
        }
2165
1740
      }
2166
1741
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2167
1742
      if(ret == -1){
2168
 
        perror_plus("close");
 
1743
        perror("close");
2169
1744
      }
2170
1745
      /* Lower privileges permanently */
2171
1746
      errno = 0;
2172
1747
      ret = setuid(uid);
2173
1748
      if(ret == -1){
2174
 
        perror_plus("setuid");
 
1749
        perror("setuid");
2175
1750
      }
2176
1751
    }
2177
1752
  }
2178
1753
  
2179
 
  /* Removes the GPGME temp directory and all files inside */
 
1754
  /* Removes the temp directory used by GPGME */
2180
1755
  if(tempdir_created){
2181
 
    struct dirent **direntries = NULL;
2182
 
    struct dirent *direntry = NULL;
2183
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2184
 
                             alphasort);
2185
 
    if (numentries > 0){
2186
 
      for(int i = 0; i < numentries; i++){
2187
 
        direntry = direntries[i];
 
1756
    DIR *d;
 
1757
    struct dirent *direntry;
 
1758
    d = opendir(tempdir);
 
1759
    if(d == NULL){
 
1760
      if(errno != ENOENT){
 
1761
        perror("opendir");
 
1762
      }
 
1763
    } else {
 
1764
      while(true){
 
1765
        direntry = readdir(d);
 
1766
        if(direntry == NULL){
 
1767
          break;
 
1768
        }
 
1769
        /* Skip "." and ".." */
 
1770
        if(direntry->d_name[0] == '.'
 
1771
           and (direntry->d_name[1] == '\0'
 
1772
                or (direntry->d_name[1] == '.'
 
1773
                    and direntry->d_name[2] == '\0'))){
 
1774
          continue;
 
1775
        }
2188
1776
        char *fullname = NULL;
2189
1777
        ret = asprintf(&fullname, "%s/%s", tempdir,
2190
1778
                       direntry->d_name);
2191
1779
        if(ret < 0){
2192
 
          perror_plus("asprintf");
 
1780
          perror("asprintf");
2193
1781
          continue;
2194
1782
        }
2195
1783
        ret = remove(fullname);
2199
1787
        }
2200
1788
        free(fullname);
2201
1789
      }
2202
 
    }
2203
 
 
2204
 
    /* need to clean even if 0 because man page doesn't specify */
2205
 
    free(direntries);
2206
 
    if (numentries == -1){
2207
 
      perror_plus("scandir");
 
1790
      closedir(d);
2208
1791
    }
2209
1792
    ret = rmdir(tempdir);
2210
1793
    if(ret == -1 and errno != ENOENT){
2211
 
      perror_plus("rmdir");
 
1794
      perror("rmdir");
2212
1795
    }
2213
1796
  }
2214
1797
  
2219
1802
                                            &old_sigterm_action,
2220
1803
                                            NULL));
2221
1804
    if(ret == -1){
2222
 
      perror_plus("sigaction");
 
1805
      perror("sigaction");
2223
1806
    }
2224
1807
    do {
2225
1808
      ret = raise(signal_received);
2226
1809
    } while(ret != 0 and errno == EINTR);
2227
1810
    if(ret != 0){
2228
 
      perror_plus("raise");
 
1811
      perror("raise");
2229
1812
      abort();
2230
1813
    }
2231
1814
    TEMP_FAILURE_RETRY(pause());