/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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-01 21:48:12 UTC
  • Revision ID: teddy@recompile.se-20120601214812-g7685v5oeiuhi2qw
* debian/copyright (Copyright): Join the two lines to one line.
* debian/mandos-client.README.Debian: Refer to new location of example
  network hooks.
* debian/mandos-client.docs (network-hooks.d): Removed.
* debian/mandos-client.examples: New; contains "network-hooks.d".
* debian/rules (binary-common): Added "dh_installexamples".
  (binary-common/dh_fixperms): Changed to exclude new location of
  "network-hooks.d".
* init.d-mandos (status): Support new "status" action.

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() */
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause() */
 
76
                                   setgid(), pause(), _exit() */
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() */
90
91
 
91
92
#ifdef __linux__
92
93
#include <sys/klog.h>           /* klogctl() */
169
170
 
170
171
/* Function to use when printing errors */
171
172
void perror_plus(const char *print_text){
 
173
  int e = errno;
172
174
  fprintf(stderr, "Mandos plugin %s: ",
173
175
          program_invocation_short_name);
 
176
  errno = e;
174
177
  perror(print_text);
175
178
}
176
179
 
 
180
__attribute__((format (gnu_printf, 2, 3)))
177
181
int fprintf_plus(FILE *stream, const char *format, ...){
178
182
  va_list ap;
179
183
  va_start (ap, format);
201
205
}
202
206
 
203
207
/* Add server to set of servers to retry periodically */
204
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
205
 
               int af){
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
209
                int af){
206
210
  int ret;
207
211
  server *new_server = malloc(sizeof(server));
208
212
  if(new_server == NULL){
209
213
    perror_plus("malloc");
210
 
    return -1;
 
214
    return false;
211
215
  }
212
216
  *new_server = (server){ .ip = strdup(ip),
213
217
                          .port = port,
215
219
                          .af = af };
216
220
  if(new_server->ip == NULL){
217
221
    perror_plus("strdup");
218
 
    return -1;
 
222
    return false;
219
223
  }
220
224
  /* Special case of first server */
221
225
  if (mc.current_server == NULL){
232
236
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
233
237
  if(ret == -1){
234
238
    perror_plus("clock_gettime");
235
 
    return -1;
 
239
    return false;
236
240
  }
237
 
  return 0;
 
241
  return true;
238
242
}
239
243
 
240
244
/* 
817
821
    goto mandos_end;
818
822
  }
819
823
  
820
 
  /* Spurious warning from -Wint-to-pointer-cast */
821
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
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);
822
829
  
823
830
  if(quit_now){
824
831
    errno = EINTR;
1029
1036
      if(ret == 0){
1030
1037
        avahi_simple_poll_quit(mc.simple_poll);
1031
1038
      } else {
1032
 
        ret = add_server(ip, port, interface,
1033
 
                         avahi_proto_to_af(proto));
 
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
        }
1034
1044
      }
1035
1045
    }
1036
1046
  }
1301
1311
    }
1302
1312
    return 0;
1303
1313
  }
 
1314
  if(debug){
 
1315
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1316
                 direntry->d_name);
 
1317
  }
1304
1318
  return 1;
1305
1319
}
1306
1320
 
1388
1402
  } else {
1389
1403
    int devnull = open("/dev/null", O_RDONLY);
1390
1404
    for(int i = 0; i < numhooks; i++){
1391
 
      direntry = direntries[0];
 
1405
      direntry = direntries[i];
1392
1406
      char *fullname = NULL;
1393
1407
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1394
1408
      if(ret < 0){
1395
1409
        perror_plus("asprintf");
1396
1410
        continue;
1397
1411
      }
 
1412
      if(debug){
 
1413
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1414
                     direntry->d_name);
 
1415
      }
1398
1416
      pid_t hook_pid = fork();
1399
1417
      if(hook_pid == 0){
1400
1418
        /* 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
        }
1401
1443
        dup2(devnull, STDIN_FILENO);
1402
1444
        close(devnull);
1403
1445
        dup2(STDERR_FILENO, STDOUT_FILENO);
1404
1446
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1405
1447
        if(ret == -1){
1406
1448
          perror_plus("setenv");
1407
 
          return false;
 
1449
          _exit(EX_OSERR);
1408
1450
        }
1409
1451
        ret = setenv("DEVICE", interface, 1);
1410
1452
        if(ret == -1){
1411
1453
          perror_plus("setenv");
1412
 
          return false;
 
1454
          _exit(EX_OSERR);
1413
1455
        }
1414
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1456
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1415
1457
        if(ret == -1){
1416
1458
          perror_plus("setenv");
1417
 
          return false;
 
1459
          _exit(EX_OSERR);
1418
1460
        }
1419
1461
        ret = setenv("MODE", mode, 1);
1420
1462
        if(ret == -1){
1421
1463
          perror_plus("setenv");
1422
 
          return false;
 
1464
          _exit(EX_OSERR);
1423
1465
        }
1424
1466
        char *delaystring;
1425
1467
        ret = asprintf(&delaystring, "%f", delay);
1426
1468
        if(ret == -1){
1427
1469
          perror_plus("asprintf");
1428
 
          return false;
 
1470
          _exit(EX_OSERR);
1429
1471
        }
1430
1472
        ret = setenv("DELAY", delaystring, 1);
1431
1473
        if(ret == -1){
1432
1474
          free(delaystring);
1433
1475
          perror_plus("setenv");
1434
 
          return false;
 
1476
          _exit(EX_OSERR);
1435
1477
        }
1436
1478
        free(delaystring);
1437
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1438
 
        perror_plus("execl");
 
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
        }
1439
1490
      } else {
1440
1491
        int status;
1441
1492
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1465
1516
        }
1466
1517
      }
1467
1518
      free(fullname);
1468
 
      if(quit_now){
1469
 
        break;
 
1519
      if(debug){
 
1520
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1521
                     direntry->d_name);
1470
1522
      }
1471
1523
    }
1472
1524
    close(devnull);
1559
1611
        .group = 2 },
1560
1612
      { .name = "retry", .key = 132,
1561
1613
        .arg = "SECONDS",
1562
 
        .doc = "Retry interval used when denied by the mandos server",
 
1614
        .doc = "Retry interval used when denied by the Mandos server",
1563
1615
        .group = 2 },
1564
1616
      { .name = "network-hook-dir", .key = 133,
1565
1617
        .arg = "DIR",
1637
1689
        argp_state_help(state, state->out_stream,
1638
1690
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1691
      case 'V':                 /* --version */
1640
 
        fprintf_plus(state->out_stream,
1641
 
                     "Mandos plugin mandos-client: ");
1642
1692
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1643
1693
        exit(argp_err_exit_status);
1644
1694
        break;
1672
1722
  {
1673
1723
    /* Work around Debian bug #633582:
1674
1724
       <http://bugs.debian.org/633582> */
1675
 
    struct stat st;
1676
1725
    
1677
1726
    /* Re-raise priviliges */
1678
1727
    errno = 0;
1679
1728
    ret = seteuid(0);
1680
1729
    if(ret == -1){
1681
1730
      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");
 
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
    
 
1776
      /* Lower privileges */
 
1777
      errno = 0;
 
1778
      ret = seteuid(uid);
 
1779
      if(ret == -1){
 
1780
        perror_plus("seteuid");
 
1781
      }
1731
1782
    }
1732
1783
  }
1733
1784
  
1734
1785
  /* Run network hooks */
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
 
    }
 
1786
  if(not run_network_hooks("start", interface, delay)){
 
1787
    goto end;
1751
1788
  }
1752
1789
  
1753
1790
  if(not debug){
2192
2229
  if(gpgme_initialized){
2193
2230
    gpgme_release(mc.ctx);
2194
2231
  }
2195
 
 
 
2232
  
2196
2233
  /* Cleans up the circular linked list of Mandos servers the client
2197
2234
     has seen */
2198
2235
  if(mc.current_server != NULL){
2204
2241
    }
2205
2242
  }
2206
2243
  
 
2244
  /* Run network hooks */
 
2245
  run_network_hooks("stop", interface, delay);
 
2246
  
2207
2247
  /* Re-raise priviliges */
2208
2248
  {
2209
2249
    errno = 0;
2211
2251
    if(ret == -1){
2212
2252
      perror_plus("seteuid");
2213
2253
    }
2214
 
    /* Run network hooks */
2215
 
    if(not run_network_hooks("stop", interface, delay)){
2216
 
      goto end;
2217
 
    }
2218
2254
    
2219
2255
    /* Take down the network interface */
2220
2256
    if(take_down_interface and geteuid() == 0){