26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
29
* Contact the authors at <mandos@recompile.se>.
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
123
123
#define PATHDIR "/conf/conf.d/mandos"
124
124
#define SECKEY "seckey.txt"
125
125
#define PUBKEY "pubkey.txt"
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
127
128
bool debug = false;
128
129
static const char mandos_protocol_version[] = "1";
129
130
const char *argp_program_version = "mandos-client " VERSION;
130
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
132
static const char sys_class_net[] = "/sys/class/net";
132
133
char *connect_to = NULL;
204
206
perror_plus("strdup");
207
/* unique case of first server */
209
/* Special case of first server */
208
210
if (mc.current_server == NULL){
209
211
new_server->next = new_server;
210
212
new_server->prev = new_server;
211
213
mc.current_server = new_server;
212
/* Placing the new server last in the list */
214
/* Place the new server last in the list */
214
216
new_server->next = mc.current_server;
215
217
new_server->prev = mc.current_server->prev;
285
/* Set GPGME home directory for the OpenPGP engine only */
287
/* Set GPGME home directory for the OpenPGP engine only */
286
288
rc = gpgme_get_engine_info(&engine_info);
287
289
if(rc != GPG_ERR_NO_ERROR){
288
290
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1084
1086
errno = old_errno;
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
1092
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1094
perror_plus("socket");
1097
strcpy(ifr->ifr_name, ifname);
1098
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1101
perror_plus("ioctl SIOCGIFFLAGS");
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
1110
/* Reject the loopback device */
1111
if(ifr->ifr_flags & IFF_LOOPBACK){
1113
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1118
/* Accept point-to-point devices only if connect_to is specified */
1119
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1121
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1126
/* Otherwise, reject non-broadcast-capable devices */
1127
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1129
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1134
/* Reject non-ARP interfaces (including dummy interfaces) */
1135
if(ifr->ifr_flags & IFF_NOARP){
1137
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1142
/* Accept this device */
1144
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1088
1150
* This function determines if a directory entry in /sys/class/net
1089
1151
* corresponds to an acceptable network device.
1090
1152
* (This function is passed to scandir(3) as a filter function.)
1092
1154
int good_interface(const struct dirent *if_entry){
1156
if(if_entry->d_name[0] == '.'){
1161
if(not get_flags(if_entry->d_name, &ifr)){
1165
if(not good_flags(if_entry->d_name, &ifr)){
1172
* This function determines if a directory entry in /sys/class/net
1173
* corresponds to an acceptable network device which is up.
1174
* (This function is passed to scandir(3) as a filter function.)
1176
int up_interface(const struct dirent *if_entry){
1094
1178
char *flagname = NULL;
1095
1179
if(if_entry->d_name[0] == '.'){
1293
/* Is this directory entry a runnable program? */
1294
int runnable_hook(const struct dirent *direntry){
1298
if((direntry->d_name)[0] == '\0'){
1303
/* Save pointer to last character */
1304
char *end = strchr(direntry->d_name, '\0')-1;
1311
if(((direntry->d_name)[0] == '#')
1313
/* Temporary #name# */
1317
/* XXX more rules here */
1319
ret = stat(direntry->d_name, &st);
1322
perror_plus("Could not stat plugin");
1326
if(not (st.st_mode & S_ISREG)){
1327
/* Not a regular file */
1330
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1331
/* Not executable */
1203
1337
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
1339
struct timespec now;
1206
1340
struct timespec waited_time;
1207
1341
intmax_t block_time;
1210
1344
if(mc.current_server == NULL){
1236
1370
block_time = ((retry_interval
1237
1371
- ((intmax_t)waited_time.tv_sec * 1000))
1238
1372
- ((intmax_t)waited_time.tv_nsec / 1000000));
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1375
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1244
1378
if(block_time <= 0){
1245
1379
ret = start_mandos_communication(mc.current_server->ip,
1246
1380
mc.current_server->port,
1412
1546
retry_interval = strtod(arg, &tmp);
1413
1547
if(errno != 0 or tmp == arg or *tmp != '\0'
1414
or (retry_interval * 1000) > INT_MAX){
1548
or (retry_interval * 1000) > INT_MAX
1549
or retry_interval < 0){
1415
1550
argp_error(state, "Bad retry interval");
1468
1603
perror_plus("seteuid");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1606
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1607
int seckey_fd = open(seckey, O_RDONLY);
1608
if(seckey_fd == -1){
1609
perror_plus("open");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1611
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1613
perror_plus("fstat");
1615
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1616
ret = fchown(seckey_fd, uid, gid);
1618
perror_plus("fchown");
1622
TEMP_FAILURE_RETRY(close(seckey_fd));
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1626
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1627
int pubkey_fd = open(pubkey, O_RDONLY);
1628
if(pubkey_fd == -1){
1629
perror_plus("open");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1631
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1633
perror_plus("fstat");
1635
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1636
ret = fchown(pubkey_fd, uid, gid);
1638
perror_plus("fchown");
1642
TEMP_FAILURE_RETRY(close(pubkey_fd));
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1507
1646
/* Lower privileges */
1654
/* Find network hooks and run them */
1656
struct dirent **direntries;
1657
struct dirent *direntry;
1658
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1660
int devnull = open("/dev/null", O_RDONLY);
1661
for(int i = 0; i < numhooks; i++){
1662
direntry = direntries[0];
1663
char *fullname = NULL;
1664
ret = asprintf(&fullname, "%s/%s", tempdir,
1667
perror_plus("asprintf");
1670
pid_t hook_pid = fork();
1673
dup2(devnull, STDIN_FILENO);
1675
dup2(STDERR_FILENO, STDOUT_FILENO);
1676
setenv("DEVICE", interface, 1);
1677
setenv("VERBOSE", debug ? "1" : "0", 1);
1678
setenv("MODE", "start", 1);
1679
/* setenv( XXX more here */
1680
ret = execl(fullname, direntry->d_name, "start");
1681
perror_plus("execl");
1516
1692
avahi_set_log_function(empty_log);
1519
1695
if(interface[0] == '\0'){
1520
1696
struct dirent **direntries;
1521
1697
ret = scandir(sys_class_net, &direntries, good_interface,
1830
2006
port = (uint16_t)tmpmax;
1831
2007
*address = '\0';
1832
address = connect_to;
1833
2008
/* Colon in address indicates IPv6 */
1835
if(strchr(address, ':') != NULL){
2010
if(strchr(connect_to, ':') != NULL){
2012
/* Accept [] around IPv6 address - see RFC 5952 */
2013
if(connect_to[0] == '[' and address[-1] == ']')
2021
address = connect_to;
1845
2027
while(not quit_now){
1846
2028
ret = start_mandos_communication(address, port, if_index, af);
1847
2029
if(quit_now or ret == 0){
1850
sleep((int)retry_interval or 1);
2033
fprintf(stderr, "Retrying in %d seconds\n",
2034
(int)retry_interval);
2036
sleep((int)retry_interval);
1853
2039
if (not quit_now){
1854
2040
exitcode = EXIT_SUCCESS;
1992
2180
if(tempdir_created){
1993
2181
struct dirent **direntries = NULL;
1994
2182
struct dirent *direntry = NULL;
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
2183
int numentries = scandir(tempdir, &direntries, notdotentries,
2185
if (numentries > 0){
2186
for(int i = 0; i < numentries; i++){
1998
2187
direntry = direntries[i];
1999
2188
char *fullname = NULL;
2000
2189
ret = asprintf(&fullname, "%s/%s", tempdir,