/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

* plugins.d/mandos-client.c (runnable_hook): Add debug output.
 (run_network_hooks): Bug fix: Ignore quit_now.  Also add debug output.
 (main): Bug fix: Ignore return value from run_network_hooks when
         passing "stop".

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() */
1407
1406
      pid_t hook_pid = fork();
1408
1407
      if(hook_pid == 0){
1409
1408
        /* 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
1409
        dup2(devnull, STDIN_FILENO);
1435
1410
        close(devnull);
1436
1411
        dup2(STDERR_FILENO, STDOUT_FILENO);
1437
1412
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1438
1413
        if(ret == -1){
1439
1414
          perror_plus("setenv");
1440
 
          _exit(EX_OSERR);
 
1415
          return false;
1441
1416
        }
1442
1417
        ret = setenv("DEVICE", interface, 1);
1443
1418
        if(ret == -1){
1444
1419
          perror_plus("setenv");
1445
 
          _exit(EX_OSERR);
 
1420
          return false;
1446
1421
        }
1447
1422
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1448
1423
        if(ret == -1){
1449
1424
          perror_plus("setenv");
1450
 
          _exit(EX_OSERR);
 
1425
          return false;
1451
1426
        }
1452
1427
        ret = setenv("MODE", mode, 1);
1453
1428
        if(ret == -1){
1454
1429
          perror_plus("setenv");
1455
 
          _exit(EX_OSERR);
 
1430
          return false;
1456
1431
        }
1457
1432
        char *delaystring;
1458
1433
        ret = asprintf(&delaystring, "%f", delay);
1459
1434
        if(ret == -1){
1460
1435
          perror_plus("asprintf");
1461
 
          _exit(EX_OSERR);
 
1436
          return false;
1462
1437
        }
1463
1438
        ret = setenv("DELAY", delaystring, 1);
1464
1439
        if(ret == -1){
1465
1440
          free(delaystring);
1466
1441
          perror_plus("setenv");
1467
 
          _exit(EX_OSERR);
 
1442
          return false;
1468
1443
        }
1469
1444
        free(delaystring);
1470
1445
        ret = execl(fullname, direntry->d_name, mode, NULL);
1703
1678
    }
1704
1679
  }
1705
1680
    
1706
 
  {
 
1681
  if(getuid() == 0){
1707
1682
    /* Work around Debian bug #633582:
1708
1683
       <http://bugs.debian.org/633582> */
 
1684
    struct stat st;
1709
1685
    
1710
1686
    /* Re-raise priviliges */
1711
1687
    errno = 0;
1712
1688
    ret = seteuid(0);
1713
1689
    if(ret == -1){
1714
1690
      perror_plus("seteuid");
1715
 
    } else {
1716
 
      struct stat st;
1717
 
      
1718
 
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1719
 
        int seckey_fd = open(seckey, O_RDONLY);
1720
 
        if(seckey_fd == -1){
1721
 
          perror_plus("open");
1722
 
        } else {
1723
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1724
 
          if(ret == -1){
1725
 
            perror_plus("fstat");
1726
 
          } else {
1727
 
            if(S_ISREG(st.st_mode)
1728
 
               and st.st_uid == 0 and st.st_gid == 0){
1729
 
              ret = fchown(seckey_fd, uid, gid);
1730
 
              if(ret == -1){
1731
 
                perror_plus("fchown");
1732
 
              }
1733
 
            }
1734
 
          }
1735
 
          TEMP_FAILURE_RETRY(close(seckey_fd));
1736
 
        }
1737
 
      }
1738
 
    
1739
 
      if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1740
 
        int pubkey_fd = open(pubkey, O_RDONLY);
1741
 
        if(pubkey_fd == -1){
1742
 
          perror_plus("open");
1743
 
        } else {
1744
 
          ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1745
 
          if(ret == -1){
1746
 
            perror_plus("fstat");
1747
 
          } else {
1748
 
            if(S_ISREG(st.st_mode)
1749
 
               and st.st_uid == 0 and st.st_gid == 0){
1750
 
              ret = fchown(pubkey_fd, uid, gid);
1751
 
              if(ret == -1){
1752
 
                perror_plus("fchown");
1753
 
              }
1754
 
            }
1755
 
          }
1756
 
          TEMP_FAILURE_RETRY(close(pubkey_fd));
1757
 
        }
1758
 
      }
1759
 
    
 
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){
1760
1757
      /* Lower privileges */
1761
1758
      errno = 0;
1762
1759
      ret = seteuid(uid);
1766
1763
    }
1767
1764
  }
1768
1765
  
1769
 
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    goto end;
1772
 
  }
1773
 
  
1774
1766
  if(not debug){
1775
1767
    avahi_set_log_function(empty_log);
1776
1768
  }
2225
2217
    }
2226
2218
  }
2227
2219
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
2230
 
  
2231
2220
  /* Re-raise priviliges */
2232
2221
  {
2233
 
    errno = 0;
2234
 
    ret = seteuid(0);
2235
 
    if(ret == -1){
2236
 
      perror_plus("seteuid");
 
2222
    if(getuid() == 0){
 
2223
      errno = 0;
 
2224
      ret = seteuid(0);
 
2225
      if(ret == -1){
 
2226
        perror_plus("seteuid");
 
2227
      }
2237
2228
    }
2238
2229
    
 
2230
    /* Run network hooks */
 
2231
    run_network_hooks("stop", interface, delay);
 
2232
    
2239
2233
    /* Take down the network interface */
2240
2234
    if(take_down_interface and geteuid() == 0){
2241
2235
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2254
2248
      }
2255
2249
    }
2256
2250
  }
2257
 
  /* Lower privileges permanently */
2258
 
  errno = 0;
2259
 
  ret = setuid(uid);
2260
 
  if(ret == -1){
2261
 
    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
    }
2262
2258
  }
2263
2259
  
2264
2260
  /* Removes the GPGME temp directory and all files inside */