/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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-15 17:18:34 UTC
  • mto: (301.1.1 release) (237.7.272 trunk)
  • mto: This revision was merged to the branch mainline in revision 302.
  • Revision ID: teddy@recompile.se-20120615171834-gzzgknth003j903u
* plugins.d/mandos-client.c (main): Bug fix: Set DEVICE environment
                                    variable correctly for network
                                    hooks.  Also, don't call
                                    run_network_hooks() with NULL
                                    value.

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;
170
175
 
171
176
/* Function to use when printing errors */
172
177
void perror_plus(const char *print_text){
 
178
  int e = errno;
173
179
  fprintf(stderr, "Mandos plugin %s: ",
174
180
          program_invocation_short_name);
 
181
  errno = e;
175
182
  perror(print_text);
176
183
}
177
184
 
 
185
__attribute__((format (gnu_printf, 2, 3)))
178
186
int fprintf_plus(FILE *stream, const char *format, ...){
179
187
  va_list ap;
180
188
  va_start (ap, format);
202
210
}
203
211
 
204
212
/* 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){
 
213
bool add_server(const char *ip, in_port_t port, AvahiIfIndex if_index,
 
214
                int af){
207
215
  int ret;
208
216
  server *new_server = malloc(sizeof(server));
209
217
  if(new_server == NULL){
210
218
    perror_plus("malloc");
211
 
    return -1;
 
219
    return false;
212
220
  }
213
221
  *new_server = (server){ .ip = strdup(ip),
214
222
                          .port = port,
216
224
                          .af = af };
217
225
  if(new_server->ip == NULL){
218
226
    perror_plus("strdup");
219
 
    return -1;
 
227
    return false;
220
228
  }
221
229
  /* Special case of first server */
222
230
  if (mc.current_server == NULL){
233
241
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
242
  if(ret == -1){
235
243
    perror_plus("clock_gettime");
236
 
    return -1;
 
244
    return false;
237
245
  }
238
 
  return 0;
 
246
  return true;
239
247
}
240
248
 
241
249
/* 
462
470
}
463
471
 
464
472
static const char * safer_gnutls_strerror(int value){
465
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
466
 
                                               -Wunreachable-code */
 
473
  const char *ret = gnutls_strerror(value);
467
474
  if(ret == NULL)
468
475
    ret = "(unknown)";
469
476
  return ret;
614
621
                      __attribute__((unused)) const char *txt){}
615
622
 
616
623
/* Called when a Mandos server is found */
617
 
static int start_mandos_communication(const char *ip, uint16_t port,
 
624
static int start_mandos_communication(const char *ip, in_port_t port,
618
625
                                      AvahiIfIndex if_index,
619
626
                                      int af){
620
627
  int ret, tcp_sd = -1;
659
666
  
660
667
  if(debug){
661
668
    fprintf_plus(stderr, "Setting up a TCP connection to %s, port %"
662
 
                 PRIu16 "\n", ip, port);
 
669
                 PRIuMAX "\n", ip, (uintmax_t)port);
663
670
  }
664
671
  
665
672
  tcp_sd = socket(pf, SOCK_STREAM, 0);
696
703
    goto mandos_end;
697
704
  }
698
705
  if(af == AF_INET6){
699
 
    to.in6.sin6_port = htons(port); /* Spurious warnings from
700
 
                                       -Wconversion and
701
 
                                       -Wunreachable-code */
702
 
    
 
706
    to.in6.sin6_port = htons(port);    
703
707
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
704
708
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
705
709
                                -Wunreachable-code*/
729
733
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
730
734
        perror_plus("if_indextoname");
731
735
      } else {
732
 
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIu16
733
 
                     "\n", ip, interface, port);
 
736
        fprintf_plus(stderr, "Connection to: %s%%%s, port %" PRIuMAX
 
737
                     "\n", ip, interface, (uintmax_t)port);
734
738
      }
735
739
    } else {
736
 
      fprintf_plus(stderr, "Connection to: %s, port %" PRIu16 "\n",
737
 
                   ip, port);
 
740
      fprintf_plus(stderr, "Connection to: %s, port %" PRIuMAX "\n",
 
741
                   ip, (uintmax_t)port);
738
742
    }
739
743
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
740
744
                 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
818
822
    goto mandos_end;
819
823
  }
820
824
  
821
 
  /* Spurious warning from -Wint-to-pointer-cast */
822
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
825
  /* This casting via intptr_t is to eliminate warning about casting
 
826
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
827
     function "set-session-transport-fd!" does it. */
 
828
  gnutls_transport_set_ptr(session,
 
829
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
823
830
  
824
831
  if(quit_now){
825
832
    errno = EINTR;
998
1005
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
999
1006
                             flags,
1000
1007
                             AVAHI_GCC_UNUSED void* userdata){
1001
 
  assert(r);
 
1008
  if(r == NULL){
 
1009
    return;
 
1010
  }
1002
1011
  
1003
1012
  /* Called whenever a service has been resolved successfully or
1004
1013
     timed out */
1025
1034
                     PRIdMAX ") on port %" PRIu16 "\n", name,
1026
1035
                     host_name, ip, (intmax_t)interface, port);
1027
1036
      }
1028
 
      int ret = start_mandos_communication(ip, port, interface,
 
1037
      int ret = start_mandos_communication(ip, (in_port_t)port,
 
1038
                                           interface,
1029
1039
                                           avahi_proto_to_af(proto));
1030
1040
      if(ret == 0){
1031
1041
        avahi_simple_poll_quit(mc.simple_poll);
1032
1042
      } else {
1033
 
        ret = add_server(ip, port, interface,
1034
 
                         avahi_proto_to_af(proto));
 
1043
        if(not add_server(ip, (in_port_t)port, interface,
 
1044
                          avahi_proto_to_af(proto))){
 
1045
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1046
                       " list\n", name);
 
1047
        }
1035
1048
      }
1036
1049
    }
1037
1050
  }
1048
1061
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
1049
1062
                            flags,
1050
1063
                            AVAHI_GCC_UNUSED void* userdata){
1051
 
  assert(b);
 
1064
  if(b == NULL){
 
1065
    return;
 
1066
  }
1052
1067
  
1053
1068
  /* Called whenever a new services becomes available on the LAN or
1054
1069
     is removed from the LAN */
1110
1125
 
1111
1126
bool get_flags(const char *ifname, struct ifreq *ifr){
1112
1127
  int ret;
 
1128
  error_t ret_errno;
1113
1129
  
1114
1130
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1115
1131
  if(s < 0){
 
1132
    ret_errno = errno;
1116
1133
    perror_plus("socket");
 
1134
    errno = ret_errno;
1117
1135
    return false;
1118
1136
  }
1119
1137
  strcpy(ifr->ifr_name, ifname);
1120
1138
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1121
1139
  if(ret == -1){
1122
1140
    if(debug){
 
1141
      ret_errno = errno;
1123
1142
      perror_plus("ioctl SIOCGIFFLAGS");
 
1143
      errno = ret_errno;
1124
1144
    }
1125
1145
    return false;
1126
1146
  }
1195
1215
}
1196
1216
 
1197
1217
/* 
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;
 
1218
 * This function determines if a network interface is up.
 
1219
 */
 
1220
bool interface_is_up(const char *interface){
 
1221
  struct ifreq ifr;
 
1222
  if(not get_flags(interface, &ifr)){
 
1223
    if(debug){
 
1224
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1225
                   "\"%s\"\n", interface);
 
1226
    }
 
1227
    return false;
 
1228
  }
 
1229
  
 
1230
  return (bool)(ifr.ifr_flags & IFF_UP);
 
1231
}
 
1232
 
 
1233
/* 
 
1234
 * This function determines if a network interface is running
 
1235
 */
 
1236
bool interface_is_running(const char *interface){
 
1237
  struct ifreq ifr;
 
1238
  if(not get_flags(interface, &ifr)){
 
1239
    if(debug){
 
1240
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1241
                   "\"%s\"\n", interface);
 
1242
    }
 
1243
    return false;
 
1244
  }
 
1245
  
 
1246
  return (bool)(ifr.ifr_flags & IFF_RUNNING);
1238
1247
}
1239
1248
 
1240
1249
int notdotentries(const struct dirent *direntry){
1381
1390
  }
1382
1391
}
1383
1392
 
 
1393
/* Set effective uid to 0, return errno */
 
1394
error_t raise_privileges(void){
 
1395
  error_t old_errno = errno;
 
1396
  error_t ret_errno = 0;
 
1397
  if(seteuid(0) == -1){
 
1398
    ret_errno = errno;
 
1399
    perror_plus("seteuid");
 
1400
  }
 
1401
  errno = old_errno;
 
1402
  return ret_errno;
 
1403
}
 
1404
 
 
1405
/* Set effective and real user ID to 0.  Return errno. */
 
1406
error_t raise_privileges_permanently(void){
 
1407
  error_t old_errno = errno;
 
1408
  error_t ret_errno = raise_privileges();
 
1409
  if(ret_errno != 0){
 
1410
    errno = old_errno;
 
1411
    return ret_errno;
 
1412
  }
 
1413
  if(setuid(0) == -1){
 
1414
    ret_errno = errno;
 
1415
    perror_plus("seteuid");
 
1416
  }
 
1417
  errno = old_errno;
 
1418
  return ret_errno;
 
1419
}
 
1420
 
 
1421
/* Set effective user ID to unprivileged saved user ID */
 
1422
error_t lower_privileges(void){
 
1423
  error_t old_errno = errno;
 
1424
  error_t ret_errno = 0;
 
1425
  if(seteuid(uid) == -1){
 
1426
    ret_errno = errno;
 
1427
    perror_plus("seteuid");
 
1428
  }
 
1429
  errno = old_errno;
 
1430
  return ret_errno;
 
1431
}
 
1432
 
 
1433
/* Lower privileges permanently */
 
1434
error_t lower_privileges_permanently(void){
 
1435
  error_t old_errno = errno;
 
1436
  error_t ret_errno = 0;
 
1437
  if(setuid(uid) == -1){
 
1438
    ret_errno = errno;
 
1439
    perror_plus("setuid");
 
1440
  }
 
1441
  errno = old_errno;
 
1442
  return ret_errno;
 
1443
}
 
1444
 
1384
1445
bool run_network_hooks(const char *mode, const char *interface,
1385
1446
                       const float delay){
1386
1447
  struct dirent **direntries;
1389
1450
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1390
1451
                         alphasort);
1391
1452
  if(numhooks == -1){
1392
 
    perror_plus("scandir");
 
1453
    if(errno == ENOENT){
 
1454
      if(debug){
 
1455
        fprintf_plus(stderr, "Network hook directory \"%s\" not"
 
1456
                     " found\n", hookdir);
 
1457
      }
 
1458
    } else {
 
1459
      perror_plus("scandir");
 
1460
    }
1393
1461
  } else {
1394
1462
    int devnull = open("/dev/null", O_RDONLY);
1395
1463
    for(int i = 0; i < numhooks; i++){
1408
1476
      if(hook_pid == 0){
1409
1477
        /* Child */
1410
1478
        /* 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
 
        }
 
1479
        raise_privileges_permanently();
1422
1480
        /* Set group */
1423
1481
        errno = 0;
1424
1482
        ret = setgid(0);
1444
1502
          perror_plus("setenv");
1445
1503
          _exit(EX_OSERR);
1446
1504
        }
1447
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1505
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1448
1506
        if(ret == -1){
1449
1507
          perror_plus("setenv");
1450
1508
          _exit(EX_OSERR);
1467
1525
          _exit(EX_OSERR);
1468
1526
        }
1469
1527
        free(delaystring);
1470
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1471
 
        perror_plus("execl");
 
1528
        if(connect_to != NULL){
 
1529
          ret = setenv("CONNECT", connect_to, 1);
 
1530
          if(ret == -1){
 
1531
            perror_plus("setenv");
 
1532
            _exit(EX_OSERR);
 
1533
          }
 
1534
        }
 
1535
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1536
          perror_plus("execl");
 
1537
          _exit(EXIT_FAILURE);
 
1538
        }
1472
1539
      } else {
1473
1540
        int status;
1474
1541
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1508
1575
  return true;
1509
1576
}
1510
1577
 
 
1578
error_t bring_up_interface(const char *const interface,
 
1579
                           const float delay){
 
1580
  int sd = -1;
 
1581
  error_t old_errno = errno;
 
1582
  error_t ret_errno = 0;
 
1583
  int ret, ret_setflags;
 
1584
  struct ifreq network;
 
1585
  unsigned int if_index = if_nametoindex(interface);
 
1586
  if(if_index == 0){
 
1587
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1588
    errno = old_errno;
 
1589
    return ENXIO;
 
1590
  }
 
1591
  
 
1592
  if(quit_now){
 
1593
    errno = old_errno;
 
1594
    return EINTR;
 
1595
  }
 
1596
  
 
1597
  if(not interface_is_up(interface)){
 
1598
    if(not get_flags(interface, &network) and debug){
 
1599
      ret_errno = errno;
 
1600
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1601
                   "\"%s\"\n", interface);
 
1602
      return ret_errno;
 
1603
    }
 
1604
    network.ifr_flags |= IFF_UP;
 
1605
    
 
1606
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1607
    if(sd < 0){
 
1608
      ret_errno = errno;
 
1609
      perror_plus("socket");
 
1610
      errno = old_errno;
 
1611
      return ret_errno;
 
1612
    }
 
1613
  
 
1614
    if(quit_now){
 
1615
      close(sd);
 
1616
      errno = old_errno;
 
1617
      return EINTR;
 
1618
    }
 
1619
    
 
1620
    if(debug){
 
1621
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
 
1622
                   interface);
 
1623
    }
 
1624
    
 
1625
    /* Raise priviliges */
 
1626
    raise_privileges();
 
1627
    
 
1628
#ifdef __linux__
 
1629
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1630
       messages about the network interface to mess up the prompt */
 
1631
    int ret_linux = klogctl(8, NULL, 5);
 
1632
    bool restore_loglevel = true;
 
1633
    if(ret_linux == -1){
 
1634
      restore_loglevel = false;
 
1635
      perror_plus("klogctl");
 
1636
    }
 
1637
#endif  /* __linux__ */
 
1638
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1639
    ret_errno = errno;
 
1640
#ifdef __linux__
 
1641
    if(restore_loglevel){
 
1642
      ret_linux = klogctl(7, NULL, 0);
 
1643
      if(ret_linux == -1){
 
1644
        perror_plus("klogctl");
 
1645
      }
 
1646
    }
 
1647
#endif  /* __linux__ */
 
1648
    
 
1649
    /* Lower privileges */
 
1650
    lower_privileges();
 
1651
    
 
1652
    /* Close the socket */
 
1653
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1654
    if(ret == -1){
 
1655
      perror_plus("close");
 
1656
    }
 
1657
    
 
1658
    if(ret_setflags == -1){
 
1659
      errno = ret_errno;
 
1660
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1661
      errno = old_errno;
 
1662
      return ret_errno;
 
1663
    }
 
1664
  } else if(debug){
 
1665
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
 
1666
                 interface);
 
1667
  }
 
1668
  
 
1669
  /* Sleep checking until interface is running.
 
1670
     Check every 0.25s, up to total time of delay */
 
1671
  for(int i=0; i < delay * 4; i++){
 
1672
    if(interface_is_running(interface)){
 
1673
      break;
 
1674
    }
 
1675
    struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1676
    ret = nanosleep(&sleeptime, NULL);
 
1677
    if(ret == -1 and errno != EINTR){
 
1678
      perror_plus("nanosleep");
 
1679
    }
 
1680
  }
 
1681
  
 
1682
  errno = old_errno;
 
1683
  return 0;
 
1684
}
 
1685
 
 
1686
error_t take_down_interface(const char *const interface){
 
1687
  int sd = -1;
 
1688
  error_t old_errno = errno;
 
1689
  error_t ret_errno = 0;
 
1690
  int ret, ret_setflags;
 
1691
  struct ifreq network;
 
1692
  unsigned int if_index = if_nametoindex(interface);
 
1693
  if(if_index == 0){
 
1694
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1695
    errno = old_errno;
 
1696
    return ENXIO;
 
1697
  }
 
1698
  if(interface_is_up(interface)){
 
1699
    if(not get_flags(interface, &network) and debug){
 
1700
      ret_errno = errno;
 
1701
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1702
                   "\"%s\"\n", interface);
 
1703
      return ret_errno;
 
1704
    }
 
1705
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1706
    
 
1707
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1708
    if(sd < 0){
 
1709
      ret_errno = errno;
 
1710
      perror_plus("socket");
 
1711
      errno = old_errno;
 
1712
      return ret_errno;
 
1713
    }
 
1714
    
 
1715
    if(debug){
 
1716
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
 
1717
                   interface);
 
1718
    }
 
1719
    
 
1720
    /* Raise priviliges */
 
1721
    raise_privileges();
 
1722
    
 
1723
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
 
1724
    ret_errno = errno;
 
1725
    
 
1726
    /* Lower privileges */
 
1727
    lower_privileges();
 
1728
    
 
1729
    /* Close the socket */
 
1730
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1731
    if(ret == -1){
 
1732
      perror_plus("close");
 
1733
    }
 
1734
    
 
1735
    if(ret_setflags == -1){
 
1736
      errno = ret_errno;
 
1737
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1738
      errno = old_errno;
 
1739
      return ret_errno;
 
1740
    }
 
1741
  } else if(debug){
 
1742
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
 
1743
                 interface);
 
1744
  }
 
1745
  
 
1746
  errno = old_errno;
 
1747
  return 0;
 
1748
}
 
1749
 
1511
1750
int main(int argc, char *argv[]){
1512
1751
  AvahiSServiceBrowser *sb = NULL;
1513
 
  int error;
 
1752
  error_t ret_errno;
1514
1753
  int ret;
1515
1754
  intmax_t tmpmax;
1516
1755
  char *tmp;
1517
1756
  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;
 
1757
  char *interfaces = NULL;
 
1758
  size_t interfaces_size = 0;
 
1759
  char *interfaces_to_take_down = NULL;
 
1760
  size_t interfaces_to_take_down_size = 0;
1524
1761
  char tempdir[] = "/tmp/mandosXXXXXX";
1525
1762
  bool tempdir_created = false;
1526
1763
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1527
1764
  const char *seckey = PATHDIR "/" SECKEY;
1528
1765
  const char *pubkey = PATHDIR "/" PUBKEY;
 
1766
  char *interfaces_hooks = NULL;
 
1767
  size_t interfaces_hooks_size = 0;
1529
1768
  
1530
1769
  bool gnutls_initialized = false;
1531
1770
  bool gpgme_initialized = false;
1593
1832
        .group = 2 },
1594
1833
      { .name = "retry", .key = 132,
1595
1834
        .arg = "SECONDS",
1596
 
        .doc = "Retry interval used when denied by the mandos server",
 
1835
        .doc = "Retry interval used when denied by the Mandos server",
1597
1836
        .group = 2 },
1598
1837
      { .name = "network-hook-dir", .key = 133,
1599
1838
        .arg = "DIR",
1622
1861
        connect_to = arg;
1623
1862
        break;
1624
1863
      case 'i':                 /* --interface */
1625
 
        interface = arg;
 
1864
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
 
1865
                                 (int)',');
 
1866
        if(ret_errno != 0){
 
1867
          argp_error(state, "%s", strerror(ret_errno));
 
1868
        }
1626
1869
        break;
1627
1870
      case 's':                 /* --seckey */
1628
1871
        seckey = arg;
1671
1914
        argp_state_help(state, state->out_stream,
1672
1915
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1916
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
1917
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1677
1918
        exit(argp_err_exit_status);
1678
1919
        break;
1708
1949
       <http://bugs.debian.org/633582> */
1709
1950
    
1710
1951
    /* Re-raise priviliges */
1711
 
    errno = 0;
1712
 
    ret = seteuid(0);
1713
 
    if(ret == -1){
1714
 
      perror_plus("seteuid");
1715
 
    } else {
 
1952
    if(raise_privileges() == 0){
1716
1953
      struct stat st;
1717
1954
      
1718
1955
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1766
2003
    }
1767
2004
  }
1768
2005
  
 
2006
  /* Remove empty interface names */
 
2007
  {
 
2008
    char *interface = NULL;
 
2009
    while((interface = argz_next(interfaces, interfaces_size,
 
2010
                                 interface))){
 
2011
      if(if_nametoindex(interface) == 0){
 
2012
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
 
2013
          fprintf_plus(stderr, "Not using nonexisting interface"
 
2014
                       " \"%s\"\n", interface);
 
2015
        }
 
2016
        argz_delete(&interfaces, &interfaces_size, interface);
 
2017
        interface = NULL;
 
2018
      }
 
2019
    }
 
2020
  }
 
2021
  
1769
2022
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    goto end;
 
2023
  {
 
2024
    
 
2025
    if(interfaces != NULL){
 
2026
      interfaces_hooks = malloc(interfaces_size);
 
2027
      if(interfaces_hooks == NULL){
 
2028
        perror_plus("malloc");
 
2029
        goto end;
 
2030
      }
 
2031
      memcpy(interfaces_hooks, interfaces, interfaces_size);
 
2032
      interfaces_hooks_size = interfaces_size;
 
2033
      argz_stringify(interfaces_hooks, interfaces_hooks_size,
 
2034
                     (int)',');
 
2035
    }
 
2036
    if(not run_network_hooks("start", interfaces_hooks != NULL ?
 
2037
                             interfaces_hooks : "", delay)){
 
2038
      goto end;
 
2039
    }
1772
2040
  }
1773
2041
  
1774
2042
  if(not debug){
1775
2043
    avahi_set_log_function(empty_log);
1776
2044
  }
1777
2045
  
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
2046
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1811
2047
     from the signal handler */
1812
2048
  /* Initialize the pseudo-RNG for Avahi */
1882
2118
    }
1883
2119
  }
1884
2120
  
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");
 
2121
  /* If no interfaces were specified, make a list */
 
2122
  if(interfaces == NULL){
 
2123
    struct dirent **direntries;
 
2124
    /* Look for any good interfaces */
 
2125
    ret = scandir(sys_class_net, &direntries, good_interface,
 
2126
                  alphasort);
 
2127
    if(ret >= 1){
 
2128
      /* Add all found interfaces to interfaces list */
 
2129
      for(int i = 0; i < ret; ++i){
 
2130
        ret_errno = argz_add(&interfaces, &interfaces_size,
 
2131
                             direntries[i]->d_name);
 
2132
        if(ret_errno != 0){
 
2133
          perror_plus("argz_add");
 
2134
          continue;
 
2135
        }
 
2136
        if(debug){
 
2137
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
 
2138
                       direntries[i]->d_name);
 
2139
        }
 
2140
      }
 
2141
      free(direntries);
 
2142
    } else {
 
2143
      free(direntries);
 
2144
      fprintf_plus(stderr, "Could not find a network interface\n");
 
2145
      exitcode = EXIT_FAILURE;
 
2146
      goto end;
 
2147
    }
 
2148
  }
 
2149
  
 
2150
  /* If we only got one interface, explicitly use only that one */
 
2151
  if(argz_count(interfaces, interfaces_size) == 1){
 
2152
    if(debug){
 
2153
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
 
2154
                   interfaces);
 
2155
    }
 
2156
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
 
2157
  }
 
2158
  
 
2159
  /* Bring up interfaces which are down */
 
2160
  if(not (argz_count(interfaces, interfaces_size) == 1
 
2161
          and strcmp(interfaces, "none") == 0)){
 
2162
    char *interface = NULL;
 
2163
    while((interface = argz_next(interfaces, interfaces_size,
 
2164
                                 interface))){
 
2165
      bool interface_was_up = interface_is_up(interface);
 
2166
      ret = bring_up_interface(interface, delay);
 
2167
      if(not interface_was_up){
 
2168
        if(ret != 0){
 
2169
          errno = ret;
 
2170
          perror_plus("Failed to bring up interface");
 
2171
        } else {
 
2172
          ret_errno = argz_add(&interfaces_to_take_down,
 
2173
                               &interfaces_to_take_down_size,
 
2174
                               interface);
 
2175
        }
 
2176
      }
 
2177
    }
 
2178
    free(interfaces);
 
2179
    interfaces = NULL;
 
2180
    interfaces_size = 0;
 
2181
    if(debug and (interfaces_to_take_down == NULL)){
 
2182
      fprintf_plus(stderr, "No interfaces were brought up\n");
2019
2183
    }
2020
2184
  }
2021
2185
  
2062
2226
    /* Connect directly, do not use Zeroconf */
2063
2227
    /* (Mainly meant for debugging) */
2064
2228
    char *address = strrchr(connect_to, ':');
 
2229
    
2065
2230
    if(address == NULL){
2066
2231
      fprintf_plus(stderr, "No colon in address\n");
2067
2232
      exitcode = EX_USAGE;
2072
2237
      goto end;
2073
2238
    }
2074
2239
    
2075
 
    uint16_t port;
 
2240
    in_port_t port;
2076
2241
    errno = 0;
2077
2242
    tmpmax = strtoimax(address+1, &tmp, 10);
2078
2243
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2079
 
       or tmpmax != (uint16_t)tmpmax){
 
2244
       or tmpmax != (in_port_t)tmpmax){
2080
2245
      fprintf_plus(stderr, "Bad port number\n");
2081
2246
      exitcode = EX_USAGE;
2082
2247
      goto end;
2086
2251
      goto end;
2087
2252
    }
2088
2253
    
2089
 
    port = (uint16_t)tmpmax;
 
2254
    port = (in_port_t)tmpmax;
2090
2255
    *address = '\0';
2091
2256
    /* Colon in address indicates IPv6 */
2092
2257
    int af;
2142
2307
    /* Allocate a new server */
2143
2308
    mc.server = avahi_server_new(avahi_simple_poll_get
2144
2309
                                 (mc.simple_poll), &config, NULL,
2145
 
                                 NULL, &error);
 
2310
                                 NULL, &ret_errno);
2146
2311
    
2147
2312
    /* Free the Avahi configuration data */
2148
2313
    avahi_server_config_free(&config);
2151
2316
  /* Check if creating the Avahi server object succeeded */
2152
2317
  if(mc.server == NULL){
2153
2318
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2154
 
                 avahi_strerror(error));
 
2319
                 avahi_strerror(ret_errno));
2155
2320
    exitcode = EX_UNAVAILABLE;
2156
2321
    goto end;
2157
2322
  }
2225
2390
    }
2226
2391
  }
2227
2392
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
2230
 
  
2231
2393
  /* Re-raise priviliges */
2232
2394
  {
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");
 
2395
    raise_privileges();
 
2396
    
 
2397
    /* Run network hooks */
 
2398
    run_network_hooks("stop", interfaces_hooks != NULL ?
 
2399
                      interfaces_hooks : "", delay);
 
2400
    
 
2401
    /* Take down the network interfaces which were brought up */
 
2402
    {
 
2403
      char *interface = NULL;
 
2404
      while((interface=argz_next(interfaces_to_take_down,
 
2405
                                 interfaces_to_take_down_size,
 
2406
                                 interface))){
 
2407
        ret_errno = take_down_interface(interface);
 
2408
        if(ret_errno != 0){
 
2409
          errno = ret_errno;
 
2410
          perror_plus("Failed to take down interface");
2249
2411
        }
2250
2412
      }
2251
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2252
 
      if(ret == -1){
2253
 
        perror_plus("close");
 
2413
      if(debug and (interfaces_to_take_down == NULL)){
 
2414
        fprintf_plus(stderr, "No interfaces needed to be taken"
 
2415
                     " down\n");
2254
2416
      }
2255
2417
    }
2256
 
  }
2257
 
  /* Lower privileges permanently */
2258
 
  errno = 0;
2259
 
  ret = setuid(uid);
2260
 
  if(ret == -1){
2261
 
    perror_plus("setuid");
2262
 
  }
 
2418
    
 
2419
    lower_privileges_permanently();
 
2420
  }
 
2421
  
 
2422
  free(interfaces_to_take_down);
 
2423
  free(interfaces_hooks);
2263
2424
  
2264
2425
  /* Removes the GPGME temp directory and all files inside */
2265
2426
  if(tempdir_created){