/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-16 22:38:54 UTC
  • Revision ID: teddy@recompile.se-20120616223854-mfxkg6fgqr56sma5
* plugins.d/mandos-client (mandos_context): Removed "simple_poll"
                                            member.
  (simple_poll): New global variable.  All users changed.

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-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 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
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
61
61
                                 */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
 
#include <assert.h>             /* assert() */
65
64
#include <errno.h>              /* perror(), errno,
66
65
                                   program_invocation_short_name */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
88
87
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
88
                                   WEXITSTATUS(), WTERMSIG() */
90
89
#include <grp.h>                /* setgroups() */
 
90
#include <argz.h>               /* argz_add_sep(), argz_next(),
 
91
                                   argz_delete(), argz_append(),
 
92
                                   argz_stringify(), argz_add(),
 
93
                                   argz_count() */
91
94
 
92
95
#ifdef __linux__
93
96
#include <sys/klog.h>           /* klogctl() */
135
138
static const char sys_class_net[] = "/sys/class/net";
136
139
char *connect_to = NULL;
137
140
const char *hookdir = HOOKDIR;
 
141
uid_t uid = 65534;
 
142
gid_t gid = 65534;
138
143
 
139
144
/* Doubly linked list that need to be circularly linked when used */
140
145
typedef struct server{
141
146
  const char *ip;
142
 
  uint16_t port;
 
147
  in_port_t port;
143
148
  AvahiIfIndex if_index;
144
149
  int af;
145
150
  struct timespec last_seen;
149
154
 
150
155
/* Used for passing in values through the Avahi callback functions */
151
156
typedef struct {
152
 
  AvahiSimplePoll *simple_poll;
153
157
  AvahiServer *server;
154
158
  gnutls_certificate_credentials_t cred;
155
159
  unsigned int dh_bits;
159
163
  server *current_server;
160
164
} mandos_context;
161
165
 
162
 
/* global context so signal handler can reach it*/
163
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
164
 
                      .dh_bits = 1024, .priority = "SECURE256"
165
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
166
 
                      .current_server = NULL };
 
166
/* global so signal handler can reach it*/
 
167
AvahiSimplePoll *simple_poll;
 
168
mandos_context mc = { .server = NULL, .dh_bits = 1024,
 
169
                      .priority = "SECURE256:!CTYPE-X.509:"
 
170
                      "+CTYPE-OPENPGP", .current_server = NULL };
167
171
 
168
172
sig_atomic_t quit_now = 0;
169
173
int signal_received = 0;
170
174
 
171
175
/* Function to use when printing errors */
172
176
void perror_plus(const char *print_text){
 
177
  int e = errno;
173
178
  fprintf(stderr, "Mandos plugin %s: ",
174
179
          program_invocation_short_name);
 
180
  errno = e;
175
181
  perror(print_text);
176
182
}
177
183
 
 
184
__attribute__((format (gnu_printf, 2, 3)))
178
185
int fprintf_plus(FILE *stream, const char *format, ...){
179
186
  va_list ap;
180
187
  va_start (ap, format);
202
209
}
203
210
 
204
211
/* Add server to set of servers to retry periodically */
205
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
 
               int af){
 
212
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
213
                int af){
207
214
  int ret;
208
215
  server *new_server = malloc(sizeof(server));
209
216
  if(new_server == NULL){
210
217
    perror_plus("malloc");
211
 
    return -1;
 
218
    return false;
212
219
  }
213
220
  *new_server = (server){ .ip = strdup(ip),
214
221
                          .port = port,
216
223
                          .af = af };
217
224
  if(new_server->ip == NULL){
218
225
    perror_plus("strdup");
219
 
    return -1;
 
226
    return false;
220
227
  }
221
228
  /* Special case of first server */
222
229
  if (mc.current_server == NULL){
233
240
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
241
  if(ret == -1){
235
242
    perror_plus("clock_gettime");
236
 
    return -1;
 
243
    return false;
237
244
  }
238
 
  return 0;
 
245
  return true;
239
246
}
240
247
 
241
248
/* 
246
253
  gpgme_error_t rc;
247
254
  gpgme_engine_info_t engine_info;
248
255
  
249
 
  
250
256
  /*
251
257
   * Helper function to insert pub and seckey to the engine keyring.
252
258
   */
462
468
}
463
469
 
464
470
static const char * safer_gnutls_strerror(int value){
465
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
466
 
                                               -Wunreachable-code */
 
471
  const char *ret = gnutls_strerror(value);
467
472
  if(ret == NULL)
468
473
    ret = "(unknown)";
469
474
  return ret;
614
619
                      __attribute__((unused)) const char *txt){}
615
620
 
616
621
/* Called when a Mandos server is found */
617
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
622
static int start_mandos_communication(const char *ip, in_port_t port,
618
623
                                      AvahiIfIndex if_index,
619
624
                                      int af){
620
625
  int ret, tcp_sd = -1;
659
664
  
660
665
  if(debug){
661
666
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
662
 
                 PRIu16 "\n", ip, port);
 
667
                 PRIuMAX "\n", ip, (uintmax_t)port);
663
668
  }
664
669
  
665
670
  tcp_sd = socket(pf, SOCK_STREAM, 0);
696
701
    goto mandos_end;
697
702
  }
698
703
  if(af == AF_INET6){
699
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
700
 
                                       -Wconversion and
701
 
                                       -Wunreachable-code */
702
 
    
 
704
    to.in6.sin6_port = htons(port);    
703
705
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
704
706
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
705
707
                                -Wunreachable-code*/
729
731
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
730
732
        perror_plus("if_indextoname");
731
733
      } else {
732
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
 
                     "\n", ip, interface, port);
 
734
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
735
                     "\n", ip, interface, (uintmax_t)port);
734
736
      }
735
737
    } else {
736
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
737
 
                   ip, port);
 
738
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
739
                   ip, (uintmax_t)port);
738
740
    }
739
741
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
740
742
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
818
820
    goto mandos_end;
819
821
  }
820
822
  
821
 
  /* Spurious warning from -Wint-to-pointer-cast */
822
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
823
  /* This casting via intptr_t is to eliminate warning about casting
 
824
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
825
     function "set-session-transport-fd!" does it. */
 
826
  gnutls_transport_set_ptr(session,
 
827
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
823
828
  
824
829
  if(quit_now){
825
830
    errno = EINTR;
998
1003
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
999
1004
                             flags,
1000
1005
                             AVAHI_GCC_UNUSED void* userdata){
1001
 
  assert(r);
 
1006
  if(r == NULL){
 
1007
    return;
 
1008
  }
1002
1009
  
1003
1010
  /* Called whenever a service has been resolved successfully or
1004
1011
     timed out */
1025
1032
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1026
1033
                     host_name, ip, (intmax_t)interface, port);
1027
1034
      }
1028
 
      int ret = start_mandos_communication(ip, port, interface,
 
1035
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1036
                                           interface,
1029
1037
                                           avahi_proto_to_af(proto));
1030
1038
      if(ret == 0){
1031
 
        avahi_simple_poll_quit(mc.simple_poll);
 
1039
        avahi_simple_poll_quit(simple_poll);
1032
1040
      } else {
1033
 
        ret = add_server(ip, port, interface,
1034
 
                         avahi_proto_to_af(proto));
 
1041
        if(not add_server(ip, (in_port_t)port, interface,
 
1042
                          avahi_proto_to_af(proto))){
 
1043
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1044
                       " list\n", name);
 
1045
        }
1035
1046
      }
1036
1047
    }
1037
1048
  }
1048
1059
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1049
1060
                            flags,
1050
1061
                            AVAHI_GCC_UNUSED void* userdata){
1051
 
  assert(b);
 
1062
  if(b == NULL){
 
1063
    return;
 
1064
  }
1052
1065
  
1053
1066
  /* Called whenever a new services becomes available on the LAN or
1054
1067
     is removed from the LAN */
1063
1076
    
1064
1077
    fprintf_plus(stderr, "(Avahi browser) %s\n",
1065
1078
                 avahi_strerror(avahi_server_errno(mc.server)));
1066
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1079
    avahi_simple_poll_quit(simple_poll);
1067
1080
    return;
1068
1081
    
1069
1082
  case AVAHI_BROWSER_NEW:
1102
1115
  signal_received = sig;
1103
1116
  int old_errno = errno;
1104
1117
  /* set main loop to exit */
1105
 
  if(mc.simple_poll != NULL){
1106
 
    avahi_simple_poll_quit(mc.simple_poll);
 
1118
  if(simple_poll != NULL){
 
1119
    avahi_simple_poll_quit(simple_poll);
1107
1120
  }
1108
1121
  errno = old_errno;
1109
1122
}
1110
1123
 
1111
1124
bool get_flags(const char *ifname, struct ifreq *ifr){
1112
1125
  int ret;
 
1126
  error_t ret_errno;
1113
1127
  
1114
1128
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1115
1129
  if(s < 0){
 
1130
    ret_errno = errno;
1116
1131
    perror_plus("socket");
 
1132
    errno = ret_errno;
1117
1133
    return false;
1118
1134
  }
1119
1135
  strcpy(ifr->ifr_name, ifname);
1120
1136
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1121
1137
  if(ret == -1){
1122
1138
    if(debug){
 
1139
      ret_errno = errno;
1123
1140
      perror_plus("ioctl SIOCGIFFLAGS");
 
1141
      errno = ret_errno;
1124
1142
    }
1125
1143
    return false;
1126
1144
  }
1195
1213
}
1196
1214
 
1197
1215
/* 
1198
 
 * This function determines if a directory entry in /sys/class/net
1199
 
 * corresponds to an acceptable network device which is up.
1200
 
 * (This function is passed to scandir(3) as a filter function.)
1201
 
 */
1202
 
int up_interface(const struct dirent *if_entry){
1203
 
  if(if_entry->d_name[0] == '.'){
1204
 
    return 0;
1205
 
  }
1206
 
  
1207
 
  struct ifreq ifr;
1208
 
  if(not get_flags(if_entry->d_name, &ifr)){
1209
 
    if(debug){
1210
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1211
 
                   "\"%s\"\n", if_entry->d_name);
1212
 
    }
1213
 
    return 0;
1214
 
  }
1215
 
  
1216
 
  /* Reject down interfaces */
1217
 
  if(not (ifr.ifr_flags & IFF_UP)){
1218
 
    if(debug){
1219
 
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1220
 
                   if_entry->d_name);
1221
 
    }
1222
 
    return 0;
1223
 
  }
1224
 
  
1225
 
  /* Reject non-running interfaces */
1226
 
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1227
 
    if(debug){
1228
 
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1229
 
                   if_entry->d_name);
1230
 
    }
1231
 
    return 0;
1232
 
  }
1233
 
  
1234
 
  if(not good_flags(if_entry->d_name, &ifr)){
1235
 
    return 0;
1236
 
  }
1237
 
  return 1;
 
1216
 * This function determines if a network interface is up.
 
1217
 */
 
1218
bool interface_is_up(const char *interface){
 
1219
  struct ifreq ifr;
 
1220
  if(not get_flags(interface, &ifr)){
 
1221
    if(debug){
 
1222
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1223
                   "\"%s\"\n", interface);
 
1224
    }
 
1225
    return false;
 
1226
  }
 
1227
  
 
1228
  return (bool)(ifr.ifr_flags & IFF_UP);
 
1229
}
 
1230
 
 
1231
/* 
 
1232
 * This function determines if a network interface is running
 
1233
 */
 
1234
bool interface_is_running(const char *interface){
 
1235
  struct ifreq ifr;
 
1236
  if(not get_flags(interface, &ifr)){
 
1237
    if(debug){
 
1238
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1239
                   "\"%s\"\n", interface);
 
1240
    }
 
1241
    return false;
 
1242
  }
 
1243
  
 
1244
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1238
1245
}
1239
1246
 
1240
1247
int notdotentries(const struct dirent *direntry){
1357
1364
                                         mc.current_server->if_index,
1358
1365
                                         mc.current_server->af);
1359
1366
        if(ret == 0){
1360
 
          avahi_simple_poll_quit(mc.simple_poll);
 
1367
          avahi_simple_poll_quit(simple_poll);
1361
1368
          return 0;
1362
1369
        }
1363
1370
        ret = clock_gettime(CLOCK_MONOTONIC,
1381
1388
  }
1382
1389
}
1383
1390
 
 
1391
/* Set effective uid to 0, return errno */
 
1392
error_t raise_privileges(void){
 
1393
  error_t old_errno = errno;
 
1394
  error_t ret_errno = 0;
 
1395
  if(seteuid(0) == -1){
 
1396
    ret_errno = errno;
 
1397
    perror_plus("seteuid");
 
1398
  }
 
1399
  errno = old_errno;
 
1400
  return ret_errno;
 
1401
}
 
1402
 
 
1403
/* Set effective and real user ID to 0.  Return errno. */
 
1404
error_t raise_privileges_permanently(void){
 
1405
  error_t old_errno = errno;
 
1406
  error_t ret_errno = raise_privileges();
 
1407
  if(ret_errno != 0){
 
1408
    errno = old_errno;
 
1409
    return ret_errno;
 
1410
  }
 
1411
  if(setuid(0) == -1){
 
1412
    ret_errno = errno;
 
1413
    perror_plus("seteuid");
 
1414
  }
 
1415
  errno = old_errno;
 
1416
  return ret_errno;
 
1417
}
 
1418
 
 
1419
/* Set effective user ID to unprivileged saved user ID */
 
1420
error_t lower_privileges(void){
 
1421
  error_t old_errno = errno;
 
1422
  error_t ret_errno = 0;
 
1423
  if(seteuid(uid) == -1){
 
1424
    ret_errno = errno;
 
1425
    perror_plus("seteuid");
 
1426
  }
 
1427
  errno = old_errno;
 
1428
  return ret_errno;
 
1429
}
 
1430
 
 
1431
/* Lower privileges permanently */
 
1432
error_t lower_privileges_permanently(void){
 
1433
  error_t old_errno = errno;
 
1434
  error_t ret_errno = 0;
 
1435
  if(setuid(uid) == -1){
 
1436
    ret_errno = errno;
 
1437
    perror_plus("setuid");
 
1438
  }
 
1439
  errno = old_errno;
 
1440
  return ret_errno;
 
1441
}
 
1442
 
1384
1443
bool run_network_hooks(const char *mode, const char *interface,
1385
1444
                       const float delay){
1386
1445
  struct dirent **direntries;
1389
1448
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1390
1449
                         alphasort);
1391
1450
  if(numhooks == -1){
1392
 
    perror_plus("scandir");
 
1451
    if(errno == ENOENT){
 
1452
      if(debug){
 
1453
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1454
                     " found\n", hookdir);
 
1455
      }
 
1456
    } else {
 
1457
      perror_plus("scandir");
 
1458
    }
1393
1459
  } else {
1394
1460
    int devnull = open("/dev/null", O_RDONLY);
1395
1461
    for(int i = 0; i < numhooks; i++){
1408
1474
      if(hook_pid == 0){
1409
1475
        /* Child */
1410
1476
        /* Raise privileges */
1411
 
        errno = 0;
1412
 
        ret = seteuid(0);
1413
 
        if(ret == -1){
1414
 
          perror_plus("seteuid");
1415
 
        }
1416
 
        /* Raise privileges even more */
1417
 
        errno = 0;
1418
 
        ret = setuid(0);
1419
 
        if(ret == -1){
1420
 
          perror_plus("setuid");
1421
 
        }
 
1477
        raise_privileges_permanently();
1422
1478
        /* Set group */
1423
1479
        errno = 0;
1424
1480
        ret = setgid(0);
1444
1500
          perror_plus("setenv");
1445
1501
          _exit(EX_OSERR);
1446
1502
        }
1447
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1503
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1448
1504
        if(ret == -1){
1449
1505
          perror_plus("setenv");
1450
1506
          _exit(EX_OSERR);
1467
1523
          _exit(EX_OSERR);
1468
1524
        }
1469
1525
        free(delaystring);
1470
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1471
 
        perror_plus("execl");
 
1526
        if(connect_to != NULL){
 
1527
          ret = setenv("CONNECT", connect_to, 1);
 
1528
          if(ret == -1){
 
1529
            perror_plus("setenv");
 
1530
            _exit(EX_OSERR);
 
1531
          }
 
1532
        }
 
1533
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1534
          perror_plus("execl");
 
1535
          _exit(EXIT_FAILURE);
 
1536
        }
1472
1537
      } else {
1473
1538
        int status;
1474
1539
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1508
1573
  return true;
1509
1574
}
1510
1575
 
 
1576
error_t bring_up_interface(const char *const interface,
 
1577
                           const float delay){
 
1578
  int sd = -1;
 
1579
  error_t old_errno = errno;
 
1580
  error_t ret_errno = 0;
 
1581
  int ret, ret_setflags;
 
1582
  struct ifreq network;
 
1583
  unsigned int if_index = if_nametoindex(interface);
 
1584
  if(if_index == 0){
 
1585
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1586
    errno = old_errno;
 
1587
    return ENXIO;
 
1588
  }
 
1589
  
 
1590
  if(quit_now){
 
1591
    errno = old_errno;
 
1592
    return EINTR;
 
1593
  }
 
1594
  
 
1595
  if(not interface_is_up(interface)){
 
1596
    if(not get_flags(interface, &network) and debug){
 
1597
      ret_errno = errno;
 
1598
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1599
                   "\"%s\"\n", interface);
 
1600
      return ret_errno;
 
1601
    }
 
1602
    network.ifr_flags |= IFF_UP;
 
1603
    
 
1604
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1605
    if(sd < 0){
 
1606
      ret_errno = errno;
 
1607
      perror_plus("socket");
 
1608
      errno = old_errno;
 
1609
      return ret_errno;
 
1610
    }
 
1611
  
 
1612
    if(quit_now){
 
1613
      close(sd);
 
1614
      errno = old_errno;
 
1615
      return EINTR;
 
1616
    }
 
1617
    
 
1618
    if(debug){
 
1619
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
 
1620
                   interface);
 
1621
    }
 
1622
    
 
1623
    /* Raise priviliges */
 
1624
    raise_privileges();
 
1625
    
 
1626
#ifdef __linux__
 
1627
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1628
       messages about the network interface to mess up the prompt */
 
1629
    int ret_linux = klogctl(8, NULL, 5);
 
1630
    bool restore_loglevel = true;
 
1631
    if(ret_linux == -1){
 
1632
      restore_loglevel = false;
 
1633
      perror_plus("klogctl");
 
1634
    }
 
1635
#endif  /* __linux__ */
 
1636
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1637
    ret_errno = errno;
 
1638
#ifdef __linux__
 
1639
    if(restore_loglevel){
 
1640
      ret_linux = klogctl(7, NULL, 0);
 
1641
      if(ret_linux == -1){
 
1642
        perror_plus("klogctl");
 
1643
      }
 
1644
    }
 
1645
#endif  /* __linux__ */
 
1646
    
 
1647
    /* Lower privileges */
 
1648
    lower_privileges();
 
1649
    
 
1650
    /* Close the socket */
 
1651
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1652
    if(ret == -1){
 
1653
      perror_plus("close");
 
1654
    }
 
1655
    
 
1656
    if(ret_setflags == -1){
 
1657
      errno = ret_errno;
 
1658
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1659
      errno = old_errno;
 
1660
      return ret_errno;
 
1661
    }
 
1662
  } else if(debug){
 
1663
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
 
1664
                 interface);
 
1665
  }
 
1666
  
 
1667
  /* Sleep checking until interface is running.
 
1668
     Check every 0.25s, up to total time of delay */
 
1669
  for(int i=0; i < delay * 4; i++){
 
1670
    if(interface_is_running(interface)){
 
1671
      break;
 
1672
    }
 
1673
    struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1674
    ret = nanosleep(&sleeptime, NULL);
 
1675
    if(ret == -1 and errno != EINTR){
 
1676
      perror_plus("nanosleep");
 
1677
    }
 
1678
  }
 
1679
  
 
1680
  errno = old_errno;
 
1681
  return 0;
 
1682
}
 
1683
 
 
1684
error_t take_down_interface(const char *const interface){
 
1685
  int sd = -1;
 
1686
  error_t old_errno = errno;
 
1687
  error_t ret_errno = 0;
 
1688
  int ret, ret_setflags;
 
1689
  struct ifreq network;
 
1690
  unsigned int if_index = if_nametoindex(interface);
 
1691
  if(if_index == 0){
 
1692
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1693
    errno = old_errno;
 
1694
    return ENXIO;
 
1695
  }
 
1696
  if(interface_is_up(interface)){
 
1697
    if(not get_flags(interface, &network) and debug){
 
1698
      ret_errno = errno;
 
1699
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1700
                   "\"%s\"\n", interface);
 
1701
      return ret_errno;
 
1702
    }
 
1703
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1704
    
 
1705
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1706
    if(sd < 0){
 
1707
      ret_errno = errno;
 
1708
      perror_plus("socket");
 
1709
      errno = old_errno;
 
1710
      return ret_errno;
 
1711
    }
 
1712
    
 
1713
    if(debug){
 
1714
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
 
1715
                   interface);
 
1716
    }
 
1717
    
 
1718
    /* Raise priviliges */
 
1719
    raise_privileges();
 
1720
    
 
1721
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1722
    ret_errno = errno;
 
1723
    
 
1724
    /* Lower privileges */
 
1725
    lower_privileges();
 
1726
    
 
1727
    /* Close the socket */
 
1728
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1729
    if(ret == -1){
 
1730
      perror_plus("close");
 
1731
    }
 
1732
    
 
1733
    if(ret_setflags == -1){
 
1734
      errno = ret_errno;
 
1735
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1736
      errno = old_errno;
 
1737
      return ret_errno;
 
1738
    }
 
1739
  } else if(debug){
 
1740
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
 
1741
                 interface);
 
1742
  }
 
1743
  
 
1744
  errno = old_errno;
 
1745
  return 0;
 
1746
}
 
1747
 
1511
1748
int main(int argc, char *argv[]){
1512
1749
  AvahiSServiceBrowser *sb = NULL;
1513
 
  int error;
 
1750
  error_t ret_errno;
1514
1751
  int ret;
1515
1752
  intmax_t tmpmax;
1516
1753
  char *tmp;
1517
1754
  int exitcode = EXIT_SUCCESS;
1518
 
  const char *interface = "";
1519
 
  struct ifreq network;
1520
 
  int sd = -1;
1521
 
  bool take_down_interface = false;
1522
 
  uid_t uid;
1523
 
  gid_t gid;
 
1755
  char *interfaces = NULL;
 
1756
  size_t interfaces_size = 0;
 
1757
  char *interfaces_to_take_down = NULL;
 
1758
  size_t interfaces_to_take_down_size = 0;
1524
1759
  char tempdir[] = "/tmp/mandosXXXXXX";
1525
1760
  bool tempdir_created = false;
1526
1761
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1527
1762
  const char *seckey = PATHDIR "/" SECKEY;
1528
1763
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1764
  char *interfaces_hooks = NULL;
 
1765
  size_t interfaces_hooks_size = 0;
1529
1766
  
1530
1767
  bool gnutls_initialized = false;
1531
1768
  bool gpgme_initialized = false;
1593
1830
        .group = 2 },
1594
1831
      { .name = "retry", .key = 132,
1595
1832
        .arg = "SECONDS",
1596
 
        .doc = "Retry interval used when denied by the mandos server",
 
1833
        .doc = "Retry interval used when denied by the Mandos server",
1597
1834
        .group = 2 },
1598
1835
      { .name = "network-hook-dir", .key = 133,
1599
1836
        .arg = "DIR",
1622
1859
        connect_to = arg;
1623
1860
        break;
1624
1861
      case 'i':                 /* --interface */
1625
 
        interface = arg;
 
1862
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
 
1863
                                 (int)',');
 
1864
        if(ret_errno != 0){
 
1865
          argp_error(state, "%s", strerror(ret_errno));
 
1866
        }
1626
1867
        break;
1627
1868
      case 's':                 /* --seckey */
1628
1869
        seckey = arg;
1671
1912
        argp_state_help(state, state->out_stream,
1672
1913
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1914
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
1915
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1677
1916
        exit(argp_err_exit_status);
1678
1917
        break;
1708
1947
       <http://bugs.debian.org/633582> */
1709
1948
    
1710
1949
    /* Re-raise priviliges */
1711
 
    errno = 0;
1712
 
    ret = seteuid(0);
1713
 
    if(ret == -1){
1714
 
      perror_plus("seteuid");
1715
 
    } else {
 
1950
    if(raise_privileges() == 0){
1716
1951
      struct stat st;
1717
1952
      
1718
1953
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1766
2001
    }
1767
2002
  }
1768
2003
  
 
2004
  /* Remove empty interface names */
 
2005
  {
 
2006
    char *interface = NULL;
 
2007
    while((interface = argz_next(interfaces, interfaces_size,
 
2008
                                 interface))){
 
2009
      if(if_nametoindex(interface) == 0){
 
2010
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
 
2011
          fprintf_plus(stderr, "Not using nonexisting interface"
 
2012
                       " \"%s\"\n", interface);
 
2013
        }
 
2014
        argz_delete(&interfaces, &interfaces_size, interface);
 
2015
        interface = NULL;
 
2016
      }
 
2017
    }
 
2018
  }
 
2019
  
1769
2020
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    goto end;
 
2021
  {
 
2022
    
 
2023
    if(interfaces != NULL){
 
2024
      interfaces_hooks = malloc(interfaces_size);
 
2025
      if(interfaces_hooks == NULL){
 
2026
        perror_plus("malloc");
 
2027
        goto end;
 
2028
      }
 
2029
      memcpy(interfaces_hooks, interfaces, interfaces_size);
 
2030
      interfaces_hooks_size = interfaces_size;
 
2031
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2032
                     (int)',');
 
2033
    }
 
2034
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2035
                             interfaces_hooks : "", delay)){
 
2036
      goto end;
 
2037
    }
1772
2038
  }
1773
2039
  
1774
2040
  if(not debug){
1775
2041
    avahi_set_log_function(empty_log);
1776
2042
  }
1777
2043
  
1778
 
  if(interface[0] == '\0'){
1779
 
    struct dirent **direntries;
1780
 
    /* First look for interfaces that are up */
1781
 
    ret = scandir(sys_class_net, &direntries, up_interface,
1782
 
                  alphasort);
1783
 
    if(ret == 0){
1784
 
      /* No up interfaces, look for any good interfaces */
1785
 
      free(direntries);
1786
 
      ret = scandir(sys_class_net, &direntries, good_interface,
1787
 
                    alphasort);
1788
 
    }
1789
 
    if(ret >= 1){
1790
 
      /* Pick the first interface returned */
1791
 
      interface = strdup(direntries[0]->d_name);
1792
 
      if(debug){
1793
 
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1794
 
      }
1795
 
      if(interface == NULL){
1796
 
        perror_plus("malloc");
1797
 
        free(direntries);
1798
 
        exitcode = EXIT_FAILURE;
1799
 
        goto end;
1800
 
      }
1801
 
      free(direntries);
1802
 
    } else {
1803
 
      free(direntries);
1804
 
      fprintf_plus(stderr, "Could not find a network interface\n");
1805
 
      exitcode = EXIT_FAILURE;
1806
 
      goto end;
1807
 
    }
1808
 
  }
1809
 
  
1810
2044
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1811
2045
     from the signal handler */
1812
2046
  /* Initialize the pseudo-RNG for Avahi */
1813
2047
  srand((unsigned int) time(NULL));
1814
 
  mc.simple_poll = avahi_simple_poll_new();
1815
 
  if(mc.simple_poll == NULL){
 
2048
  simple_poll = avahi_simple_poll_new();
 
2049
  if(simple_poll == NULL){
1816
2050
    fprintf_plus(stderr,
1817
2051
                 "Avahi: Failed to create simple poll object.\n");
1818
2052
    exitcode = EX_UNAVAILABLE;
1882
2116
    }
1883
2117
  }
1884
2118
  
1885
 
  /* If the interface is down, bring it up */
1886
 
  if(strcmp(interface, "none") != 0){
1887
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1888
 
    if(if_index == 0){
1889
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1890
 
      exitcode = EX_UNAVAILABLE;
1891
 
      goto end;
1892
 
    }
1893
 
    
1894
 
    if(quit_now){
1895
 
      goto end;
1896
 
    }
1897
 
    
1898
 
    /* Re-raise priviliges */
1899
 
    errno = 0;
1900
 
    ret = seteuid(0);
1901
 
    if(ret == -1){
1902
 
      perror_plus("seteuid");
1903
 
    }
1904
 
    
1905
 
#ifdef __linux__
1906
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1907
 
       messages about the network interface to mess up the prompt */
1908
 
    ret = klogctl(8, NULL, 5);
1909
 
    bool restore_loglevel = true;
1910
 
    if(ret == -1){
1911
 
      restore_loglevel = false;
1912
 
      perror_plus("klogctl");
1913
 
    }
1914
 
#endif  /* __linux__ */
1915
 
    
1916
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1917
 
    if(sd < 0){
1918
 
      perror_plus("socket");
1919
 
      exitcode = EX_OSERR;
1920
 
#ifdef __linux__
1921
 
      if(restore_loglevel){
1922
 
        ret = klogctl(7, NULL, 0);
1923
 
        if(ret == -1){
1924
 
          perror_plus("klogctl");
1925
 
        }
1926
 
      }
1927
 
#endif  /* __linux__ */
1928
 
      /* Lower privileges */
1929
 
      errno = 0;
1930
 
      ret = seteuid(uid);
1931
 
      if(ret == -1){
1932
 
        perror_plus("seteuid");
1933
 
      }
1934
 
      goto end;
1935
 
    }
1936
 
    strcpy(network.ifr_name, interface);
1937
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1938
 
    if(ret == -1){
1939
 
      perror_plus("ioctl SIOCGIFFLAGS");
1940
 
#ifdef __linux__
1941
 
      if(restore_loglevel){
1942
 
        ret = klogctl(7, NULL, 0);
1943
 
        if(ret == -1){
1944
 
          perror_plus("klogctl");
1945
 
        }
1946
 
      }
1947
 
#endif  /* __linux__ */
1948
 
      exitcode = EX_OSERR;
1949
 
      /* Lower privileges */
1950
 
      errno = 0;
1951
 
      ret = seteuid(uid);
1952
 
      if(ret == -1){
1953
 
        perror_plus("seteuid");
1954
 
      }
1955
 
      goto end;
1956
 
    }
1957
 
    if((network.ifr_flags & IFF_UP) == 0){
1958
 
      network.ifr_flags |= IFF_UP;
1959
 
      take_down_interface = true;
1960
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1961
 
      if(ret == -1){
1962
 
        take_down_interface = false;
1963
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1964
 
        exitcode = EX_OSERR;
1965
 
#ifdef __linux__
1966
 
        if(restore_loglevel){
1967
 
          ret = klogctl(7, NULL, 0);
1968
 
          if(ret == -1){
1969
 
            perror_plus("klogctl");
1970
 
          }
1971
 
        }
1972
 
#endif  /* __linux__ */
1973
 
        /* Lower privileges */
1974
 
        errno = 0;
1975
 
        ret = seteuid(uid);
1976
 
        if(ret == -1){
1977
 
          perror_plus("seteuid");
1978
 
        }
1979
 
        goto end;
1980
 
      }
1981
 
    }
1982
 
    /* Sleep checking until interface is running.
1983
 
       Check every 0.25s, up to total time of delay */
1984
 
    for(int i=0; i < delay * 4; i++){
1985
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1986
 
      if(ret == -1){
1987
 
        perror_plus("ioctl SIOCGIFFLAGS");
1988
 
      } else if(network.ifr_flags & IFF_RUNNING){
1989
 
        break;
1990
 
      }
1991
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1992
 
      ret = nanosleep(&sleeptime, NULL);
1993
 
      if(ret == -1 and errno != EINTR){
1994
 
        perror_plus("nanosleep");
1995
 
      }
1996
 
    }
1997
 
    if(not take_down_interface){
1998
 
      /* We won't need the socket anymore */
1999
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2000
 
      if(ret == -1){
2001
 
        perror_plus("close");
2002
 
      }
2003
 
    }
2004
 
#ifdef __linux__
2005
 
    if(restore_loglevel){
2006
 
      /* Restores kernel loglevel to default */
2007
 
      ret = klogctl(7, NULL, 0);
2008
 
      if(ret == -1){
2009
 
        perror_plus("klogctl");
2010
 
      }
2011
 
    }
2012
 
#endif  /* __linux__ */
2013
 
    /* Lower privileges */
2014
 
    errno = 0;
2015
 
    /* Lower privileges */
2016
 
    ret = seteuid(uid);
2017
 
    if(ret == -1){
2018
 
      perror_plus("seteuid");
 
2119
  /* If no interfaces were specified, make a list */
 
2120
  if(interfaces == NULL){
 
2121
    struct dirent **direntries;
 
2122
    /* Look for any good interfaces */
 
2123
    ret = scandir(sys_class_net, &direntries, good_interface,
 
2124
                  alphasort);
 
2125
    if(ret >= 1){
 
2126
      /* Add all found interfaces to interfaces list */
 
2127
      for(int i = 0; i < ret; ++i){
 
2128
        ret_errno = argz_add(&interfaces, &interfaces_size,
 
2129
                             direntries[i]->d_name);
 
2130
        if(ret_errno != 0){
 
2131
          perror_plus("argz_add");
 
2132
          continue;
 
2133
        }
 
2134
        if(debug){
 
2135
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
 
2136
                       direntries[i]->d_name);
 
2137
        }
 
2138
      }
 
2139
      free(direntries);
 
2140
    } else {
 
2141
      free(direntries);
 
2142
      fprintf_plus(stderr, "Could not find a network interface\n");
 
2143
      exitcode = EXIT_FAILURE;
 
2144
      goto end;
 
2145
    }
 
2146
  }
 
2147
  
 
2148
  /* If we only got one interface, explicitly use only that one */
 
2149
  if(argz_count(interfaces, interfaces_size) == 1){
 
2150
    if(debug){
 
2151
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2152
                   interfaces);
 
2153
    }
 
2154
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
 
2155
  }
 
2156
  
 
2157
  /* Bring up interfaces which are down */
 
2158
  if(not (argz_count(interfaces, interfaces_size) == 1
 
2159
          and strcmp(interfaces, "none") == 0)){
 
2160
    char *interface = NULL;
 
2161
    while((interface = argz_next(interfaces, interfaces_size,
 
2162
                                 interface))){
 
2163
      bool interface_was_up = interface_is_up(interface);
 
2164
      ret = bring_up_interface(interface, delay);
 
2165
      if(not interface_was_up){
 
2166
        if(ret != 0){
 
2167
          errno = ret;
 
2168
          perror_plus("Failed to bring up interface");
 
2169
        } else {
 
2170
          ret_errno = argz_add(&interfaces_to_take_down,
 
2171
                               &interfaces_to_take_down_size,
 
2172
                               interface);
 
2173
        }
 
2174
      }
 
2175
    }
 
2176
    free(interfaces);
 
2177
    interfaces = NULL;
 
2178
    interfaces_size = 0;
 
2179
    if(debug and (interfaces_to_take_down == NULL)){
 
2180
      fprintf_plus(stderr, "No interfaces were brought up\n");
2019
2181
    }
2020
2182
  }
2021
2183
  
2062
2224
    /* Connect directly, do not use Zeroconf */
2063
2225
    /* (Mainly meant for debugging) */
2064
2226
    char *address = strrchr(connect_to, ':');
 
2227
    
2065
2228
    if(address == NULL){
2066
2229
      fprintf_plus(stderr, "No colon in address\n");
2067
2230
      exitcode = EX_USAGE;
2072
2235
      goto end;
2073
2236
    }
2074
2237
    
2075
 
    uint16_t port;
 
2238
    in_port_t port;
2076
2239
    errno = 0;
2077
2240
    tmpmax = strtoimax(address+1, &tmp, 10);
2078
2241
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2079
 
       or tmpmax != (uint16_t)tmpmax){
 
2242
       or tmpmax != (in_port_t)tmpmax){
2080
2243
      fprintf_plus(stderr, "Bad port number\n");
2081
2244
      exitcode = EX_USAGE;
2082
2245
      goto end;
2086
2249
      goto end;
2087
2250
    }
2088
2251
    
2089
 
    port = (uint16_t)tmpmax;
 
2252
    port = (in_port_t)tmpmax;
2090
2253
    *address = '\0';
2091
2254
    /* Colon in address indicates IPv6 */
2092
2255
    int af;
2140
2303
    config.publish_domain = 0;
2141
2304
    
2142
2305
    /* Allocate a new server */
2143
 
    mc.server = avahi_server_new(avahi_simple_poll_get
2144
 
                                 (mc.simple_poll), &config, NULL,
2145
 
                                 NULL, &error);
 
2306
    mc.server = avahi_server_new(avahi_simple_poll_get(simple_poll),
 
2307
                                 &config, NULL, NULL, &ret_errno);
2146
2308
    
2147
2309
    /* Free the Avahi configuration data */
2148
2310
    avahi_server_config_free(&config);
2151
2313
  /* Check if creating the Avahi server object succeeded */
2152
2314
  if(mc.server == NULL){
2153
2315
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2154
 
                 avahi_strerror(error));
 
2316
                 avahi_strerror(ret_errno));
2155
2317
    exitcode = EX_UNAVAILABLE;
2156
2318
    goto end;
2157
2319
  }
2181
2343
    fprintf_plus(stderr, "Starting Avahi loop search\n");
2182
2344
  }
2183
2345
 
2184
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
2346
  ret = avahi_loop_with_timeout(simple_poll,
2185
2347
                                (int)(retry_interval * 1000));
2186
2348
  if(debug){
2187
2349
    fprintf_plus(stderr, "avahi_loop_with_timeout exited %s\n",
2201
2363
  if(mc.server != NULL)
2202
2364
    avahi_server_free(mc.server);
2203
2365
  
2204
 
  if(mc.simple_poll != NULL)
2205
 
    avahi_simple_poll_free(mc.simple_poll);
 
2366
  if(simple_poll != NULL)
 
2367
    avahi_simple_poll_free(simple_poll);
2206
2368
  
2207
2369
  if(gnutls_initialized){
2208
2370
    gnutls_certificate_free_credentials(mc.cred);
2225
2387
    }
2226
2388
  }
2227
2389
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
2230
 
  
2231
2390
  /* Re-raise priviliges */
2232
2391
  {
2233
 
    errno = 0;
2234
 
    ret = seteuid(0);
2235
 
    if(ret == -1){
2236
 
      perror_plus("seteuid");
2237
 
    }
2238
 
    
2239
 
    /* Take down the network interface */
2240
 
    if(take_down_interface and geteuid() == 0){
2241
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2242
 
      if(ret == -1){
2243
 
        perror_plus("ioctl SIOCGIFFLAGS");
2244
 
      } else if(network.ifr_flags & IFF_UP){
2245
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2246
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
2247
 
        if(ret == -1){
2248
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
2392
    raise_privileges();
 
2393
    
 
2394
    /* Run network hooks */
 
2395
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2396
                      interfaces_hooks : "", delay);
 
2397
    
 
2398
    /* Take down the network interfaces which were brought up */
 
2399
    {
 
2400
      char *interface = NULL;
 
2401
      while((interface=argz_next(interfaces_to_take_down,
 
2402
                                 interfaces_to_take_down_size,
 
2403
                                 interface))){
 
2404
        ret_errno = take_down_interface(interface);
 
2405
        if(ret_errno != 0){
 
2406
          errno = ret_errno;
 
2407
          perror_plus("Failed to take down interface");
2249
2408
        }
2250
2409
      }
2251
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2252
 
      if(ret == -1){
2253
 
        perror_plus("close");
 
2410
      if(debug and (interfaces_to_take_down == NULL)){
 
2411
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2412
                     " down\n");
2254
2413
      }
2255
2414
    }
2256
 
  }
2257
 
  /* Lower privileges permanently */
2258
 
  errno = 0;
2259
 
  ret = setuid(uid);
2260
 
  if(ret == -1){
2261
 
    perror_plus("setuid");
2262
 
  }
 
2415
    
 
2416
    lower_privileges_permanently();
 
2417
  }
 
2418
  
 
2419
  free(interfaces_to_take_down);
 
2420
  free(interfaces_hooks);
2263
2421
  
2264
2422
  /* Removes the GPGME temp directory and all files inside */
2265
2423
  if(tempdir_created){