/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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-06 19:08:17 UTC
  • mto: (237.7.144 trunk)
  • mto: This revision was merged to the branch mainline in revision 302.
  • Revision ID: teddy@recompile.se-20120606190817-c2x5o98vzn07xufd
* plugins.d/mandos-client.c: Refactoring.
  (uid, gid): New global variables.
  (raise_privileges, raise_privileges_permanently, lower_privileges,
  bring_up_interface): New functions.
  (main): Changed to use new functions.

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
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
135
135
static const char sys_class_net[] = "/sys/class/net";
136
136
char *connect_to = NULL;
137
137
const char *hookdir = HOOKDIR;
 
138
uid_t uid = 65534;
 
139
gid_t gid = 65534;
138
140
 
139
141
/* Doubly linked list that need to be circularly linked when used */
140
142
typedef struct server{
821
823
    goto mandos_end;
822
824
  }
823
825
  
824
 
  /* Spurious warning from -Wint-to-pointer-cast */
825
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
826
  /* This casting via intptr_t is to eliminate warning about casting
 
827
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
828
     function "set-session-transport-fd!" does it. */
 
829
  gnutls_transport_set_ptr(session,
 
830
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
826
831
  
827
832
  if(quit_now){
828
833
    errno = EINTR;
1387
1392
  }
1388
1393
}
1389
1394
 
 
1395
/* Set effective uid to 0, return errno */
 
1396
int raise_privileges(void){
 
1397
  int old_errno = errno;
 
1398
  int ret_errno = 0;
 
1399
  errno = 0;
 
1400
  if(seteuid(0) == -1){
 
1401
    perror_plus("seteuid");
 
1402
  }
 
1403
  ret_errno = errno;
 
1404
  errno = old_errno;
 
1405
  return ret_errno;
 
1406
}
 
1407
 
 
1408
/* Set effective and real user ID to 0.  Return errno. */
 
1409
int raise_privileges_permanently(void){
 
1410
  int old_errno = errno;
 
1411
  int ret_errno = raise_privileges();
 
1412
  if(ret_errno != 0){
 
1413
    errno = old_errno;
 
1414
    return ret_errno;
 
1415
  }
 
1416
  errno = 0;
 
1417
  if(setuid(0) == -1){
 
1418
    perror_plus("seteuid");
 
1419
  }
 
1420
  ret_errno = errno;
 
1421
  errno = old_errno;
 
1422
  return ret_errno;
 
1423
}
 
1424
 
 
1425
/* Set effective user ID to unprivileged saved user ID */
 
1426
int lower_privileges(void){
 
1427
  int old_errno = errno;
 
1428
  int ret_errno = 0;
 
1429
  errno = 0;
 
1430
  if(seteuid(uid) == -1){
 
1431
    perror_plus("seteuid");
 
1432
  }
 
1433
  ret_errno = errno;
 
1434
  errno = old_errno;
 
1435
  return ret_errno;
 
1436
}
 
1437
 
1390
1438
bool run_network_hooks(const char *mode, const char *interface,
1391
1439
                       const float delay){
1392
1440
  struct dirent **direntries;
1414
1462
      if(hook_pid == 0){
1415
1463
        /* Child */
1416
1464
        /* 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
 
        }
 
1465
        raise_privileges_permanently();
1428
1466
        /* Set group */
1429
1467
        errno = 0;
1430
1468
        ret = setgid(0);
1450
1488
          perror_plus("setenv");
1451
1489
          _exit(EX_OSERR);
1452
1490
        }
1453
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1491
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1454
1492
        if(ret == -1){
1455
1493
          perror_plus("setenv");
1456
1494
          _exit(EX_OSERR);
1473
1511
          _exit(EX_OSERR);
1474
1512
        }
1475
1513
        free(delaystring);
 
1514
        if(connect_to != NULL){
 
1515
          ret = setenv("CONNECT", connect_to, 1);
 
1516
          if(ret == -1){
 
1517
            perror_plus("setenv");
 
1518
            _exit(EX_OSERR);
 
1519
          }
 
1520
        }
1476
1521
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1477
1522
          perror_plus("execl");
1478
1523
          _exit(EXIT_FAILURE);
1516
1561
  return true;
1517
1562
}
1518
1563
 
 
1564
int bring_up_interface(const char * const interface, const float delay){
 
1565
  int sd = -1;
 
1566
  int ret;
 
1567
  struct ifreq network;
 
1568
  AvahiIfIndex if_index = (AvahiIfIndex)if_nametoindex(interface);
 
1569
  if(if_index == 0){
 
1570
    fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
 
1571
    return EX_UNAVAILABLE;
 
1572
  }
 
1573
  
 
1574
  if(quit_now){
 
1575
    return EINTR;
 
1576
  }
 
1577
  
 
1578
  /* Re-raise priviliges */
 
1579
  raise_privileges();
 
1580
    
 
1581
#ifdef __linux__
 
1582
  /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1583
     messages about the network interface to mess up the prompt */
 
1584
  ret = klogctl(8, NULL, 5);
 
1585
  bool restore_loglevel = true;
 
1586
  if(ret == -1){
 
1587
    restore_loglevel = false;
 
1588
    perror_plus("klogctl");
 
1589
  }
 
1590
#endif  /* __linux__ */
 
1591
    
 
1592
  sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1593
  if(sd < 0){
 
1594
    perror_plus("socket");
 
1595
#ifdef __linux__
 
1596
    if(restore_loglevel){
 
1597
      ret = klogctl(7, NULL, 0);
 
1598
      if(ret == -1){
 
1599
        perror_plus("klogctl");
 
1600
      }
 
1601
    }
 
1602
#endif  /* __linux__ */
 
1603
    /* Lower privileges */
 
1604
    lower_privileges();
 
1605
    return EX_OSERR;
 
1606
;
 
1607
  }
 
1608
  strcpy(network.ifr_name, interface);
 
1609
  ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1610
  if(ret == -1){
 
1611
    perror_plus("ioctl SIOCGIFFLAGS");
 
1612
#ifdef __linux__
 
1613
    if(restore_loglevel){
 
1614
      ret = klogctl(7, NULL, 0);
 
1615
      if(ret == -1){
 
1616
        perror_plus("klogctl");
 
1617
      }
 
1618
    }
 
1619
#endif  /* __linux__ */
 
1620
    /* Lower privileges */
 
1621
    lower_privileges();
 
1622
    return EX_OSERR;
 
1623
  }
 
1624
  if((network.ifr_flags & IFF_UP) == 0){
 
1625
    network.ifr_flags |= IFF_UP;
 
1626
    ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1627
    if(ret == -1){
 
1628
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1629
#ifdef __linux__
 
1630
      if(restore_loglevel){
 
1631
        ret = klogctl(7, NULL, 0);
 
1632
        if(ret == -1){
 
1633
          perror_plus("klogctl");
 
1634
        }
 
1635
      }
 
1636
#endif  /* __linux__ */
 
1637
        /* Lower privileges */
 
1638
      lower_privileges();
 
1639
      return EX_OSERR;
 
1640
    }
 
1641
  }
 
1642
  /* Sleep checking until interface is running.
 
1643
     Check every 0.25s, up to total time of delay */
 
1644
  for(int i=0; i < delay * 4; i++){
 
1645
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1646
    if(ret == -1){
 
1647
      perror_plus("ioctl SIOCGIFFLAGS");
 
1648
    } else if(network.ifr_flags & IFF_RUNNING){
 
1649
      break;
 
1650
    }
 
1651
    struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1652
    ret = nanosleep(&sleeptime, NULL);
 
1653
    if(ret == -1 and errno != EINTR){
 
1654
      perror_plus("nanosleep");
 
1655
    }
 
1656
  }
 
1657
  /* Close the socket */
 
1658
  ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1659
  if(ret == -1){
 
1660
    perror_plus("close");
 
1661
  }
 
1662
#ifdef __linux__
 
1663
  if(restore_loglevel){
 
1664
    /* Restores kernel loglevel to default */
 
1665
    ret = klogctl(7, NULL, 0);
 
1666
    if(ret == -1){
 
1667
      perror_plus("klogctl");
 
1668
    }
 
1669
  }
 
1670
#endif  /* __linux__ */
 
1671
  /* Lower privileges */
 
1672
  lower_privileges();
 
1673
  return errno;
 
1674
}
 
1675
 
1519
1676
int main(int argc, char *argv[]){
1520
1677
  AvahiSServiceBrowser *sb = NULL;
1521
1678
  int error;
1527
1684
  struct ifreq network;
1528
1685
  int sd = -1;
1529
1686
  bool take_down_interface = false;
1530
 
  uid_t uid;
1531
 
  gid_t gid;
1532
1687
  char tempdir[] = "/tmp/mandosXXXXXX";
1533
1688
  bool tempdir_created = false;
1534
1689
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1601
1756
        .group = 2 },
1602
1757
      { .name = "retry", .key = 132,
1603
1758
        .arg = "SECONDS",
1604
 
        .doc = "Retry interval used when denied by the mandos server",
 
1759
        .doc = "Retry interval used when denied by the Mandos server",
1605
1760
        .group = 2 },
1606
1761
      { .name = "network-hook-dir", .key = 133,
1607
1762
        .arg = "DIR",
1714
1869
       <http://bugs.debian.org/633582> */
1715
1870
    
1716
1871
    /* Re-raise priviliges */
1717
 
    errno = 0;
1718
 
    ret = seteuid(0);
1719
 
    if(ret == -1){
1720
 
      perror_plus("seteuid");
1721
 
    } else {
 
1872
    if(raise_privileges() == 0){
1722
1873
      struct stat st;
1723
1874
      
1724
1875
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1889
2040
  }
1890
2041
  
1891
2042
  /* If the interface is down, bring it up */
1892
 
  if(strcmp(interface, "none") != 0){
1893
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1894
 
    if(if_index == 0){
1895
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1896
 
      exitcode = EX_UNAVAILABLE;
1897
 
      goto end;
1898
 
    }
1899
 
    
1900
 
    if(quit_now){
1901
 
      goto end;
1902
 
    }
1903
 
    
1904
 
    /* Re-raise priviliges */
1905
 
    errno = 0;
1906
 
    ret = seteuid(0);
1907
 
    if(ret == -1){
1908
 
      perror_plus("seteuid");
1909
 
    }
1910
 
    
1911
 
#ifdef __linux__
1912
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1913
 
       messages about the network interface to mess up the prompt */
1914
 
    ret = klogctl(8, NULL, 5);
1915
 
    bool restore_loglevel = true;
1916
 
    if(ret == -1){
1917
 
      restore_loglevel = false;
1918
 
      perror_plus("klogctl");
1919
 
    }
1920
 
#endif  /* __linux__ */
1921
 
    
1922
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1923
 
    if(sd < 0){
1924
 
      perror_plus("socket");
1925
 
      exitcode = EX_OSERR;
1926
 
#ifdef __linux__
1927
 
      if(restore_loglevel){
1928
 
        ret = klogctl(7, NULL, 0);
1929
 
        if(ret == -1){
1930
 
          perror_plus("klogctl");
1931
 
        }
1932
 
      }
1933
 
#endif  /* __linux__ */
1934
 
      /* Lower privileges */
1935
 
      errno = 0;
1936
 
      ret = seteuid(uid);
1937
 
      if(ret == -1){
1938
 
        perror_plus("seteuid");
1939
 
      }
1940
 
      goto end;
1941
 
    }
1942
 
    strcpy(network.ifr_name, interface);
1943
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1944
 
    if(ret == -1){
1945
 
      perror_plus("ioctl SIOCGIFFLAGS");
1946
 
#ifdef __linux__
1947
 
      if(restore_loglevel){
1948
 
        ret = klogctl(7, NULL, 0);
1949
 
        if(ret == -1){
1950
 
          perror_plus("klogctl");
1951
 
        }
1952
 
      }
1953
 
#endif  /* __linux__ */
1954
 
      exitcode = EX_OSERR;
1955
 
      /* Lower privileges */
1956
 
      errno = 0;
1957
 
      ret = seteuid(uid);
1958
 
      if(ret == -1){
1959
 
        perror_plus("seteuid");
1960
 
      }
1961
 
      goto end;
1962
 
    }
1963
 
    if((network.ifr_flags & IFF_UP) == 0){
1964
 
      network.ifr_flags |= IFF_UP;
1965
 
      take_down_interface = true;
1966
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1967
 
      if(ret == -1){
1968
 
        take_down_interface = false;
1969
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1970
 
        exitcode = EX_OSERR;
1971
 
#ifdef __linux__
1972
 
        if(restore_loglevel){
1973
 
          ret = klogctl(7, NULL, 0);
1974
 
          if(ret == -1){
1975
 
            perror_plus("klogctl");
1976
 
          }
1977
 
        }
1978
 
#endif  /* __linux__ */
1979
 
        /* Lower privileges */
1980
 
        errno = 0;
1981
 
        ret = seteuid(uid);
1982
 
        if(ret == -1){
1983
 
          perror_plus("seteuid");
1984
 
        }
1985
 
        goto end;
1986
 
      }
1987
 
    }
1988
 
    /* Sleep checking until interface is running.
1989
 
       Check every 0.25s, up to total time of delay */
1990
 
    for(int i=0; i < delay * 4; i++){
1991
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1992
 
      if(ret == -1){
1993
 
        perror_plus("ioctl SIOCGIFFLAGS");
1994
 
      } else if(network.ifr_flags & IFF_RUNNING){
1995
 
        break;
1996
 
      }
1997
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1998
 
      ret = nanosleep(&sleeptime, NULL);
1999
 
      if(ret == -1 and errno != EINTR){
2000
 
        perror_plus("nanosleep");
2001
 
      }
2002
 
    }
2003
 
    if(not take_down_interface){
2004
 
      /* We won't need the socket anymore */
2005
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2006
 
      if(ret == -1){
2007
 
        perror_plus("close");
2008
 
      }
2009
 
    }
2010
 
#ifdef __linux__
2011
 
    if(restore_loglevel){
2012
 
      /* Restores kernel loglevel to default */
2013
 
      ret = klogctl(7, NULL, 0);
2014
 
      if(ret == -1){
2015
 
        perror_plus("klogctl");
2016
 
      }
2017
 
    }
2018
 
#endif  /* __linux__ */
2019
 
    /* Lower privileges */
2020
 
    errno = 0;
2021
 
    /* Lower privileges */
2022
 
    ret = seteuid(uid);
2023
 
    if(ret == -1){
2024
 
      perror_plus("seteuid");
 
2043
  if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
 
2044
    ret = bring_up_interface(interface, delay);
 
2045
    if(ret){
 
2046
      errno = ret;
 
2047
      perror_plus("Failed to bring up interface");
2025
2048
    }
2026
2049
  }
2027
2050
  
2068
2091
    /* Connect directly, do not use Zeroconf */
2069
2092
    /* (Mainly meant for debugging) */
2070
2093
    char *address = strrchr(connect_to, ':');
 
2094
    
2071
2095
    if(address == NULL){
2072
2096
      fprintf_plus(stderr, "No colon in address\n");
2073
2097
      exitcode = EX_USAGE;
2236
2260
  
2237
2261
  /* Re-raise priviliges */
2238
2262
  {
2239
 
    errno = 0;
2240
 
    ret = seteuid(0);
2241
 
    if(ret == -1){
2242
 
      perror_plus("seteuid");
2243
 
    }
 
2263
    raise_privileges();
2244
2264
    
2245
2265
    /* Take down the network interface */
2246
2266
    if(take_down_interface and geteuid() == 0){