/mandos/release

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

« back to all changes in this revision

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

* mandos-monitor (MandosClientWidget.checker_completed): Do not log a
                                                         successful
                                                         checker.

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
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 */
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,
127
126
bool debug = false;
128
127
static const char mandos_protocol_version[] = "1";
129
128
const char *argp_program_version = "mandos-client " VERSION;
130
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
130
static const char sys_class_net[] = "/sys/class/net";
132
131
char *connect_to = NULL;
133
132
 
134
 
/* Doubly linked list that need to be circularly linked when used */
135
 
typedef struct server{
136
 
  const char *ip;
137
 
  uint16_t port;
138
 
  AvahiIfIndex if_index;
139
 
  int af;
140
 
  struct timespec last_seen;
141
 
  struct server *next;
142
 
  struct server *prev;
143
 
} server;
144
 
 
145
133
/* Used for passing in values through the Avahi callback functions */
146
134
typedef struct {
147
135
  AvahiSimplePoll *simple_poll;
151
139
  gnutls_dh_params_t dh_params;
152
140
  const char *priority;
153
141
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
142
} mandos_context;
156
143
 
157
144
/* global context so signal handler can reach it*/
158
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
146
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
148
 
163
149
sig_atomic_t quit_now = 0;
164
150
int signal_received = 0;
165
151
 
166
 
/* Function to use when printing errors */
167
 
void perror_plus(const char *print_text){
168
 
  fprintf(stderr, "Mandos plugin %s: ",
169
 
          program_invocation_short_name);
170
 
  perror(print_text);
171
 
}
172
 
 
173
152
/*
174
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
154
 * bytes. "buffer_capacity" is how much is currently allocated,
187
166
  return buffer_capacity;
188
167
}
189
168
 
190
 
/* Add server to set of servers to retry periodically */
191
 
int add_server(const char *ip, uint16_t port,
192
 
                 AvahiIfIndex if_index,
193
 
                 int af){
194
 
  int ret;
195
 
  server *new_server = malloc(sizeof(server));
196
 
  if(new_server == NULL){
197
 
    perror_plus("malloc");
198
 
    return -1;
199
 
  }
200
 
  *new_server = (server){ .ip = strdup(ip),
201
 
                         .port = port,
202
 
                         .if_index = if_index,
203
 
                         .af = af };
204
 
  if(new_server->ip == NULL){
205
 
    perror_plus("strdup");
206
 
    return -1;
207
 
  }
208
 
  /* Special case of first server */
209
 
  if (mc.current_server == NULL){
210
 
    new_server->next = new_server;
211
 
    new_server->prev = new_server;
212
 
    mc.current_server = new_server;
213
 
  /* Place the new server last in the list */
214
 
  } else {
215
 
    new_server->next = mc.current_server;
216
 
    new_server->prev = mc.current_server->prev;
217
 
    new_server->prev->next = new_server;
218
 
    mc.current_server->prev = new_server;
219
 
  }
220
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
221
 
  if(ret == -1){
222
 
    perror_plus("clock_gettime");
223
 
    return -1;
224
 
  }
225
 
  return 0;
226
 
}
227
 
 
228
169
/* 
229
170
 * Initialize GPGME.
230
171
 */
244
185
    
245
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
246
187
    if(fd == -1){
247
 
      perror_plus("open");
 
188
      perror("open");
248
189
      return false;
249
190
    }
250
191
    
264
205
    
265
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
266
207
    if(ret == -1){
267
 
      perror_plus("close");
 
208
      perror("close");
268
209
    }
269
210
    gpgme_data_release(pgp_data);
270
211
    return true;
283
224
    return false;
284
225
  }
285
226
  
286
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
227
    /* Set GPGME home directory for the OpenPGP engine only */
287
228
  rc = gpgme_get_engine_info(&engine_info);
288
229
  if(rc != GPG_ERR_NO_ERROR){
289
230
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
395
336
  
396
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
397
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
398
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
399
340
    plaintext_length = -1;
400
341
    goto decrypt_end;
401
342
  }
406
347
                                      (size_t)plaintext_length,
407
348
                                      plaintext_capacity);
408
349
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
410
351
        plaintext_length = -1;
411
352
        goto decrypt_end;
412
353
    }
419
360
      break;
420
361
    }
421
362
    if(ret < 0){
422
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
423
364
      plaintext_length = -1;
424
365
      goto decrypt_end;
425
366
    }
482
423
  }
483
424
  
484
425
  /* OpenPGP credentials */
485
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
426
  gnutls_certificate_allocate_credentials(&mc.cred);
486
427
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
428
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
429
                                                    from
 
430
                                                    -Wunreachable-code
 
431
                                                 */
488
432
            safer_gnutls_strerror(ret));
489
433
    gnutls_global_deinit();
490
434
    return -1;
645
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
646
590
  if(tcp_sd < 0){
647
591
    int e = errno;
648
 
    perror_plus("socket");
 
592
    perror("socket");
649
593
    errno = e;
650
594
    goto mandos_end;
651
595
  }
665
609
  }
666
610
  if(ret < 0 ){
667
611
    int e = errno;
668
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
669
613
    errno = e;
670
614
    goto mandos_end;
671
615
  }
707
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
708
652
      char interface[IF_NAMESIZE];
709
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
711
655
      } else {
712
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
657
                ip, interface, port);
727
671
                        sizeof(addrstr));
728
672
    }
729
673
    if(pcret == NULL){
730
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
731
675
    } else {
732
676
      if(strcmp(addrstr, ip) != 0){
733
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
746
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
747
691
  }
748
692
  if(ret < 0){
749
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
750
 
      int e = errno;
751
 
      perror_plus("connect");
752
 
      errno = e;
753
 
    }
 
693
    int e = errno;
 
694
    perror("connect");
 
695
    errno = e;
754
696
    goto mandos_end;
755
697
  }
756
698
  
767
709
                                   out_size - written));
768
710
    if(ret == -1){
769
711
      int e = errno;
770
 
      perror_plus("write");
 
712
      perror("write");
771
713
      errno = e;
772
714
      goto mandos_end;
773
715
    }
798
740
    goto mandos_end;
799
741
  }
800
742
  
801
 
  /* Spurious warning from -Wint-to-pointer-cast */
802
743
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
803
744
  
804
745
  if(quit_now){
841
782
                                   buffer_capacity);
842
783
    if(buffer_capacity == 0){
843
784
      int e = errno;
844
 
      perror_plus("incbuffer");
 
785
      perror("incbuffer");
845
786
      errno = e;
846
787
      goto mandos_end;
847
788
    }
952
893
      if(e == 0){
953
894
        e = errno;
954
895
      }
955
 
      perror_plus("close");
 
896
      perror("close");
956
897
    }
957
898
    gnutls_deinit(session);
958
 
    errno = e;
959
899
    if(quit_now){
960
 
      errno = EINTR;
 
900
      e = EINTR;
961
901
      retval = -1;
962
902
    }
 
903
    errno = e;
963
904
  }
964
905
  return retval;
965
906
}
1008
949
                                           avahi_proto_to_af(proto));
1009
950
      if(ret == 0){
1010
951
        avahi_simple_poll_quit(mc.simple_poll);
1011
 
      } else {
1012
 
        ret = add_server(ip, port, interface,
1013
 
                         avahi_proto_to_af(proto));
1014
952
      }
1015
953
    }
1016
954
  }
1070
1008
  }
1071
1009
}
1072
1010
 
1073
 
/* Signal handler that stops main loop after SIGTERM */
 
1011
/* stop main loop after sigterm has been called */
1074
1012
static void handle_sigterm(int sig){
1075
1013
  if(quit_now){
1076
1014
    return;
1078
1016
  quit_now = 1;
1079
1017
  signal_received = sig;
1080
1018
  int old_errno = errno;
1081
 
  /* set main loop to exit */
1082
1019
  if(mc.simple_poll != NULL){
1083
1020
    avahi_simple_poll_quit(mc.simple_poll);
1084
1021
  }
1092
1029
 */
1093
1030
int good_interface(const struct dirent *if_entry){
1094
1031
  ssize_t ssret;
1095
 
  int ret;
 
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
  }
1096
1039
  if(if_entry->d_name[0] == '.'){
1097
1040
    return 0;
1098
1041
  }
1099
 
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1100
 
  if(s < 0){
1101
 
    perror_plus("socket");
1102
 
    return 0;
1103
 
  }
1104
 
  struct ifreq ifr;
1105
 
  strcpy(ifr.ifr_name, if_entry->d_name);
1106
 
  ret = ioctl(s, SIOCGIFFLAGS, &ifr);
1107
 
  if(ret == -1){
 
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){
1108
1079
    if(debug){
1109
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1080
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1081
              flagstring, if_entry->d_name);
1110
1082
    }
 
1083
    free(flagstring);
1111
1084
    return 0;
1112
1085
  }
 
1086
  free(flagstring);
 
1087
  ifreq_flags flags = (ifreq_flags)tmpmax;
1113
1088
  /* Reject the loopback device */
1114
 
  if(ifr.ifr_flags & IFF_LOOPBACK){
 
1089
  if(flags & IFF_LOOPBACK){
1115
1090
    if(debug){
1116
1091
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1117
1092
              if_entry->d_name);
1119
1094
    return 0;
1120
1095
  }
1121
1096
  /* Accept point-to-point devices only if connect_to is specified */
1122
 
  if(connect_to != NULL and (ifr.ifr_flags & IFF_POINTOPOINT)){
 
1097
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1123
1098
    if(debug){
1124
1099
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
1100
              if_entry->d_name);
1127
1102
    return 1;
1128
1103
  }
1129
1104
  /* Otherwise, reject non-broadcast-capable devices */
1130
 
  if(not (ifr.ifr_flags & IFF_BROADCAST)){
 
1105
  if(not (flags & IFF_BROADCAST)){
1131
1106
    if(debug){
1132
1107
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1133
1108
              if_entry->d_name);
1134
1109
    }
1135
1110
    return 0;
1136
1111
  }
1137
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1138
 
  if(ifr.ifr_flags & IFF_NOARP){
1139
 
    if(debug){
1140
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1141
 
              if_entry->d_name);
1142
 
    }
1143
 
    return 0;
1144
 
  }
1145
1112
  /* Accept this device */
1146
1113
  if(debug){
1147
1114
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1150
1117
  return 1;
1151
1118
}
1152
1119
 
1153
 
int notdotentries(const struct dirent *direntry){
1154
 
  /* Skip "." and ".." */
1155
 
  if(direntry->d_name[0] == '.'
1156
 
     and (direntry->d_name[1] == '\0'
1157
 
          or (direntry->d_name[1] == '.'
1158
 
              and direntry->d_name[2] == '\0'))){
1159
 
    return 0;
1160
 
  }
1161
 
  return 1;
1162
 
}
1163
 
 
1164
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1165
 
  int ret;
1166
 
  struct timespec now;
1167
 
  struct timespec waited_time;
1168
 
  intmax_t block_time;
1169
 
  
1170
 
  while(true){
1171
 
    if(mc.current_server == NULL){
1172
 
      if (debug){
1173
 
        fprintf(stderr,
1174
 
                "Wait until first server is found. No timeout!\n");
1175
 
      }
1176
 
      ret = avahi_simple_poll_iterate(s, -1);
1177
 
    } else {
1178
 
      if (debug){
1179
 
        fprintf(stderr, "Check current_server if we should run it,"
1180
 
                " or wait\n");
1181
 
      }
1182
 
      /* the current time */
1183
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1184
 
      if(ret == -1){
1185
 
        perror_plus("clock_gettime");
1186
 
        return -1;
1187
 
      }
1188
 
      /* Calculating in ms how long time between now and server
1189
 
         who we visted longest time ago. Now - last seen.  */
1190
 
      waited_time.tv_sec = (now.tv_sec
1191
 
                            - mc.current_server->last_seen.tv_sec);
1192
 
      waited_time.tv_nsec = (now.tv_nsec
1193
 
                             - mc.current_server->last_seen.tv_nsec);
1194
 
      /* total time is 10s/10,000ms.
1195
 
         Converting to s from ms by dividing by 1,000,
1196
 
         and ns to ms by dividing by 1,000,000. */
1197
 
      block_time = ((retry_interval
1198
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1199
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1200
 
      
1201
 
      if (debug){
1202
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1203
 
      }
1204
 
      
1205
 
      if(block_time <= 0){
1206
 
        ret = start_mandos_communication(mc.current_server->ip,
1207
 
                                         mc.current_server->port,
1208
 
                                         mc.current_server->if_index,
1209
 
                                         mc.current_server->af);
1210
 
        if(ret == 0){
1211
 
          avahi_simple_poll_quit(mc.simple_poll);
1212
 
          return 0;
1213
 
        }
1214
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1215
 
                            &mc.current_server->last_seen);
1216
 
        if(ret == -1){
1217
 
          perror_plus("clock_gettime");
1218
 
          return -1;
1219
 
        }
1220
 
        mc.current_server = mc.current_server->next;
1221
 
        block_time = 0;         /* Call avahi to find new Mandos
1222
 
                                   servers, but don't block */
1223
 
      }
1224
 
      
1225
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1226
 
    }
1227
 
    if(ret != 0){
1228
 
      if (ret > 0 or errno != EINTR) {
1229
 
        return (ret != 1) ? ret : 0;
1230
 
      }
1231
 
    }
1232
 
  }
1233
 
}
1234
 
 
1235
1120
int main(int argc, char *argv[]){
1236
1121
  AvahiSServiceBrowser *sb = NULL;
1237
1122
  int error;
1254
1139
  bool gnutls_initialized = false;
1255
1140
  bool gpgme_initialized = false;
1256
1141
  float delay = 2.5f;
1257
 
  double retry_interval = 10; /* 10s between trying a server and
1258
 
                                 retrying the same server again */
1259
1142
  
1260
1143
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1261
1144
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1267
1150
  errno = 0;
1268
1151
  ret = setgid(gid);
1269
1152
  if(ret == -1){
1270
 
    perror_plus("setgid");
 
1153
    perror("setgid");
1271
1154
  }
1272
1155
  
1273
1156
  /* Lower user privileges (temporarily) */
1274
1157
  errno = 0;
1275
1158
  ret = seteuid(uid);
1276
1159
  if(ret == -1){
1277
 
    perror_plus("seteuid");
 
1160
    perror("seteuid");
1278
1161
  }
1279
1162
  
1280
1163
  if(quit_now){
1315
1198
        .arg = "SECONDS",
1316
1199
        .doc = "Maximum delay to wait for interface startup",
1317
1200
        .group = 2 },
1318
 
      { .name = "retry", .key = 132,
1319
 
        .arg = "SECONDS",
1320
 
        .doc = "Retry interval used when denied by the mandos server",
1321
 
        .group = 2 },
1322
1201
      /*
1323
1202
       * These reproduce what we would get without ARGP_NO_HELP
1324
1203
       */
1368
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1369
1248
          argp_error(state, "Bad delay");
1370
1249
        }
1371
 
      case 132:                 /* --retry */
1372
 
        errno = 0;
1373
 
        retry_interval = strtod(arg, &tmp);
1374
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1375
 
           or (retry_interval * 1000) > INT_MAX
1376
 
           or retry_interval < 0){
1377
 
          argp_error(state, "Bad retry interval");
1378
 
        }
1379
1250
        break;
1380
1251
        /*
1381
1252
         * These reproduce what we would get without ARGP_NO_HELP
1409
1280
    case ENOMEM:
1410
1281
    default:
1411
1282
      errno = ret;
1412
 
      perror_plus("argp_parse");
 
1283
      perror("argp_parse");
1413
1284
      exitcode = EX_OSERR;
1414
1285
      goto end;
1415
1286
    case EINVAL:
1417
1288
      goto end;
1418
1289
    }
1419
1290
  }
1420
 
    
1421
 
  {
1422
 
    /* Work around Debian bug #633582:
1423
 
       <http://bugs.debian.org/633582> */
1424
 
    struct stat st;
1425
 
    
1426
 
    /* Re-raise priviliges */
1427
 
    errno = 0;
1428
 
    ret = seteuid(0);
1429
 
    if(ret == -1){
1430
 
      perror_plus("seteuid");
1431
 
    }
1432
 
    
1433
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1434
 
      int seckey_fd = open(seckey, O_RDONLY);
1435
 
      if(seckey_fd == -1){
1436
 
        perror_plus("open");
1437
 
      } else {
1438
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1439
 
        if(ret == -1){
1440
 
          perror_plus("fstat");
1441
 
        } else {
1442
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1443
 
            ret = fchown(seckey_fd, uid, gid);
1444
 
            if(ret == -1){
1445
 
              perror_plus("fchown");
1446
 
            }
1447
 
          }
1448
 
        }
1449
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1450
 
      }
1451
 
    }
1452
 
    
1453
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1454
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1455
 
      if(pubkey_fd == -1){
1456
 
        perror_plus("open");
1457
 
      } else {
1458
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1459
 
        if(ret == -1){
1460
 
          perror_plus("fstat");
1461
 
        } else {
1462
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1463
 
            ret = fchown(pubkey_fd, uid, gid);
1464
 
            if(ret == -1){
1465
 
              perror_plus("fchown");
1466
 
            }
1467
 
          }
1468
 
        }
1469
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1470
 
      }
1471
 
    }
1472
 
    
1473
 
    /* Lower privileges */
1474
 
    errno = 0;
1475
 
    ret = seteuid(uid);
1476
 
    if(ret == -1){
1477
 
      perror_plus("seteuid");
1478
 
    }
1479
 
  }
1480
1291
  
1481
1292
  if(not debug){
1482
1293
    avahi_set_log_function(empty_log);
1493
1304
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1494
1305
      }
1495
1306
      if(interface == NULL){
1496
 
        perror_plus("malloc");
 
1307
        perror("malloc");
1497
1308
        free(direntries);
1498
1309
        exitcode = EXIT_FAILURE;
1499
1310
        goto end;
1521
1332
  sigemptyset(&sigterm_action.sa_mask);
1522
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1523
1334
  if(ret == -1){
1524
 
    perror_plus("sigaddset");
 
1335
    perror("sigaddset");
1525
1336
    exitcode = EX_OSERR;
1526
1337
    goto end;
1527
1338
  }
1528
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1529
1340
  if(ret == -1){
1530
 
    perror_plus("sigaddset");
 
1341
    perror("sigaddset");
1531
1342
    exitcode = EX_OSERR;
1532
1343
    goto end;
1533
1344
  }
1534
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1535
1346
  if(ret == -1){
1536
 
    perror_plus("sigaddset");
 
1347
    perror("sigaddset");
1537
1348
    exitcode = EX_OSERR;
1538
1349
    goto end;
1539
1350
  }
1543
1354
  */
1544
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1545
1356
  if(ret == -1){
1546
 
    perror_plus("sigaction");
 
1357
    perror("sigaction");
1547
1358
    return EX_OSERR;
1548
1359
  }
1549
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1550
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1551
1362
    if(ret == -1){
1552
 
      perror_plus("sigaction");
 
1363
      perror("sigaction");
1553
1364
      exitcode = EX_OSERR;
1554
1365
      goto end;
1555
1366
    }
1556
1367
  }
1557
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1558
1369
  if(ret == -1){
1559
 
    perror_plus("sigaction");
 
1370
    perror("sigaction");
1560
1371
    return EX_OSERR;
1561
1372
  }
1562
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1563
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1564
1375
    if(ret == -1){
1565
 
      perror_plus("sigaction");
 
1376
      perror("sigaction");
1566
1377
      exitcode = EX_OSERR;
1567
1378
      goto end;
1568
1379
    }
1569
1380
  }
1570
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1571
1382
  if(ret == -1){
1572
 
    perror_plus("sigaction");
 
1383
    perror("sigaction");
1573
1384
    return EX_OSERR;
1574
1385
  }
1575
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1576
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1577
1388
    if(ret == -1){
1578
 
      perror_plus("sigaction");
 
1389
      perror("sigaction");
1579
1390
      exitcode = EX_OSERR;
1580
1391
      goto end;
1581
1392
    }
1598
1409
    errno = 0;
1599
1410
    ret = seteuid(0);
1600
1411
    if(ret == -1){
1601
 
      perror_plus("seteuid");
 
1412
      perror("seteuid");
1602
1413
    }
1603
1414
    
1604
1415
#ifdef __linux__
1608
1419
    bool restore_loglevel = true;
1609
1420
    if(ret == -1){
1610
1421
      restore_loglevel = false;
1611
 
      perror_plus("klogctl");
 
1422
      perror("klogctl");
1612
1423
    }
1613
1424
#endif  /* __linux__ */
1614
1425
    
1615
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1616
1427
    if(sd < 0){
1617
 
      perror_plus("socket");
 
1428
      perror("socket");
1618
1429
      exitcode = EX_OSERR;
1619
1430
#ifdef __linux__
1620
1431
      if(restore_loglevel){
1621
1432
        ret = klogctl(7, NULL, 0);
1622
1433
        if(ret == -1){
1623
 
          perror_plus("klogctl");
 
1434
          perror("klogctl");
1624
1435
        }
1625
1436
      }
1626
1437
#endif  /* __linux__ */
1628
1439
      errno = 0;
1629
1440
      ret = seteuid(uid);
1630
1441
      if(ret == -1){
1631
 
        perror_plus("seteuid");
 
1442
        perror("seteuid");
1632
1443
      }
1633
1444
      goto end;
1634
1445
    }
1635
1446
    strcpy(network.ifr_name, interface);
1636
1447
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1637
1448
    if(ret == -1){
1638
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1449
      perror("ioctl SIOCGIFFLAGS");
1639
1450
#ifdef __linux__
1640
1451
      if(restore_loglevel){
1641
1452
        ret = klogctl(7, NULL, 0);
1642
1453
        if(ret == -1){
1643
 
          perror_plus("klogctl");
 
1454
          perror("klogctl");
1644
1455
        }
1645
1456
      }
1646
1457
#endif  /* __linux__ */
1649
1460
      errno = 0;
1650
1461
      ret = seteuid(uid);
1651
1462
      if(ret == -1){
1652
 
        perror_plus("seteuid");
 
1463
        perror("seteuid");
1653
1464
      }
1654
1465
      goto end;
1655
1466
    }
1659
1470
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1660
1471
      if(ret == -1){
1661
1472
        take_down_interface = false;
1662
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1663
1474
        exitcode = EX_OSERR;
1664
1475
#ifdef __linux__
1665
1476
        if(restore_loglevel){
1666
1477
          ret = klogctl(7, NULL, 0);
1667
1478
          if(ret == -1){
1668
 
            perror_plus("klogctl");
 
1479
            perror("klogctl");
1669
1480
          }
1670
1481
        }
1671
1482
#endif  /* __linux__ */
1673
1484
        errno = 0;
1674
1485
        ret = seteuid(uid);
1675
1486
        if(ret == -1){
1676
 
          perror_plus("seteuid");
 
1487
          perror("seteuid");
1677
1488
        }
1678
1489
        goto end;
1679
1490
      }
1680
1491
    }
1681
 
    /* Sleep checking until interface is running.
1682
 
       Check every 0.25s, up to total time of delay */
 
1492
    /* sleep checking until interface is running */
1683
1493
    for(int i=0; i < delay * 4; i++){
1684
1494
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1685
1495
      if(ret == -1){
1686
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1496
        perror("ioctl SIOCGIFFLAGS");
1687
1497
      } else if(network.ifr_flags & IFF_RUNNING){
1688
1498
        break;
1689
1499
      }
1690
1500
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1691
1501
      ret = nanosleep(&sleeptime, NULL);
1692
1502
      if(ret == -1 and errno != EINTR){
1693
 
        perror_plus("nanosleep");
 
1503
        perror("nanosleep");
1694
1504
      }
1695
1505
    }
1696
1506
    if(not take_down_interface){
1697
1507
      /* We won't need the socket anymore */
1698
1508
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1699
1509
      if(ret == -1){
1700
 
        perror_plus("close");
 
1510
        perror("close");
1701
1511
      }
1702
1512
    }
1703
1513
#ifdef __linux__
1705
1515
      /* Restores kernel loglevel to default */
1706
1516
      ret = klogctl(7, NULL, 0);
1707
1517
      if(ret == -1){
1708
 
        perror_plus("klogctl");
 
1518
        perror("klogctl");
1709
1519
      }
1710
1520
    }
1711
1521
#endif  /* __linux__ */
1715
1525
      /* Lower privileges */
1716
1526
      ret = seteuid(uid);
1717
1527
      if(ret == -1){
1718
 
        perror_plus("seteuid");
 
1528
        perror("seteuid");
1719
1529
      }
1720
1530
    } else {
1721
1531
      /* Lower privileges permanently */
1722
1532
      ret = setuid(uid);
1723
1533
      if(ret == -1){
1724
 
        perror_plus("setuid");
 
1534
        perror("setuid");
1725
1535
      }
1726
1536
    }
1727
1537
  }
1743
1553
    goto end;
1744
1554
  }
1745
1555
  
 
1556
  tempdir_created = true;
1746
1557
  if(mkdtemp(tempdir) == NULL){
1747
 
    perror_plus("mkdtemp");
 
1558
    tempdir_created = false;
 
1559
    perror("mkdtemp");
1748
1560
    goto end;
1749
1561
  }
1750
 
  tempdir_created = true;
1751
1562
  
1752
1563
  if(quit_now){
1753
1564
    goto end;
1795
1606
    
1796
1607
    port = (uint16_t)tmpmax;
1797
1608
    *address = '\0';
 
1609
    address = connect_to;
1798
1610
    /* Colon in address indicates IPv6 */
1799
1611
    int af;
1800
 
    if(strchr(connect_to, ':') != NULL){
 
1612
    if(strchr(address, ':') != NULL){
1801
1613
      af = AF_INET6;
1802
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1803
 
      if(connect_to[0] == '[' and address[-1] == ']')
1804
 
        {
1805
 
          connect_to++;
1806
 
          address[-1] = '\0';
1807
 
        }
1808
1614
    } else {
1809
1615
      af = AF_INET;
1810
1616
    }
1811
 
    address = connect_to;
1812
1617
    
1813
1618
    if(quit_now){
1814
1619
      goto end;
1815
1620
    }
1816
1621
    
1817
 
    while(not quit_now){
1818
 
      ret = start_mandos_communication(address, port, if_index, af);
1819
 
      if(quit_now or ret == 0){
1820
 
        break;
1821
 
      }
1822
 
      if(debug){
1823
 
        fprintf(stderr, "Retrying in %d seconds\n",
1824
 
                (int)retry_interval);
1825
 
      }
1826
 
      sleep((int)retry_interval);
1827
 
    }
1828
 
    
1829
 
    if (not quit_now){
 
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 {
1830
1644
      exitcode = EXIT_SUCCESS;
1831
1645
    }
1832
 
 
1833
1646
    goto end;
1834
1647
  }
1835
1648
  
1887
1700
  if(debug){
1888
1701
    fprintf(stderr, "Starting Avahi loop search\n");
1889
1702
  }
1890
 
 
1891
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1892
 
                                (int)(retry_interval * 1000));
1893
 
  if(debug){
1894
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1895
 
            (ret == 0) ? "successfully" : "with error");
1896
 
  }
 
1703
  
 
1704
  avahi_simple_poll_loop(mc.simple_poll);
1897
1705
  
1898
1706
 end:
1899
1707
  
1920
1728
  if(gpgme_initialized){
1921
1729
    gpgme_release(mc.ctx);
1922
1730
  }
1923
 
 
1924
 
  /* Cleans up the circular linked list of Mandos servers the client
1925
 
     has seen */
1926
 
  if(mc.current_server != NULL){
1927
 
    mc.current_server->prev->next = NULL;
1928
 
    while(mc.current_server != NULL){
1929
 
      server *next = mc.current_server->next;
1930
 
      free(mc.current_server);
1931
 
      mc.current_server = next;
1932
 
    }
1933
 
  }
1934
1731
  
1935
1732
  /* Take down the network interface */
1936
1733
  if(take_down_interface){
1938
1735
    errno = 0;
1939
1736
    ret = seteuid(0);
1940
1737
    if(ret == -1){
1941
 
      perror_plus("seteuid");
 
1738
      perror("seteuid");
1942
1739
    }
1943
1740
    if(geteuid() == 0){
1944
1741
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1945
1742
      if(ret == -1){
1946
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1743
        perror("ioctl SIOCGIFFLAGS");
1947
1744
      } else if(network.ifr_flags & IFF_UP) {
1948
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1949
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1950
1747
        if(ret == -1){
1951
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1952
1749
        }
1953
1750
      }
1954
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1955
1752
      if(ret == -1){
1956
 
        perror_plus("close");
 
1753
        perror("close");
1957
1754
      }
1958
1755
      /* Lower privileges permanently */
1959
1756
      errno = 0;
1960
1757
      ret = setuid(uid);
1961
1758
      if(ret == -1){
1962
 
        perror_plus("setuid");
 
1759
        perror("setuid");
1963
1760
      }
1964
1761
    }
1965
1762
  }
1966
1763
  
1967
 
  /* Removes the GPGME temp directory and all files inside */
 
1764
  /* Removes the temp directory used by GPGME */
1968
1765
  if(tempdir_created){
1969
 
    struct dirent **direntries = NULL;
1970
 
    struct dirent *direntry = NULL;
1971
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
1972
 
                             alphasort);
1973
 
    if (numentries > 0){
1974
 
      for(int i = 0; i < numentries; i++){
1975
 
        direntry = direntries[i];
 
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
        }
1976
1786
        char *fullname = NULL;
1977
1787
        ret = asprintf(&fullname, "%s/%s", tempdir,
1978
1788
                       direntry->d_name);
1979
1789
        if(ret < 0){
1980
 
          perror_plus("asprintf");
 
1790
          perror("asprintf");
1981
1791
          continue;
1982
1792
        }
1983
1793
        ret = remove(fullname);
1987
1797
        }
1988
1798
        free(fullname);
1989
1799
      }
1990
 
    }
1991
 
 
1992
 
    /* need to clean even if 0 because man page doesn't specify */
1993
 
    free(direntries);
1994
 
    if (numentries == -1){
1995
 
      perror_plus("scandir");
 
1800
      closedir(d);
1996
1801
    }
1997
1802
    ret = rmdir(tempdir);
1998
1803
    if(ret == -1 and errno != ENOENT){
1999
 
      perror_plus("rmdir");
 
1804
      perror("rmdir");
2000
1805
    }
2001
1806
  }
2002
1807
  
2007
1812
                                            &old_sigterm_action,
2008
1813
                                            NULL));
2009
1814
    if(ret == -1){
2010
 
      perror_plus("sigaction");
 
1815
      perror("sigaction");
2011
1816
    }
2012
1817
    do {
2013
1818
      ret = raise(signal_received);
2014
1819
    } while(ret != 0 and errno == EINTR);
2015
1820
    if(ret != 0){
2016
 
      perror_plus("raise");
 
1821
      perror("raise");
2017
1822
      abort();
2018
1823
    }
2019
1824
    TEMP_FAILURE_RETRY(pause());