/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-06 19:08:17 UTC
  • mto: This revision was merged to the branch mainline in revision 596.
  • 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{
170
172
 
171
173
/* Function to use when printing errors */
172
174
void perror_plus(const char *print_text){
 
175
  int e = errno;
173
176
  fprintf(stderr, "Mandos plugin %s: ",
174
177
          program_invocation_short_name);
 
178
  errno = e;
175
179
  perror(print_text);
176
180
}
177
181
 
 
182
__attribute__((format (gnu_printf, 2, 3)))
178
183
int fprintf_plus(FILE *stream, const char *format, ...){
179
184
  va_list ap;
180
185
  va_start (ap, format);
202
207
}
203
208
 
204
209
/* Add server to set of servers to retry periodically */
205
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
 
               int af){
 
210
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
211
                int af){
207
212
  int ret;
208
213
  server *new_server = malloc(sizeof(server));
209
214
  if(new_server == NULL){
210
215
    perror_plus("malloc");
211
 
    return -1;
 
216
    return false;
212
217
  }
213
218
  *new_server = (server){ .ip = strdup(ip),
214
219
                          .port = port,
216
221
                          .af = af };
217
222
  if(new_server->ip == NULL){
218
223
    perror_plus("strdup");
219
 
    return -1;
 
224
    return false;
220
225
  }
221
226
  /* Special case of first server */
222
227
  if (mc.current_server == NULL){
233
238
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
239
  if(ret == -1){
235
240
    perror_plus("clock_gettime");
236
 
    return -1;
 
241
    return false;
237
242
  }
238
 
  return 0;
 
243
  return true;
239
244
}
240
245
 
241
246
/* 
818
823
    goto mandos_end;
819
824
  }
820
825
  
821
 
  /* Spurious warning from -Wint-to-pointer-cast */
822
 
  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);
823
831
  
824
832
  if(quit_now){
825
833
    errno = EINTR;
1030
1038
      if(ret == 0){
1031
1039
        avahi_simple_poll_quit(mc.simple_poll);
1032
1040
      } else {
1033
 
        ret = add_server(ip, port, interface,
1034
 
                         avahi_proto_to_af(proto));
 
1041
        if(not add_server(ip, port, interface,
 
1042
                          avahi_proto_to_af(proto))){
 
1043
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1044
                       " list\n", name);
 
1045
        }
1035
1046
      }
1036
1047
    }
1037
1048
  }
1381
1392
  }
1382
1393
}
1383
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
 
1384
1438
bool run_network_hooks(const char *mode, const char *interface,
1385
1439
                       const float delay){
1386
1440
  struct dirent **direntries;
1408
1462
      if(hook_pid == 0){
1409
1463
        /* Child */
1410
1464
        /* 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
 
        }
 
1465
        raise_privileges_permanently();
1422
1466
        /* Set group */
1423
1467
        errno = 0;
1424
1468
        ret = setgid(0);
1444
1488
          perror_plus("setenv");
1445
1489
          _exit(EX_OSERR);
1446
1490
        }
1447
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1491
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1448
1492
        if(ret == -1){
1449
1493
          perror_plus("setenv");
1450
1494
          _exit(EX_OSERR);
1467
1511
          _exit(EX_OSERR);
1468
1512
        }
1469
1513
        free(delaystring);
1470
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1471
 
        perror_plus("execl");
 
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
        }
 
1521
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1522
          perror_plus("execl");
 
1523
          _exit(EXIT_FAILURE);
 
1524
        }
1472
1525
      } else {
1473
1526
        int status;
1474
1527
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1508
1561
  return true;
1509
1562
}
1510
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
 
1511
1676
int main(int argc, char *argv[]){
1512
1677
  AvahiSServiceBrowser *sb = NULL;
1513
1678
  int error;
1519
1684
  struct ifreq network;
1520
1685
  int sd = -1;
1521
1686
  bool take_down_interface = false;
1522
 
  uid_t uid;
1523
 
  gid_t gid;
1524
1687
  char tempdir[] = "/tmp/mandosXXXXXX";
1525
1688
  bool tempdir_created = false;
1526
1689
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1593
1756
        .group = 2 },
1594
1757
      { .name = "retry", .key = 132,
1595
1758
        .arg = "SECONDS",
1596
 
        .doc = "Retry interval used when denied by the mandos server",
 
1759
        .doc = "Retry interval used when denied by the Mandos server",
1597
1760
        .group = 2 },
1598
1761
      { .name = "network-hook-dir", .key = 133,
1599
1762
        .arg = "DIR",
1671
1834
        argp_state_help(state, state->out_stream,
1672
1835
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1836
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
1837
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1677
1838
        exit(argp_err_exit_status);
1678
1839
        break;
1708
1869
       <http://bugs.debian.org/633582> */
1709
1870
    
1710
1871
    /* Re-raise priviliges */
1711
 
    errno = 0;
1712
 
    ret = seteuid(0);
1713
 
    if(ret == -1){
1714
 
      perror_plus("seteuid");
1715
 
    } else {
 
1872
    if(raise_privileges() == 0){
1716
1873
      struct stat st;
1717
1874
      
1718
1875
      if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1883
2040
  }
1884
2041
  
1885
2042
  /* If the interface is down, bring it up */
1886
 
  if(strcmp(interface, "none") != 0){
1887
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1888
 
    if(if_index == 0){
1889
 
      fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1890
 
      exitcode = EX_UNAVAILABLE;
1891
 
      goto end;
1892
 
    }
1893
 
    
1894
 
    if(quit_now){
1895
 
      goto end;
1896
 
    }
1897
 
    
1898
 
    /* Re-raise priviliges */
1899
 
    errno = 0;
1900
 
    ret = seteuid(0);
1901
 
    if(ret == -1){
1902
 
      perror_plus("seteuid");
1903
 
    }
1904
 
    
1905
 
#ifdef __linux__
1906
 
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1907
 
       messages about the network interface to mess up the prompt */
1908
 
    ret = klogctl(8, NULL, 5);
1909
 
    bool restore_loglevel = true;
1910
 
    if(ret == -1){
1911
 
      restore_loglevel = false;
1912
 
      perror_plus("klogctl");
1913
 
    }
1914
 
#endif  /* __linux__ */
1915
 
    
1916
 
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1917
 
    if(sd < 0){
1918
 
      perror_plus("socket");
1919
 
      exitcode = EX_OSERR;
1920
 
#ifdef __linux__
1921
 
      if(restore_loglevel){
1922
 
        ret = klogctl(7, NULL, 0);
1923
 
        if(ret == -1){
1924
 
          perror_plus("klogctl");
1925
 
        }
1926
 
      }
1927
 
#endif  /* __linux__ */
1928
 
      /* Lower privileges */
1929
 
      errno = 0;
1930
 
      ret = seteuid(uid);
1931
 
      if(ret == -1){
1932
 
        perror_plus("seteuid");
1933
 
      }
1934
 
      goto end;
1935
 
    }
1936
 
    strcpy(network.ifr_name, interface);
1937
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1938
 
    if(ret == -1){
1939
 
      perror_plus("ioctl SIOCGIFFLAGS");
1940
 
#ifdef __linux__
1941
 
      if(restore_loglevel){
1942
 
        ret = klogctl(7, NULL, 0);
1943
 
        if(ret == -1){
1944
 
          perror_plus("klogctl");
1945
 
        }
1946
 
      }
1947
 
#endif  /* __linux__ */
1948
 
      exitcode = EX_OSERR;
1949
 
      /* Lower privileges */
1950
 
      errno = 0;
1951
 
      ret = seteuid(uid);
1952
 
      if(ret == -1){
1953
 
        perror_plus("seteuid");
1954
 
      }
1955
 
      goto end;
1956
 
    }
1957
 
    if((network.ifr_flags & IFF_UP) == 0){
1958
 
      network.ifr_flags |= IFF_UP;
1959
 
      take_down_interface = true;
1960
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1961
 
      if(ret == -1){
1962
 
        take_down_interface = false;
1963
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1964
 
        exitcode = EX_OSERR;
1965
 
#ifdef __linux__
1966
 
        if(restore_loglevel){
1967
 
          ret = klogctl(7, NULL, 0);
1968
 
          if(ret == -1){
1969
 
            perror_plus("klogctl");
1970
 
          }
1971
 
        }
1972
 
#endif  /* __linux__ */
1973
 
        /* Lower privileges */
1974
 
        errno = 0;
1975
 
        ret = seteuid(uid);
1976
 
        if(ret == -1){
1977
 
          perror_plus("seteuid");
1978
 
        }
1979
 
        goto end;
1980
 
      }
1981
 
    }
1982
 
    /* Sleep checking until interface is running.
1983
 
       Check every 0.25s, up to total time of delay */
1984
 
    for(int i=0; i < delay * 4; i++){
1985
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1986
 
      if(ret == -1){
1987
 
        perror_plus("ioctl SIOCGIFFLAGS");
1988
 
      } else if(network.ifr_flags & IFF_RUNNING){
1989
 
        break;
1990
 
      }
1991
 
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1992
 
      ret = nanosleep(&sleeptime, NULL);
1993
 
      if(ret == -1 and errno != EINTR){
1994
 
        perror_plus("nanosleep");
1995
 
      }
1996
 
    }
1997
 
    if(not take_down_interface){
1998
 
      /* We won't need the socket anymore */
1999
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2000
 
      if(ret == -1){
2001
 
        perror_plus("close");
2002
 
      }
2003
 
    }
2004
 
#ifdef __linux__
2005
 
    if(restore_loglevel){
2006
 
      /* Restores kernel loglevel to default */
2007
 
      ret = klogctl(7, NULL, 0);
2008
 
      if(ret == -1){
2009
 
        perror_plus("klogctl");
2010
 
      }
2011
 
    }
2012
 
#endif  /* __linux__ */
2013
 
    /* Lower privileges */
2014
 
    errno = 0;
2015
 
    /* Lower privileges */
2016
 
    ret = seteuid(uid);
2017
 
    if(ret == -1){
2018
 
      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");
2019
2048
    }
2020
2049
  }
2021
2050
  
2062
2091
    /* Connect directly, do not use Zeroconf */
2063
2092
    /* (Mainly meant for debugging) */
2064
2093
    char *address = strrchr(connect_to, ':');
 
2094
    
2065
2095
    if(address == NULL){
2066
2096
      fprintf_plus(stderr, "No colon in address\n");
2067
2097
      exitcode = EX_USAGE;
2230
2260
  
2231
2261
  /* Re-raise priviliges */
2232
2262
  {
2233
 
    errno = 0;
2234
 
    ret = seteuid(0);
2235
 
    if(ret == -1){
2236
 
      perror_plus("seteuid");
2237
 
    }
 
2263
    raise_privileges();
2238
2264
    
2239
2265
    /* Take down the network interface */
2240
2266
    if(take_down_interface and geteuid() == 0){