/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

* 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() */
1302
1301
    }
1303
1302
    return 0;
1304
1303
  }
1305
 
  if(debug){
1306
 
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
1307
 
                 direntry->d_name);
1308
 
  }
1309
1304
  return 1;
1310
1305
}
1311
1306
 
1393
1388
  } else {
1394
1389
    int devnull = open("/dev/null", O_RDONLY);
1395
1390
    for(int i = 0; i < numhooks; i++){
1396
 
      direntry = direntries[i];
 
1391
      direntry = direntries[0];
1397
1392
      char *fullname = NULL;
1398
1393
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1399
1394
      if(ret < 0){
1400
1395
        perror_plus("asprintf");
1401
1396
        continue;
1402
1397
      }
1403
 
      if(debug){
1404
 
        fprintf_plus(stderr, "Running network hook \"%s\"\n",
1405
 
                     direntry->d_name);
1406
 
      }
1407
1398
      pid_t hook_pid = fork();
1408
1399
      if(hook_pid == 0){
1409
1400
        /* Child */
1410
 
        /* Raise privileges */
1411
 
        errno = 0;
1412
 
        ret = seteuid(0);
1413
 
        if(ret == -1){
1414
 
          perror_plus("seteuid");
1415
 
        }
1416
 
        /* Raise privileges even more */
1417
 
        errno = 0;
1418
 
        ret = setuid(0);
1419
 
        if(ret == -1){
1420
 
          perror_plus("setuid");
1421
 
        }
1422
 
        /* Set group */
1423
 
        errno = 0;
1424
 
        ret = setgid(0);
1425
 
        if(ret == -1){
1426
 
          perror_plus("setgid");
1427
 
        }
1428
 
        /* Reset supplementary groups */
1429
 
        errno = 0;
1430
 
        ret = setgroups(0, NULL);
1431
 
        if(ret == -1){
1432
 
          perror_plus("setgroups");
1433
 
        }
1434
 
        fprintf_plus(stderr, "Child: getuid() = %d\n", getuid());
1435
 
        fprintf_plus(stderr, "Child: geteuid() = %d\n", geteuid());
1436
1401
        dup2(devnull, STDIN_FILENO);
1437
1402
        close(devnull);
1438
1403
        dup2(STDERR_FILENO, STDOUT_FILENO);
1439
1404
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1440
1405
        if(ret == -1){
1441
1406
          perror_plus("setenv");
1442
 
          _exit(EX_OSERR);
 
1407
          return false;
1443
1408
        }
1444
1409
        ret = setenv("DEVICE", interface, 1);
1445
1410
        if(ret == -1){
1446
1411
          perror_plus("setenv");
1447
 
          _exit(EX_OSERR);
 
1412
          return false;
1448
1413
        }
1449
1414
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1450
1415
        if(ret == -1){
1451
1416
          perror_plus("setenv");
1452
 
          _exit(EX_OSERR);
 
1417
          return false;
1453
1418
        }
1454
1419
        ret = setenv("MODE", mode, 1);
1455
1420
        if(ret == -1){
1456
1421
          perror_plus("setenv");
1457
 
          _exit(EX_OSERR);
 
1422
          return false;
1458
1423
        }
1459
1424
        char *delaystring;
1460
1425
        ret = asprintf(&delaystring, "%f", delay);
1461
1426
        if(ret == -1){
1462
1427
          perror_plus("asprintf");
1463
 
          _exit(EX_OSERR);
 
1428
          return false;
1464
1429
        }
1465
1430
        ret = setenv("DELAY", delaystring, 1);
1466
1431
        if(ret == -1){
1467
1432
          free(delaystring);
1468
1433
          perror_plus("setenv");
1469
 
          _exit(EX_OSERR);
 
1434
          return false;
1470
1435
        }
1471
1436
        free(delaystring);
1472
1437
        ret = execl(fullname, direntry->d_name, mode, NULL);
1500
1465
        }
1501
1466
      }
1502
1467
      free(fullname);
1503
 
      if(debug){
1504
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1505
 
                     direntry->d_name);
 
1468
      if(quit_now){
 
1469
        break;
1506
1470
      }
1507
1471
    }
1508
1472
    close(devnull);
1708
1672
  {
1709
1673
    /* Work around Debian bug #633582:
1710
1674
       <http://bugs.debian.org/633582> */
 
1675
    struct stat st;
1711
1676
    
1712
1677
    /* Re-raise priviliges */
1713
1678
    errno = 0;
1714
1679
    ret = seteuid(0);
1715
1680
    if(ret == -1){
1716
1681
      perror_plus("seteuid");
1717
 
    } else {
1718
 
      struct stat st;
1719
 
      
1720
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1721
 
        int seckey_fd = open(seckey, O_RDONLY);
1722
 
        if(seckey_fd == -1){
1723
 
          perror_plus("open");
1724
 
        } else {
1725
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1726
 
          if(ret == -1){
1727
 
            perror_plus("fstat");
1728
 
          } else {
1729
 
            if(S_ISREG(st.st_mode)
1730
 
               and st.st_uid == 0 and st.st_gid == 0){
1731
 
              ret = fchown(seckey_fd, uid, gid);
1732
 
              if(ret == -1){
1733
 
                perror_plus("fchown");
1734
 
              }
1735
 
            }
1736
 
          }
1737
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1738
 
        }
1739
 
      }
1740
 
    
1741
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1742
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1743
 
        if(pubkey_fd == -1){
1744
 
          perror_plus("open");
1745
 
        } else {
1746
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1747
 
          if(ret == -1){
1748
 
            perror_plus("fstat");
1749
 
          } else {
1750
 
            if(S_ISREG(st.st_mode)
1751
 
               and st.st_uid == 0 and st.st_gid == 0){
1752
 
              ret = fchown(pubkey_fd, uid, gid);
1753
 
              if(ret == -1){
1754
 
                perror_plus("fchown");
1755
 
              }
1756
 
            }
1757
 
          }
1758
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1759
 
        }
1760
 
      }
1761
 
    
1762
 
      /* Lower privileges */
1763
 
      errno = 0;
1764
 
      ret = seteuid(uid);
1765
 
      if(ret == -1){
1766
 
        perror_plus("seteuid");
1767
 
      }
 
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");
1768
1731
    }
1769
1732
  }
1770
1733
  
1771
1734
  /* Run network hooks */
1772
 
  if(not run_network_hooks("start", interface, delay)){
1773
 
    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
    }
1774
1751
  }
1775
1752
  
1776
1753
  if(not debug){
2215
2192
  if(gpgme_initialized){
2216
2193
    gpgme_release(mc.ctx);
2217
2194
  }
2218
 
  
 
2195
 
2219
2196
  /* Cleans up the circular linked list of Mandos servers the client
2220
2197
     has seen */
2221
2198
  if(mc.current_server != NULL){
2227
2204
    }
2228
2205
  }
2229
2206
  
2230
 
  /* Run network hooks */
2231
 
  run_network_hooks("stop", interface, delay);
2232
 
  
2233
2207
  /* Re-raise priviliges */
2234
2208
  {
2235
2209
    errno = 0;
2237
2211
    if(ret == -1){
2238
2212
      perror_plus("seteuid");
2239
2213
    }
 
2214
    /* Run network hooks */
 
2215
    if(not run_network_hooks("stop", interface, delay)){
 
2216
      goto end;
 
2217
    }
2240
2218
    
2241
2219
    /* Take down the network interface */
2242
2220
    if(take_down_interface and geteuid() == 0){