/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

Intermediate commit - this does *not* work yet.

* plugins.d/mandos-client.c (HOOKDIR): New.
  (up_interface): New.
  (runnable_hook): New.
  (main): Run network hooks with "start" argument.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 * along with this program.  If not, see
27
27
 * <http://www.gnu.org/licenses/>.
28
28
 * 
29
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
29
 * Contact the authors at <mandos@recompile.se>.
30
30
 */
31
31
 
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"
126
127
 
127
128
bool debug = false;
128
129
static const char mandos_protocol_version[] = "1";
129
130
const char *argp_program_version = "mandos-client " VERSION;
130
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
131
const char *argp_program_bug_address = "<mandos@recompile.se>";
131
132
static const char sys_class_net[] = "/sys/class/net";
132
133
char *connect_to = NULL;
133
134
 
187
188
  return buffer_capacity;
188
189
}
189
190
 
 
191
/* Add server to set of servers to retry periodically */
190
192
int add_server(const char *ip, uint16_t port,
191
193
                 AvahiIfIndex if_index,
192
194
                 int af){
204
206
    perror_plus("strdup");
205
207
    return -1;
206
208
  }
207
 
  /* unique case of first server */
 
209
  /* Special case of first server */
208
210
  if (mc.current_server == NULL){
209
211
    new_server->next = new_server;
210
212
    new_server->prev = new_server;
211
213
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
 
214
  /* Place the new server last in the list */
213
215
  } else {
214
216
    new_server->next = mc.current_server;
215
217
    new_server->prev = mc.current_server->prev;
282
284
    return false;
283
285
  }
284
286
  
285
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
287
  /* Set GPGME home directory for the OpenPGP engine only */
286
288
  rc = gpgme_get_engine_info(&engine_info);
287
289
  if(rc != GPG_ERR_NO_ERROR){
288
290
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1084
1086
  errno = old_errno;
1085
1087
}
1086
1088
 
 
1089
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1090
  int ret;
 
1091
  
 
1092
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1093
  if(s < 0){
 
1094
    perror_plus("socket");
 
1095
    return false;
 
1096
  }
 
1097
  strcpy(ifr->ifr_name, ifname);
 
1098
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1099
  if(ret == -1){
 
1100
    if(debug){
 
1101
      perror_plus("ioctl SIOCGIFFLAGS");
 
1102
    }
 
1103
    return false;
 
1104
  }
 
1105
  return true;
 
1106
}
 
1107
 
 
1108
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1109
  
 
1110
  /* Reject the loopback device */
 
1111
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1112
    if(debug){
 
1113
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1114
              ifname);
 
1115
    }
 
1116
    return false;
 
1117
  }
 
1118
  /* Accept point-to-point devices only if connect_to is specified */
 
1119
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1120
    if(debug){
 
1121
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1122
              ifname);
 
1123
    }
 
1124
    return true;
 
1125
  }
 
1126
  /* Otherwise, reject non-broadcast-capable devices */
 
1127
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1128
    if(debug){
 
1129
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1130
              ifname);
 
1131
    }
 
1132
    return false;
 
1133
  }
 
1134
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1135
  if(ifr->ifr_flags & IFF_NOARP){
 
1136
    if(debug){
 
1137
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1138
    }
 
1139
    return false;
 
1140
  }
 
1141
  
 
1142
  /* Accept this device */
 
1143
  if(debug){
 
1144
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1145
  }
 
1146
  return true;
 
1147
}
 
1148
 
1087
1149
/* 
1088
1150
 * This function determines if a directory entry in /sys/class/net
1089
1151
 * corresponds to an acceptable network device.
1090
1152
 * (This function is passed to scandir(3) as a filter function.)
1091
1153
 */
1092
1154
int good_interface(const struct dirent *if_entry){
 
1155
  int ret;
 
1156
  if(if_entry->d_name[0] == '.'){
 
1157
    return 0;
 
1158
  }
 
1159
  struct ifreq ifr;
 
1160
 
 
1161
  if(not get_flags(if_entry->d_name, &ifr)){
 
1162
    return 0;
 
1163
  }
 
1164
  
 
1165
  if(not good_flags(if_entry->d_name, &ifr)){
 
1166
    return 0;
 
1167
  }
 
1168
  return 1;
 
1169
}
 
1170
 
 
1171
/* 
 
1172
 * This function determines if a directory entry in /sys/class/net
 
1173
 * corresponds to an acceptable network device which is up.
 
1174
 * (This function is passed to scandir(3) as a filter function.)
 
1175
 */
 
1176
int up_interface(const struct dirent *if_entry){
1093
1177
  ssize_t ssret;
1094
1178
  char *flagname = NULL;
1095
1179
  if(if_entry->d_name[0] == '.'){
1157
1241
    }
1158
1242
    return 0;
1159
1243
  }
 
1244
 
 
1245
  /* Reject down interfaces */
 
1246
  if(not (flags & IFF_UP)){
 
1247
    return 0;
 
1248
  }
 
1249
  
1160
1250
  /* Accept point-to-point devices only if connect_to is specified */
1161
1251
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1162
1252
    if(debug){
1200
1290
  return 1;
1201
1291
}
1202
1292
 
 
1293
/* Is this directory entry a runnable program? */
 
1294
int runnable_hook(const struct dirent *direntry){
 
1295
  int ret;
 
1296
  struct stat st;
 
1297
  
 
1298
  if((direntry->d_name)[0] == '\0'){
 
1299
    /* Empty name? */
 
1300
    return 0;
 
1301
  }
 
1302
  
 
1303
  /* Save pointer to last character */
 
1304
  char *end = strchr(direntry->d_name, '\0')-1;
 
1305
  
 
1306
  if(*end == '~'){
 
1307
    /* Backup name~ */
 
1308
    return 0;
 
1309
  }
 
1310
  
 
1311
  if(((direntry->d_name)[0] == '#')
 
1312
     and (*end == '#')){
 
1313
    /* Temporary #name# */
 
1314
    return 0;
 
1315
  }
 
1316
  
 
1317
  /* XXX more rules here */
 
1318
  
 
1319
  ret = stat(direntry->d_name, &st);
 
1320
  if(ret == -1){
 
1321
    if(debug){
 
1322
      perror_plus("Could not stat plugin");
 
1323
    }
 
1324
    return 0;
 
1325
  }
 
1326
  if(not (st.st_mode & S_ISREG)){
 
1327
    /* Not a regular file */
 
1328
    return 0;
 
1329
  }
 
1330
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1331
    /* Not executable */
 
1332
    return 0;
 
1333
  }
 
1334
  return 1;
 
1335
}
 
1336
 
1203
1337
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
1338
  int ret;
1205
1339
  struct timespec now;
1206
1340
  struct timespec waited_time;
1207
1341
  intmax_t block_time;
1208
 
 
 
1342
  
1209
1343
  while(true){
1210
1344
    if(mc.current_server == NULL){
1211
1345
      if (debug){
1236
1370
      block_time = ((retry_interval
1237
1371
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
1372
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1239
 
 
 
1373
      
1240
1374
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1375
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1242
1376
      }
1243
 
 
 
1377
      
1244
1378
      if(block_time <= 0){
1245
1379
        ret = start_mandos_communication(mc.current_server->ip,
1246
1380
                                         mc.current_server->port,
1411
1545
        errno = 0;
1412
1546
        retry_interval = strtod(arg, &tmp);
1413
1547
        if(errno != 0 or tmp == arg or *tmp != '\0'
1414
 
           or (retry_interval * 1000) > INT_MAX){
 
1548
           or (retry_interval * 1000) > INT_MAX
 
1549
           or retry_interval < 0){
1415
1550
          argp_error(state, "Bad retry interval");
1416
1551
        }
1417
1552
        break;
1468
1603
      perror_plus("seteuid");
1469
1604
    }
1470
1605
    
1471
 
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
 
    if(seckey_fd == -1){
1473
 
      perror_plus("open");
1474
 
    } else {
1475
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1476
 
      if(ret == -1){
1477
 
        perror_plus("fstat");
 
1606
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1607
      int seckey_fd = open(seckey, O_RDONLY);
 
1608
      if(seckey_fd == -1){
 
1609
        perror_plus("open");
1478
1610
      } else {
1479
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
 
          ret = fchown(seckey_fd, uid, gid);
1481
 
          if(ret == -1){
1482
 
            perror_plus("fchown");
 
1611
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1612
        if(ret == -1){
 
1613
          perror_plus("fstat");
 
1614
        } else {
 
1615
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1616
            ret = fchown(seckey_fd, uid, gid);
 
1617
            if(ret == -1){
 
1618
              perror_plus("fchown");
 
1619
            }
1483
1620
          }
1484
1621
        }
 
1622
        TEMP_FAILURE_RETRY(close(seckey_fd));
1485
1623
      }
1486
 
      TEMP_FAILURE_RETRY(close(seckey_fd));
1487
1624
    }
1488
1625
    
1489
 
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
 
    if(pubkey_fd == -1){
1491
 
      perror_plus("open");
1492
 
    } else {
1493
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1494
 
      if(ret == -1){
1495
 
        perror_plus("fstat");
 
1626
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1627
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1628
      if(pubkey_fd == -1){
 
1629
        perror_plus("open");
1496
1630
      } else {
1497
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
 
          ret = fchown(pubkey_fd, uid, gid);
1499
 
          if(ret == -1){
1500
 
            perror_plus("fchown");
 
1631
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1632
        if(ret == -1){
 
1633
          perror_plus("fstat");
 
1634
        } else {
 
1635
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1636
            ret = fchown(pubkey_fd, uid, gid);
 
1637
            if(ret == -1){
 
1638
              perror_plus("fchown");
 
1639
            }
1501
1640
          }
1502
1641
        }
 
1642
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1503
1643
      }
1504
 
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1505
1644
    }
1506
1645
    
1507
1646
    /* Lower privileges */
1512
1651
    }
1513
1652
  }
1514
1653
  
 
1654
  /* Find network hooks and run them */
 
1655
  {
 
1656
    struct dirent **direntries;
 
1657
    struct dirent *direntry;
 
1658
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1659
                           alphasort);
 
1660
    int devnull = open("/dev/null", O_RDONLY);
 
1661
    for(int i = 0; i < numhooks; i++){
 
1662
      direntry = direntries[0];
 
1663
      char *fullname = NULL;
 
1664
      ret = asprintf(&fullname, "%s/%s", tempdir,
 
1665
                     direntry->d_name);
 
1666
      if(ret < 0){
 
1667
        perror_plus("asprintf");
 
1668
        continue;
 
1669
      }
 
1670
      pid_t hook_pid = fork();
 
1671
      if(hook_pid == 0){
 
1672
        /* Child */
 
1673
        dup2(devnull, STDIN_FILENO);
 
1674
        close(devnull);
 
1675
        dup2(STDERR_FILENO, STDOUT_FILENO);
 
1676
        setenv("DEVICE", interface, 1);
 
1677
        setenv("VERBOSE", debug ? "1" : "0", 1);
 
1678
        setenv("MODE", "start", 1);
 
1679
        /* setenv( XXX more here */
 
1680
        ret = execl(fullname, direntry->d_name, "start");
 
1681
        perror_plus("execl");
 
1682
      }
 
1683
      free(fullname);
 
1684
      if(quit_now){
 
1685
        goto end;
 
1686
      }
 
1687
    }
 
1688
    close(devnull);
 
1689
  }
 
1690
  
1515
1691
  if(not debug){
1516
1692
    avahi_set_log_function(empty_log);
1517
1693
  }
1518
 
 
 
1694
  
1519
1695
  if(interface[0] == '\0'){
1520
1696
    struct dirent **direntries;
1521
1697
    ret = scandir(sys_class_net, &direntries, good_interface,
1829
2005
    
1830
2006
    port = (uint16_t)tmpmax;
1831
2007
    *address = '\0';
1832
 
    address = connect_to;
1833
2008
    /* Colon in address indicates IPv6 */
1834
2009
    int af;
1835
 
    if(strchr(address, ':') != NULL){
 
2010
    if(strchr(connect_to, ':') != NULL){
1836
2011
      af = AF_INET6;
 
2012
      /* Accept [] around IPv6 address - see RFC 5952 */
 
2013
      if(connect_to[0] == '[' and address[-1] == ']')
 
2014
        {
 
2015
          connect_to++;
 
2016
          address[-1] = '\0';
 
2017
        }
1837
2018
    } else {
1838
2019
      af = AF_INET;
1839
2020
    }
 
2021
    address = connect_to;
1840
2022
    
1841
2023
    if(quit_now){
1842
2024
      goto end;
1843
2025
    }
1844
 
 
 
2026
    
1845
2027
    while(not quit_now){
1846
2028
      ret = start_mandos_communication(address, port, if_index, af);
1847
2029
      if(quit_now or ret == 0){
1848
2030
        break;
1849
2031
      }
1850
 
      sleep((int)retry_interval or 1);
1851
 
    };
1852
 
 
 
2032
      if(debug){
 
2033
        fprintf(stderr, "Retrying in %d seconds\n",
 
2034
                (int)retry_interval);
 
2035
      }
 
2036
      sleep((int)retry_interval);
 
2037
    }
 
2038
    
1853
2039
    if (not quit_now){
1854
2040
      exitcode = EXIT_SUCCESS;
1855
2041
    }
1856
 
 
 
2042
    
1857
2043
    goto end;
1858
2044
  }
1859
2045
  
1956
2142
    }
1957
2143
  }
1958
2144
  
 
2145
  /* XXX run network hooks "stop" here  */
 
2146
  
1959
2147
  /* Take down the network interface */
1960
2148
  if(take_down_interface){
1961
2149
    /* Re-raise priviliges */
1992
2180
  if(tempdir_created){
1993
2181
    struct dirent **direntries = NULL;
1994
2182
    struct dirent *direntry = NULL;
1995
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1996
 
    if (ret > 0){
1997
 
      for(int i = 0; i < ret; i++){
 
2183
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2184
                             alphasort);
 
2185
    if (numentries > 0){
 
2186
      for(int i = 0; i < numentries; i++){
1998
2187
        direntry = direntries[i];
1999
2188
        char *fullname = NULL;
2000
2189
        ret = asprintf(&fullname, "%s/%s", tempdir,
2012
2201
      }
2013
2202
    }
2014
2203
 
2015
 
    /* need to be cleaned even if ret == 0 because man page doesn't
2016
 
       specify */
 
2204
    /* need to clean even if 0 because man page doesn't specify */
2017
2205
    free(direntries);
2018
 
    if (ret == -1){
 
2206
    if (numentries == -1){
2019
2207
      perror_plus("scandir");
2020
2208
    }
2021
2209
    ret = rmdir(tempdir);