/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

* plugins.d/mandos-client.c: Merge unified printing system.

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
1401
        dup2(devnull, STDIN_FILENO);
1435
1402
        close(devnull);
1436
1403
        dup2(STDERR_FILENO, STDOUT_FILENO);
1437
1404
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1438
1405
        if(ret == -1){
1439
1406
          perror_plus("setenv");
1440
 
          _exit(EX_OSERR);
 
1407
          return false;
1441
1408
        }
1442
1409
        ret = setenv("DEVICE", interface, 1);
1443
1410
        if(ret == -1){
1444
1411
          perror_plus("setenv");
1445
 
          _exit(EX_OSERR);
 
1412
          return false;
1446
1413
        }
1447
1414
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1448
1415
        if(ret == -1){
1449
1416
          perror_plus("setenv");
1450
 
          _exit(EX_OSERR);
 
1417
          return false;
1451
1418
        }
1452
1419
        ret = setenv("MODE", mode, 1);
1453
1420
        if(ret == -1){
1454
1421
          perror_plus("setenv");
1455
 
          _exit(EX_OSERR);
 
1422
          return false;
1456
1423
        }
1457
1424
        char *delaystring;
1458
1425
        ret = asprintf(&delaystring, "%f", delay);
1459
1426
        if(ret == -1){
1460
1427
          perror_plus("asprintf");
1461
 
          _exit(EX_OSERR);
 
1428
          return false;
1462
1429
        }
1463
1430
        ret = setenv("DELAY", delaystring, 1);
1464
1431
        if(ret == -1){
1465
1432
          free(delaystring);
1466
1433
          perror_plus("setenv");
1467
 
          _exit(EX_OSERR);
 
1434
          return false;
1468
1435
        }
1469
1436
        free(delaystring);
1470
1437
        ret = execl(fullname, direntry->d_name, mode, NULL);
1498
1465
        }
1499
1466
      }
1500
1467
      free(fullname);
1501
 
      if(debug){
1502
 
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1503
 
                     direntry->d_name);
 
1468
      if(quit_now){
 
1469
        break;
1504
1470
      }
1505
1471
    }
1506
1472
    close(devnull);
1706
1672
  {
1707
1673
    /* Work around Debian bug #633582:
1708
1674
       <http://bugs.debian.org/633582> */
 
1675
    struct stat st;
1709
1676
    
1710
1677
    /* Re-raise priviliges */
1711
1678
    errno = 0;
1712
1679
    ret = seteuid(0);
1713
1680
    if(ret == -1){
1714
1681
      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
 
    
1760
 
      /* Lower privileges */
1761
 
      errno = 0;
1762
 
      ret = seteuid(uid);
1763
 
      if(ret == -1){
1764
 
        perror_plus("seteuid");
1765
 
      }
 
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");
1766
1731
    }
1767
1732
  }
1768
1733
  
1769
1734
  /* Run network hooks */
1770
 
  if(not run_network_hooks("start", interface, delay)){
1771
 
    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
    }
1772
1751
  }
1773
1752
  
1774
1753
  if(not debug){
2213
2192
  if(gpgme_initialized){
2214
2193
    gpgme_release(mc.ctx);
2215
2194
  }
2216
 
  
 
2195
 
2217
2196
  /* Cleans up the circular linked list of Mandos servers the client
2218
2197
     has seen */
2219
2198
  if(mc.current_server != NULL){
2225
2204
    }
2226
2205
  }
2227
2206
  
2228
 
  /* Run network hooks */
2229
 
  run_network_hooks("stop", interface, delay);
2230
 
  
2231
2207
  /* Re-raise priviliges */
2232
2208
  {
2233
2209
    errno = 0;
2235
2211
    if(ret == -1){
2236
2212
      perror_plus("seteuid");
2237
2213
    }
 
2214
    /* Run network hooks */
 
2215
    if(not run_network_hooks("stop", interface, delay)){
 
2216
      goto end;
 
2217
    }
2238
2218
    
2239
2219
    /* Take down the network interface */
2240
2220
    if(take_down_interface and geteuid() == 0){