/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: Björn Påhlsson
  • Date: 2011-11-14 18:03:35 UTC
  • mto: (505.3.9 client-network-hooks)
  • mto: This revision was merged to the branch mainline in revision 525.
  • Revision ID: belorn@fukt.bsnet.se-20111114180335-3j4x5mjr7lix2jyj
New convinence error printer: fprintf_plus

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
 
170
171
  perror(print_text);
171
172
}
172
173
 
 
174
int fprintf_plus(FILE *stream, const char *format, ...){
 
175
  va_list ap;
 
176
  va_start (ap, format);
 
177
  
 
178
  TEMP_FAILURE_RETRY(fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name));
 
179
  return TEMP_FAILURE_RETRY(vfprintf(stream, format, ap));
 
180
}
 
181
 
173
182
/*
174
183
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
184
 * bytes. "buffer_capacity" is how much is currently allocated,
187
196
  return buffer_capacity;
188
197
}
189
198
 
 
199
/* Add server to set of servers to retry periodically */
190
200
int add_server(const char *ip, uint16_t port,
191
201
                 AvahiIfIndex if_index,
192
202
                 int af){
204
214
    perror_plus("strdup");
205
215
    return -1;
206
216
  }
207
 
  /* unique case of first server */
 
217
  /* Special case of first server */
208
218
  if (mc.current_server == NULL){
209
219
    new_server->next = new_server;
210
220
    new_server->prev = new_server;
211
221
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
 
222
  /* Place the new server last in the list */
213
223
  } else {
214
224
    new_server->next = mc.current_server;
215
225
    new_server->prev = mc.current_server->prev;
282
292
    return false;
283
293
  }
284
294
  
285
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
295
  /* Set GPGME home directory for the OpenPGP engine only */
286
296
  rc = gpgme_get_engine_info(&engine_info);
287
297
  if(rc != GPG_ERR_NO_ERROR){
288
298
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
1084
1094
  errno = old_errno;
1085
1095
}
1086
1096
 
 
1097
bool get_flags(const char *ifname, struct ifreq *ifr){
 
1098
  int ret;
 
1099
  
 
1100
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
1101
  if(s < 0){
 
1102
    perror_plus("socket");
 
1103
    return false;
 
1104
  }
 
1105
  strcpy(ifr->ifr_name, ifname);
 
1106
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
 
1107
  if(ret == -1){
 
1108
    if(debug){
 
1109
      perror_plus("ioctl SIOCGIFFLAGS");
 
1110
    }
 
1111
    return false;
 
1112
  }
 
1113
  return true;
 
1114
}
 
1115
 
 
1116
bool good_flags(const char *ifname, const struct ifreq *ifr){
 
1117
  
 
1118
  /* Reject the loopback device */
 
1119
  if(ifr->ifr_flags & IFF_LOOPBACK){
 
1120
    if(debug){
 
1121
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
 
1122
              ifname);
 
1123
    }
 
1124
    return false;
 
1125
  }
 
1126
  /* Accept point-to-point devices only if connect_to is specified */
 
1127
  if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
 
1128
    if(debug){
 
1129
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
 
1130
              ifname);
 
1131
    }
 
1132
    return true;
 
1133
  }
 
1134
  /* Otherwise, reject non-broadcast-capable devices */
 
1135
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
 
1136
    if(debug){
 
1137
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
 
1138
              ifname);
 
1139
    }
 
1140
    return false;
 
1141
  }
 
1142
  /* Reject non-ARP interfaces (including dummy interfaces) */
 
1143
  if(ifr->ifr_flags & IFF_NOARP){
 
1144
    if(debug){
 
1145
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
 
1146
    }
 
1147
    return false;
 
1148
  }
 
1149
  
 
1150
  /* Accept this device */
 
1151
  if(debug){
 
1152
    fprintf(stderr, "Interface \"%s\" is good\n", ifname);
 
1153
  }
 
1154
  return true;
 
1155
}
 
1156
 
1087
1157
/* 
1088
1158
 * This function determines if a directory entry in /sys/class/net
1089
1159
 * corresponds to an acceptable network device.
1090
1160
 * (This function is passed to scandir(3) as a filter function.)
1091
1161
 */
1092
1162
int good_interface(const struct dirent *if_entry){
 
1163
  int ret;
 
1164
  if(if_entry->d_name[0] == '.'){
 
1165
    return 0;
 
1166
  }
 
1167
  struct ifreq ifr;
 
1168
 
 
1169
  if(not get_flags(if_entry->d_name, &ifr)){
 
1170
    return 0;
 
1171
  }
 
1172
  
 
1173
  if(not good_flags(if_entry->d_name, &ifr)){
 
1174
    return 0;
 
1175
  }
 
1176
  return 1;
 
1177
}
 
1178
 
 
1179
/* 
 
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.)
 
1183
 */
 
1184
int up_interface(const struct dirent *if_entry){
1093
1185
  ssize_t ssret;
1094
1186
  char *flagname = NULL;
1095
1187
  if(if_entry->d_name[0] == '.'){
1157
1249
    }
1158
1250
    return 0;
1159
1251
  }
 
1252
 
 
1253
  /* Reject down interfaces */
 
1254
  if(not (flags & IFF_UP)){
 
1255
    return 0;
 
1256
  }
 
1257
  
1160
1258
  /* Accept point-to-point devices only if connect_to is specified */
1161
1259
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1162
1260
    if(debug){
1200
1298
  return 1;
1201
1299
}
1202
1300
 
 
1301
/* Is this directory entry a runnable program? */
 
1302
int runnable_hook(const struct dirent *direntry){
 
1303
  int ret;
 
1304
  struct stat st;
 
1305
  
 
1306
  if((direntry->d_name)[0] == '\0'){
 
1307
    /* Empty name? */
 
1308
    return 0;
 
1309
  }
 
1310
  
 
1311
  /* Save pointer to last character */
 
1312
  char *end = strchr(direntry->d_name, '\0')-1;
 
1313
  
 
1314
  if(*end == '~'){
 
1315
    /* Backup name~ */
 
1316
    return 0;
 
1317
  }
 
1318
  
 
1319
  if(((direntry->d_name)[0] == '#')
 
1320
     and (*end == '#')){
 
1321
    /* Temporary #name# */
 
1322
    return 0;
 
1323
  }
 
1324
  
 
1325
  /* XXX more rules here */
 
1326
  
 
1327
  ret = stat(direntry->d_name, &st);
 
1328
  if(ret == -1){
 
1329
    if(debug){
 
1330
      perror_plus("Could not stat plugin");
 
1331
    }
 
1332
    return 0;
 
1333
  }
 
1334
  if(not (st.st_mode & S_ISREG)){
 
1335
    /* Not a regular file */
 
1336
    return 0;
 
1337
  }
 
1338
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
 
1339
    /* Not executable */
 
1340
    return 0;
 
1341
  }
 
1342
  return 1;
 
1343
}
 
1344
 
1203
1345
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
1346
  int ret;
1205
1347
  struct timespec now;
1206
1348
  struct timespec waited_time;
1207
1349
  intmax_t block_time;
1208
 
 
 
1350
  
1209
1351
  while(true){
1210
1352
    if(mc.current_server == NULL){
1211
1353
      if (debug){
1236
1378
      block_time = ((retry_interval
1237
1379
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
1380
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1239
 
 
 
1381
      
1240
1382
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1383
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1242
1384
      }
1243
 
 
 
1385
      
1244
1386
      if(block_time <= 0){
1245
1387
        ret = start_mandos_communication(mc.current_server->ip,
1246
1388
                                         mc.current_server->port,
1411
1553
        errno = 0;
1412
1554
        retry_interval = strtod(arg, &tmp);
1413
1555
        if(errno != 0 or tmp == arg or *tmp != '\0'
1414
 
           or (retry_interval * 1000) > INT_MAX){
 
1556
           or (retry_interval * 1000) > INT_MAX
 
1557
           or retry_interval < 0){
1415
1558
          argp_error(state, "Bad retry interval");
1416
1559
        }
1417
1560
        break;
1455
1598
      goto end;
1456
1599
    }
1457
1600
  }
 
1601
    
 
1602
  {
 
1603
    /* Work around Debian bug #633582:
 
1604
       <http://bugs.debian.org/633582> */
 
1605
    struct stat st;
 
1606
    
 
1607
    /* Re-raise priviliges */
 
1608
    errno = 0;
 
1609
    ret = seteuid(0);
 
1610
    if(ret == -1){
 
1611
      perror_plus("seteuid");
 
1612
    }
 
1613
    
 
1614
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1615
      int seckey_fd = open(seckey, O_RDONLY);
 
1616
      if(seckey_fd == -1){
 
1617
        perror_plus("open");
 
1618
      } else {
 
1619
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1620
        if(ret == -1){
 
1621
          perror_plus("fstat");
 
1622
        } else {
 
1623
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1624
            ret = fchown(seckey_fd, uid, gid);
 
1625
            if(ret == -1){
 
1626
              perror_plus("fchown");
 
1627
            }
 
1628
          }
 
1629
        }
 
1630
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1631
      }
 
1632
    }
 
1633
    
 
1634
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1635
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1636
      if(pubkey_fd == -1){
 
1637
        perror_plus("open");
 
1638
      } else {
 
1639
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1640
        if(ret == -1){
 
1641
          perror_plus("fstat");
 
1642
        } else {
 
1643
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1644
            ret = fchown(pubkey_fd, uid, gid);
 
1645
            if(ret == -1){
 
1646
              perror_plus("fchown");
 
1647
            }
 
1648
          }
 
1649
        }
 
1650
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1651
      }
 
1652
    }
 
1653
    
 
1654
    /* Lower privileges */
 
1655
    errno = 0;
 
1656
    ret = seteuid(uid);
 
1657
    if(ret == -1){
 
1658
      perror_plus("seteuid");
 
1659
    }
 
1660
  }
 
1661
  
 
1662
  /* Find network hooks and run them */
 
1663
  {
 
1664
    struct dirent **direntries;
 
1665
    struct dirent *direntry;
 
1666
    int numhooks = scandir(HOOKDIR, &direntries, runnable_hook,
 
1667
                           alphasort);
 
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,
 
1673
                     direntry->d_name);
 
1674
      if(ret < 0){
 
1675
        perror_plus("asprintf");
 
1676
        continue;
 
1677
      }
 
1678
      pid_t hook_pid = fork();
 
1679
      if(hook_pid == 0){
 
1680
        /* Child */
 
1681
        dup2(devnull, STDIN_FILENO);
 
1682
        close(devnull);
 
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");
 
1690
      }
 
1691
      free(fullname);
 
1692
      if(quit_now){
 
1693
        goto end;
 
1694
      }
 
1695
    }
 
1696
    close(devnull);
 
1697
  }
1458
1698
  
1459
1699
  if(not debug){
1460
1700
    avahi_set_log_function(empty_log);
1461
1701
  }
1462
 
 
 
1702
  
1463
1703
  if(interface[0] == '\0'){
1464
1704
    struct dirent **direntries;
1465
1705
    ret = scandir(sys_class_net, &direntries, good_interface,
1773
2013
    
1774
2014
    port = (uint16_t)tmpmax;
1775
2015
    *address = '\0';
1776
 
    address = connect_to;
1777
2016
    /* Colon in address indicates IPv6 */
1778
2017
    int af;
1779
 
    if(strchr(address, ':') != NULL){
 
2018
    if(strchr(connect_to, ':') != NULL){
1780
2019
      af = AF_INET6;
 
2020
      /* Accept [] around IPv6 address - see RFC 5952 */
 
2021
      if(connect_to[0] == '[' and address[-1] == ']')
 
2022
        {
 
2023
          connect_to++;
 
2024
          address[-1] = '\0';
 
2025
        }
1781
2026
    } else {
1782
2027
      af = AF_INET;
1783
2028
    }
 
2029
    address = connect_to;
1784
2030
    
1785
2031
    if(quit_now){
1786
2032
      goto end;
1787
2033
    }
1788
 
 
 
2034
    
1789
2035
    while(not quit_now){
1790
2036
      ret = start_mandos_communication(address, port, if_index, af);
1791
2037
      if(quit_now or ret == 0){
1792
2038
        break;
1793
2039
      }
1794
 
      sleep((int)retry_interval or 1);
1795
 
    };
1796
 
 
 
2040
      if(debug){
 
2041
        fprintf(stderr, "Retrying in %d seconds\n",
 
2042
                (int)retry_interval);
 
2043
      }
 
2044
      sleep((int)retry_interval);
 
2045
    }
 
2046
    
1797
2047
    if (not quit_now){
1798
2048
      exitcode = EXIT_SUCCESS;
1799
2049
    }
1800
 
 
 
2050
    
1801
2051
    goto end;
1802
2052
  }
1803
2053
  
1900
2150
    }
1901
2151
  }
1902
2152
  
 
2153
  /* XXX run network hooks "stop" here  */
 
2154
  
1903
2155
  /* Take down the network interface */
1904
2156
  if(take_down_interface){
1905
2157
    /* Re-raise priviliges */
1936
2188
  if(tempdir_created){
1937
2189
    struct dirent **direntries = NULL;
1938
2190
    struct dirent *direntry = NULL;
1939
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1940
 
    if (ret > 0){
1941
 
      for(int i = 0; i < ret; i++){
 
2191
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2192
                             alphasort);
 
2193
    if (numentries > 0){
 
2194
      for(int i = 0; i < numentries; i++){
1942
2195
        direntry = direntries[i];
1943
2196
        char *fullname = NULL;
1944
2197
        ret = asprintf(&fullname, "%s/%s", tempdir,
1956
2209
      }
1957
2210
    }
1958
2211
 
1959
 
    /* need to be cleaned even if ret == 0 because man page doesn't
1960
 
       specify */
 
2212
    /* need to clean even if 0 because man page doesn't specify */
1961
2213
    free(direntries);
1962
 
    if (ret == -1){
 
2214
    if (numentries == -1){
1963
2215
      perror_plus("scandir");
1964
2216
    }
1965
2217
    ret = rmdir(tempdir);