/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

Documentation changes:

* DBUS-API: New file documenting the server D-Bus interface.

* clients.conf: Add examples of new approval settings.

* debian/mandos.docs: Added "DBUS-API".

* mandos-clients.conf.xml (OPTIONS): Added "approved_by_default",
                                     "approval_delay", and
                                     "approval_duration".
* mandos.xml (D-BUS INTERFACE): Refer to the "DBUS-API" file.
  (BUGS): Remove mention of lack of a remote query interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno, program_invocation_short_name */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
130
130
static const char sys_class_net[] = "/sys/class/net";
131
131
char *connect_to = NULL;
132
132
 
133
 
/* Doubly linked list that need to be circular linked when ever used */
134
 
typedef struct server{
135
 
  const char *ip;
136
 
  uint16_t port;
137
 
  AvahiIfIndex if_index;
138
 
  int af;
139
 
  struct timespec last_seen;
140
 
  struct server *next;
141
 
  struct server *prev;
142
 
} server;
143
 
 
144
133
/* Used for passing in values through the Avahi callback functions */
145
134
typedef struct {
146
135
  AvahiSimplePoll *simple_poll;
150
139
  gnutls_dh_params_t dh_params;
151
140
  const char *priority;
152
141
  gpgme_ctx_t ctx;
153
 
  server *current_server;
154
142
} mandos_context;
155
143
 
156
144
/* global context so signal handler can reach it*/
157
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
158
146
                      .dh_bits = 1024, .priority = "SECURE256"
159
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
 
147
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
160
148
 
161
149
sig_atomic_t quit_now = 0;
162
150
int signal_received = 0;
163
151
 
164
 
/* Function to use when printing errors */
165
 
void perror_plus(const char *print_text){
166
 
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
167
 
  perror(print_text);
168
 
}
169
 
 
170
152
/*
171
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
172
154
 * bytes. "buffer_capacity" is how much is currently allocated,
184
166
  return buffer_capacity;
185
167
}
186
168
 
187
 
int add_server(const char *ip, uint16_t port,
188
 
                 AvahiIfIndex if_index,
189
 
                 int af){
190
 
  int ret;
191
 
  server *new_server = malloc(sizeof(server));
192
 
  if(new_server == NULL){
193
 
    perror_plus("malloc");
194
 
    return -1;
195
 
  }
196
 
  *new_server = (server){ .ip = strdup(ip),
197
 
                         .port = port,
198
 
                         .if_index = if_index,
199
 
                         .af = af };
200
 
  if(new_server->ip == NULL){
201
 
    perror_plus("strdup");
202
 
    return -1;    
203
 
  }
204
 
  /* uniqe case of first server */
205
 
  if (mc.current_server == NULL){
206
 
    new_server->next = new_server;
207
 
    new_server->prev = new_server;
208
 
    mc.current_server = new_server;
209
 
  /* Placing the new server last in the list */
210
 
  } else {
211
 
    new_server->next = mc.current_server;
212
 
    new_server->prev = mc.current_server->prev;
213
 
    new_server->prev->next = new_server;
214
 
    mc.current_server->prev = new_server;
215
 
  }
216
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
217
 
  if(ret == -1){
218
 
    perror_plus("clock_gettime");
219
 
    return -1;
220
 
  }
221
 
  return 0;
222
 
}
223
 
 
224
169
/* 
225
170
 * Initialize GPGME.
226
171
 */
240
185
    
241
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
242
187
    if(fd == -1){
243
 
      perror_plus("open");
 
188
      perror("open");
244
189
      return false;
245
190
    }
246
191
    
260
205
    
261
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
262
207
    if(ret == -1){
263
 
      perror_plus("close");
 
208
      perror("close");
264
209
    }
265
210
    gpgme_data_release(pgp_data);
266
211
    return true;
391
336
  
392
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
393
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
394
 
    perror_plus("gpgme_data_seek");
 
339
    perror("gpgme_data_seek");
395
340
    plaintext_length = -1;
396
341
    goto decrypt_end;
397
342
  }
402
347
                                      (size_t)plaintext_length,
403
348
                                      plaintext_capacity);
404
349
    if(plaintext_capacity == 0){
405
 
        perror_plus("incbuffer");
 
350
        perror("incbuffer");
406
351
        plaintext_length = -1;
407
352
        goto decrypt_end;
408
353
    }
415
360
      break;
416
361
    }
417
362
    if(ret < 0){
418
 
      perror_plus("gpgme_data_read");
 
363
      perror("gpgme_data_read");
419
364
      plaintext_length = -1;
420
365
      goto decrypt_end;
421
366
    }
644
589
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
590
  if(tcp_sd < 0){
646
591
    int e = errno;
647
 
    perror_plus("socket");
 
592
    perror("socket");
648
593
    errno = e;
649
594
    goto mandos_end;
650
595
  }
664
609
  }
665
610
  if(ret < 0 ){
666
611
    int e = errno;
667
 
    perror_plus("inet_pton");
 
612
    perror("inet_pton");
668
613
    errno = e;
669
614
    goto mandos_end;
670
615
  }
706
651
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
652
      char interface[IF_NAMESIZE];
708
653
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
654
        perror("if_indextoname");
710
655
      } else {
711
656
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
657
                ip, interface, port);
726
671
                        sizeof(addrstr));
727
672
    }
728
673
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
674
      perror("inet_ntop");
730
675
    } else {
731
676
      if(strcmp(addrstr, ip) != 0){
732
677
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
745
690
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
746
691
  }
747
692
  if(ret < 0){
748
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
 
      int e = errno;
750
 
      perror_plus("connect");
751
 
      errno = e;
752
 
    }
 
693
    int e = errno;
 
694
    perror("connect");
 
695
    errno = e;
753
696
    goto mandos_end;
754
697
  }
755
698
  
766
709
                                   out_size - written));
767
710
    if(ret == -1){
768
711
      int e = errno;
769
 
      perror_plus("write");
 
712
      perror("write");
770
713
      errno = e;
771
714
      goto mandos_end;
772
715
    }
796
739
    errno = EINTR;
797
740
    goto mandos_end;
798
741
  }
799
 
 
800
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
742
  
801
743
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
744
  
803
745
  if(quit_now){
840
782
                                   buffer_capacity);
841
783
    if(buffer_capacity == 0){
842
784
      int e = errno;
843
 
      perror_plus("incbuffer");
 
785
      perror("incbuffer");
844
786
      errno = e;
845
787
      goto mandos_end;
846
788
    }
951
893
      if(e == 0){
952
894
        e = errno;
953
895
      }
954
 
      perror_plus("close");
 
896
      perror("close");
955
897
    }
956
898
    gnutls_deinit(session);
957
 
    errno = e;
958
899
    if(quit_now){
959
 
      errno = EINTR;
 
900
      e = EINTR;
960
901
      retval = -1;
961
902
    }
 
903
    errno = e;
962
904
  }
963
905
  return retval;
964
906
}
1007
949
                                           avahi_proto_to_af(proto));
1008
950
      if(ret == 0){
1009
951
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
952
      }
1014
953
    }
1015
954
  }
1069
1008
  }
1070
1009
}
1071
1010
 
1072
 
/* Signal handler that stops main loop after sigterm has been called */
 
1011
/* stop main loop after sigterm has been called */
1073
1012
static void handle_sigterm(int sig){
1074
1013
  if(quit_now){
1075
1014
    return;
1077
1016
  quit_now = 1;
1078
1017
  signal_received = sig;
1079
1018
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
1019
  if(mc.simple_poll != NULL){
1082
1020
    avahi_simple_poll_quit(mc.simple_poll);
1083
1021
  }
1092
1030
int good_interface(const struct dirent *if_entry){
1093
1031
  ssize_t ssret;
1094
1032
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
1033
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
1034
                     if_entry->d_name);
1100
1035
  if(ret < 0){
1101
 
    perror_plus("asprintf");
 
1036
    perror("asprintf");
 
1037
    return 0;
 
1038
  }
 
1039
  if(if_entry->d_name[0] == '.'){
1102
1040
    return 0;
1103
1041
  }
1104
1042
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
1043
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
 
1044
    perror("open");
1108
1045
    return 0;
1109
1046
  }
1110
 
  free(flagname);
1111
1047
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1048
  /* read line from flags_fd */
1113
1049
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1050
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1051
  flagstring[(size_t)to_read] = '\0';
1116
1052
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
 
1053
    perror("malloc");
1118
1054
    close(flags_fd);
1119
1055
    return 0;
1120
1056
  }
1122
1058
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
1059
                                             (size_t)to_read));
1124
1060
    if(ssret == -1){
1125
 
      perror_plus("read");
 
1061
      perror("read");
1126
1062
      free(flagstring);
1127
1063
      close(flags_fd);
1128
1064
      return 0;
1173
1109
    }
1174
1110
    return 0;
1175
1111
  }
1176
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1177
 
  if(flags & IFF_NOARP){
1178
 
    if(debug){
1179
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1180
 
              if_entry->d_name);
1181
 
    }
1182
 
    return 0;
1183
 
  }
1184
1112
  /* Accept this device */
1185
1113
  if(debug){
1186
1114
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1189
1117
  return 1;
1190
1118
}
1191
1119
 
1192
 
int notdotentries(const struct dirent *direntry){
1193
 
  /* Skip "." and ".." */
1194
 
  if(direntry->d_name[0] == '.'
1195
 
     and (direntry->d_name[1] == '\0'
1196
 
          or (direntry->d_name[1] == '.'
1197
 
              and direntry->d_name[2] == '\0'))){
1198
 
    return 0;
1199
 
  }
1200
 
  return 1;
1201
 
}
1202
 
 
1203
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
 
  int ret;
1205
 
  struct timespec now;
1206
 
  struct timespec waited_time;
1207
 
  intmax_t block_time;
1208
 
 
1209
 
  while(true){
1210
 
    if(mc.current_server == NULL){
1211
 
      if (debug){
1212
 
        fprintf(stderr, "Wait until first server is found. No timeout!\n");     
1213
 
      }
1214
 
      ret = avahi_simple_poll_iterate(s, -1);
1215
 
    } else {
1216
 
      if (debug){
1217
 
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
1218
 
      }
1219
 
      /* the current time */
1220
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1221
 
      if(ret == -1){
1222
 
        perror_plus("clock_gettime");
1223
 
        return -1;
1224
 
      }
1225
 
      /* Calculating in ms how long time between now and server
1226
 
         who we visted longest time ago. Now - last seen.  */
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
 
 
1232
 
      if (debug){
1233
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
1234
 
      }
1235
 
 
1236
 
      if(block_time <= 0){
1237
 
        ret = start_mandos_communication(mc.current_server->ip,
1238
 
                                   mc.current_server->port,
1239
 
                                   mc.current_server->if_index,
1240
 
                                   mc.current_server->af);
1241
 
        if(ret == 0){
1242
 
          avahi_simple_poll_quit(mc.simple_poll);
1243
 
          return 0;
1244
 
        }
1245
 
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1246
 
        if(ret == -1){
1247
 
          perror_plus("clock_gettime");
1248
 
          return -1;
1249
 
        }
1250
 
        mc.current_server = mc.current_server->next;
1251
 
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
1252
 
      }
1253
 
      
1254
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1255
 
    }
1256
 
    if(ret != 0){
1257
 
      if (ret > 0 or errno != EINTR) {
1258
 
        return (ret != 1) ? ret : 0;
1259
 
      }
1260
 
    }
1261
 
  }
1262
 
}
1263
 
 
1264
1120
int main(int argc, char *argv[]){
1265
1121
  AvahiSServiceBrowser *sb = NULL;
1266
1122
  int error;
1283
1139
  bool gnutls_initialized = false;
1284
1140
  bool gpgme_initialized = false;
1285
1141
  float delay = 2.5f;
1286
 
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
1287
1142
  
1288
1143
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1289
1144
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1295
1150
  errno = 0;
1296
1151
  ret = setgid(gid);
1297
1152
  if(ret == -1){
1298
 
    perror_plus("setgid");
 
1153
    perror("setgid");
1299
1154
  }
1300
1155
  
1301
1156
  /* Lower user privileges (temporarily) */
1302
1157
  errno = 0;
1303
1158
  ret = seteuid(uid);
1304
1159
  if(ret == -1){
1305
 
    perror_plus("seteuid");
 
1160
    perror("seteuid");
1306
1161
  }
1307
1162
  
1308
1163
  if(quit_now){
1343
1198
        .arg = "SECONDS",
1344
1199
        .doc = "Maximum delay to wait for interface startup",
1345
1200
        .group = 2 },
1346
 
      { .name = "retry", .key = 132,
1347
 
        .arg = "SECONDS",
1348
 
        .doc = "Retry interval used when denied by the mandos server",
1349
 
        .group = 2 },
1350
1201
      /*
1351
1202
       * These reproduce what we would get without ARGP_NO_HELP
1352
1203
       */
1396
1247
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1397
1248
          argp_error(state, "Bad delay");
1398
1249
        }
1399
 
      case 132:                 /* --retry */
1400
 
        errno = 0;
1401
 
        retry_interval = strtod(arg, &tmp);
1402
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1403
 
           or (retry_interval * 1000) > INT_MAX){
1404
 
          argp_error(state, "Bad retry interval");
1405
 
        }
1406
1250
        break;
1407
1251
        /*
1408
1252
         * These reproduce what we would get without ARGP_NO_HELP
1436
1280
    case ENOMEM:
1437
1281
    default:
1438
1282
      errno = ret;
1439
 
      perror_plus("argp_parse");
 
1283
      perror("argp_parse");
1440
1284
      exitcode = EX_OSERR;
1441
1285
      goto end;
1442
1286
    case EINVAL:
1460
1304
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1461
1305
      }
1462
1306
      if(interface == NULL){
1463
 
        perror_plus("malloc");
 
1307
        perror("malloc");
1464
1308
        free(direntries);
1465
1309
        exitcode = EXIT_FAILURE;
1466
1310
        goto end;
1488
1332
  sigemptyset(&sigterm_action.sa_mask);
1489
1333
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1490
1334
  if(ret == -1){
1491
 
    perror_plus("sigaddset");
 
1335
    perror("sigaddset");
1492
1336
    exitcode = EX_OSERR;
1493
1337
    goto end;
1494
1338
  }
1495
1339
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1496
1340
  if(ret == -1){
1497
 
    perror_plus("sigaddset");
 
1341
    perror("sigaddset");
1498
1342
    exitcode = EX_OSERR;
1499
1343
    goto end;
1500
1344
  }
1501
1345
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1502
1346
  if(ret == -1){
1503
 
    perror_plus("sigaddset");
 
1347
    perror("sigaddset");
1504
1348
    exitcode = EX_OSERR;
1505
1349
    goto end;
1506
1350
  }
1510
1354
  */
1511
1355
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1512
1356
  if(ret == -1){
1513
 
    perror_plus("sigaction");
 
1357
    perror("sigaction");
1514
1358
    return EX_OSERR;
1515
1359
  }
1516
1360
  if(old_sigterm_action.sa_handler != SIG_IGN){
1517
1361
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1518
1362
    if(ret == -1){
1519
 
      perror_plus("sigaction");
 
1363
      perror("sigaction");
1520
1364
      exitcode = EX_OSERR;
1521
1365
      goto end;
1522
1366
    }
1523
1367
  }
1524
1368
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1525
1369
  if(ret == -1){
1526
 
    perror_plus("sigaction");
 
1370
    perror("sigaction");
1527
1371
    return EX_OSERR;
1528
1372
  }
1529
1373
  if(old_sigterm_action.sa_handler != SIG_IGN){
1530
1374
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1531
1375
    if(ret == -1){
1532
 
      perror_plus("sigaction");
 
1376
      perror("sigaction");
1533
1377
      exitcode = EX_OSERR;
1534
1378
      goto end;
1535
1379
    }
1536
1380
  }
1537
1381
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1538
1382
  if(ret == -1){
1539
 
    perror_plus("sigaction");
 
1383
    perror("sigaction");
1540
1384
    return EX_OSERR;
1541
1385
  }
1542
1386
  if(old_sigterm_action.sa_handler != SIG_IGN){
1543
1387
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1544
1388
    if(ret == -1){
1545
 
      perror_plus("sigaction");
 
1389
      perror("sigaction");
1546
1390
      exitcode = EX_OSERR;
1547
1391
      goto end;
1548
1392
    }
1565
1409
    errno = 0;
1566
1410
    ret = seteuid(0);
1567
1411
    if(ret == -1){
1568
 
      perror_plus("seteuid");
 
1412
      perror("seteuid");
1569
1413
    }
1570
1414
    
1571
1415
#ifdef __linux__
1575
1419
    bool restore_loglevel = true;
1576
1420
    if(ret == -1){
1577
1421
      restore_loglevel = false;
1578
 
      perror_plus("klogctl");
 
1422
      perror("klogctl");
1579
1423
    }
1580
1424
#endif  /* __linux__ */
1581
1425
    
1582
1426
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1583
1427
    if(sd < 0){
1584
 
      perror_plus("socket");
 
1428
      perror("socket");
1585
1429
      exitcode = EX_OSERR;
1586
1430
#ifdef __linux__
1587
1431
      if(restore_loglevel){
1588
1432
        ret = klogctl(7, NULL, 0);
1589
1433
        if(ret == -1){
1590
 
          perror_plus("klogctl");
 
1434
          perror("klogctl");
1591
1435
        }
1592
1436
      }
1593
1437
#endif  /* __linux__ */
1595
1439
      errno = 0;
1596
1440
      ret = seteuid(uid);
1597
1441
      if(ret == -1){
1598
 
        perror_plus("seteuid");
 
1442
        perror("seteuid");
1599
1443
      }
1600
1444
      goto end;
1601
1445
    }
1602
1446
    strcpy(network.ifr_name, interface);
1603
1447
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1604
1448
    if(ret == -1){
1605
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1449
      perror("ioctl SIOCGIFFLAGS");
1606
1450
#ifdef __linux__
1607
1451
      if(restore_loglevel){
1608
1452
        ret = klogctl(7, NULL, 0);
1609
1453
        if(ret == -1){
1610
 
          perror_plus("klogctl");
 
1454
          perror("klogctl");
1611
1455
        }
1612
1456
      }
1613
1457
#endif  /* __linux__ */
1616
1460
      errno = 0;
1617
1461
      ret = seteuid(uid);
1618
1462
      if(ret == -1){
1619
 
        perror_plus("seteuid");
 
1463
        perror("seteuid");
1620
1464
      }
1621
1465
      goto end;
1622
1466
    }
1626
1470
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1627
1471
      if(ret == -1){
1628
1472
        take_down_interface = false;
1629
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1473
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
1630
1474
        exitcode = EX_OSERR;
1631
1475
#ifdef __linux__
1632
1476
        if(restore_loglevel){
1633
1477
          ret = klogctl(7, NULL, 0);
1634
1478
          if(ret == -1){
1635
 
            perror_plus("klogctl");
 
1479
            perror("klogctl");
1636
1480
          }
1637
1481
        }
1638
1482
#endif  /* __linux__ */
1640
1484
        errno = 0;
1641
1485
        ret = seteuid(uid);
1642
1486
        if(ret == -1){
1643
 
          perror_plus("seteuid");
 
1487
          perror("seteuid");
1644
1488
        }
1645
1489
        goto end;
1646
1490
      }
1647
1491
    }
1648
 
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
 
1492
    /* sleep checking until interface is running */
1649
1493
    for(int i=0; i < delay * 4; i++){
1650
1494
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1651
1495
      if(ret == -1){
1652
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1496
        perror("ioctl SIOCGIFFLAGS");
1653
1497
      } else if(network.ifr_flags & IFF_RUNNING){
1654
1498
        break;
1655
1499
      }
1656
1500
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1657
1501
      ret = nanosleep(&sleeptime, NULL);
1658
1502
      if(ret == -1 and errno != EINTR){
1659
 
        perror_plus("nanosleep");
 
1503
        perror("nanosleep");
1660
1504
      }
1661
1505
    }
1662
1506
    if(not take_down_interface){
1663
1507
      /* We won't need the socket anymore */
1664
1508
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1665
1509
      if(ret == -1){
1666
 
        perror_plus("close");
 
1510
        perror("close");
1667
1511
      }
1668
1512
    }
1669
1513
#ifdef __linux__
1671
1515
      /* Restores kernel loglevel to default */
1672
1516
      ret = klogctl(7, NULL, 0);
1673
1517
      if(ret == -1){
1674
 
        perror_plus("klogctl");
 
1518
        perror("klogctl");
1675
1519
      }
1676
1520
    }
1677
1521
#endif  /* __linux__ */
1681
1525
      /* Lower privileges */
1682
1526
      ret = seteuid(uid);
1683
1527
      if(ret == -1){
1684
 
        perror_plus("seteuid");
 
1528
        perror("seteuid");
1685
1529
      }
1686
1530
    } else {
1687
1531
      /* Lower privileges permanently */
1688
1532
      ret = setuid(uid);
1689
1533
      if(ret == -1){
1690
 
        perror_plus("setuid");
 
1534
        perror("setuid");
1691
1535
      }
1692
1536
    }
1693
1537
  }
1709
1553
    goto end;
1710
1554
  }
1711
1555
  
 
1556
  tempdir_created = true;
1712
1557
  if(mkdtemp(tempdir) == NULL){
1713
 
    perror_plus("mkdtemp");
 
1558
    tempdir_created = false;
 
1559
    perror("mkdtemp");
1714
1560
    goto end;
1715
1561
  }
1716
 
  tempdir_created = true;
1717
1562
  
1718
1563
  if(quit_now){
1719
1564
    goto end;
1773
1618
    if(quit_now){
1774
1619
      goto end;
1775
1620
    }
1776
 
 
1777
 
    while(not quit_now){
1778
 
      ret = start_mandos_communication(address, port, if_index, af);
1779
 
      if(quit_now or ret == 0){
 
1621
    
 
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;
1780
1641
        break;
1781
1642
      }
1782
 
      sleep((int)retry_interval or 1);
1783
 
    };
1784
 
 
1785
 
    if (not quit_now){
 
1643
    } else {
1786
1644
      exitcode = EXIT_SUCCESS;
1787
1645
    }
1788
 
 
1789
1646
    goto end;
1790
1647
  }
1791
1648
  
1843
1700
  if(debug){
1844
1701
    fprintf(stderr, "Starting Avahi loop search\n");
1845
1702
  }
1846
 
 
1847
 
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1848
 
  if(debug){
1849
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
 
            (ret == 0) ? "successfully" : "with error");
1851
 
  }
 
1703
  
 
1704
  avahi_simple_poll_loop(mc.simple_poll);
1852
1705
  
1853
1706
 end:
1854
1707
  
1875
1728
  if(gpgme_initialized){
1876
1729
    gpgme_release(mc.ctx);
1877
1730
  }
1878
 
 
1879
 
  /* cleans up the circular linked list of mandos servers the client has seen */
1880
 
  if(mc.current_server != NULL){
1881
 
    mc.current_server->prev->next = NULL;
1882
 
    while(mc.current_server != NULL){
1883
 
      server *next = mc.current_server->next;
1884
 
      free(mc.current_server);
1885
 
      mc.current_server = next;
1886
 
    }
1887
 
  }
1888
1731
  
1889
1732
  /* Take down the network interface */
1890
1733
  if(take_down_interface){
1892
1735
    errno = 0;
1893
1736
    ret = seteuid(0);
1894
1737
    if(ret == -1){
1895
 
      perror_plus("seteuid");
 
1738
      perror("seteuid");
1896
1739
    }
1897
1740
    if(geteuid() == 0){
1898
1741
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1899
1742
      if(ret == -1){
1900
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1743
        perror("ioctl SIOCGIFFLAGS");
1901
1744
      } else if(network.ifr_flags & IFF_UP) {
1902
1745
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1903
1746
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1904
1747
        if(ret == -1){
1905
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1748
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1906
1749
        }
1907
1750
      }
1908
1751
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1909
1752
      if(ret == -1){
1910
 
        perror_plus("close");
 
1753
        perror("close");
1911
1754
      }
1912
1755
      /* Lower privileges permanently */
1913
1756
      errno = 0;
1914
1757
      ret = setuid(uid);
1915
1758
      if(ret == -1){
1916
 
        perror_plus("setuid");
 
1759
        perror("setuid");
1917
1760
      }
1918
1761
    }
1919
1762
  }
1920
1763
  
1921
 
  /* Removes the GPGME temp directory and all files inside */
 
1764
  /* Removes the temp directory used by GPGME */
1922
1765
  if(tempdir_created){
1923
 
    struct dirent **direntries = NULL;
1924
 
    struct dirent *direntry = NULL;
1925
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1926
 
    if (ret > 0){
1927
 
      for(int i = 0; i < ret; i++){
1928
 
        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
        }
1929
1786
        char *fullname = NULL;
1930
1787
        ret = asprintf(&fullname, "%s/%s", tempdir,
1931
1788
                       direntry->d_name);
1932
1789
        if(ret < 0){
1933
 
          perror_plus("asprintf");
 
1790
          perror("asprintf");
1934
1791
          continue;
1935
1792
        }
1936
1793
        ret = remove(fullname);
1940
1797
        }
1941
1798
        free(fullname);
1942
1799
      }
1943
 
    }
1944
 
 
1945
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
1946
 
    free(direntries);
1947
 
    if (ret == -1){
1948
 
      perror_plus("scandir");
 
1800
      closedir(d);
1949
1801
    }
1950
1802
    ret = rmdir(tempdir);
1951
1803
    if(ret == -1 and errno != ENOENT){
1952
 
      perror_plus("rmdir");
 
1804
      perror("rmdir");
1953
1805
    }
1954
1806
  }
1955
1807
  
1960
1812
                                            &old_sigterm_action,
1961
1813
                                            NULL));
1962
1814
    if(ret == -1){
1963
 
      perror_plus("sigaction");
 
1815
      perror("sigaction");
1964
1816
    }
1965
1817
    do {
1966
1818
      ret = raise(signal_received);
1967
1819
    } while(ret != 0 and errno == EINTR);
1968
1820
    if(ret != 0){
1969
 
      perror_plus("raise");
 
1821
      perror("raise");
1970
1822
      abort();
1971
1823
    }
1972
1824
    TEMP_FAILURE_RETRY(pause());