26
26
* along with this program. If not, see
27
27
* <http://www.gnu.org/licenses/>.
29
* Contact the authors at <mandos@recompile.se>.
29
* Contact the authors at <mandos@fukt.bsnet.se>.
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
123
123
#define PATHDIR "/conf/conf.d/mandos"
124
124
#define SECKEY "seckey.txt"
125
125
#define PUBKEY "pubkey.txt"
126
#define HOOKDIR "/lib/mandos/network-hooks.d"
128
127
bool debug = false;
129
128
static const char mandos_protocol_version[] = "1";
130
129
const char *argp_program_version = "mandos-client " VERSION;
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
130
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
132
131
static const char sys_class_net[] = "/sys/class/net";
133
132
char *connect_to = NULL;
171
170
perror(print_text);
174
int fprintf_plus(FILE *stream, const char *format, ...){
176
va_start (ap, format);
178
TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
179
return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
183
174
* Make additional room in "buffer" for at least BUFFER_SIZE more
184
175
* bytes. "buffer_capacity" is how much is currently allocated,
214
204
perror_plus("strdup");
217
/* Special case of first server */
207
/* unique case of first server */
218
208
if (mc.current_server == NULL){
219
209
new_server->next = new_server;
220
210
new_server->prev = new_server;
221
211
mc.current_server = new_server;
222
/* Place the new server last in the list */
212
/* Placing the new server last in the list */
224
214
new_server->next = mc.current_server;
225
215
new_server->prev = mc.current_server->prev;
295
/* Set GPGME home directory for the OpenPGP engine only */
285
/* Set GPGME home directory for the OpenPGP engine only */
296
286
rc = gpgme_get_engine_info(&engine_info);
297
287
if(rc != GPG_ERR_NO_ERROR){
298
288
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1094
1084
errno = old_errno;
1097
bool get_flags(const char *ifname, struct ifreq *ifr){
1100
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1102
perror_plus("socket");
1105
strcpy(ifr->ifr_name, ifname);
1106
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1109
perror_plus("ioctl SIOCGIFFLAGS");
1116
bool good_flags(const char *ifname, const struct ifreq *ifr){
1118
/* Reject the loopback device */
1119
if(ifr->ifr_flags & IFF_LOOPBACK){
1121
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1126
/* Accept point-to-point devices only if connect_to is specified */
1127
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1129
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1134
/* Otherwise, reject non-broadcast-capable devices */
1135
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1137
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1142
/* Reject non-ARP interfaces (including dummy interfaces) */
1143
if(ifr->ifr_flags & IFF_NOARP){
1145
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1150
/* Accept this device */
1152
fprintf(stderr, "Interface \"%s\" is good\n", ifname);
1158
1088
* This function determines if a directory entry in /sys/class/net
1159
1089
* corresponds to an acceptable network device.
1160
1090
* (This function is passed to scandir(3) as a filter function.)
1162
1092
int good_interface(const struct dirent *if_entry){
1164
if(if_entry->d_name[0] == '.'){
1169
if(not get_flags(if_entry->d_name, &ifr)){
1173
if(not good_flags(if_entry->d_name, &ifr)){
1180
* This function determines if a directory entry in /sys/class/net
1181
* corresponds to an acceptable network device which is up.
1182
* (This function is passed to scandir(3) as a filter function.)
1184
int up_interface(const struct dirent *if_entry){
1186
1094
char *flagname = NULL;
1187
1095
if(if_entry->d_name[0] == '.'){
1301
/* Is this directory entry a runnable program? */
1302
int runnable_hook(const struct dirent *direntry){
1306
if((direntry->d_name)[0] == '\0'){
1311
/* Save pointer to last character */
1312
char *end = strchr(direntry->d_name, '\0')-1;
1319
if(((direntry->d_name)[0] == '#')
1321
/* Temporary #name# */
1325
/* XXX more rules here */
1327
ret = stat(direntry->d_name, &st);
1330
perror_plus("Could not stat plugin");
1334
if(not (st.st_mode & S_ISREG)){
1335
/* Not a regular file */
1338
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1339
/* Not executable */
1345
1203
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1347
1205
struct timespec now;
1348
1206
struct timespec waited_time;
1349
1207
intmax_t block_time;
1352
1210
if(mc.current_server == NULL){
1378
1236
block_time = ((retry_interval
1379
1237
- ((intmax_t)waited_time.tv_sec * 1000))
1380
1238
- ((intmax_t)waited_time.tv_nsec / 1000000));
1383
fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1241
fprintf(stderr, "Blocking for %ld ms\n", block_time);
1386
1244
if(block_time <= 0){
1387
1245
ret = start_mandos_communication(mc.current_server->ip,
1388
1246
mc.current_server->port,
1554
1412
retry_interval = strtod(arg, &tmp);
1555
1413
if(errno != 0 or tmp == arg or *tmp != '\0'
1556
or (retry_interval * 1000) > INT_MAX
1557
or retry_interval < 0){
1414
or (retry_interval * 1000) > INT_MAX){
1558
1415
argp_error(state, "Bad retry interval");
1611
1468
perror_plus("seteuid");
1614
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1615
int seckey_fd = open(seckey, O_RDONLY);
1616
if(seckey_fd == -1){
1617
perror_plus("open");
1471
int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
if(seckey_fd == -1){
1473
perror_plus("open");
1475
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1477
perror_plus("fstat");
1619
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1621
perror_plus("fstat");
1623
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1624
ret = fchown(seckey_fd, uid, gid);
1626
perror_plus("fchown");
1479
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
ret = fchown(seckey_fd, uid, gid);
1482
perror_plus("fchown");
1630
TEMP_FAILURE_RETRY(close(seckey_fd));
1486
TEMP_FAILURE_RETRY(close(seckey_fd));
1634
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1635
int pubkey_fd = open(pubkey, O_RDONLY);
1636
if(pubkey_fd == -1){
1637
perror_plus("open");
1489
int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
if(pubkey_fd == -1){
1491
perror_plus("open");
1493
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1495
perror_plus("fstat");
1639
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1641
perror_plus("fstat");
1643
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1644
ret = fchown(pubkey_fd, uid, gid);
1646
perror_plus("fchown");
1497
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
ret = fchown(pubkey_fd, uid, gid);
1500
perror_plus("fchown");
1650
TEMP_FAILURE_RETRY(close(pubkey_fd));
1504
TEMP_FAILURE_RETRY(close(pubkey_fd));
1654
1507
/* Lower privileges */
1662
/* Find network hooks and run them */
1664
struct dirent **direntries;
1665
struct dirent *direntry;
1666
int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
1668
int devnull = open("/dev/null", O_RDONLY);
1669
for(int i = 0; i < numhooks; i++){
1670
direntry = direntries[0];
1671
char *fullname = NULL;
1672
ret = asprintf(&fullname, "%s/%s", tempdir,
1675
perror_plus("asprintf");
1678
pid_t hook_pid = fork();
1681
dup2(devnull, STDIN_FILENO);
1683
dup2(STDERR_FILENO, STDOUT_FILENO);
1684
setenv("DEVICE", interface, 1);
1685
setenv("VERBOSE", debug ? "1" : "0", 1);
1686
setenv("MODE", "start", 1);
1687
/* setenv( XXX more here */
1688
ret = execl(fullname, direntry->d_name, "start");
1689
perror_plus("execl");
1700
1516
avahi_set_log_function(empty_log);
1703
1519
if(interface[0] == '\0'){
1704
1520
struct dirent **direntries;
1705
1521
ret = scandir(sys_class_net, &direntries, good_interface,
2014
1830
port = (uint16_t)tmpmax;
2015
1831
*address = '\0';
1832
address = connect_to;
2016
1833
/* Colon in address indicates IPv6 */
2018
if(strchr(connect_to, ':') != NULL){
1835
if(strchr(address, ':') != NULL){
2020
/* Accept [] around IPv6 address - see RFC 5952 */
2021
if(connect_to[0] == '[' and address[-1] == ']')
2029
address = connect_to;
2035
1845
while(not quit_now){
2036
1846
ret = start_mandos_communication(address, port, if_index, af);
2037
1847
if(quit_now or ret == 0){
2041
fprintf(stderr, "Retrying in %d seconds\n",
2042
(int)retry_interval);
2044
sleep((int)retry_interval);
1850
sleep((int)retry_interval or 1);
2047
1853
if (not quit_now){
2048
1854
exitcode = EXIT_SUCCESS;
2188
1992
if(tempdir_created){
2189
1993
struct dirent **direntries = NULL;
2190
1994
struct dirent *direntry = NULL;
2191
int numentries = scandir(tempdir, &direntries, notdotentries,
2193
if (numentries > 0){
2194
for(int i = 0; i < numentries; i++){
1995
ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1997
for(int i = 0; i < ret; i++){
2195
1998
direntry = direntries[i];
2196
1999
char *fullname = NULL;
2197
2000
ret = asprintf(&fullname, "%s/%s", tempdir,