/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

* 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:
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
90
 
92
91
#ifdef __linux__
93
92
#include <sys/klog.h>           /* klogctl() */
175
174
  perror(print_text);
176
175
}
177
176
 
178
 
__attribute__((format (gnu_printf, 2, 3)))
179
177
int fprintf_plus(FILE *stream, const char *format, ...){
180
178
  va_list ap;
181
179
  va_start (ap, format);
203
201
}
204
202
 
205
203
/* Add server to set of servers to retry periodically */
206
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
207
 
                int af){
 
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
205
               int af){
208
206
  int ret;
209
207
  server *new_server = malloc(sizeof(server));
210
208
  if(new_server == NULL){
211
209
    perror_plus("malloc");
212
 
    return false;
 
210
    return -1;
213
211
  }
214
212
  *new_server = (server){ .ip = strdup(ip),
215
213
                          .port = port,
217
215
                          .af = af };
218
216
  if(new_server->ip == NULL){
219
217
    perror_plus("strdup");
220
 
    return false;
 
218
    return -1;
221
219
  }
222
220
  /* Special case of first server */
223
221
  if (mc.current_server == NULL){
234
232
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
235
233
  if(ret == -1){
236
234
    perror_plus("clock_gettime");
237
 
    return false;
 
235
    return -1;
238
236
  }
239
 
  return true;
 
237
  return 0;
240
238
}
241
239
 
242
240
/* 
1031
1029
      if(ret == 0){
1032
1030
        avahi_simple_poll_quit(mc.simple_poll);
1033
1031
      } else {
1034
 
        if(not add_server(ip, port, interface,
1035
 
                          avahi_proto_to_af(proto))){
1036
 
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1037
 
                       " list\n", name);
1038
 
        }
 
1032
        ret = add_server(ip, port, interface,
 
1033
                         avahi_proto_to_af(proto));
1039
1034
      }
1040
1035
    }
1041
1036
  }
1306
1301
    }
1307
1302
    return 0;
1308
1303
  }
1309
 
  if(debug){
1310
 
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1311
 
                 direntry->d_name);
1312
 
  }
1313
1304
  return 1;
1314
1305
}
1315
1306
 
1397
1388
  } else {
1398
1389
    int devnull = open("/dev/null", O_RDONLY);
1399
1390
    for(int i = 0; i < numhooks; i++){
1400
 
      direntry = direntries[i];
 
1391
      direntry = direntries[0];
1401
1392
      char *fullname = NULL;
1402
1393
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1403
1394
      if(ret < 0){
1404
1395
        perror_plus("asprintf");
1405
1396
        continue;
1406
1397
      }
1407
 
      if(debug){
1408
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1409
 
                     direntry->d_name);
1410
 
      }
1411
1398
      pid_t hook_pid = fork();
1412
1399
      if(hook_pid == 0){
1413
1400
        /* Child */
1414
 
        /* Raise privileges */
1415
 
        errno = 0;
1416
 
        ret = seteuid(0);
1417
 
        if(ret == -1){
1418
 
          perror_plus("seteuid");
1419
 
        }
1420
 
        /* Raise privileges even more */
1421
 
        errno = 0;
1422
 
        ret = setuid(0);
1423
 
        if(ret == -1){
1424
 
          perror_plus("setuid");
1425
 
        }
1426
 
        /* Set group */
1427
 
        errno = 0;
1428
 
        ret = setgid(0);
1429
 
        if(ret == -1){
1430
 
          perror_plus("setgid");
1431
 
        }
1432
 
        /* Reset supplementary groups */
1433
 
        errno = 0;
1434
 
        ret = setgroups(0, NULL);
1435
 
        if(ret == -1){
1436
 
          perror_plus("setgroups");
1437
 
        }
1438
1401
        dup2(devnull, STDIN_FILENO);
1439
1402
        close(devnull);
1440
1403
        dup2(STDERR_FILENO, STDOUT_FILENO);
1441
1404
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1442
1405
        if(ret == -1){
1443
1406
          perror_plus("setenv");
1444
 
          _exit(EX_OSERR);
 
1407
          return false;
1445
1408
        }
1446
1409
        ret = setenv("DEVICE", interface, 1);
1447
1410
        if(ret == -1){
1448
1411
          perror_plus("setenv");
1449
 
          _exit(EX_OSERR);
 
1412
          return false;
1450
1413
        }
1451
1414
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1452
1415
        if(ret == -1){
1453
1416
          perror_plus("setenv");
1454
 
          _exit(EX_OSERR);
 
1417
          return false;
1455
1418
        }
1456
1419
        ret = setenv("MODE", mode, 1);
1457
1420
        if(ret == -1){
1458
1421
          perror_plus("setenv");
1459
 
          _exit(EX_OSERR);
 
1422
          return false;
1460
1423
        }
1461
1424
        char *delaystring;
1462
1425
        ret = asprintf(&delaystring, "%f", delay);
1463
1426
        if(ret == -1){
1464
1427
          perror_plus("asprintf");
1465
 
          _exit(EX_OSERR);
 
1428
          return false;
1466
1429
        }
1467
1430
        ret = setenv("DELAY", delaystring, 1);
1468
1431
        if(ret == -1){
1469
1432
          free(delaystring);
1470
1433
          perror_plus("setenv");
1471
 
          _exit(EX_OSERR);
 
1434
          return false;
1472
1435
        }
1473
1436
        free(delaystring);
1474
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1475
 
          perror_plus("execl");
1476
 
          _exit(EXIT_FAILURE);
1477
 
        }
 
1437
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1438
        perror_plus("execl");
1478
1439
      } else {
1479
1440
        int status;
1480
1441
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1504
1465
        }
1505
1466
      }
1506
1467
      free(fullname);
1507
 
      if(debug){
1508
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1509
 
                     direntry->d_name);
 
1468
      if(quit_now){
 
1469
        break;
1510
1470
      }
1511
1471
    }
1512
1472
    close(devnull);
1677
1637
        argp_state_help(state, state->out_stream,
1678
1638
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1679
1639
      case 'V':                 /* --version */
 
1640
        fprintf_plus(state->out_stream,
 
1641
                     "Mandos plugin mandos-client: ");
1680
1642
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1681
1643
        exit(argp_err_exit_status);
1682
1644
        break;
1710
1672
  {
1711
1673
    /* Work around Debian bug #633582:
1712
1674
       <http://bugs.debian.org/633582> */
 
1675
    struct stat st;
1713
1676
    
1714
1677
    /* Re-raise priviliges */
1715
1678
    errno = 0;
1716
1679
    ret = seteuid(0);
1717
1680
    if(ret == -1){
1718
1681
      perror_plus("seteuid");
1719
 
    } else {
1720
 
      struct stat st;
1721
 
      
1722
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1723
 
        int seckey_fd = open(seckey, O_RDONLY);
1724
 
        if(seckey_fd == -1){
1725
 
          perror_plus("open");
1726
 
        } else {
1727
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1728
 
          if(ret == -1){
1729
 
            perror_plus("fstat");
1730
 
          } else {
1731
 
            if(S_ISREG(st.st_mode)
1732
 
               and st.st_uid == 0 and st.st_gid == 0){
1733
 
              ret = fchown(seckey_fd, uid, gid);
1734
 
              if(ret == -1){
1735
 
                perror_plus("fchown");
1736
 
              }
1737
 
            }
1738
 
          }
1739
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1740
 
        }
1741
 
      }
1742
 
    
1743
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1744
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1745
 
        if(pubkey_fd == -1){
1746
 
          perror_plus("open");
1747
 
        } else {
1748
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1749
 
          if(ret == -1){
1750
 
            perror_plus("fstat");
1751
 
          } else {
1752
 
            if(S_ISREG(st.st_mode)
1753
 
               and st.st_uid == 0 and st.st_gid == 0){
1754
 
              ret = fchown(pubkey_fd, uid, gid);
1755
 
              if(ret == -1){
1756
 
                perror_plus("fchown");
1757
 
              }
1758
 
            }
1759
 
          }
1760
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1761
 
        }
1762
 
      }
1763
 
    
1764
 
      /* Lower privileges */
1765
 
      errno = 0;
1766
 
      ret = seteuid(uid);
1767
 
      if(ret == -1){
1768
 
        perror_plus("seteuid");
1769
 
      }
 
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");
1770
1731
    }
1771
1732
  }
1772
1733
  
1773
1734
  /* Run network hooks */
1774
 
  if(not run_network_hooks("start", interface, delay)){
1775
 
    goto end;
 
1735
  {
 
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");
 
1750
    }
1776
1751
  }
1777
1752
  
1778
1753
  if(not debug){
2217
2192
  if(gpgme_initialized){
2218
2193
    gpgme_release(mc.ctx);
2219
2194
  }
2220
 
  
 
2195
 
2221
2196
  /* Cleans up the circular linked list of Mandos servers the client
2222
2197
     has seen */
2223
2198
  if(mc.current_server != NULL){
2229
2204
    }
2230
2205
  }
2231
2206
  
2232
 
  /* Run network hooks */
2233
 
  run_network_hooks("stop", interface, delay);
2234
 
  
2235
2207
  /* Re-raise priviliges */
2236
2208
  {
2237
2209
    errno = 0;
2239
2211
    if(ret == -1){
2240
2212
      perror_plus("seteuid");
2241
2213
    }
 
2214
    /* Run network hooks */
 
2215
    if(not run_network_hooks("stop", interface, delay)){
 
2216
      goto end;
 
2217
    }
2242
2218
    
2243
2219
    /* Take down the network interface */
2244
2220
    if(take_down_interface and geteuid() == 0){