/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: 2011-11-26 20:59:56 UTC
  • mto: (518.1.8 mandos-persistent)
  • mto: This revision was merged to the branch mainline in revision 524.
  • Revision ID: teddy@recompile.se-20111126205956-vft6g0z2i6my0165
Use GPG to encrypt instead of AES.

* Makefile (run-server): Use "--no-restore" option.
* debian/control (mandos/Depends): Added "python-gnupginterface".
* mandos: (CryptoError, Crypto): New; uses GPG.
  (Client.encrypt_secret, Client.decrypt_secret): Removed.
  (ClientHandler.fingerprint): Use binascii.hexlify().
  (main): Use Crypto class to decrypt.
  (main/cleanup): Use Crypto class to encrypt.  Handle EACCES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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"
127
126
 
128
127
bool debug = false;
129
128
static const char mandos_protocol_version[] = "1";
1086
1085
  errno = old_errno;
1087
1086
}
1088
1087
 
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
 
 
1149
1088
/* 
1150
1089
 * This function determines if a directory entry in /sys/class/net
1151
1090
 * corresponds to an acceptable network device.
1152
1091
 * (This function is passed to scandir(3) as a filter function.)
1153
1092
 */
1154
1093
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){
1177
1094
  ssize_t ssret;
1178
1095
  char *flagname = NULL;
1179
1096
  if(if_entry->d_name[0] == '.'){
1241
1158
    }
1242
1159
    return 0;
1243
1160
  }
1244
 
 
1245
 
  /* Reject down interfaces */
1246
 
  if(not (flags & IFF_UP)){
1247
 
    return 0;
1248
 
  }
1249
 
  
1250
1161
  /* Accept point-to-point devices only if connect_to is specified */
1251
1162
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1252
1163
    if(debug){
1290
1201
  return 1;
1291
1202
}
1292
1203
 
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
 
 
1337
1204
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1338
1205
  int ret;
1339
1206
  struct timespec now;
1651
1518
    }
1652
1519
  }
1653
1520
  
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
 
  
1691
1521
  if(not debug){
1692
1522
    avahi_set_log_function(empty_log);
1693
1523
  }
1694
 
  
 
1524
 
1695
1525
  if(interface[0] == '\0'){
1696
1526
    struct dirent **direntries;
1697
1527
    ret = scandir(sys_class_net, &direntries, good_interface,
2039
1869
    if (not quit_now){
2040
1870
      exitcode = EXIT_SUCCESS;
2041
1871
    }
2042
 
    
 
1872
 
2043
1873
    goto end;
2044
1874
  }
2045
1875
  
2142
1972
    }
2143
1973
  }
2144
1974
  
2145
 
  /* XXX run network hooks "stop" here  */
2146
 
  
2147
1975
  /* Take down the network interface */
2148
1976
  if(take_down_interface){
2149
1977
    /* Re-raise priviliges */