/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

Hooks take new "modules" argument, and hook names can contain periods.

* debian/mandos-client.README.Debian: Adjust wording.
* debian/rules (binary-common): Exclude nework-hooks.d directory from
                                dh_fixperms.
* initramfs-tools-hook: Also create network hook directory.  Allow
                        periods in hook names.  Only run executable
                        files in hook directory.  Copy needed modules.
* network-hooks.d/bridge: Fix "/usr/bin/brctl" to "/usr/sbin/brctl".
                          Also take "modules" argument.
* plugins.d/mandos-client.xml (NETWORK HOOKS/REQUIREMENTS): Document
                                                            the
                                                            allowing
                                                            of periods
                                                            in hook
                                                            names.
  (NETWORK HOOKS/REQUIREMENTS/files): Adjust wording.
  (NETWORK HOOKS/REQUIREMENTS/modules): New.
  (NETWORK HOOKS/REQUIREMENTS/MANDOSNETHOOKDIR): Clarify.
  (NETWORK HOOKS/REQUIREMENTS/MODE): Add "modules".

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
90
 
92
91
#ifdef __linux__
93
92
#include <sys/klog.h>           /* klogctl() */
170
169
 
171
170
/* Function to use when printing errors */
172
171
void perror_plus(const char *print_text){
173
 
  int e = errno;
174
172
  fprintf(stderr, "Mandos plugin %s: ",
175
173
          program_invocation_short_name);
176
 
  errno = e;
177
174
  perror(print_text);
178
175
}
179
176
 
180
 
__attribute__((format (gnu_printf, 2, 3)))
181
177
int fprintf_plus(FILE *stream, const char *format, ...){
182
178
  va_list ap;
183
179
  va_start (ap, format);
205
201
}
206
202
 
207
203
/* Add server to set of servers to retry periodically */
208
 
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
209
 
                int af){
 
204
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
205
               int af){
210
206
  int ret;
211
207
  server *new_server = malloc(sizeof(server));
212
208
  if(new_server == NULL){
213
209
    perror_plus("malloc");
214
 
    return false;
 
210
    return -1;
215
211
  }
216
212
  *new_server = (server){ .ip = strdup(ip),
217
213
                          .port = port,
219
215
                          .af = af };
220
216
  if(new_server->ip == NULL){
221
217
    perror_plus("strdup");
222
 
    return false;
 
218
    return -1;
223
219
  }
224
220
  /* Special case of first server */
225
221
  if (mc.current_server == NULL){
236
232
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
237
233
  if(ret == -1){
238
234
    perror_plus("clock_gettime");
239
 
    return false;
 
235
    return -1;
240
236
  }
241
 
  return true;
 
237
  return 0;
242
238
}
243
239
 
244
240
/* 
821
817
    goto mandos_end;
822
818
  }
823
819
  
824
 
  /* This casting via intptr_t is to eliminate warning about casting
825
 
     an int to a pointer type.  This is exactly how the GnuTLS Guile
826
 
     function "set-session-transport-fd!" does it. */
827
 
  gnutls_transport_set_ptr(session,
828
 
                           (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);
829
822
  
830
823
  if(quit_now){
831
824
    errno = EINTR;
1036
1029
      if(ret == 0){
1037
1030
        avahi_simple_poll_quit(mc.simple_poll);
1038
1031
      } else {
1039
 
        if(not add_server(ip, port, interface,
1040
 
                          avahi_proto_to_af(proto))){
1041
 
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
1042
 
                       " list\n", name);
1043
 
        }
 
1032
        ret = add_server(ip, port, interface,
 
1033
                         avahi_proto_to_af(proto));
1044
1034
      }
1045
1035
    }
1046
1036
  }
1416
1406
      pid_t hook_pid = fork();
1417
1407
      if(hook_pid == 0){
1418
1408
        /* Child */
1419
 
        /* Raise privileges */
1420
 
        errno = 0;
1421
 
        ret = seteuid(0);
1422
 
        if(ret == -1){
1423
 
          perror_plus("seteuid");
1424
 
        }
1425
 
        /* Raise privileges even more */
1426
 
        errno = 0;
1427
 
        ret = setuid(0);
1428
 
        if(ret == -1){
1429
 
          perror_plus("setuid");
1430
 
        }
1431
 
        /* Set group */
1432
 
        errno = 0;
1433
 
        ret = setgid(0);
1434
 
        if(ret == -1){
1435
 
          perror_plus("setgid");
1436
 
        }
1437
 
        /* Reset supplementary groups */
1438
 
        errno = 0;
1439
 
        ret = setgroups(0, NULL);
1440
 
        if(ret == -1){
1441
 
          perror_plus("setgroups");
1442
 
        }
1443
1409
        dup2(devnull, STDIN_FILENO);
1444
1410
        close(devnull);
1445
1411
        dup2(STDERR_FILENO, STDOUT_FILENO);
1446
1412
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1447
1413
        if(ret == -1){
1448
1414
          perror_plus("setenv");
1449
 
          _exit(EX_OSERR);
 
1415
          return false;
1450
1416
        }
1451
1417
        ret = setenv("DEVICE", interface, 1);
1452
1418
        if(ret == -1){
1453
1419
          perror_plus("setenv");
1454
 
          _exit(EX_OSERR);
 
1420
          return false;
1455
1421
        }
1456
 
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
 
1422
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1457
1423
        if(ret == -1){
1458
1424
          perror_plus("setenv");
1459
 
          _exit(EX_OSERR);
 
1425
          return false;
1460
1426
        }
1461
1427
        ret = setenv("MODE", mode, 1);
1462
1428
        if(ret == -1){
1463
1429
          perror_plus("setenv");
1464
 
          _exit(EX_OSERR);
 
1430
          return false;
1465
1431
        }
1466
1432
        char *delaystring;
1467
1433
        ret = asprintf(&delaystring, "%f", delay);
1468
1434
        if(ret == -1){
1469
1435
          perror_plus("asprintf");
1470
 
          _exit(EX_OSERR);
 
1436
          return false;
1471
1437
        }
1472
1438
        ret = setenv("DELAY", delaystring, 1);
1473
1439
        if(ret == -1){
1474
1440
          free(delaystring);
1475
1441
          perror_plus("setenv");
1476
 
          _exit(EX_OSERR);
 
1442
          return false;
1477
1443
        }
1478
1444
        free(delaystring);
1479
 
        if(connect_to != NULL){
1480
 
          ret = setenv("CONNECT", connect_to, 1);
1481
 
          if(ret == -1){
1482
 
            perror_plus("setenv");
1483
 
            _exit(EX_OSERR);
1484
 
          }
1485
 
        }
1486
 
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1487
 
          perror_plus("execl");
1488
 
          _exit(EXIT_FAILURE);
1489
 
        }
 
1445
        ret = execl(fullname, direntry->d_name, mode, NULL);
 
1446
        perror_plus("execl");
1490
1447
      } else {
1491
1448
        int status;
1492
1449
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1611
1568
        .group = 2 },
1612
1569
      { .name = "retry", .key = 132,
1613
1570
        .arg = "SECONDS",
1614
 
        .doc = "Retry interval used when denied by the Mandos server",
 
1571
        .doc = "Retry interval used when denied by the mandos server",
1615
1572
        .group = 2 },
1616
1573
      { .name = "network-hook-dir", .key = 133,
1617
1574
        .arg = "DIR",
1689
1646
        argp_state_help(state, state->out_stream,
1690
1647
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1691
1648
      case 'V':                 /* --version */
 
1649
        fprintf_plus(state->out_stream,
 
1650
                     "Mandos plugin mandos-client: ");
1692
1651
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1693
1652
        exit(argp_err_exit_status);
1694
1653
        break;
1719
1678
    }
1720
1679
  }
1721
1680
    
1722
 
  {
 
1681
  if(getuid() == 0){
1723
1682
    /* Work around Debian bug #633582:
1724
1683
       <http://bugs.debian.org/633582> */
 
1684
    struct stat st;
1725
1685
    
1726
1686
    /* Re-raise priviliges */
1727
1687
    errno = 0;
1728
1688
    ret = seteuid(0);
1729
1689
    if(ret == -1){
1730
1690
      perror_plus("seteuid");
1731
 
    } else {
1732
 
      struct stat st;
1733
 
      
1734
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1735
 
        int seckey_fd = open(seckey, O_RDONLY);
1736
 
        if(seckey_fd == -1){
1737
 
          perror_plus("open");
1738
 
        } else {
1739
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1740
 
          if(ret == -1){
1741
 
            perror_plus("fstat");
1742
 
          } else {
1743
 
            if(S_ISREG(st.st_mode)
1744
 
               and st.st_uid == 0 and st.st_gid == 0){
1745
 
              ret = fchown(seckey_fd, uid, gid);
1746
 
              if(ret == -1){
1747
 
                perror_plus("fchown");
1748
 
              }
1749
 
            }
1750
 
          }
1751
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1752
 
        }
1753
 
      }
1754
 
    
1755
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1756
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1757
 
        if(pubkey_fd == -1){
1758
 
          perror_plus("open");
1759
 
        } else {
1760
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1761
 
          if(ret == -1){
1762
 
            perror_plus("fstat");
1763
 
          } else {
1764
 
            if(S_ISREG(st.st_mode)
1765
 
               and st.st_uid == 0 and st.st_gid == 0){
1766
 
              ret = fchown(pubkey_fd, uid, gid);
1767
 
              if(ret == -1){
1768
 
                perror_plus("fchown");
1769
 
              }
1770
 
            }
1771
 
          }
1772
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1773
 
        }
1774
 
      }
1775
 
    
 
1691
    }
 
1692
    
 
1693
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1694
      int seckey_fd = open(seckey, O_RDONLY);
 
1695
      if(seckey_fd == -1){
 
1696
        perror_plus("open");
 
1697
      } else {
 
1698
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1699
        if(ret == -1){
 
1700
          perror_plus("fstat");
 
1701
        } else {
 
1702
          if(S_ISREG(st.st_mode)
 
1703
             and st.st_uid == 0 and st.st_gid == 0){
 
1704
            ret = fchown(seckey_fd, uid, gid);
 
1705
            if(ret == -1){
 
1706
              perror_plus("fchown");
 
1707
            }
 
1708
          }
 
1709
        }
 
1710
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1711
      }
 
1712
    }
 
1713
    
 
1714
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1715
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1716
      if(pubkey_fd == -1){
 
1717
        perror_plus("open");
 
1718
      } else {
 
1719
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1720
        if(ret == -1){
 
1721
          perror_plus("fstat");
 
1722
        } else {
 
1723
          if(S_ISREG(st.st_mode)
 
1724
             and st.st_uid == 0 and st.st_gid == 0){
 
1725
            ret = fchown(pubkey_fd, uid, gid);
 
1726
            if(ret == -1){
 
1727
              perror_plus("fchown");
 
1728
            }
 
1729
          }
 
1730
        }
 
1731
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1732
      }
 
1733
    }
 
1734
    
 
1735
    /* Lower privileges */
 
1736
    errno = 0;
 
1737
    ret = seteuid(uid);
 
1738
    if(ret == -1){
 
1739
      perror_plus("seteuid");
 
1740
    }
 
1741
  }
 
1742
  
 
1743
  /* Run network hooks */
 
1744
  {
 
1745
    if(getuid() == 0){
 
1746
      /* Re-raise priviliges */
 
1747
      errno = 0;
 
1748
      ret = seteuid(0);
 
1749
      if(ret == -1){
 
1750
        perror_plus("seteuid");
 
1751
      }
 
1752
    }
 
1753
    if(not run_network_hooks("start", interface, delay)){
 
1754
      goto end;
 
1755
    }
 
1756
    if(getuid() == 0){
1776
1757
      /* Lower privileges */
1777
1758
      errno = 0;
1778
1759
      ret = seteuid(uid);
1782
1763
    }
1783
1764
  }
1784
1765
  
1785
 
  /* Run network hooks */
1786
 
  if(not run_network_hooks("start", interface, delay)){
1787
 
    goto end;
1788
 
  }
1789
 
  
1790
1766
  if(not debug){
1791
1767
    avahi_set_log_function(empty_log);
1792
1768
  }
2241
2217
    }
2242
2218
  }
2243
2219
  
2244
 
  /* Run network hooks */
2245
 
  run_network_hooks("stop", interface, delay);
2246
 
  
2247
2220
  /* Re-raise priviliges */
2248
2221
  {
2249
 
    errno = 0;
2250
 
    ret = seteuid(0);
2251
 
    if(ret == -1){
2252
 
      perror_plus("seteuid");
 
2222
    if(getuid() == 0){
 
2223
      errno = 0;
 
2224
      ret = seteuid(0);
 
2225
      if(ret == -1){
 
2226
        perror_plus("seteuid");
 
2227
      }
2253
2228
    }
2254
2229
    
 
2230
    /* Run network hooks */
 
2231
    run_network_hooks("stop", interface, delay);
 
2232
    
2255
2233
    /* Take down the network interface */
2256
2234
    if(take_down_interface and geteuid() == 0){
2257
2235
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2270
2248
      }
2271
2249
    }
2272
2250
  }
2273
 
  /* Lower privileges permanently */
2274
 
  errno = 0;
2275
 
  ret = setuid(uid);
2276
 
  if(ret == -1){
2277
 
    perror_plus("setuid");
 
2251
  if(getuid() == 0){
 
2252
    /* Lower privileges permanently */
 
2253
    errno = 0;
 
2254
    ret = setuid(uid);
 
2255
    if(ret == -1){
 
2256
      perror_plus("setuid");
 
2257
    }
2278
2258
  }
2279
2259
  
2280
2260
  /* Removes the GPGME temp directory and all files inside */