/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: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
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() */
53
53
                                   sockaddr_in6, PF_INET6,
54
54
                                   SOCK_STREAM, uid_t, gid_t, open(),
55
55
                                   opendir(), DIR */
56
 
#include <sys/stat.h>           /* open(), S_ISREG */
 
56
#include <sys/stat.h>           /* open() */
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect() */
59
59
#include <fcntl.h>              /* open() */
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, program_invocation_short_name */
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 */
 
133
/* Doubly linked list that need to be circular linked when ever used */
136
134
typedef struct server{
137
135
  const char *ip;
138
136
  uint16_t port;
158
156
/* global context so signal handler can reach it*/
159
157
mandos_context mc = { .simple_poll = NULL, .server = NULL,
160
158
                      .dh_bits = 1024, .priority = "SECURE256"
161
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
162
 
                      .current_server = NULL };
 
159
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
163
160
 
164
161
sig_atomic_t quit_now = 0;
165
162
int signal_received = 0;
166
163
 
167
164
/* Function to use when printing errors */
168
165
void perror_plus(const char *print_text){
169
 
  fprintf(stderr, "Mandos plugin %s: ",
170
 
          program_invocation_short_name);
 
166
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
171
167
  perror(print_text);
172
168
}
173
169
 
188
184
  return buffer_capacity;
189
185
}
190
186
 
191
 
/* Add server to set of servers to retry periodically */
192
187
int add_server(const char *ip, uint16_t port,
193
188
                 AvahiIfIndex if_index,
194
189
                 int af){
204
199
                         .af = af };
205
200
  if(new_server->ip == NULL){
206
201
    perror_plus("strdup");
207
 
    return -1;
 
202
    return -1;    
208
203
  }
209
 
  /* Special case of first server */
 
204
  /* uniqe case of first server */
210
205
  if (mc.current_server == NULL){
211
206
    new_server->next = new_server;
212
207
    new_server->prev = new_server;
213
208
    mc.current_server = new_server;
214
 
  /* Place the new server last in the list */
 
209
  /* Placing the new server last in the list */
215
210
  } else {
216
211
    new_server->next = mc.current_server;
217
212
    new_server->prev = mc.current_server->prev;
284
279
    return false;
285
280
  }
286
281
  
287
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
282
    /* Set GPGME home directory for the OpenPGP engine only */
288
283
  rc = gpgme_get_engine_info(&engine_info);
289
284
  if(rc != GPG_ERR_NO_ERROR){
290
285
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
483
478
  }
484
479
  
485
480
  /* OpenPGP credentials */
486
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
481
  gnutls_certificate_allocate_credentials(&mc.cred);
487
482
  if(ret != GNUTLS_E_SUCCESS){
488
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
483
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
484
                                                    from
 
485
                                                    -Wunreachable-code
 
486
                                                 */
489
487
            safer_gnutls_strerror(ret));
490
488
    gnutls_global_deinit();
491
489
    return -1;
798
796
    errno = EINTR;
799
797
    goto mandos_end;
800
798
  }
801
 
  
802
 
  /* Spurious warning from -Wint-to-pointer-cast */
 
799
 
 
800
  /* Spurious warnings from -Wint-to-pointer-cast */
803
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
804
802
  
805
803
  if(quit_now){
1071
1069
  }
1072
1070
}
1073
1071
 
1074
 
/* Signal handler that stops main loop after SIGTERM */
 
1072
/* Signal handler that stops main loop after sigterm has been called */
1075
1073
static void handle_sigterm(int sig){
1076
1074
  if(quit_now){
1077
1075
    return;
1086
1084
  errno = old_errno;
1087
1085
}
1088
1086
 
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){
 
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 = (sizeof(ifreq_flags)*2)+3; /* "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){
1100
1143
    if(debug){
1101
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1144
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
 
1145
              flagstring, if_entry->d_name);
1102
1146
    }
1103
 
    return false;
 
1147
    free(flagstring);
 
1148
    return 0;
1104
1149
  }
1105
 
  return true;
1106
 
}
1107
 
 
1108
 
bool good_flags(const char *ifname, const struct ifreq *ifr){
1109
 
  
 
1150
  free(flagstring);
 
1151
  ifreq_flags flags = (ifreq_flags)tmpmax;
1110
1152
  /* Reject the loopback device */
1111
 
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1153
  if(flags & IFF_LOOPBACK){
1112
1154
    if(debug){
1113
1155
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1114
 
              ifname);
 
1156
              if_entry->d_name);
1115
1157
    }
1116
 
    return false;
 
1158
    return 0;
1117
1159
  }
1118
1160
  /* Accept point-to-point devices only if connect_to is specified */
1119
 
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1161
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1120
1162
    if(debug){
1121
1163
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1122
 
              ifname);
 
1164
              if_entry->d_name);
1123
1165
    }
1124
 
    return true;
 
1166
    return 1;
1125
1167
  }
1126
1168
  /* Otherwise, reject non-broadcast-capable devices */
1127
 
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1169
  if(not (flags & IFF_BROADCAST)){
1128
1170
    if(debug){
1129
1171
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1130
 
              ifname);
 
1172
              if_entry->d_name);
1131
1173
    }
1132
 
    return false;
 
1174
    return 0;
1133
1175
  }
1134
1176
  /* Reject non-ARP interfaces (including dummy interfaces) */
1135
 
  if(ifr->ifr_flags & IFF_NOARP){
 
1177
  if(flags & IFF_NOARP){
1136
1178
    if(debug){
1137
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1179
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
 
1180
              if_entry->d_name);
1138
1181
    }
1139
 
    return false;
 
1182
    return 0;
1140
1183
  }
1141
 
  
1142
1184
  /* Accept this device */
1143
1185
  if(debug){
1144
 
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1145
 
  }
1146
 
  return true;
1147
 
}
1148
 
 
1149
 
/* 
1150
 
 * This function determines if a directory entry in /sys/class/net
1151
 
 * corresponds to an acceptable network device.
1152
 
 * (This function is passed to scandir(3) as a filter function.)
1153
 
 */
1154
 
int good_interface(const struct dirent *if_entry){
1155
 
  if(if_entry->d_name[0] == '.'){
1156
 
    return 0;
1157
 
  }
1158
 
  
1159
 
  struct ifreq ifr;
1160
 
  if(not get_flags(if_entry->d_name, &ifr)){
1161
 
    if(debug){
1162
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1163
 
              if_entry->d_name);
1164
 
    }
1165
 
    return 0;
1166
 
  }
1167
 
  
1168
 
  if(not good_flags(if_entry->d_name, &ifr)){
1169
 
    return 0;
1170
 
  }
1171
 
  return 1;
1172
 
}
1173
 
 
1174
 
/* 
1175
 
 * This function determines if a directory entry in /sys/class/net
1176
 
 * corresponds to an acceptable network device which is up.
1177
 
 * (This function is passed to scandir(3) as a filter function.)
1178
 
 */
1179
 
int up_interface(const struct dirent *if_entry){
1180
 
  if(if_entry->d_name[0] == '.'){
1181
 
    return 0;
1182
 
  }
1183
 
  
1184
 
  struct ifreq ifr;
1185
 
  if(not get_flags(if_entry->d_name, &ifr)){
1186
 
    if(debug){
1187
 
      fprintf(stderr, "Failed to get flags for interface \"%s\"\n",
1188
 
              if_entry->d_name);
1189
 
    }
1190
 
    return 0;
1191
 
  }
1192
 
  
1193
 
  /* Reject down interfaces */
1194
 
  if(not (ifr.ifr_flags & IFF_UP)){
1195
 
    if(debug){
1196
 
      fprintf(stderr, "Rejecting down interface \"%s\"\n",
1197
 
              if_entry->d_name);
1198
 
    }
1199
 
    return 0;
1200
 
  }
1201
 
  
1202
 
  /* Reject non-running interfaces */
1203
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1204
 
    if(debug){
1205
 
      fprintf(stderr, "Rejecting non-running interface \"%s\"\n",
1206
 
              if_entry->d_name);
1207
 
    }
1208
 
    return 0;
1209
 
  }
1210
 
  
1211
 
  if(not good_flags(if_entry->d_name, &ifr)){
1212
 
    return 0;
 
1186
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
 
1187
            if_entry->d_name);
1213
1188
  }
1214
1189
  return 1;
1215
1190
}
1225
1200
  return 1;
1226
1201
}
1227
1202
 
1228
 
/* Is this directory entry a runnable program? */
1229
 
int runnable_hook(const struct dirent *direntry){
1230
 
  int ret;
1231
 
  struct stat st;
1232
 
  
1233
 
  if((direntry->d_name)[0] == '\0'){
1234
 
    /* Empty name? */
1235
 
    return 0;
1236
 
  }
1237
 
  
1238
 
  /* Save pointer to last character */
1239
 
  char *end = strchr(direntry->d_name, '\0')-1;
1240
 
  
1241
 
  if(*end == '~'){
1242
 
    /* Backup name~ */
1243
 
    return 0;
1244
 
  }
1245
 
  
1246
 
  if(((direntry->d_name)[0] == '#')
1247
 
     and (*end == '#')){
1248
 
    /* Temporary #name# */
1249
 
    return 0;
1250
 
  }
1251
 
  
1252
 
  /* XXX more rules here */
1253
 
  
1254
 
  ret = stat(direntry->d_name, &st);
1255
 
  if(ret == -1){
1256
 
    if(debug){
1257
 
      perror_plus("Could not stat plugin");
1258
 
    }
1259
 
    return 0;
1260
 
  }
1261
 
  if(not (S_ISREG(st.st_mode))){
1262
 
    /* Not a regular file */
1263
 
    return 0;
1264
 
  }
1265
 
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1266
 
    /* Not executable */
1267
 
    return 0;
1268
 
  }
1269
 
  return 1;
1270
 
}
1271
 
 
1272
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1273
1204
  int ret;
1274
1205
  struct timespec now;
1275
1206
  struct timespec waited_time;
1276
1207
  intmax_t block_time;
1277
 
  
 
1208
 
1278
1209
  while(true){
1279
1210
    if(mc.current_server == NULL){
1280
1211
      if (debug){
1281
 
        fprintf(stderr,
1282
 
                "Wait until first server is found. No timeout!\n");
 
1212
        fprintf(stderr, "Wait until first server is found. No timeout!\n");     
1283
1213
      }
1284
1214
      ret = avahi_simple_poll_iterate(s, -1);
1285
1215
    } else {
1286
1216
      if (debug){
1287
 
        fprintf(stderr, "Check current_server if we should run it,"
1288
 
                " or wait\n");
 
1217
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
1289
1218
      }
1290
1219
      /* the current time */
1291
1220
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1295
1224
      }
1296
1225
      /* Calculating in ms how long time between now and server
1297
1226
         who we visted longest time ago. Now - last seen.  */
1298
 
      waited_time.tv_sec = (now.tv_sec
1299
 
                            - mc.current_server->last_seen.tv_sec);
1300
 
      waited_time.tv_nsec = (now.tv_nsec
1301
 
                             - mc.current_server->last_seen.tv_nsec);
1302
 
      /* total time is 10s/10,000ms.
1303
 
         Converting to s from ms by dividing by 1,000,
1304
 
         and ns to ms by dividing by 1,000,000. */
1305
 
      block_time = ((retry_interval
1306
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1307
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1308
 
      
 
1227
      waited_time.tv_sec = now.tv_sec - mc.current_server->last_seen.tv_sec;
 
1228
      waited_time.tv_nsec = now.tv_nsec - mc.current_server->last_seen.tv_nsec;
 
1229
      /* total time is 10s/10000ms. Converting to s to ms by 1000/s, and ns to ms by divind by 1000000. */
 
1230
      block_time = (retry_interval - ((intmax_t)waited_time.tv_sec * 1000)) - ((intmax_t)waited_time.tv_nsec / 1000000);
 
1231
 
1309
1232
      if (debug){
1310
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
 
1233
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
1311
1234
      }
1312
 
      
 
1235
 
1313
1236
      if(block_time <= 0){
1314
1237
        ret = start_mandos_communication(mc.current_server->ip,
1315
 
                                         mc.current_server->port,
1316
 
                                         mc.current_server->if_index,
1317
 
                                         mc.current_server->af);
 
1238
                                   mc.current_server->port,
 
1239
                                   mc.current_server->if_index,
 
1240
                                   mc.current_server->af);
1318
1241
        if(ret == 0){
1319
1242
          avahi_simple_poll_quit(mc.simple_poll);
1320
1243
          return 0;
1321
1244
        }
1322
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1323
 
                            &mc.current_server->last_seen);
 
1245
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1324
1246
        if(ret == -1){
1325
1247
          perror_plus("clock_gettime");
1326
1248
          return -1;
1327
1249
        }
1328
1250
        mc.current_server = mc.current_server->next;
1329
 
        block_time = 0;         /* Call avahi to find new Mandos
1330
 
                                   servers, but don't block */
 
1251
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
1331
1252
      }
1332
1253
      
1333
1254
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1362
1283
  bool gnutls_initialized = false;
1363
1284
  bool gpgme_initialized = false;
1364
1285
  float delay = 2.5f;
1365
 
  double retry_interval = 10; /* 10s between trying a server and
1366
 
                                 retrying the same server again */
 
1286
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
1367
1287
  
1368
1288
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1369
1289
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1480
1400
        errno = 0;
1481
1401
        retry_interval = strtod(arg, &tmp);
1482
1402
        if(errno != 0 or tmp == arg or *tmp != '\0'
1483
 
           or (retry_interval * 1000) > INT_MAX
1484
 
           or retry_interval < 0){
 
1403
           or (retry_interval * 1000) > INT_MAX){
1485
1404
          argp_error(state, "Bad retry interval");
1486
1405
        }
1487
1406
        break;
1525
1444
      goto end;
1526
1445
    }
1527
1446
  }
1528
 
    
1529
 
  {
1530
 
    /* Work around Debian bug #633582:
1531
 
       <http://bugs.debian.org/633582> */
1532
 
    struct stat st;
1533
 
    
1534
 
    /* Re-raise priviliges */
1535
 
    errno = 0;
1536
 
    ret = seteuid(0);
1537
 
    if(ret == -1){
1538
 
      perror_plus("seteuid");
1539
 
    }
1540
 
    
1541
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1542
 
      int seckey_fd = open(seckey, O_RDONLY);
1543
 
      if(seckey_fd == -1){
1544
 
        perror_plus("open");
1545
 
      } else {
1546
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1547
 
        if(ret == -1){
1548
 
          perror_plus("fstat");
1549
 
        } else {
1550
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1551
 
            ret = fchown(seckey_fd, uid, gid);
1552
 
            if(ret == -1){
1553
 
              perror_plus("fchown");
1554
 
            }
1555
 
          }
1556
 
        }
1557
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1558
 
      }
1559
 
    }
1560
 
    
1561
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1562
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1563
 
      if(pubkey_fd == -1){
1564
 
        perror_plus("open");
1565
 
      } else {
1566
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1567
 
        if(ret == -1){
1568
 
          perror_plus("fstat");
1569
 
        } else {
1570
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1571
 
            ret = fchown(pubkey_fd, uid, gid);
1572
 
            if(ret == -1){
1573
 
              perror_plus("fchown");
1574
 
            }
1575
 
          }
1576
 
        }
1577
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1578
 
      }
1579
 
    }
1580
 
    
1581
 
    /* Lower privileges */
1582
 
    errno = 0;
1583
 
    ret = seteuid(uid);
1584
 
    if(ret == -1){
1585
 
      perror_plus("seteuid");
1586
 
    }
1587
 
  }
1588
 
  
1589
 
  /* Find network hooks and run them */
1590
 
  {
1591
 
    struct dirent **direntries;
1592
 
    struct dirent *direntry;
1593
 
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1594
 
                           alphasort);
1595
 
    int devnull = open("/dev/null", O_RDONLY);
1596
 
    for(int i = 0; i < numhooks; i++){
1597
 
      direntry = direntries[0];
1598
 
      char *fullname = NULL;
1599
 
      ret = asprintf(&fullname, "%s/%s", tempdir,
1600
 
                     direntry->d_name);
1601
 
      if(ret < 0){
1602
 
        perror_plus("asprintf");
1603
 
        continue;
1604
 
      }
1605
 
      pid_t hook_pid = fork();
1606
 
      if(hook_pid == 0){
1607
 
        /* Child */
1608
 
        dup2(devnull, STDIN_FILENO);
1609
 
        close(devnull);
1610
 
        dup2(STDERR_FILENO, STDOUT_FILENO);
1611
 
        ret = setenv("DEVICE", interface, 1);
1612
 
        if(ret == -1){
1613
 
          perror_plus("setenv");
1614
 
          exit(1);
1615
 
        }
1616
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1617
 
        if(ret == -1){
1618
 
          perror_plus("setenv");
1619
 
          exit(1);
1620
 
        }
1621
 
        ret = setenv("MODE", "start", 1);
1622
 
        if(ret == -1){
1623
 
          perror_plus("setenv");
1624
 
          exit(1);
1625
 
        }
1626
 
        char *delaystring;
1627
 
        ret = asprintf(&delaystring, "%f", delay);
1628
 
        if(ret == -1){
1629
 
          perror_plus("asprintf");
1630
 
          exit(1);
1631
 
        }
1632
 
        ret = setenv("DELAY", delaystring, 1);
1633
 
        if(ret == -1){
1634
 
          free(delaystring);
1635
 
          perror_plus("setenv");
1636
 
          exit(1);
1637
 
        }
1638
 
        free(delaystring);
1639
 
        ret = execl(fullname, direntry->d_name, "start", NULL);
1640
 
        perror_plus("execl");
1641
 
      }
1642
 
      free(fullname);
1643
 
      if(quit_now){
1644
 
        goto end;
1645
 
      }
1646
 
    }
1647
 
    close(devnull);
1648
 
  }
1649
1447
  
1650
1448
  if(not debug){
1651
1449
    avahi_set_log_function(empty_log);
1652
1450
  }
1653
 
  
 
1451
 
1654
1452
  if(interface[0] == '\0'){
1655
1453
    struct dirent **direntries;
1656
 
    /* First look for interfaces that are up */
1657
 
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1454
    ret = scandir(sys_class_net, &direntries, good_interface,
1658
1455
                  alphasort);
1659
 
    if(ret == 0){
1660
 
      /* No up interfaces, look for any good interfaces */
1661
 
      free(direntries);
1662
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1663
 
                    alphasort);
1664
 
    }
1665
1456
    if(ret >= 1){
1666
 
      /* Pick the first interface returned */
 
1457
      /* Pick the first good interface */
1667
1458
      interface = strdup(direntries[0]->d_name);
1668
1459
      if(debug){
1669
1460
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1854
1645
        goto end;
1855
1646
      }
1856
1647
    }
1857
 
    /* Sleep checking until interface is running.
1858
 
       Check every 0.25s, up to total time of delay */
 
1648
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
1859
1649
    for(int i=0; i < delay * 4; i++){
1860
1650
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1861
1651
      if(ret == -1){
1971
1761
    
1972
1762
    port = (uint16_t)tmpmax;
1973
1763
    *address = '\0';
 
1764
    address = connect_to;
1974
1765
    /* Colon in address indicates IPv6 */
1975
1766
    int af;
1976
 
    if(strchr(connect_to, ':') != NULL){
 
1767
    if(strchr(address, ':') != NULL){
1977
1768
      af = AF_INET6;
1978
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1979
 
      if(connect_to[0] == '[' and address[-1] == ']')
1980
 
        {
1981
 
          connect_to++;
1982
 
          address[-1] = '\0';
1983
 
        }
1984
1769
    } else {
1985
1770
      af = AF_INET;
1986
1771
    }
1987
 
    address = connect_to;
1988
1772
    
1989
1773
    if(quit_now){
1990
1774
      goto end;
1991
1775
    }
1992
 
    
 
1776
 
1993
1777
    while(not quit_now){
1994
1778
      ret = start_mandos_communication(address, port, if_index, af);
1995
1779
      if(quit_now or ret == 0){
1996
1780
        break;
1997
1781
      }
1998
 
      if(debug){
1999
 
        fprintf(stderr, "Retrying in %d seconds\n",
2000
 
                (int)retry_interval);
2001
 
      }
2002
 
      sleep((int)retry_interval);
2003
 
    }
2004
 
    
 
1782
      sleep((int)retry_interval or 1);
 
1783
    };
 
1784
 
2005
1785
    if (not quit_now){
2006
1786
      exitcode = EXIT_SUCCESS;
2007
1787
    }
2008
 
    
 
1788
 
2009
1789
    goto end;
2010
1790
  }
2011
1791
  
2064
1844
    fprintf(stderr, "Starting Avahi loop search\n");
2065
1845
  }
2066
1846
 
2067
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
2068
 
                                (int)(retry_interval * 1000));
 
1847
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
2069
1848
  if(debug){
2070
1849
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2071
1850
            (ret == 0) ? "successfully" : "with error");
2097
1876
    gpgme_release(mc.ctx);
2098
1877
  }
2099
1878
 
2100
 
  /* Cleans up the circular linked list of Mandos servers the client
2101
 
     has seen */
 
1879
  /* cleans up the circular linked list of mandos servers the client has seen */
2102
1880
  if(mc.current_server != NULL){
2103
1881
    mc.current_server->prev->next = NULL;
2104
1882
    while(mc.current_server != NULL){
2108
1886
    }
2109
1887
  }
2110
1888
  
2111
 
  /* XXX run network hooks "stop" here  */
2112
 
  
2113
1889
  /* Take down the network interface */
2114
1890
  if(take_down_interface){
2115
1891
    /* Re-raise priviliges */
2146
1922
  if(tempdir_created){
2147
1923
    struct dirent **direntries = NULL;
2148
1924
    struct dirent *direntry = NULL;
2149
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2150
 
                             alphasort);
2151
 
    if (numentries > 0){
2152
 
      for(int i = 0; i < numentries; i++){
 
1925
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
 
1926
    if (ret > 0){
 
1927
      for(int i = 0; i < ret; i++){
2153
1928
        direntry = direntries[i];
2154
1929
        char *fullname = NULL;
2155
1930
        ret = asprintf(&fullname, "%s/%s", tempdir,
2167
1942
      }
2168
1943
    }
2169
1944
 
2170
 
    /* need to clean even if 0 because man page doesn't specify */
 
1945
    /* need to be cleaned even if ret == 0 because man page dont specify */
2171
1946
    free(direntries);
2172
 
    if (numentries == -1){
 
1947
    if (ret == -1){
2173
1948
      perror_plus("scandir");
2174
1949
    }
2175
1950
    ret = rmdir(tempdir);