/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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-2012 Teddy Hogeborn
13
 
 * Copyright © 2008-2012 Björn Påhlsson
 
12
 * Copyright © 2008-2011 Teddy Hogeborn
 
13
 * Copyright © 2008-2011 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, intptr_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit() */
 
76
                                   setgid(), pause() */
77
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
78
78
#include <iso646.h>             /* not, or, and */
79
79
#include <argp.h>               /* struct argp_option, error_t, struct
87
87
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
88
88
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
89
89
                                   WEXITSTATUS(), WTERMSIG() */
90
 
#include <grp.h>                /* setgroups() */
91
 
#include <argz.h>               /* argz_add_sep(), argz_next(),
92
 
                                   argz_delete(), argz_append(),
93
 
                                   argz_stringify(), argz_add(),
94
 
                                   argz_count() */
95
90
 
96
91
#ifdef __linux__
97
92
#include <sys/klog.h>           /* klogctl() */
139
134
static const char sys_class_net[] = "/sys/class/net";
140
135
char *connect_to = NULL;
141
136
const char *hookdir = HOOKDIR;
142
 
uid_t uid = 65534;
143
 
gid_t gid = 65534;
144
137
 
145
138
/* Doubly linked list that need to be circularly linked when used */
146
139
typedef struct server{
176
169
 
177
170
/* Function to use when printing errors */
178
171
void perror_plus(const char *print_text){
179
 
  int e = errno;
180
172
  fprintf(stderr, "Mandos plugin %s: ",
181
173
          program_invocation_short_name);
182
 
  errno = e;
183
174
  perror(print_text);
184
175
}
185
176
 
186
 
__attribute__((format (gnu_printf, 2, 3)))
187
177
int fprintf_plus(FILE *stream, const char *format, ...){
188
178
  va_list ap;
189
179
  va_start (ap, format);
211
201
}
212
202
 
213
203
/* Add server to set of servers to retry periodically */
214
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
215
 
                int af){
 
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
205
               int af){
216
206
  int ret;
217
207
  server *new_server = malloc(sizeof(server));
218
208
  if(new_server == NULL){
219
209
    perror_plus("malloc");
220
 
    return false;
 
210
    return -1;
221
211
  }
222
212
  *new_server = (server){ .ip = strdup(ip),
223
213
                          .port = port,
225
215
                          .af = af };
226
216
  if(new_server->ip == NULL){
227
217
    perror_plus("strdup");
228
 
    return false;
 
218
    return -1;
229
219
  }
230
220
  /* Special case of first server */
231
221
  if (mc.current_server == NULL){
242
232
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
243
233
  if(ret == -1){
244
234
    perror_plus("clock_gettime");
245
 
    return false;
 
235
    return -1;
246
236
  }
247
 
  return true;
 
237
  return 0;
248
238
}
249
239
 
250
240
/* 
827
817
    goto mandos_end;
828
818
  }
829
819
  
830
 
  /* This casting via intptr_t is to eliminate warning about casting
831
 
     an int to a pointer type.  This is exactly how the GnuTLS Guile
832
 
     function "set-session-transport-fd!" does it. */
833
 
  gnutls_transport_set_ptr(session,
834
 
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
 
820
  /* Spurious warning from -Wint-to-pointer-cast */
 
821
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
835
822
  
836
823
  if(quit_now){
837
824
    errno = EINTR;
1042
1029
      if(ret == 0){
1043
1030
        avahi_simple_poll_quit(mc.simple_poll);
1044
1031
      } else {
1045
 
        if(not add_server(ip, port, interface,
1046
 
                          avahi_proto_to_af(proto))){
1047
 
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1048
 
                       " list\n", name);
1049
 
        }
 
1032
        ret = add_server(ip, port, interface,
 
1033
                         avahi_proto_to_af(proto));
1050
1034
      }
1051
1035
    }
1052
1036
  }
1125
1109
 
1126
1110
bool get_flags(const char *ifname, struct ifreq *ifr){
1127
1111
  int ret;
1128
 
  error_t ret_errno;
1129
1112
  
1130
1113
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1131
1114
  if(s < 0){
1132
 
    ret_errno = errno;
1133
1115
    perror_plus("socket");
1134
 
    errno = ret_errno;
1135
1116
    return false;
1136
1117
  }
1137
1118
  strcpy(ifr->ifr_name, ifname);
1138
1119
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
1139
1120
  if(ret == -1){
1140
1121
    if(debug){
1141
 
      ret_errno = errno;
1142
1122
      perror_plus("ioctl SIOCGIFFLAGS");
1143
 
      errno = ret_errno;
1144
1123
    }
1145
1124
    return false;
1146
1125
  }
1215
1194
}
1216
1195
 
1217
1196
/* 
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);
 
1197
 * This function determines if a directory entry in /sys/class/net
 
1198
 * corresponds to an acceptable network device which is up.
 
1199
 * (This function is passed to scandir(3) as a filter function.)
 
1200
 */
 
1201
int up_interface(const struct dirent *if_entry){
 
1202
  if(if_entry->d_name[0] == '.'){
 
1203
    return 0;
 
1204
  }
 
1205
  
 
1206
  struct ifreq ifr;
 
1207
  if(not get_flags(if_entry->d_name, &ifr)){
 
1208
    if(debug){
 
1209
      fprintf_plus(stderr, "Failed to get flags for interface "
 
1210
                   "\"%s\"\n", if_entry->d_name);
 
1211
    }
 
1212
    return 0;
 
1213
  }
 
1214
  
 
1215
  /* Reject down interfaces */
 
1216
  if(not (ifr.ifr_flags & IFF_UP)){
 
1217
    if(debug){
 
1218
      fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
 
1219
                   if_entry->d_name);
 
1220
    }
 
1221
    return 0;
 
1222
  }
 
1223
  
 
1224
  /* Reject non-running interfaces */
 
1225
  if(not (ifr.ifr_flags & IFF_RUNNING)){
 
1226
    if(debug){
 
1227
      fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
 
1228
                   if_entry->d_name);
 
1229
    }
 
1230
    return 0;
 
1231
  }
 
1232
  
 
1233
  if(not good_flags(if_entry->d_name, &ifr)){
 
1234
    return 0;
 
1235
  }
 
1236
  return 1;
1247
1237
}
1248
1238
 
1249
1239
int notdotentries(const struct dirent *direntry){
1311
1301
    }
1312
1302
    return 0;
1313
1303
  }
1314
 
  if(debug){
1315
 
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1316
 
                 direntry->d_name);
1317
 
  }
1318
1304
  return 1;
1319
1305
}
1320
1306
 
1390
1376
  }
1391
1377
}
1392
1378
 
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
 
 
1445
1379
bool run_network_hooks(const char *mode, const char *interface,
1446
1380
                       const float delay){
1447
1381
  struct dirent **direntries;
1454
1388
  } else {
1455
1389
    int devnull = open("/dev/null", O_RDONLY);
1456
1390
    for(int i = 0; i < numhooks; i++){
1457
 
      direntry = direntries[i];
 
1391
      direntry = direntries[0];
1458
1392
      char *fullname = NULL;
1459
1393
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1460
1394
      if(ret < 0){
1461
1395
        perror_plus("asprintf");
1462
1396
        continue;
1463
1397
      }
1464
 
      if(debug){
1465
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1466
 
                     direntry->d_name);
1467
 
      }
1468
1398
      pid_t hook_pid = fork();
1469
1399
      if(hook_pid == 0){
1470
1400
        /* Child */
1471
 
        /* Raise privileges */
1472
 
        raise_privileges_permanently();
1473
 
        /* Set group */
1474
 
        errno = 0;
1475
 
        ret = setgid(0);
1476
 
        if(ret == -1){
1477
 
          perror_plus("setgid");
1478
 
        }
1479
 
        /* Reset supplementary groups */
1480
 
        errno = 0;
1481
 
        ret = setgroups(0, NULL);
1482
 
        if(ret == -1){
1483
 
          perror_plus("setgroups");
1484
 
        }
1485
1401
        dup2(devnull, STDIN_FILENO);
1486
1402
        close(devnull);
1487
1403
        dup2(STDERR_FILENO, STDOUT_FILENO);
1488
1404
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1489
1405
        if(ret == -1){
1490
1406
          perror_plus("setenv");
1491
 
          _exit(EX_OSERR);
 
1407
          return false;
1492
1408
        }
1493
1409
        ret = setenv("DEVICE", interface, 1);
1494
1410
        if(ret == -1){
1495
1411
          perror_plus("setenv");
1496
 
          _exit(EX_OSERR);
 
1412
          return false;
1497
1413
        }
1498
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1414
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1499
1415
        if(ret == -1){
1500
1416
          perror_plus("setenv");
1501
 
          _exit(EX_OSERR);
 
1417
          return false;
1502
1418
        }
1503
1419
        ret = setenv("MODE", mode, 1);
1504
1420
        if(ret == -1){
1505
1421
          perror_plus("setenv");
1506
 
          _exit(EX_OSERR);
 
1422
          return false;
1507
1423
        }
1508
1424
        char *delaystring;
1509
1425
        ret = asprintf(&delaystring, "%f", delay);
1510
1426
        if(ret == -1){
1511
1427
          perror_plus("asprintf");
1512
 
          _exit(EX_OSERR);
 
1428
          return false;
1513
1429
        }
1514
1430
        ret = setenv("DELAY", delaystring, 1);
1515
1431
        if(ret == -1){
1516
1432
          free(delaystring);
1517
1433
          perror_plus("setenv");
1518
 
          _exit(EX_OSERR);
 
1434
          return false;
1519
1435
        }
1520
1436
        free(delaystring);
1521
 
        if(connect_to != NULL){
1522
 
          ret = setenv("CONNECT", connect_to, 1);
1523
 
          if(ret == -1){
1524
 
            perror_plus("setenv");
1525
 
            _exit(EX_OSERR);
1526
 
          }
1527
 
        }
1528
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1529
 
          perror_plus("execl");
1530
 
          _exit(EXIT_FAILURE);
1531
 
        }
 
1437
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1438
        perror_plus("execl");
1532
1439
      } else {
1533
1440
        int status;
1534
1441
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1558
1465
        }
1559
1466
      }
1560
1467
      free(fullname);
1561
 
      if(debug){
1562
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1563
 
                     direntry->d_name);
 
1468
      if(quit_now){
 
1469
        break;
1564
1470
      }
1565
1471
    }
1566
1472
    close(devnull);
1568
1474
  return true;
1569
1475
}
1570
1476
 
1571
 
error_t bring_up_interface(const char *const interface,
1572
 
                           const float delay){
1573
 
  int sd = -1;
1574
 
  error_t old_errno = errno;
1575
 
  error_t ret_errno = 0;
1576
 
  int ret, ret_setflags;
1577
 
  struct ifreq network;
1578
 
  unsigned int if_index = if_nametoindex(interface);
1579
 
  if(if_index == 0){
1580
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1581
 
    errno = old_errno;
1582
 
    return ENXIO;
1583
 
  }
1584
 
  
1585
 
  if(quit_now){
1586
 
    errno = old_errno;
1587
 
    return EINTR;
1588
 
  }
1589
 
  
1590
 
  if(not interface_is_up(interface)){
1591
 
    if(not get_flags(interface, &network) and debug){
1592
 
      ret_errno = errno;
1593
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1594
 
                   "\"%s\"\n", interface);
1595
 
      return ret_errno;
1596
 
    }
1597
 
    network.ifr_flags |= IFF_UP;
1598
 
    
1599
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1600
 
    if(sd < 0){
1601
 
      ret_errno = errno;
1602
 
      perror_plus("socket");
1603
 
      errno = old_errno;
1604
 
      return ret_errno;
1605
 
    }
1606
 
  
1607
 
    if(quit_now){
1608
 
      close(sd);
1609
 
      errno = old_errno;
1610
 
      return EINTR;
1611
 
    }
1612
 
    
1613
 
    if(debug){
1614
 
      fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1615
 
                   interface);
1616
 
    }
1617
 
    
1618
 
    /* Raise priviliges */
1619
 
    raise_privileges();
1620
 
    
1621
 
#ifdef __linux__
1622
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1623
 
       messages about the network interface to mess up the prompt */
1624
 
    int ret_linux = klogctl(8, NULL, 5);
1625
 
    bool restore_loglevel = true;
1626
 
    if(ret_linux == -1){
1627
 
      restore_loglevel = false;
1628
 
      perror_plus("klogctl");
1629
 
    }
1630
 
#endif  /* __linux__ */
1631
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1632
 
    ret_errno = errno;
1633
 
#ifdef __linux__
1634
 
    if(restore_loglevel){
1635
 
      ret_linux = klogctl(7, NULL, 0);
1636
 
      if(ret_linux == -1){
1637
 
        perror_plus("klogctl");
1638
 
      }
1639
 
    }
1640
 
#endif  /* __linux__ */
1641
 
    
1642
 
    /* Lower privileges */
1643
 
    lower_privileges();
1644
 
    
1645
 
    /* Close the socket */
1646
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1647
 
    if(ret == -1){
1648
 
      perror_plus("close");
1649
 
    }
1650
 
    
1651
 
    if(ret_setflags == -1){
1652
 
      errno = ret_errno;
1653
 
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1654
 
      errno = old_errno;
1655
 
      return ret_errno;
1656
 
    }
1657
 
  } else if(debug){
1658
 
    fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1659
 
                 interface);
1660
 
  }
1661
 
  
1662
 
  /* Sleep checking until interface is running.
1663
 
     Check every 0.25s, up to total time of delay */
1664
 
  for(int i=0; i < delay * 4; i++){
1665
 
    if(interface_is_running(interface)){
1666
 
      break;
1667
 
    }
1668
 
    struct timespec sleeptime = { .tv_nsec = 250000000 };
1669
 
    ret = nanosleep(&sleeptime, NULL);
1670
 
    if(ret == -1 and errno != EINTR){
1671
 
      perror_plus("nanosleep");
1672
 
    }
1673
 
  }
1674
 
  
1675
 
  errno = old_errno;
1676
 
  return 0;
1677
 
}
1678
 
 
1679
 
error_t take_down_interface(const char *const interface){
1680
 
  int sd = -1;
1681
 
  error_t old_errno = errno;
1682
 
  error_t ret_errno = 0;
1683
 
  int ret, ret_setflags;
1684
 
  struct ifreq network;
1685
 
  unsigned int if_index = if_nametoindex(interface);
1686
 
  if(if_index == 0){
1687
 
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1688
 
    errno = old_errno;
1689
 
    return ENXIO;
1690
 
  }
1691
 
  if(interface_is_up(interface)){
1692
 
    if(not get_flags(interface, &network) and debug){
1693
 
      ret_errno = errno;
1694
 
      fprintf_plus(stderr, "Failed to get flags for interface "
1695
 
                   "\"%s\"\n", interface);
1696
 
      return ret_errno;
1697
 
    }
1698
 
    network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1699
 
    
1700
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1701
 
    if(sd < 0){
1702
 
      ret_errno = errno;
1703
 
      perror_plus("socket");
1704
 
      errno = old_errno;
1705
 
      return ret_errno;
1706
 
    }
1707
 
    
1708
 
    if(debug){
1709
 
      fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1710
 
                   interface);
1711
 
    }
1712
 
    
1713
 
    /* Raise priviliges */
1714
 
    raise_privileges();
1715
 
    
1716
 
    ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1717
 
    ret_errno = errno;
1718
 
    
1719
 
    /* Lower privileges */
1720
 
    lower_privileges();
1721
 
    
1722
 
    /* Close the socket */
1723
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1724
 
    if(ret == -1){
1725
 
      perror_plus("close");
1726
 
    }
1727
 
    
1728
 
    if(ret_setflags == -1){
1729
 
      errno = ret_errno;
1730
 
      perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1731
 
      errno = old_errno;
1732
 
      return ret_errno;
1733
 
    }
1734
 
  } else if(debug){
1735
 
    fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1736
 
                 interface);
1737
 
  }
1738
 
  
1739
 
  errno = old_errno;
1740
 
  return 0;
1741
 
}
1742
 
 
1743
1477
int main(int argc, char *argv[]){
1744
1478
  AvahiSServiceBrowser *sb = NULL;
1745
 
  error_t ret_errno;
 
1479
  int error;
1746
1480
  int ret;
1747
1481
  intmax_t tmpmax;
1748
1482
  char *tmp;
1749
1483
  int exitcode = EXIT_SUCCESS;
1750
 
  char *interfaces = NULL;
1751
 
  size_t interfaces_size = 0;
1752
 
  char *interfaces_to_take_down = NULL;
1753
 
  size_t interfaces_to_take_down_size = 0;
 
1484
  const char *interface = "";
 
1485
  struct ifreq network;
 
1486
  int sd = -1;
 
1487
  bool take_down_interface = false;
 
1488
  uid_t uid;
 
1489
  gid_t gid;
1754
1490
  char tempdir[] = "/tmp/mandosXXXXXX";
1755
1491
  bool tempdir_created = false;
1756
1492
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1757
1493
  const char *seckey = PATHDIR "/" SECKEY;
1758
1494
  const char *pubkey = PATHDIR "/" PUBKEY;
1759
 
  char *interfaces_hooks = NULL;
1760
 
  size_t interfaces_hooks_size = 0;
1761
1495
  
1762
1496
  bool gnutls_initialized = false;
1763
1497
  bool gpgme_initialized = false;
1825
1559
        .group = 2 },
1826
1560
      { .name = "retry", .key = 132,
1827
1561
        .arg = "SECONDS",
1828
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1562
        .doc = "Retry interval used when denied by the mandos server",
1829
1563
        .group = 2 },
1830
1564
      { .name = "network-hook-dir", .key = 133,
1831
1565
        .arg = "DIR",
1854
1588
        connect_to = arg;
1855
1589
        break;
1856
1590
      case 'i':                 /* --interface */
1857
 
        ret_errno = argz_add_sep(&interfaces, &interfaces_size, arg,
1858
 
                                 (int)',');
1859
 
        if(ret_errno != 0){
1860
 
          argp_error(state, "%s", strerror(ret_errno));
1861
 
        }
 
1591
        interface = arg;
1862
1592
        break;
1863
1593
      case 's':                 /* --seckey */
1864
1594
        seckey = arg;
1907
1637
        argp_state_help(state, state->out_stream,
1908
1638
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1909
1639
      case 'V':                 /* --version */
 
1640
        fprintf_plus(state->out_stream,
 
1641
                     "Mandos plugin mandos-client: ");
1910
1642
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1911
1643
        exit(argp_err_exit_status);
1912
1644
        break;
1940
1672
  {
1941
1673
    /* Work around Debian bug #633582:
1942
1674
       <http://bugs.debian.org/633582> */
 
1675
    struct stat st;
1943
1676
    
1944
1677
    /* Re-raise priviliges */
1945
 
    if(raise_privileges() == 0){
1946
 
      struct stat st;
1947
 
      
1948
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1949
 
        int seckey_fd = open(seckey, O_RDONLY);
1950
 
        if(seckey_fd == -1){
1951
 
          perror_plus("open");
1952
 
        } else {
1953
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1954
 
          if(ret == -1){
1955
 
            perror_plus("fstat");
1956
 
          } else {
1957
 
            if(S_ISREG(st.st_mode)
1958
 
               and st.st_uid == 0 and st.st_gid == 0){
1959
 
              ret = fchown(seckey_fd, uid, gid);
1960
 
              if(ret == -1){
1961
 
                perror_plus("fchown");
1962
 
              }
1963
 
            }
1964
 
          }
1965
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1966
 
        }
1967
 
      }
1968
 
    
1969
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1970
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1971
 
        if(pubkey_fd == -1){
1972
 
          perror_plus("open");
1973
 
        } else {
1974
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1975
 
          if(ret == -1){
1976
 
            perror_plus("fstat");
1977
 
          } else {
1978
 
            if(S_ISREG(st.st_mode)
1979
 
               and st.st_uid == 0 and st.st_gid == 0){
1980
 
              ret = fchown(pubkey_fd, uid, gid);
1981
 
              if(ret == -1){
1982
 
                perror_plus("fchown");
1983
 
              }
1984
 
            }
1985
 
          }
1986
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1987
 
        }
1988
 
      }
1989
 
    
1990
 
      /* Lower privileges */
1991
 
      errno = 0;
1992
 
      ret = seteuid(uid);
1993
 
      if(ret == -1){
1994
 
        perror_plus("seteuid");
1995
 
      }
1996
 
    }
1997
 
  }
1998
 
  
1999
 
  /* Remove empty interface names */
2000
 
  {
2001
 
    char *interface = NULL;
2002
 
    while((interface = argz_next(interfaces, interfaces_size,
2003
 
                                 interface))){
2004
 
      if(if_nametoindex(interface) == 0){
2005
 
        if(interface[0] != '\0' and strcmp(interface, "none") != 0){
2006
 
          fprintf_plus(stderr, "Not using nonexisting interface"
2007
 
                       " \"%s\"\n", interface);
2008
 
        }
2009
 
        argz_delete(&interfaces, &interfaces_size, interface);
2010
 
        interface = NULL;
2011
 
      }
 
1678
    errno = 0;
 
1679
    ret = seteuid(0);
 
1680
    if(ret == -1){
 
1681
      perror_plus("seteuid");
 
1682
    }
 
1683
    
 
1684
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1685
      int seckey_fd = open(seckey, O_RDONLY);
 
1686
      if(seckey_fd == -1){
 
1687
        perror_plus("open");
 
1688
      } else {
 
1689
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1690
        if(ret == -1){
 
1691
          perror_plus("fstat");
 
1692
        } else {
 
1693
          if(S_ISREG(st.st_mode)
 
1694
             and st.st_uid == 0 and st.st_gid == 0){
 
1695
            ret = fchown(seckey_fd, uid, gid);
 
1696
            if(ret == -1){
 
1697
              perror_plus("fchown");
 
1698
            }
 
1699
          }
 
1700
        }
 
1701
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1702
      }
 
1703
    }
 
1704
    
 
1705
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1706
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1707
      if(pubkey_fd == -1){
 
1708
        perror_plus("open");
 
1709
      } else {
 
1710
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1711
        if(ret == -1){
 
1712
          perror_plus("fstat");
 
1713
        } else {
 
1714
          if(S_ISREG(st.st_mode)
 
1715
             and st.st_uid == 0 and st.st_gid == 0){
 
1716
            ret = fchown(pubkey_fd, uid, gid);
 
1717
            if(ret == -1){
 
1718
              perror_plus("fchown");
 
1719
            }
 
1720
          }
 
1721
        }
 
1722
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1723
      }
 
1724
    }
 
1725
    
 
1726
    /* Lower privileges */
 
1727
    errno = 0;
 
1728
    ret = seteuid(uid);
 
1729
    if(ret == -1){
 
1730
      perror_plus("seteuid");
2012
1731
    }
2013
1732
  }
2014
1733
  
2015
1734
  /* Run network hooks */
2016
1735
  {
2017
 
    ret_errno = argz_append(&interfaces_hooks, &interfaces_hooks_size,
2018
 
                            interfaces, interfaces_size);
2019
 
    if(ret_errno != 0){
2020
 
      errno = ret_errno;
2021
 
      perror_plus("argz_append");
2022
 
      goto end;
2023
 
    }
2024
 
    argz_stringify(interfaces_hooks, interfaces_hooks_size, (int)',');
2025
 
    if(not run_network_hooks("start", interfaces_hooks, delay)){
2026
 
      goto end;
 
1736
    /* Re-raise priviliges */
 
1737
    errno = 0;
 
1738
    ret = seteuid(0);
 
1739
    if(ret == -1){
 
1740
      perror_plus("seteuid");
 
1741
    }
 
1742
    if(not run_network_hooks("start", interface, delay)){
 
1743
      goto end;
 
1744
    }
 
1745
    /* Lower privileges */
 
1746
    errno = 0;
 
1747
    ret = seteuid(uid);
 
1748
    if(ret == -1){
 
1749
      perror_plus("seteuid");
2027
1750
    }
2028
1751
  }
2029
1752
  
2031
1754
    avahi_set_log_function(empty_log);
2032
1755
  }
2033
1756
  
 
1757
  if(interface[0] == '\0'){
 
1758
    struct dirent **direntries;
 
1759
    /* First look for interfaces that are up */
 
1760
    ret = scandir(sys_class_net, &direntries, up_interface,
 
1761
                  alphasort);
 
1762
    if(ret == 0){
 
1763
      /* No up interfaces, look for any good interfaces */
 
1764
      free(direntries);
 
1765
      ret = scandir(sys_class_net, &direntries, good_interface,
 
1766
                    alphasort);
 
1767
    }
 
1768
    if(ret >= 1){
 
1769
      /* Pick the first interface returned */
 
1770
      interface = strdup(direntries[0]->d_name);
 
1771
      if(debug){
 
1772
        fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
 
1773
      }
 
1774
      if(interface == NULL){
 
1775
        perror_plus("malloc");
 
1776
        free(direntries);
 
1777
        exitcode = EXIT_FAILURE;
 
1778
        goto end;
 
1779
      }
 
1780
      free(direntries);
 
1781
    } else {
 
1782
      free(direntries);
 
1783
      fprintf_plus(stderr, "Could not find a network interface\n");
 
1784
      exitcode = EXIT_FAILURE;
 
1785
      goto end;
 
1786
    }
 
1787
  }
 
1788
  
2034
1789
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
2035
1790
     from the signal handler */
2036
1791
  /* Initialize the pseudo-RNG for Avahi */
2106
1861
    }
2107
1862
  }
2108
1863
  
2109
 
  /* If no interfaces were specified, make a list */
2110
 
  if(interfaces == NULL){
2111
 
    struct dirent **direntries;
2112
 
    /* Look for any good interfaces */
2113
 
    ret = scandir(sys_class_net, &direntries, good_interface,
2114
 
                  alphasort);
2115
 
    if(ret >= 1){
2116
 
      /* Add all found interfaces to interfaces list */
2117
 
      for(int i = 0; i < ret; ++i){
2118
 
        ret_errno = argz_add(&interfaces, &interfaces_size,
2119
 
                             direntries[i]->d_name);
2120
 
        if(ret_errno != 0){
2121
 
          perror_plus("argz_add");
2122
 
          continue;
2123
 
        }
2124
 
        if(debug){
2125
 
          fprintf_plus(stderr, "Will use interface \"%s\"\n",
2126
 
                       direntries[i]->d_name);
2127
 
        }
2128
 
      }
2129
 
      free(direntries);
2130
 
    } else {
2131
 
      free(direntries);
2132
 
      fprintf_plus(stderr, "Could not find a network interface\n");
2133
 
      exitcode = EXIT_FAILURE;
2134
 
      goto end;
2135
 
    }
2136
 
  }
2137
 
  
2138
 
  /* If we only got one interface, explicitly use only that one */
2139
 
  if(argz_count(interfaces, interfaces_size) == 1){
2140
 
    if(debug){
2141
 
      fprintf_plus(stderr, "Using only interface \"%s\"\n",
2142
 
                   interfaces);
2143
 
    }
2144
 
    if_index = (AvahiIfIndex)if_nametoindex(interfaces);
2145
 
  }
2146
 
  
2147
 
  /* Bring up interfaces which are down */
2148
 
  if(not (argz_count(interfaces, interfaces_size) == 1
2149
 
          and strcmp(interfaces, "none") == 0)){
2150
 
    char *interface = NULL;
2151
 
    while((interface = argz_next(interfaces, interfaces_size,
2152
 
                                 interface))){
2153
 
      bool interface_was_up = interface_is_up(interface);
2154
 
      ret = bring_up_interface(interface, delay);
2155
 
      if(not interface_was_up){
2156
 
        if(ret != 0){
2157
 
          errno = ret;
2158
 
          perror_plus("Failed to bring up interface");
2159
 
        } else {
2160
 
          ret_errno = argz_add(&interfaces_to_take_down,
2161
 
                               &interfaces_to_take_down_size,
2162
 
                               interface);
2163
 
        }
2164
 
      }
2165
 
    }
2166
 
    free(interfaces);
2167
 
    interfaces = NULL;
2168
 
    interfaces_size = 0;
2169
 
    if(debug and (interfaces_to_take_down == NULL)){
2170
 
      fprintf_plus(stderr, "No interfaces were brought up\n");
 
1864
  /* If the interface is down, bring it up */
 
1865
  if(strcmp(interface, "none") != 0){
 
1866
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1867
    if(if_index == 0){
 
1868
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1869
      exitcode = EX_UNAVAILABLE;
 
1870
      goto end;
 
1871
    }
 
1872
    
 
1873
    if(quit_now){
 
1874
      goto end;
 
1875
    }
 
1876
    
 
1877
    /* Re-raise priviliges */
 
1878
    errno = 0;
 
1879
    ret = seteuid(0);
 
1880
    if(ret == -1){
 
1881
      perror_plus("seteuid");
 
1882
    }
 
1883
    
 
1884
#ifdef __linux__
 
1885
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1886
       messages about the network interface to mess up the prompt */
 
1887
    ret = klogctl(8, NULL, 5);
 
1888
    bool restore_loglevel = true;
 
1889
    if(ret == -1){
 
1890
      restore_loglevel = false;
 
1891
      perror_plus("klogctl");
 
1892
    }
 
1893
#endif  /* __linux__ */
 
1894
    
 
1895
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1896
    if(sd < 0){
 
1897
      perror_plus("socket");
 
1898
      exitcode = EX_OSERR;
 
1899
#ifdef __linux__
 
1900
      if(restore_loglevel){
 
1901
        ret = klogctl(7, NULL, 0);
 
1902
        if(ret == -1){
 
1903
          perror_plus("klogctl");
 
1904
        }
 
1905
      }
 
1906
#endif  /* __linux__ */
 
1907
      /* Lower privileges */
 
1908
      errno = 0;
 
1909
      ret = seteuid(uid);
 
1910
      if(ret == -1){
 
1911
        perror_plus("seteuid");
 
1912
      }
 
1913
      goto end;
 
1914
    }
 
1915
    strcpy(network.ifr_name, interface);
 
1916
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1917
    if(ret == -1){
 
1918
      perror_plus("ioctl SIOCGIFFLAGS");
 
1919
#ifdef __linux__
 
1920
      if(restore_loglevel){
 
1921
        ret = klogctl(7, NULL, 0);
 
1922
        if(ret == -1){
 
1923
          perror_plus("klogctl");
 
1924
        }
 
1925
      }
 
1926
#endif  /* __linux__ */
 
1927
      exitcode = EX_OSERR;
 
1928
      /* Lower privileges */
 
1929
      errno = 0;
 
1930
      ret = seteuid(uid);
 
1931
      if(ret == -1){
 
1932
        perror_plus("seteuid");
 
1933
      }
 
1934
      goto end;
 
1935
    }
 
1936
    if((network.ifr_flags & IFF_UP) == 0){
 
1937
      network.ifr_flags |= IFF_UP;
 
1938
      take_down_interface = true;
 
1939
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1940
      if(ret == -1){
 
1941
        take_down_interface = false;
 
1942
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1943
        exitcode = EX_OSERR;
 
1944
#ifdef __linux__
 
1945
        if(restore_loglevel){
 
1946
          ret = klogctl(7, NULL, 0);
 
1947
          if(ret == -1){
 
1948
            perror_plus("klogctl");
 
1949
          }
 
1950
        }
 
1951
#endif  /* __linux__ */
 
1952
        /* Lower privileges */
 
1953
        errno = 0;
 
1954
        ret = seteuid(uid);
 
1955
        if(ret == -1){
 
1956
          perror_plus("seteuid");
 
1957
        }
 
1958
        goto end;
 
1959
      }
 
1960
    }
 
1961
    /* Sleep checking until interface is running.
 
1962
       Check every 0.25s, up to total time of delay */
 
1963
    for(int i=0; i < delay * 4; i++){
 
1964
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1965
      if(ret == -1){
 
1966
        perror_plus("ioctl SIOCGIFFLAGS");
 
1967
      } else if(network.ifr_flags & IFF_RUNNING){
 
1968
        break;
 
1969
      }
 
1970
      struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1971
      ret = nanosleep(&sleeptime, NULL);
 
1972
      if(ret == -1 and errno != EINTR){
 
1973
        perror_plus("nanosleep");
 
1974
      }
 
1975
    }
 
1976
    if(not take_down_interface){
 
1977
      /* We won't need the socket anymore */
 
1978
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1979
      if(ret == -1){
 
1980
        perror_plus("close");
 
1981
      }
 
1982
    }
 
1983
#ifdef __linux__
 
1984
    if(restore_loglevel){
 
1985
      /* Restores kernel loglevel to default */
 
1986
      ret = klogctl(7, NULL, 0);
 
1987
      if(ret == -1){
 
1988
        perror_plus("klogctl");
 
1989
      }
 
1990
    }
 
1991
#endif  /* __linux__ */
 
1992
    /* Lower privileges */
 
1993
    errno = 0;
 
1994
    /* Lower privileges */
 
1995
    ret = seteuid(uid);
 
1996
    if(ret == -1){
 
1997
      perror_plus("seteuid");
2171
1998
    }
2172
1999
  }
2173
2000
  
2214
2041
    /* Connect directly, do not use Zeroconf */
2215
2042
    /* (Mainly meant for debugging) */
2216
2043
    char *address = strrchr(connect_to, ':');
2217
 
    
2218
2044
    if(address == NULL){
2219
2045
      fprintf_plus(stderr, "No colon in address\n");
2220
2046
      exitcode = EX_USAGE;
2295
2121
    /* Allocate a new server */
2296
2122
    mc.server = avahi_server_new(avahi_simple_poll_get
2297
2123
                                 (mc.simple_poll), &config, NULL,
2298
 
                                 NULL, &ret_errno);
 
2124
                                 NULL, &error);
2299
2125
    
2300
2126
    /* Free the Avahi configuration data */
2301
2127
    avahi_server_config_free(&config);
2304
2130
  /* Check if creating the Avahi server object succeeded */
2305
2131
  if(mc.server == NULL){
2306
2132
    fprintf_plus(stderr, "Failed to create Avahi server: %s\n",
2307
 
                 avahi_strerror(ret_errno));
 
2133
                 avahi_strerror(error));
2308
2134
    exitcode = EX_UNAVAILABLE;
2309
2135
    goto end;
2310
2136
  }
2366
2192
  if(gpgme_initialized){
2367
2193
    gpgme_release(mc.ctx);
2368
2194
  }
2369
 
  
 
2195
 
2370
2196
  /* Cleans up the circular linked list of Mandos servers the client
2371
2197
     has seen */
2372
2198
  if(mc.current_server != NULL){
2380
2206
  
2381
2207
  /* Re-raise priviliges */
2382
2208
  {
2383
 
    raise_privileges();
2384
 
    
 
2209
    errno = 0;
 
2210
    ret = seteuid(0);
 
2211
    if(ret == -1){
 
2212
      perror_plus("seteuid");
 
2213
    }
2385
2214
    /* Run network hooks */
2386
 
    run_network_hooks("stop", interfaces_hooks, delay);
 
2215
    if(not run_network_hooks("stop", interface, delay)){
 
2216
      goto end;
 
2217
    }
2387
2218
    
2388
 
    /* Take down the network interfaces which were brought up */
2389
 
    {
2390
 
      char *interface = NULL;
2391
 
      while((interface=argz_next(interfaces_to_take_down,
2392
 
                                 interfaces_to_take_down_size,
2393
 
                                 interface))){
2394
 
        ret_errno = take_down_interface(interface);
2395
 
        if(ret_errno != 0){
2396
 
          errno = ret_errno;
2397
 
          perror_plus("Failed to take down interface");
 
2219
    /* Take down the network interface */
 
2220
    if(take_down_interface and geteuid() == 0){
 
2221
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
2222
      if(ret == -1){
 
2223
        perror_plus("ioctl SIOCGIFFLAGS");
 
2224
      } else if(network.ifr_flags & IFF_UP){
 
2225
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
2226
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
2227
        if(ret == -1){
 
2228
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2398
2229
        }
2399
2230
      }
2400
 
      if(debug and (interfaces_to_take_down == NULL)){
2401
 
        fprintf_plus(stderr, "No interfaces needed to be taken"
2402
 
                     " down\n");
 
2231
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
2232
      if(ret == -1){
 
2233
        perror_plus("close");
2403
2234
      }
2404
2235
    }
2405
 
    
2406
 
    lower_privileges_permanently();
2407
 
  }
2408
 
  
2409
 
  free(interfaces_to_take_down);
2410
 
  free(interfaces_hooks);
 
2236
  }
 
2237
  /* Lower privileges permanently */
 
2238
  errno = 0;
 
2239
  ret = setuid(uid);
 
2240
  if(ret == -1){
 
2241
    perror_plus("setuid");
 
2242
  }
2411
2243
  
2412
2244
  /* Removes the GPGME temp directory and all files inside */
2413
2245
  if(tempdir_created){