/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: 2009-01-31 10:33:17 UTC
  • mfrom: (24.1.129 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20090131103317-wzqvyr532sjcjt7u
Merge from Björn:

* mandos-ctl: New option "--remove-client".  Only default to listing
              clients if no clients were given on the command line.
* plugins.d/mandos-client.c: Lower kernel log level while bringing up
                             network interface.  New option "--delay"
                             to control the maximum delay to wait for
                             running interface.
* plugins.d/mandos-client.xml (SYNOPSIS, OPTIONS): New option
                                                   "--delay".

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
40
41
#include <stdint.h>             /* uint16_t, uint32_t */
41
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
57
58
#include <fcntl.h>              /* open() */
58
59
#include <dirent.h>             /* opendir(), struct dirent, readdir()
59
60
                                 */
60
 
#include <inttypes.h>           /* PRIu16, SCNu16 */
 
61
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
61
62
#include <assert.h>             /* assert() */
62
63
#include <errno.h>              /* perror(), errno */
63
 
#include <time.h>               /* time() */
 
64
#include <time.h>               /* nanosleep(), time() */
64
65
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
65
66
                                   SIOCSIFFLAGS, if_indextoname(),
66
67
                                   if_nametoindex(), IF_NAMESIZE */
74
75
                                   argp_state, struct argp,
75
76
                                   argp_parse(), ARGP_KEY_ARG,
76
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
78
#include <sys/klog.h>           /* klogctl() */
77
79
 
78
80
/* Avahi */
79
81
/* All Avahi types, constants and functions
153
155
  
154
156
  
155
157
  /*
156
 
   * Helper function to insert pub and seckey to the enigne keyring.
 
158
   * Helper function to insert pub and seckey to the engine keyring.
157
159
   */
158
160
  bool import_key(const char *filename){
159
161
    int fd;
365
367
}
366
368
 
367
369
static const char * safer_gnutls_strerror(int value) {
368
 
  const char *ret = gnutls_strerror(value); /* Spurious warning */
 
370
  const char *ret = gnutls_strerror(value); /* Spurious warning from
 
371
                                               -Wunreachable-code */
369
372
  if(ret == NULL)
370
373
    ret = "(unknown)";
371
374
  return ret;
404
407
  /* OpenPGP credentials */
405
408
  gnutls_certificate_allocate_credentials(&mc->cred);
406
409
  if(ret != GNUTLS_E_SUCCESS){
407
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious
408
 
                                                    warning */
 
410
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
411
                                                  * from
 
412
                                                  * -Wunreachable-code
 
413
                                                  */
409
414
            safer_gnutls_strerror(ret));
410
415
    gnutls_global_deinit();
411
416
    return -1;
553
558
    fprintf(stderr, "Bad address: %s\n", ip);
554
559
    return -1;
555
560
  }
556
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
561
  to.in6.sin6_port = htons(port); /* Spurious warnings from
 
562
                                     -Wconversion and
 
563
                                     -Wunreachable-code */
557
564
  
558
565
  to.in6.sin6_scope_id = (uint32_t)if_index;
559
566
  
749
756
      avahi_address_snprint(ip, sizeof(ip), address);
750
757
      if(debug){
751
758
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
752
 
                PRIu16 ") on port %d\n", name, host_name, ip,
753
 
                interface, port);
 
759
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
760
                ip, (intmax_t)interface, port);
754
761
      }
755
762
      int ret = start_mandos_communication(ip, port, interface, mc);
756
763
      if(ret == 0){
816
823
    AvahiSServiceBrowser *sb = NULL;
817
824
    int error;
818
825
    int ret;
 
826
    intmax_t tmpmax;
 
827
    int numchars;
819
828
    int exitcode = EXIT_SUCCESS;
820
829
    const char *interface = "eth0";
821
830
    struct ifreq network;
824
833
    gid_t gid;
825
834
    char *connect_to = NULL;
826
835
    char tempdir[] = "/tmp/mandosXXXXXX";
 
836
    bool tempdir_created = false;
827
837
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
828
838
    const char *seckey = PATHDIR "/" SECKEY;
829
839
    const char *pubkey = PATHDIR "/" PUBKEY;
831
841
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
832
842
                          .dh_bits = 1024, .priority = "SECURE256"
833
843
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
834
 
    bool gnutls_initalized = false;
835
 
    bool gpgme_initalized = false;
 
844
    bool gnutls_initialized = false;
 
845
    bool gpgme_initialized = false;
 
846
    double delay = 2.5;
836
847
    
837
848
    {
838
849
      struct argp_option options[] = {
864
875
          .arg = "STRING",
865
876
          .doc = "GnuTLS priority string for the TLS handshake",
866
877
          .group = 1 },
 
878
        { .name = "delay", .key = 131,
 
879
          .arg = "SECONDS",
 
880
          .doc = "Maximum delay to wait for interface startup",
 
881
          .group = 2 },
867
882
        { .name = NULL }
868
883
      };
869
884
      
886
901
          pubkey = arg;
887
902
          break;
888
903
        case 129:               /* --dh-bits */
889
 
          ret = sscanf(arg, "%u", &mc.dh_bits);
890
 
          if(ret != 1){
 
904
          ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
905
          if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
906
             or arg[numchars] != '\0'){
891
907
            fprintf(stderr, "Bad number of DH bits\n");
892
908
            exit(EXIT_FAILURE);
893
909
          }
 
910
          mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
894
911
          break;
895
912
        case 130:               /* --priority */
896
913
          mc.priority = arg;
897
914
          break;
 
915
        case 131:               /* --delay */
 
916
          ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
917
          if(ret < 1 or arg[numchars] != '\0'){
 
918
            fprintf(stderr, "Bad delay\n");
 
919
            exit(EXIT_FAILURE);
 
920
          }
 
921
          break;
898
922
        case ARGP_KEY_ARG:
899
923
          argp_usage(state);
900
924
        case ARGP_KEY_END:
919
943
    
920
944
    /* If the interface is down, bring it up */
921
945
    {
 
946
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
 
947
         messages to mess up the prompt */
 
948
      ret = klogctl(8, NULL, 5);
 
949
      if(ret == -1){
 
950
        perror("klogctl");
 
951
      }
 
952
      
922
953
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
923
954
      if(sd < 0) {
924
955
        perror("socket");
925
956
        exitcode = EXIT_FAILURE;
 
957
        ret = klogctl(7, NULL, 0);
 
958
        if(ret == -1){
 
959
          perror("klogctl");
 
960
        }
926
961
        goto end;
927
962
      }
928
963
      strcpy(network.ifr_name, interface);
929
964
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
930
965
      if(ret == -1){
931
966
        perror("ioctl SIOCGIFFLAGS");
 
967
        ret = klogctl(7, NULL, 0);
 
968
        if(ret == -1){
 
969
          perror("klogctl");
 
970
        }
932
971
        exitcode = EXIT_FAILURE;
933
972
        goto end;
934
973
      }
938
977
        if(ret == -1){
939
978
          perror("ioctl SIOCSIFFLAGS");
940
979
          exitcode = EXIT_FAILURE;
 
980
          ret = klogctl(7, NULL, 0);
 
981
          if(ret == -1){
 
982
            perror("klogctl");
 
983
          }
941
984
          goto end;
942
985
        }
943
986
      }
 
987
      /* sleep checking until interface is running */
 
988
      for(int i=0; i < delay * 4; i++){
 
989
        ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
990
        if(ret == -1){
 
991
          perror("ioctl SIOCGIFFLAGS");
 
992
        } else if(network.ifr_flags & IFF_RUNNING){
 
993
          break;
 
994
        }
 
995
        struct timespec sleeptime = { .tv_nsec = 250000000 };
 
996
        nanosleep(&sleeptime, NULL);
 
997
      }
944
998
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
945
999
      if(ret == -1){
946
1000
        perror("close");
947
1001
      }
 
1002
      /* Restores kernel loglevel to default */
 
1003
      ret = klogctl(7, NULL, 0);
 
1004
      if(ret == -1){
 
1005
        perror("klogctl");
 
1006
      }
948
1007
    }
949
1008
    
950
1009
    uid = getuid();
951
1010
    gid = getgid();
952
1011
    
 
1012
    setgid(gid);
 
1013
    if(ret == -1){
 
1014
      perror("setgid");
 
1015
    }
 
1016
    
953
1017
    ret = setuid(uid);
954
1018
    if(ret == -1){
955
1019
      perror("setuid");
956
1020
    }
957
1021
    
958
 
    setgid(gid);
959
 
    if(ret == -1){
960
 
      perror("setgid");
961
 
    }
962
 
    
963
1022
    ret = init_gnutls_global(&mc, pubkey, seckey);
964
1023
    if(ret == -1){
965
1024
      fprintf(stderr, "init_gnutls_global failed\n");
966
1025
      exitcode = EXIT_FAILURE;
967
1026
      goto end;
968
1027
    } else {
969
 
      gnutls_initalized = true;
 
1028
      gnutls_initialized = true;
970
1029
    }
971
1030
    
972
1031
    if(mkdtemp(tempdir) == NULL){
973
1032
      perror("mkdtemp");
974
 
      tempdir[0] = '\0';
975
1033
      goto end;
976
1034
    }
 
1035
    tempdir_created = true;
977
1036
    
978
1037
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
979
 
      fprintf(stderr, "gpgme_initalized failed\n");
 
1038
      fprintf(stderr, "init_gpgme failed\n");
980
1039
      exitcode = EXIT_FAILURE;
981
1040
      goto end;
982
1041
    } else {
983
 
      gpgme_initalized = true;
 
1042
      gpgme_initialized = true;
984
1043
    }
985
1044
    
986
1045
    if_index = (AvahiIfIndex) if_nametoindex(interface);
987
1046
    if(if_index == 0){
988
1047
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
989
 
      exit(EXIT_FAILURE);
 
1048
      exitcode = EXIT_FAILURE;
 
1049
      goto end;
990
1050
    }
991
1051
    
992
1052
    if(connect_to != NULL){
999
1059
        goto end;
1000
1060
      }
1001
1061
      uint16_t port;
1002
 
      ret = sscanf(address+1, "%" SCNu16, &port);
1003
 
      if(ret != 1){
 
1062
      ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1063
      if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1064
         or address[numchars+1] != '\0'){
1004
1065
        fprintf(stderr, "Bad port number\n");
1005
1066
        exitcode = EXIT_FAILURE;
1006
1067
        goto end;
1007
1068
      }
 
1069
      port = (uint16_t)tmpmax;
1008
1070
      *address = '\0';
1009
1071
      address = connect_to;
1010
1072
      ret = start_mandos_communication(address, port, if_index, &mc);
1075
1137
    if(debug){
1076
1138
      fprintf(stderr, "Starting Avahi loop search\n");
1077
1139
    }
1078
 
    
 
1140
 
1079
1141
    avahi_simple_poll_loop(mc.simple_poll);
1080
1142
    
1081
1143
 end:
1094
1156
    if(mc.simple_poll != NULL)
1095
1157
        avahi_simple_poll_free(mc.simple_poll);
1096
1158
    
1097
 
    if(gnutls_initalized){
 
1159
    if(gnutls_initialized){
1098
1160
      gnutls_certificate_free_credentials(mc.cred);
1099
1161
      gnutls_global_deinit();
1100
1162
      gnutls_dh_params_deinit(mc.dh_params);
1101
1163
    }
1102
1164
    
1103
 
    if(gpgme_initalized){
 
1165
    if(gpgme_initialized){
1104
1166
      gpgme_release(mc.ctx);
1105
1167
    }
1106
1168
    
1107
1169
    /* Removes the temp directory used by GPGME */
1108
 
    if(tempdir[0] != '\0'){
 
1170
    if(tempdir_created){
1109
1171
      DIR *d;
1110
1172
      struct dirent *direntry;
1111
1173
      d = opendir(tempdir);
1119
1181
          if(direntry == NULL){
1120
1182
            break;
1121
1183
          }
1122
 
          if(direntry->d_type == DT_REG){
1123
 
            char *fullname = NULL;
1124
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1125
 
                           direntry->d_name);
1126
 
            if(ret < 0){
1127
 
              perror("asprintf");
1128
 
              continue;
1129
 
            }
1130
 
            ret = unlink(fullname);
1131
 
            if(ret == -1){
1132
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1133
 
                      fullname, strerror(errno));
1134
 
            }
1135
 
            free(fullname);
1136
 
          }
 
1184
          /* Skip "." and ".." */
 
1185
          if(direntry->d_name[0] == '.'
 
1186
             and (direntry->d_name[1] == '\0'
 
1187
                  or (direntry->d_name[1] == '.'
 
1188
                      and direntry->d_name[2] == '\0'))){
 
1189
            continue;
 
1190
          }
 
1191
          char *fullname = NULL;
 
1192
          ret = asprintf(&fullname, "%s/%s", tempdir,
 
1193
                         direntry->d_name);
 
1194
          if(ret < 0){
 
1195
            perror("asprintf");
 
1196
            continue;
 
1197
          }
 
1198
          ret = remove(fullname);
 
1199
          if(ret == -1){
 
1200
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
1201
                    strerror(errno));
 
1202
          }
 
1203
          free(fullname);
1137
1204
        }
1138
1205
        closedir(d);
1139
1206
      }
1142
1209
        perror("rmdir");
1143
1210
      }
1144
1211
    }
1145
 
          
 
1212
    
1146
1213
    return exitcode;
1147
1214
}