/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

* network-hooks.d/bridge: Bug fix - use the found brctl.

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() */
 
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() */
1301
1302
    }
1302
1303
    return 0;
1303
1304
  }
 
1305
  if(debug){
 
1306
    fprintf_plus(stderr, "Hook \"%s\" is acceptable\n",
 
1307
                 direntry->d_name);
 
1308
  }
1304
1309
  return 1;
1305
1310
}
1306
1311
 
1402
1407
      pid_t hook_pid = fork();
1403
1408
      if(hook_pid == 0){
1404
1409
        /* 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
        }
1405
1434
        dup2(devnull, STDIN_FILENO);
1406
1435
        close(devnull);
1407
1436
        dup2(STDERR_FILENO, STDOUT_FILENO);
1408
1437
        ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1409
1438
        if(ret == -1){
1410
1439
          perror_plus("setenv");
1411
 
          return false;
 
1440
          _exit(EX_OSERR);
1412
1441
        }
1413
1442
        ret = setenv("DEVICE", interface, 1);
1414
1443
        if(ret == -1){
1415
1444
          perror_plus("setenv");
1416
 
          return false;
 
1445
          _exit(EX_OSERR);
1417
1446
        }
1418
1447
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1419
1448
        if(ret == -1){
1420
1449
          perror_plus("setenv");
1421
 
          return false;
 
1450
          _exit(EX_OSERR);
1422
1451
        }
1423
1452
        ret = setenv("MODE", mode, 1);
1424
1453
        if(ret == -1){
1425
1454
          perror_plus("setenv");
1426
 
          return false;
 
1455
          _exit(EX_OSERR);
1427
1456
        }
1428
1457
        char *delaystring;
1429
1458
        ret = asprintf(&delaystring, "%f", delay);
1430
1459
        if(ret == -1){
1431
1460
          perror_plus("asprintf");
1432
 
          return false;
 
1461
          _exit(EX_OSERR);
1433
1462
        }
1434
1463
        ret = setenv("DELAY", delaystring, 1);
1435
1464
        if(ret == -1){
1436
1465
          free(delaystring);
1437
1466
          perror_plus("setenv");
1438
 
          return false;
 
1467
          _exit(EX_OSERR);
1439
1468
        }
1440
1469
        free(delaystring);
1441
1470
        ret = execl(fullname, direntry->d_name, mode, NULL);
1469
1498
        }
1470
1499
      }
1471
1500
      free(fullname);
1472
 
      if(quit_now){
1473
 
        break;
 
1501
      if(debug){
 
1502
        fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
 
1503
                     direntry->d_name);
1474
1504
      }
1475
1505
    }
1476
1506
    close(devnull);
1673
1703
    }
1674
1704
  }
1675
1705
    
1676
 
  if(getuid() == 0){
 
1706
  {
1677
1707
    /* Work around Debian bug #633582:
1678
1708
       <http://bugs.debian.org/633582> */
1679
 
    struct stat st;
1680
1709
    
1681
1710
    /* Re-raise priviliges */
1682
1711
    errno = 0;
1683
1712
    ret = seteuid(0);
1684
1713
    if(ret == -1){
1685
1714
      perror_plus("seteuid");
1686
 
    }
1687
 
    
1688
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1689
 
      int seckey_fd = open(seckey, O_RDONLY);
1690
 
      if(seckey_fd == -1){
1691
 
        perror_plus("open");
1692
 
      } else {
1693
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1694
 
        if(ret == -1){
1695
 
          perror_plus("fstat");
1696
 
        } else {
1697
 
          if(S_ISREG(st.st_mode)
1698
 
             and st.st_uid == 0 and st.st_gid == 0){
1699
 
            ret = fchown(seckey_fd, uid, gid);
1700
 
            if(ret == -1){
1701
 
              perror_plus("fchown");
1702
 
            }
1703
 
          }
1704
 
        }
1705
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1706
 
      }
1707
 
    }
1708
 
    
1709
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1710
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1711
 
      if(pubkey_fd == -1){
1712
 
        perror_plus("open");
1713
 
      } else {
1714
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1715
 
        if(ret == -1){
1716
 
          perror_plus("fstat");
1717
 
        } else {
1718
 
          if(S_ISREG(st.st_mode)
1719
 
             and st.st_uid == 0 and st.st_gid == 0){
1720
 
            ret = fchown(pubkey_fd, uid, gid);
1721
 
            if(ret == -1){
1722
 
              perror_plus("fchown");
1723
 
            }
1724
 
          }
1725
 
        }
1726
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1727
 
      }
1728
 
    }
1729
 
    
1730
 
    /* Lower privileges */
1731
 
    errno = 0;
1732
 
    ret = seteuid(uid);
1733
 
    if(ret == -1){
1734
 
      perror_plus("seteuid");
1735
 
    }
1736
 
  }
1737
 
  
1738
 
  /* Run network hooks */
1739
 
  {
1740
 
    if(getuid() == 0){
1741
 
      /* Re-raise priviliges */
1742
 
      errno = 0;
1743
 
      ret = seteuid(0);
1744
 
      if(ret == -1){
1745
 
        perror_plus("seteuid");
1746
 
      }
1747
 
    }
1748
 
    if(not run_network_hooks("start", interface, delay)){
1749
 
      goto end;
1750
 
    }
1751
 
    if(getuid() == 0){
 
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
    
1752
1760
      /* Lower privileges */
1753
1761
      errno = 0;
1754
1762
      ret = seteuid(uid);
1758
1766
    }
1759
1767
  }
1760
1768
  
 
1769
  /* Run network hooks */
 
1770
  if(not run_network_hooks("start", interface, delay)){
 
1771
    goto end;
 
1772
  }
 
1773
  
1761
1774
  if(not debug){
1762
1775
    avahi_set_log_function(empty_log);
1763
1776
  }
2200
2213
  if(gpgme_initialized){
2201
2214
    gpgme_release(mc.ctx);
2202
2215
  }
2203
 
 
 
2216
  
2204
2217
  /* Cleans up the circular linked list of Mandos servers the client
2205
2218
     has seen */
2206
2219
  if(mc.current_server != NULL){
2212
2225
    }
2213
2226
  }
2214
2227
  
 
2228
  /* Run network hooks */
 
2229
  run_network_hooks("stop", interface, delay);
 
2230
  
2215
2231
  /* Re-raise priviliges */
2216
2232
  {
2217
 
    if(getuid() == 0){
2218
 
      errno = 0;
2219
 
      ret = seteuid(0);
2220
 
      if(ret == -1){
2221
 
        perror_plus("seteuid");
2222
 
      }
2223
 
    }
2224
 
    /* Run network hooks */
2225
 
    if(not run_network_hooks("stop", interface, delay)){
2226
 
      goto end;
 
2233
    errno = 0;
 
2234
    ret = seteuid(0);
 
2235
    if(ret == -1){
 
2236
      perror_plus("seteuid");
2227
2237
    }
2228
2238
    
2229
2239
    /* Take down the network interface */
2244
2254
      }
2245
2255
    }
2246
2256
  }
2247
 
  if(getuid() == 0){
2248
 
    /* Lower privileges permanently */
2249
 
    errno = 0;
2250
 
    ret = setuid(uid);
2251
 
    if(ret == -1){
2252
 
      perror_plus("setuid");
2253
 
    }
 
2257
  /* Lower privileges permanently */
 
2258
  errno = 0;
 
2259
  ret = setuid(uid);
 
2260
  if(ret == -1){
 
2261
    perror_plus("setuid");
2254
2262
  }
2255
2263
  
2256
2264
  /* Removes the GPGME temp directory and all files inside */