/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

Earlier signal handling

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
 */
31
31
 
32
32
/* Needed by GPGME, specifically gpgme_data_seek() */
33
 
#ifndef _LARGEFILE_SOURCE
34
33
#define _LARGEFILE_SOURCE
35
 
#endif
36
 
#ifndef _FILE_OFFSET_BITS
37
34
#define _FILE_OFFSET_BITS 64
38
 
#endif
39
35
 
40
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
37
 
42
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
44
41
#include <stdint.h>             /* uint16_t, uint32_t */
45
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
47
 
                                   srand(), strtof() */
 
44
                                   srand() */
48
45
#include <stdbool.h>            /* bool, false, true */
49
46
#include <string.h>             /* memset(), strcmp(), strlen(),
50
47
                                   strerror(), asprintf(), strcpy() */
59
56
#include <fcntl.h>              /* open() */
60
57
#include <dirent.h>             /* opendir(), struct dirent, readdir()
61
58
                                 */
62
 
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
 
                                   strtoimax() */
 
59
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
64
60
#include <assert.h>             /* assert() */
65
61
#include <errno.h>              /* perror(), errno */
66
62
#include <time.h>               /* nanosleep(), time() */
79
75
                                   argp_state, struct argp,
80
76
                                   argp_parse(), ARGP_KEY_ARG,
81
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
 
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sig_atomic_t,
84
 
                                   raise() */
85
 
 
 
78
#include <signal.h>             /* sigemptyset(), sigaddset(), sigaction(), SIGTERM, sigaction */
86
79
#ifdef __linux__
87
80
#include <sys/klog.h>           /* klogctl() */
88
 
#endif  /* __linux__ */
 
81
#endif
89
82
 
90
83
/* Avahi */
91
84
/* All Avahi types, constants and functions
143
136
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
144
137
 
145
138
/*
146
 
 * Make additional room in "buffer" for at least BUFFER_SIZE more
147
 
 * bytes. "buffer_capacity" is how much is currently allocated,
148
 
 * "buffer_length" is how much is already used.
 
139
 * Make additional room in "buffer" for at least BUFFER_SIZE
 
140
 * additional bytes. "buffer_capacity" is how much is currently
 
141
 * allocated, "buffer_length" is how much is already used.
149
142
 */
150
143
size_t incbuffer(char **buffer, size_t buffer_length,
151
144
                  size_t buffer_capacity){
205
198
  }
206
199
  
207
200
  if(debug){
208
 
    fprintf(stderr, "Initializing GPGME\n");
 
201
    fprintf(stderr, "Initialize gpgme\n");
209
202
  }
210
203
  
211
204
  /* Init GPGME */
309
302
        }
310
303
        gpgme_recipient_t recipient;
311
304
        recipient = result->recipients;
312
 
        while(recipient != NULL){
313
 
          fprintf(stderr, "Public key algorithm: %s\n",
314
 
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
315
 
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
316
 
          fprintf(stderr, "Secret key available: %s\n",
317
 
                  recipient->status == GPG_ERR_NO_SECKEY
318
 
                  ? "No" : "Yes");
319
 
          recipient = recipient->next;
 
305
        if(recipient){
 
306
          while(recipient != NULL){
 
307
            fprintf(stderr, "Public key algorithm: %s\n",
 
308
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
309
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
310
            fprintf(stderr, "Secret key available: %s\n",
 
311
                    recipient->status == GPG_ERR_NO_SECKEY
 
312
                    ? "No" : "Yes");
 
313
            recipient = recipient->next;
 
314
          }
320
315
        }
321
316
      }
322
317
    }
514
509
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
515
510
                      __attribute__((unused)) const char *txt){}
516
511
 
517
 
sig_atomic_t quit_now = 0;
518
 
int signal_received = 0;
519
 
 
520
512
/* Called when a Mandos server is found */
521
513
static int start_mandos_communication(const char *ip, uint16_t port,
522
514
                                      AvahiIfIndex if_index,
567
559
  
568
560
  memset(&to, 0, sizeof(to));
569
561
  if(af == AF_INET6){
570
 
    to.in6.sin6_family = (sa_family_t)af;
 
562
    to.in6.sin6_family = (uint16_t)af;
571
563
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
572
564
  } else {                      /* IPv4 */
573
565
    to.in.sin_family = (sa_family_t)af;
796
788
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
797
789
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
798
790
                             flags,
799
 
                             AVAHI_GCC_UNUSED void* userdata){
 
791
                             __attribute__((unused)) void* userdata){
800
792
  assert(r);
801
793
  
802
794
  /* Called whenever a service has been resolved successfully or
803
795
     timed out */
804
796
  
805
 
  if(quit_now){
806
 
    return;
807
 
  }
808
 
  
809
797
  switch(event){
810
798
  default:
811
799
  case AVAHI_RESOLVER_FAILURE:
842
830
                            const char *domain,
843
831
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
844
832
                            flags,
845
 
                            AVAHI_GCC_UNUSED void* userdata){
 
833
                            __attribute__((unused)) void* userdata){
846
834
  assert(b);
847
835
  
848
836
  /* Called whenever a new services becomes available on the LAN or
863
851
       the callback function is called the Avahi server will free the
864
852
       resolver for us. */
865
853
    
866
 
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
867
 
                                    name, type, domain, protocol, 0,
868
 
                                    resolve_callback, NULL) == NULL)
 
854
    if(!(avahi_s_service_resolver_new(mc.server, interface,
 
855
                                       protocol, name, type, domain,
 
856
                                       AVAHI_PROTO_INET6, 0,
 
857
                                       resolve_callback, NULL)))
869
858
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
870
859
              name, avahi_strerror(avahi_server_errno(mc.server)));
871
860
    break;
883
872
}
884
873
 
885
874
/* stop main loop after sigterm has been called */
886
 
static void handle_sigterm(int sig){
887
 
  if(quit_now){
888
 
    return;
889
 
  }
890
 
  quit_now = 1;
891
 
  signal_received = sig;
 
875
static void handle_sigterm(__attribute__((unused)) int sig){
892
876
  int old_errno = errno;
893
 
  if(mc.simple_poll != NULL){
894
 
    avahi_simple_poll_quit(mc.simple_poll);
895
 
  }
 
877
  avahi_simple_poll_quit(mc.simple_poll);
896
878
  errno = old_errno;
897
879
}
898
880
 
901
883
  int error;
902
884
  int ret;
903
885
  intmax_t tmpmax;
904
 
  char *tmp;
 
886
  int numchars;
905
887
  int exitcode = EXIT_SUCCESS;
906
888
  const char *interface = "eth0";
907
889
  struct ifreq network;
908
 
  int sd = -1;
909
 
  bool take_down_interface = false;
 
890
  int sd;
910
891
  uid_t uid;
911
892
  gid_t gid;
912
893
  char *connect_to = NULL;
918
899
  
919
900
  bool gnutls_initialized = false;
920
901
  bool gpgme_initialized = false;
921
 
  float delay = 2.5f;
922
 
  
 
902
  double delay = 2.5;
 
903
 
923
904
  struct sigaction old_sigterm_action;
924
905
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
925
 
  
 
906
 
926
907
  {
927
908
    struct argp_option options[] = {
928
909
      { .name = "debug", .key = 128,
979
960
        pubkey = arg;
980
961
        break;
981
962
      case 129:                 /* --dh-bits */
982
 
        errno = 0;
983
 
        tmpmax = strtoimax(arg, &tmp, 10);
984
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
985
 
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
963
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
964
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
965
           or arg[numchars] != '\0'){
986
966
          fprintf(stderr, "Bad number of DH bits\n");
987
967
          exit(EXIT_FAILURE);
988
968
        }
992
972
        mc.priority = arg;
993
973
        break;
994
974
      case 131:                 /* --delay */
995
 
        errno = 0;
996
 
        delay = strtof(arg, &tmp);
997
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
975
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
976
        if(ret < 1 or arg[numchars] != '\0'){
998
977
          fprintf(stderr, "Bad delay\n");
999
978
          exit(EXIT_FAILURE);
1000
979
        }
1020
999
      goto end;
1021
1000
    }
1022
1001
  }
1023
 
  
 
1002
 
1024
1003
  if(not debug){
1025
1004
    avahi_set_log_function(empty_log);
1026
1005
  }
1027
 
  
 
1006
 
1028
1007
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1029
1008
     from the signal handler */
1030
1009
  /* Initialize the pseudo-RNG for Avahi */
1035
1014
    exitcode = EXIT_FAILURE;
1036
1015
    goto end;
1037
1016
  }
1038
 
  
 
1017
 
1039
1018
  sigemptyset(&sigterm_action.sa_mask);
1040
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1041
 
  if(ret == -1){
1042
 
    perror("sigaddset");
1043
 
    exitcode = EXIT_FAILURE;
1044
 
    goto end;
1045
 
  }
1046
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1047
 
  if(ret == -1){
1048
 
    perror("sigaddset");
1049
 
    exitcode = EXIT_FAILURE;
1050
 
    goto end;
1051
 
  }
1052
1019
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1053
1020
  if(ret == -1){
1054
1021
    perror("sigaddset");
1055
1022
    exitcode = EXIT_FAILURE;
1056
1023
    goto end;
1057
1024
  }
1058
 
  ret = sigaction(SIGINT, &sigterm_action, &old_sigterm_action);
1059
 
  if(ret == -1){
1060
 
    perror("sigaction");
1061
 
    exitcode = EXIT_FAILURE;
1062
 
    goto end;
1063
 
  }
1064
 
  ret = sigaction(SIGHUP, &sigterm_action, NULL);
1065
 
  if(ret == -1){
1066
 
    perror("sigaction");
1067
 
    exitcode = EXIT_FAILURE;
1068
 
    goto end;
1069
 
  }
1070
 
  ret = sigaction(SIGTERM, &sigterm_action, NULL);
1071
 
  if(ret == -1){
1072
 
    perror("sigaction");
1073
 
    exitcode = EXIT_FAILURE;
1074
 
    goto end;
1075
 
  }
 
1025
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1026
  if(ret == -1){
 
1027
    perror("sigaction");
 
1028
    exitcode = EXIT_FAILURE;
 
1029
    goto end;
 
1030
  }  
 
1031
 
1076
1032
  
1077
1033
  /* If the interface is down, bring it up */
1078
1034
  if(interface[0] != '\0'){
1079
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1080
 
    if(if_index == 0){
1081
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1082
 
      exitcode = EXIT_FAILURE;
1083
 
      goto end;
1084
 
    }
1085
 
    
1086
 
    if(quit_now){
1087
 
      goto end;
1088
 
    }
1089
 
    
1090
1035
#ifdef __linux__
1091
1036
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1092
1037
       messages to mess up the prompt */
1096
1041
      restore_loglevel = false;
1097
1042
      perror("klogctl");
1098
1043
    }
1099
 
#endif  /* __linux__ */
 
1044
#endif
1100
1045
    
1101
1046
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1102
1047
    if(sd < 0){
1109
1054
          perror("klogctl");
1110
1055
        }
1111
1056
      }
1112
 
#endif  /* __linux__ */
 
1057
#endif
1113
1058
      goto end;
1114
1059
    }
1115
1060
    strcpy(network.ifr_name, interface);
1123
1068
          perror("klogctl");
1124
1069
        }
1125
1070
      }
1126
 
#endif  /* __linux__ */
 
1071
#endif
1127
1072
      exitcode = EXIT_FAILURE;
1128
1073
      goto end;
1129
1074
    }
1130
1075
    if((network.ifr_flags & IFF_UP) == 0){
1131
1076
      network.ifr_flags |= IFF_UP;
1132
 
      take_down_interface = true;
1133
1077
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1134
1078
      if(ret == -1){
1135
 
        take_down_interface = false;
1136
1079
        perror("ioctl SIOCSIFFLAGS");
1137
1080
        exitcode = EXIT_FAILURE;
1138
1081
#ifdef __linux__
1142
1085
            perror("klogctl");
1143
1086
          }
1144
1087
        }
1145
 
#endif  /* __linux__ */
 
1088
#endif
1146
1089
        goto end;
1147
1090
      }
1148
1091
    }
1160
1103
        perror("nanosleep");
1161
1104
      }
1162
1105
    }
1163
 
    if(not take_down_interface){
1164
 
      /* We won't need the socket anymore */
1165
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1166
 
      if(ret == -1){
1167
 
        perror("close");
1168
 
      }
 
1106
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1107
    if(ret == -1){
 
1108
      perror("close");
1169
1109
    }
1170
1110
#ifdef __linux__
1171
1111
    if(restore_loglevel){
1175
1115
        perror("klogctl");
1176
1116
      }
1177
1117
    }
1178
 
#endif  /* __linux__ */
1179
 
  }
1180
 
  
1181
 
  if(quit_now){
1182
 
    goto end;
 
1118
#endif
1183
1119
  }
1184
1120
  
1185
1121
  uid = getuid();
1196
1132
    perror("setuid");
1197
1133
  }
1198
1134
  
1199
 
  if(quit_now){
1200
 
    goto end;
1201
 
  }
1202
 
  
1203
1135
  ret = init_gnutls_global(pubkey, seckey);
1204
1136
  if(ret == -1){
1205
1137
    fprintf(stderr, "init_gnutls_global failed\n");
1209
1141
    gnutls_initialized = true;
1210
1142
  }
1211
1143
  
1212
 
  if(quit_now){
1213
 
    goto end;
1214
 
  }
1215
 
  
1216
 
  tempdir_created = true;
1217
1144
  if(mkdtemp(tempdir) == NULL){
1218
 
    tempdir_created = false;
1219
1145
    perror("mkdtemp");
1220
1146
    goto end;
1221
1147
  }
1222
 
  
1223
 
  if(quit_now){
1224
 
    goto end;
1225
 
  }
 
1148
  tempdir_created = true;
1226
1149
  
1227
1150
  if(not init_gpgme(pubkey, seckey, tempdir)){
1228
1151
    fprintf(stderr, "init_gpgme failed\n");
1232
1155
    gpgme_initialized = true;
1233
1156
  }
1234
1157
  
1235
 
  if(quit_now){
1236
 
    goto end;
 
1158
  if(interface[0] != '\0'){
 
1159
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1160
    if(if_index == 0){
 
1161
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1162
      exitcode = EXIT_FAILURE;
 
1163
      goto end;
 
1164
    }
1237
1165
  }
1238
1166
  
1239
1167
  if(connect_to != NULL){
1245
1173
      exitcode = EXIT_FAILURE;
1246
1174
      goto end;
1247
1175
    }
1248
 
    
1249
 
    if(quit_now){
1250
 
      goto end;
1251
 
    }
1252
 
    
1253
1176
    uint16_t port;
1254
 
    errno = 0;
1255
 
    tmpmax = strtoimax(address+1, &tmp, 10);
1256
 
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1257
 
       or tmpmax != (uint16_t)tmpmax){
 
1177
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1178
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1179
       or address[numchars+1] != '\0'){
1258
1180
      fprintf(stderr, "Bad port number\n");
1259
1181
      exitcode = EXIT_FAILURE;
1260
1182
      goto end;
1261
1183
    }
1262
 
  
1263
 
    if(quit_now){
1264
 
      goto end;
1265
 
    }
1266
 
    
1267
1184
    port = (uint16_t)tmpmax;
1268
1185
    *address = '\0';
1269
1186
    address = connect_to;
1274
1191
    } else {
1275
1192
      af = AF_INET;
1276
1193
    }
1277
 
    
1278
 
    if(quit_now){
1279
 
      goto end;
1280
 
    }
1281
 
    
1282
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1194
    ret = start_mandos_communication(address, port, if_index,
 
1195
                                     af);
1283
1196
    if(ret < 0){
1284
1197
      exitcode = EXIT_FAILURE;
1285
1198
    } else {
1287
1200
    }
1288
1201
    goto end;
1289
1202
  }
1290
 
  
1291
 
  if(quit_now){
1292
 
    goto end;
1293
 
  }
1294
 
  
 
1203
    
1295
1204
  {
1296
1205
    AvahiServerConfig config;
1297
1206
    /* Do not publish any local Zeroconf records */
1318
1227
    goto end;
1319
1228
  }
1320
1229
  
1321
 
  if(quit_now){
1322
 
    goto end;
1323
 
  }
1324
 
  
1325
1230
  /* Create the Avahi service browser */
1326
1231
  sb = avahi_s_service_browser_new(mc.server, if_index,
1327
 
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
 
1232
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
1328
1233
                                   NULL, 0, browse_callback, NULL);
1329
1234
  if(sb == NULL){
1330
1235
    fprintf(stderr, "Failed to create service browser: %s\n",
1333
1238
    goto end;
1334
1239
  }
1335
1240
  
1336
 
  if(quit_now){
1337
 
    goto end;
1338
 
  }
1339
 
  
1340
1241
  /* Run the main loop */
1341
1242
  
1342
1243
  if(debug){
1371
1272
    gpgme_release(mc.ctx);
1372
1273
  }
1373
1274
  
1374
 
  /* Take down the network interface */
1375
 
  if(take_down_interface){
1376
 
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1377
 
    if(ret == -1){
1378
 
      perror("ioctl SIOCGIFFLAGS");
1379
 
    } else if(network.ifr_flags & IFF_UP) {
1380
 
      network.ifr_flags &= ~IFF_UP; /* clear flag */
1381
 
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1382
 
      if(ret == -1){
1383
 
        perror("ioctl SIOCSIFFLAGS");
1384
 
      }
1385
 
    }
1386
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1387
 
    if(ret == -1){
1388
 
      perror("close");
1389
 
    }
1390
 
  }
1391
 
  
1392
1275
  /* Removes the temp directory used by GPGME */
1393
1276
  if(tempdir_created){
1394
1277
    DIR *d;
1433
1316
    }
1434
1317
  }
1435
1318
  
1436
 
  if(quit_now){
1437
 
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
1438
 
    if(ret == -1){
1439
 
      perror("sigaction");
1440
 
    }
1441
 
    raise(signal_received);
1442
 
  }
1443
 
  
1444
1319
  return exitcode;
1445
1320
}