/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

* README: Hint that the intro(8mandos) manual page is in the server
          package.

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
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
/* 
1029
1033
      if(ret == 0){
1030
1034
        avahi_simple_poll_quit(mc.simple_poll);
1031
1035
      } else {
1032
 
        ret = add_server(ip, port, interface,
1033
 
                         avahi_proto_to_af(proto));
 
1036
        if(not add_server(ip, port, interface,
 
1037
                          avahi_proto_to_af(proto))){
 
1038
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1039
                       " list\n", name);
 
1040
        }
1034
1041
      }
1035
1042
    }
1036
1043
  }
1301
1308
    }
1302
1309
    return 0;
1303
1310
  }
 
1311
  if(debug){
 
1312
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1313
                 direntry->d_name);
 
1314
  }
1304
1315
  return 1;
1305
1316
}
1306
1317
 
1388
1399
  } else {
1389
1400
    int devnull = open("/dev/null", O_RDONLY);
1390
1401
    for(int i = 0; i < numhooks; i++){
1391
 
      direntry = direntries[0];
 
1402
      direntry = direntries[i];
1392
1403
      char *fullname = NULL;
1393
1404
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1394
1405
      if(ret < 0){
1395
1406
        perror_plus("asprintf");
1396
1407
        continue;
1397
1408
      }
 
1409
      if(debug){
 
1410
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
 
1411
                     direntry->d_name);
 
1412
      }
1398
1413
      pid_t hook_pid = fork();
1399
1414
      if(hook_pid == 0){
1400
1415
        /* Child */
 
1416
        /* Raise privileges */
 
1417
        errno = 0;
 
1418
        ret = seteuid(0);
 
1419
        if(ret == -1){
 
1420
          perror_plus("seteuid");
 
1421
        }
 
1422
        /* Raise privileges even more */
 
1423
        errno = 0;
 
1424
        ret = setuid(0);
 
1425
        if(ret == -1){
 
1426
          perror_plus("setuid");
 
1427
        }
 
1428
        /* Set group */
 
1429
        errno = 0;
 
1430
        ret = setgid(0);
 
1431
        if(ret == -1){
 
1432
          perror_plus("setgid");
 
1433
        }
 
1434
        /* Reset supplementary groups */
 
1435
        errno = 0;
 
1436
        ret = setgroups(0, NULL);
 
1437
        if(ret == -1){
 
1438
          perror_plus("setgroups");
 
1439
        }
1401
1440
        dup2(devnull, STDIN_FILENO);
1402
1441
        close(devnull);
1403
1442
        dup2(STDERR_FILENO, STDOUT_FILENO);
1404
1443
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1405
1444
        if(ret == -1){
1406
1445
          perror_plus("setenv");
1407
 
          return false;
 
1446
          _exit(EX_OSERR);
1408
1447
        }
1409
1448
        ret = setenv("DEVICE", interface, 1);
1410
1449
        if(ret == -1){
1411
1450
          perror_plus("setenv");
1412
 
          return false;
 
1451
          _exit(EX_OSERR);
1413
1452
        }
1414
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1453
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1415
1454
        if(ret == -1){
1416
1455
          perror_plus("setenv");
1417
 
          return false;
 
1456
          _exit(EX_OSERR);
1418
1457
        }
1419
1458
        ret = setenv("MODE", mode, 1);
1420
1459
        if(ret == -1){
1421
1460
          perror_plus("setenv");
1422
 
          return false;
 
1461
          _exit(EX_OSERR);
1423
1462
        }
1424
1463
        char *delaystring;
1425
1464
        ret = asprintf(&delaystring, "%f", delay);
1426
1465
        if(ret == -1){
1427
1466
          perror_plus("asprintf");
1428
 
          return false;
 
1467
          _exit(EX_OSERR);
1429
1468
        }
1430
1469
        ret = setenv("DELAY", delaystring, 1);
1431
1470
        if(ret == -1){
1432
1471
          free(delaystring);
1433
1472
          perror_plus("setenv");
1434
 
          return false;
 
1473
          _exit(EX_OSERR);
1435
1474
        }
1436
1475
        free(delaystring);
1437
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1438
 
        perror_plus("execl");
 
1476
        if(connect_to != NULL){
 
1477
          ret = setenv("CONNECT", connect_to, 1);
 
1478
          if(ret == -1){
 
1479
            perror_plus("setenv");
 
1480
            _exit(EX_OSERR);
 
1481
          }
 
1482
        }
 
1483
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1484
          perror_plus("execl");
 
1485
          _exit(EXIT_FAILURE);
 
1486
        }
1439
1487
      } else {
1440
1488
        int status;
1441
1489
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1465
1513
        }
1466
1514
      }
1467
1515
      free(fullname);
1468
 
      if(quit_now){
1469
 
        break;
 
1516
      if(debug){
 
1517
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1518
                     direntry->d_name);
1470
1519
      }
1471
1520
    }
1472
1521
    close(devnull);
1559
1608
        .group = 2 },
1560
1609
      { .name = "retry", .key = 132,
1561
1610
        .arg = "SECONDS",
1562
 
        .doc = "Retry interval used when denied by the mandos server",
 
1611
        .doc = "Retry interval used when denied by the Mandos server",
1563
1612
        .group = 2 },
1564
1613
      { .name = "network-hook-dir", .key = 133,
1565
1614
        .arg = "DIR",
1637
1686
        argp_state_help(state, state->out_stream,
1638
1687
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1639
1688
      case 'V':                 /* --version */
1640
 
        fprintf_plus(state->out_stream,
1641
 
                     "Mandos plugin mandos-client: ");
1642
1689
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1643
1690
        exit(argp_err_exit_status);
1644
1691
        break;
1672
1719
  {
1673
1720
    /* Work around Debian bug #633582:
1674
1721
       <http://bugs.debian.org/633582> */
1675
 
    struct stat st;
1676
1722
    
1677
1723
    /* Re-raise priviliges */
1678
1724
    errno = 0;
1679
1725
    ret = seteuid(0);
1680
1726
    if(ret == -1){
1681
1727
      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");
 
1728
    } else {
 
1729
      struct stat st;
 
1730
      
 
1731
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1732
        int seckey_fd = open(seckey, O_RDONLY);
 
1733
        if(seckey_fd == -1){
 
1734
          perror_plus("open");
 
1735
        } else {
 
1736
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1737
          if(ret == -1){
 
1738
            perror_plus("fstat");
 
1739
          } else {
 
1740
            if(S_ISREG(st.st_mode)
 
1741
               and st.st_uid == 0 and st.st_gid == 0){
 
1742
              ret = fchown(seckey_fd, uid, gid);
 
1743
              if(ret == -1){
 
1744
                perror_plus("fchown");
 
1745
              }
 
1746
            }
 
1747
          }
 
1748
          TEMP_FAILURE_RETRY(close(seckey_fd));
 
1749
        }
 
1750
      }
 
1751
    
 
1752
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1753
        int pubkey_fd = open(pubkey, O_RDONLY);
 
1754
        if(pubkey_fd == -1){
 
1755
          perror_plus("open");
 
1756
        } else {
 
1757
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1758
          if(ret == -1){
 
1759
            perror_plus("fstat");
 
1760
          } else {
 
1761
            if(S_ISREG(st.st_mode)
 
1762
               and st.st_uid == 0 and st.st_gid == 0){
 
1763
              ret = fchown(pubkey_fd, uid, gid);
 
1764
              if(ret == -1){
 
1765
                perror_plus("fchown");
 
1766
              }
 
1767
            }
 
1768
          }
 
1769
          TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1770
        }
 
1771
      }
 
1772
    
 
1773
      /* Lower privileges */
 
1774
      errno = 0;
 
1775
      ret = seteuid(uid);
 
1776
      if(ret == -1){
 
1777
        perror_plus("seteuid");
 
1778
      }
1731
1779
    }
1732
1780
  }
1733
1781
  
1734
1782
  /* 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
 
    }
 
1783
  if(not run_network_hooks("start", interface, delay)){
 
1784
    goto end;
1751
1785
  }
1752
1786
  
1753
1787
  if(not debug){
2192
2226
  if(gpgme_initialized){
2193
2227
    gpgme_release(mc.ctx);
2194
2228
  }
2195
 
 
 
2229
  
2196
2230
  /* Cleans up the circular linked list of Mandos servers the client
2197
2231
     has seen */
2198
2232
  if(mc.current_server != NULL){
2204
2238
    }
2205
2239
  }
2206
2240
  
 
2241
  /* Run network hooks */
 
2242
  run_network_hooks("stop", interface, delay);
 
2243
  
2207
2244
  /* Re-raise priviliges */
2208
2245
  {
2209
2246
    errno = 0;
2211
2248
    if(ret == -1){
2212
2249
      perror_plus("seteuid");
2213
2250
    }
2214
 
    /* Run network hooks */
2215
 
    if(not run_network_hooks("stop", interface, delay)){
2216
 
      goto end;
2217
 
    }
2218
2251
    
2219
2252
    /* Take down the network interface */
2220
2253
    if(take_down_interface and geteuid() == 0){