/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

  • Committer: Teddy Hogeborn
  • Date: 2012-06-09 23:41:07 UTC
  • mto: This revision was merged to the branch mainline in revision 596.
  • Revision ID: teddy@recompile.se-20120609234107-vm6zilzz0y6aihsm
* plugins.d/mandos-client.c (raise_privileges,
  raise_privileges_permanently, lower_privileges): Return error_t and
                                                   save old errno
                                                   before calling any
                                                   other function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
error_t raise_privileges(void){
 
1397
  error_t old_errno = errno;
 
1398
  error_t ret_errno = 0;
 
1399
  if(seteuid(0) == -1){
 
1400
    ret_errno = errno;
 
1401
    perror_plus("seteuid");
 
1402
  }
 
1403
  errno = old_errno;
 
1404
  return ret_errno;
 
1405
}
 
1406
 
 
1407
/* Set effective and real user ID to 0.  Return errno. */
 
1408
error_t raise_privileges_permanently(void){
 
1409
  error_t old_errno = errno;
 
1410
  error_t ret_errno = raise_privileges();
 
1411
  if(ret_errno != 0){
 
1412
    errno = old_errno;
 
1413
    return ret_errno;
 
1414
  }
 
1415
  if(setuid(0) == -1){
 
1416
    ret_errno = errno;
 
1417
    perror_plus("seteuid");
 
1418
  }
 
1419
  errno = old_errno;
 
1420
  return ret_errno;
 
1421
}
 
1422
 
 
1423
/* Set effective user ID to unprivileged saved user ID */
 
1424
error_t lower_privileges(void){
 
1425
  error_t old_errno = errno;
 
1426
  error_t ret_errno = 0;
 
1427
  if(seteuid(uid) == -1){
 
1428
    ret_errno = errno;
 
1429
    perror_plus("seteuid");
 
1430
  }
 
1431
  errno = old_errno;
 
1432
  return ret_errno;
 
1433
}
 
1434
 
1390
1435
bool run_network_hooks(const char *mode, const char *interface,
1391
1436
                       const float delay){
1392
1437
  struct dirent **direntries;
1414
1459
      if(hook_pid == 0){
1415
1460
        /* Child */
1416
1461
        /* 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
 
        }
 
1462
        raise_privileges_permanently();
1428
1463
        /* Set group */
1429
1464
        errno = 0;
1430
1465
        ret = setgid(0);
1523
1558
  return true;
1524
1559
}
1525
1560
 
 
1561
int bring_up_interface(const char *const interface,
 
1562
                       const float delay){
 
1563
  int sd = -1;
 
1564
  int old_errno = errno;
 
1565
  int ret_errno = 0;
 
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
    errno = old_errno;
 
1572
    return ENXIO;
 
1573
  }
 
1574
  
 
1575
  if(quit_now){
 
1576
    errno = old_errno;
 
1577
    return EINTR;
 
1578
  }
 
1579
  
 
1580
  /* Re-raise priviliges */
 
1581
  raise_privileges();
 
1582
  
 
1583
#ifdef __linux__
 
1584
  /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
1585
     messages about the network interface to mess up the prompt */
 
1586
  ret = klogctl(8, NULL, 5);
 
1587
  bool restore_loglevel = true;
 
1588
  if(ret == -1){
 
1589
    restore_loglevel = false;
 
1590
    perror_plus("klogctl");
 
1591
  }
 
1592
#endif  /* __linux__ */
 
1593
    
 
1594
  sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1595
  if(sd < 0){
 
1596
    ret_errno = errno;
 
1597
    perror_plus("socket");
 
1598
#ifdef __linux__
 
1599
    if(restore_loglevel){
 
1600
      ret = klogctl(7, NULL, 0);
 
1601
      if(ret == -1){
 
1602
        perror_plus("klogctl");
 
1603
      }
 
1604
    }
 
1605
#endif  /* __linux__ */
 
1606
    /* Lower privileges */
 
1607
    lower_privileges();
 
1608
    errno = old_errno;
 
1609
    return ret_errno;
 
1610
  }
 
1611
  strcpy(network.ifr_name, interface);
 
1612
  ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1613
  if(ret == -1){
 
1614
    ret_errno = errno;
 
1615
    perror_plus("ioctl SIOCGIFFLAGS");
 
1616
#ifdef __linux__
 
1617
    if(restore_loglevel){
 
1618
      ret = klogctl(7, NULL, 0);
 
1619
      if(ret == -1){
 
1620
        perror_plus("klogctl");
 
1621
      }
 
1622
    }
 
1623
#endif  /* __linux__ */
 
1624
    /* Lower privileges */
 
1625
    lower_privileges();
 
1626
    errno = old_errno;
 
1627
    return ret_errno;
 
1628
  }
 
1629
  if((network.ifr_flags & IFF_UP) == 0){
 
1630
    network.ifr_flags |= IFF_UP;
 
1631
    ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1632
    if(ret == -1){
 
1633
      ret_errno = errno;
 
1634
      perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
 
1635
#ifdef __linux__
 
1636
      if(restore_loglevel){
 
1637
        ret = klogctl(7, NULL, 0);
 
1638
        if(ret == -1){
 
1639
          perror_plus("klogctl");
 
1640
        }
 
1641
      }
 
1642
#endif  /* __linux__ */
 
1643
        /* Lower privileges */
 
1644
      lower_privileges();
 
1645
      errno = old_errno;
 
1646
      return ret_errno;
 
1647
    }
 
1648
  }
 
1649
  /* Sleep checking until interface is running.
 
1650
     Check every 0.25s, up to total time of delay */
 
1651
  for(int i=0; i < delay * 4; i++){
 
1652
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1653
    if(ret == -1){
 
1654
      perror_plus("ioctl SIOCGIFFLAGS");
 
1655
    } else if(network.ifr_flags & IFF_RUNNING){
 
1656
      break;
 
1657
    }
 
1658
    struct timespec sleeptime = { .tv_nsec = 250000000 };
 
1659
    ret = nanosleep(&sleeptime, NULL);
 
1660
    if(ret == -1 and errno != EINTR){
 
1661
      perror_plus("nanosleep");
 
1662
    }
 
1663
  }
 
1664
  /* Close the socket */
 
1665
  ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1666
  if(ret == -1){
 
1667
    perror_plus("close");
 
1668
  }
 
1669
#ifdef __linux__
 
1670
  if(restore_loglevel){
 
1671
    /* Restores kernel loglevel to default */
 
1672
    ret = klogctl(7, NULL, 0);
 
1673
    if(ret == -1){
 
1674
      perror_plus("klogctl");
 
1675
    }
 
1676
  }
 
1677
#endif  /* __linux__ */
 
1678
  /* Lower privileges */
 
1679
  lower_privileges();
 
1680
  errno = old_errno;
 
1681
  return 0;
 
1682
}
 
1683
 
1526
1684
int main(int argc, char *argv[]){
1527
1685
  AvahiSServiceBrowser *sb = NULL;
1528
1686
  int error;
1534
1692
  struct ifreq network;
1535
1693
  int sd = -1;
1536
1694
  bool take_down_interface = false;
1537
 
  uid_t uid;
1538
 
  gid_t gid;
1539
1695
  char tempdir[] = "/tmp/mandosXXXXXX";
1540
1696
  bool tempdir_created = false;
1541
1697
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1721
1877
       <http://bugs.debian.org/633582> */
1722
1878
    
1723
1879
    /* Re-raise priviliges */
1724
 
    errno = 0;
1725
 
    ret = seteuid(0);
1726
 
    if(ret == -1){
1727
 
      perror_plus("seteuid");
1728
 
    } else {
 
1880
    if(raise_privileges() == 0){
1729
1881
      struct stat st;
1730
1882
      
1731
1883
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1896
2048
  }
1897
2049
  
1898
2050
  /* If the interface is down, bring it up */
1899
 
  if(strcmp(interface, "none") != 0){
1900
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1901
 
    if(if_index == 0){
1902
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1903
 
      exitcode = EX_UNAVAILABLE;
1904
 
      goto end;
1905
 
    }
1906
 
    
1907
 
    if(quit_now){
1908
 
      goto end;
1909
 
    }
1910
 
    
1911
 
    /* Re-raise priviliges */
1912
 
    errno = 0;
1913
 
    ret = seteuid(0);
1914
 
    if(ret == -1){
1915
 
      perror_plus("seteuid");
1916
 
    }
1917
 
    
1918
 
#ifdef __linux__
1919
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1920
 
       messages about the network interface to mess up the prompt */
1921
 
    ret = klogctl(8, NULL, 5);
1922
 
    bool restore_loglevel = true;
1923
 
    if(ret == -1){
1924
 
      restore_loglevel = false;
1925
 
      perror_plus("klogctl");
1926
 
    }
1927
 
#endif  /* __linux__ */
1928
 
    
1929
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1930
 
    if(sd < 0){
1931
 
      perror_plus("socket");
1932
 
      exitcode = EX_OSERR;
1933
 
#ifdef __linux__
1934
 
      if(restore_loglevel){
1935
 
        ret = klogctl(7, NULL, 0);
1936
 
        if(ret == -1){
1937
 
          perror_plus("klogctl");
1938
 
        }
1939
 
      }
1940
 
#endif  /* __linux__ */
1941
 
      /* Lower privileges */
1942
 
      errno = 0;
1943
 
      ret = seteuid(uid);
1944
 
      if(ret == -1){
1945
 
        perror_plus("seteuid");
1946
 
      }
1947
 
      goto end;
1948
 
    }
1949
 
    strcpy(network.ifr_name, interface);
1950
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1951
 
    if(ret == -1){
1952
 
      perror_plus("ioctl SIOCGIFFLAGS");
1953
 
#ifdef __linux__
1954
 
      if(restore_loglevel){
1955
 
        ret = klogctl(7, NULL, 0);
1956
 
        if(ret == -1){
1957
 
          perror_plus("klogctl");
1958
 
        }
1959
 
      }
1960
 
#endif  /* __linux__ */
1961
 
      exitcode = EX_OSERR;
1962
 
      /* Lower privileges */
1963
 
      errno = 0;
1964
 
      ret = seteuid(uid);
1965
 
      if(ret == -1){
1966
 
        perror_plus("seteuid");
1967
 
      }
1968
 
      goto end;
1969
 
    }
1970
 
    if((network.ifr_flags & IFF_UP) == 0){
1971
 
      network.ifr_flags |= IFF_UP;
1972
 
      take_down_interface = true;
1973
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
 
      if(ret == -1){
1975
 
        take_down_interface = false;
1976
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1977
 
        exitcode = EX_OSERR;
1978
 
#ifdef __linux__
1979
 
        if(restore_loglevel){
1980
 
          ret = klogctl(7, NULL, 0);
1981
 
          if(ret == -1){
1982
 
            perror_plus("klogctl");
1983
 
          }
1984
 
        }
1985
 
#endif  /* __linux__ */
1986
 
        /* Lower privileges */
1987
 
        errno = 0;
1988
 
        ret = seteuid(uid);
1989
 
        if(ret == -1){
1990
 
          perror_plus("seteuid");
1991
 
        }
1992
 
        goto end;
1993
 
      }
1994
 
    }
1995
 
    /* Sleep checking until interface is running.
1996
 
       Check every 0.25s, up to total time of delay */
1997
 
    for(int i=0; i < delay * 4; i++){
1998
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1999
 
      if(ret == -1){
2000
 
        perror_plus("ioctl SIOCGIFFLAGS");
2001
 
      } else if(network.ifr_flags & IFF_RUNNING){
2002
 
        break;
2003
 
      }
2004
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
2005
 
      ret = nanosleep(&sleeptime, NULL);
2006
 
      if(ret == -1 and errno != EINTR){
2007
 
        perror_plus("nanosleep");
2008
 
      }
2009
 
    }
2010
 
    if(not take_down_interface){
2011
 
      /* We won't need the socket anymore */
2012
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2013
 
      if(ret == -1){
2014
 
        perror_plus("close");
2015
 
      }
2016
 
    }
2017
 
#ifdef __linux__
2018
 
    if(restore_loglevel){
2019
 
      /* Restores kernel loglevel to default */
2020
 
      ret = klogctl(7, NULL, 0);
2021
 
      if(ret == -1){
2022
 
        perror_plus("klogctl");
2023
 
      }
2024
 
    }
2025
 
#endif  /* __linux__ */
2026
 
    /* Lower privileges */
2027
 
    errno = 0;
2028
 
    /* Lower privileges */
2029
 
    ret = seteuid(uid);
2030
 
    if(ret == -1){
2031
 
      perror_plus("seteuid");
 
2051
  if((interface[0] != '\0') and (strcmp(interface, "none") != 0)){
 
2052
    ret = bring_up_interface(interface, delay);
 
2053
    if(ret != 0){
 
2054
      errno = ret;
 
2055
      perror_plus("Failed to bring up interface");
2032
2056
    }
2033
2057
  }
2034
2058
  
2075
2099
    /* Connect directly, do not use Zeroconf */
2076
2100
    /* (Mainly meant for debugging) */
2077
2101
    char *address = strrchr(connect_to, ':');
 
2102
    
2078
2103
    if(address == NULL){
2079
2104
      fprintf_plus(stderr, "No colon in address\n");
2080
2105
      exitcode = EX_USAGE;
2243
2268
  
2244
2269
  /* Re-raise priviliges */
2245
2270
  {
2246
 
    errno = 0;
2247
 
    ret = seteuid(0);
2248
 
    if(ret == -1){
2249
 
      perror_plus("seteuid");
2250
 
    }
 
2271
    raise_privileges();
2251
2272
    
2252
2273
    /* Take down the network interface */
2253
2274
    if(take_down_interface and geteuid() == 0){