/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;
754
756
      avahi_address_snprint(ip, sizeof(ip), address);
755
757
      if(debug){
756
758
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
757
 
                PRIu16 ") on port %d\n", name, host_name, ip,
758
 
                interface, port);
 
759
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
 
760
                ip, (intmax_t)interface, port);
759
761
      }
760
762
      int ret = start_mandos_communication(ip, port, interface, mc);
761
763
      if(ret == 0){
821
823
    AvahiSServiceBrowser *sb = NULL;
822
824
    int error;
823
825
    int ret;
 
826
    intmax_t tmpmax;
 
827
    int numchars;
824
828
    int exitcode = EXIT_SUCCESS;
825
829
    const char *interface = "eth0";
826
830
    struct ifreq network;
829
833
    gid_t gid;
830
834
    char *connect_to = NULL;
831
835
    char tempdir[] = "/tmp/mandosXXXXXX";
 
836
    bool tempdir_created = false;
832
837
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
833
838
    const char *seckey = PATHDIR "/" SECKEY;
834
839
    const char *pubkey = PATHDIR "/" PUBKEY;
836
841
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
837
842
                          .dh_bits = 1024, .priority = "SECURE256"
838
843
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
839
 
    bool gnutls_initalized = false;
840
 
    bool gpgme_initalized = false;
 
844
    bool gnutls_initialized = false;
 
845
    bool gpgme_initialized = false;
 
846
    double delay = 2.5;
841
847
    
842
848
    {
843
849
      struct argp_option options[] = {
869
875
          .arg = "STRING",
870
876
          .doc = "GnuTLS priority string for the TLS handshake",
871
877
          .group = 1 },
 
878
        { .name = "delay", .key = 131,
 
879
          .arg = "SECONDS",
 
880
          .doc = "Maximum delay to wait for interface startup",
 
881
          .group = 2 },
872
882
        { .name = NULL }
873
883
      };
874
884
      
891
901
          pubkey = arg;
892
902
          break;
893
903
        case 129:               /* --dh-bits */
894
 
          ret = sscanf(arg, "%u", &mc.dh_bits);
895
 
          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'){
896
907
            fprintf(stderr, "Bad number of DH bits\n");
897
908
            exit(EXIT_FAILURE);
898
909
          }
 
910
          mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
899
911
          break;
900
912
        case 130:               /* --priority */
901
913
          mc.priority = arg;
902
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;
903
922
        case ARGP_KEY_ARG:
904
923
          argp_usage(state);
905
924
        case ARGP_KEY_END:
924
943
    
925
944
    /* If the interface is down, bring it up */
926
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
      
927
953
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
928
954
      if(sd < 0) {
929
955
        perror("socket");
930
956
        exitcode = EXIT_FAILURE;
 
957
        ret = klogctl(7, NULL, 0);
 
958
        if(ret == -1){
 
959
          perror("klogctl");
 
960
        }
931
961
        goto end;
932
962
      }
933
963
      strcpy(network.ifr_name, interface);
934
964
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
935
965
      if(ret == -1){
936
966
        perror("ioctl SIOCGIFFLAGS");
 
967
        ret = klogctl(7, NULL, 0);
 
968
        if(ret == -1){
 
969
          perror("klogctl");
 
970
        }
937
971
        exitcode = EXIT_FAILURE;
938
972
        goto end;
939
973
      }
943
977
        if(ret == -1){
944
978
          perror("ioctl SIOCSIFFLAGS");
945
979
          exitcode = EXIT_FAILURE;
 
980
          ret = klogctl(7, NULL, 0);
 
981
          if(ret == -1){
 
982
            perror("klogctl");
 
983
          }
946
984
          goto end;
947
985
        }
948
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
      }
949
998
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
950
999
      if(ret == -1){
951
1000
        perror("close");
952
1001
      }
 
1002
      /* Restores kernel loglevel to default */
 
1003
      ret = klogctl(7, NULL, 0);
 
1004
      if(ret == -1){
 
1005
        perror("klogctl");
 
1006
      }
953
1007
    }
954
1008
    
955
1009
    uid = getuid();
956
1010
    gid = getgid();
957
1011
    
 
1012
    setgid(gid);
 
1013
    if(ret == -1){
 
1014
      perror("setgid");
 
1015
    }
 
1016
    
958
1017
    ret = setuid(uid);
959
1018
    if(ret == -1){
960
1019
      perror("setuid");
961
1020
    }
962
1021
    
963
 
    setgid(gid);
964
 
    if(ret == -1){
965
 
      perror("setgid");
966
 
    }
967
 
    
968
1022
    ret = init_gnutls_global(&mc, pubkey, seckey);
969
1023
    if(ret == -1){
970
1024
      fprintf(stderr, "init_gnutls_global failed\n");
971
1025
      exitcode = EXIT_FAILURE;
972
1026
      goto end;
973
1027
    } else {
974
 
      gnutls_initalized = true;
 
1028
      gnutls_initialized = true;
975
1029
    }
976
1030
    
977
1031
    if(mkdtemp(tempdir) == NULL){
978
1032
      perror("mkdtemp");
979
 
      tempdir[0] = '\0';
980
1033
      goto end;
981
1034
    }
 
1035
    tempdir_created = true;
982
1036
    
983
1037
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
984
 
      fprintf(stderr, "gpgme_initalized failed\n");
 
1038
      fprintf(stderr, "init_gpgme failed\n");
985
1039
      exitcode = EXIT_FAILURE;
986
1040
      goto end;
987
1041
    } else {
988
 
      gpgme_initalized = true;
 
1042
      gpgme_initialized = true;
989
1043
    }
990
1044
    
991
1045
    if_index = (AvahiIfIndex) if_nametoindex(interface);
992
1046
    if(if_index == 0){
993
1047
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
994
 
      exit(EXIT_FAILURE);
 
1048
      exitcode = EXIT_FAILURE;
 
1049
      goto end;
995
1050
    }
996
1051
    
997
1052
    if(connect_to != NULL){
1004
1059
        goto end;
1005
1060
      }
1006
1061
      uint16_t port;
1007
 
      ret = sscanf(address+1, "%" SCNu16, &port);
1008
 
      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'){
1009
1065
        fprintf(stderr, "Bad port number\n");
1010
1066
        exitcode = EXIT_FAILURE;
1011
1067
        goto end;
1012
1068
      }
 
1069
      port = (uint16_t)tmpmax;
1013
1070
      *address = '\0';
1014
1071
      address = connect_to;
1015
1072
      ret = start_mandos_communication(address, port, if_index, &mc);
1080
1137
    if(debug){
1081
1138
      fprintf(stderr, "Starting Avahi loop search\n");
1082
1139
    }
1083
 
    
 
1140
 
1084
1141
    avahi_simple_poll_loop(mc.simple_poll);
1085
1142
    
1086
1143
 end:
1099
1156
    if(mc.simple_poll != NULL)
1100
1157
        avahi_simple_poll_free(mc.simple_poll);
1101
1158
    
1102
 
    if(gnutls_initalized){
 
1159
    if(gnutls_initialized){
1103
1160
      gnutls_certificate_free_credentials(mc.cred);
1104
1161
      gnutls_global_deinit();
1105
1162
      gnutls_dh_params_deinit(mc.dh_params);
1106
1163
    }
1107
1164
    
1108
 
    if(gpgme_initalized){
 
1165
    if(gpgme_initialized){
1109
1166
      gpgme_release(mc.ctx);
1110
1167
    }
1111
1168
    
1112
1169
    /* Removes the temp directory used by GPGME */
1113
 
    if(tempdir[0] != '\0'){
 
1170
    if(tempdir_created){
1114
1171
      DIR *d;
1115
1172
      struct dirent *direntry;
1116
1173
      d = opendir(tempdir);
1124
1181
          if(direntry == NULL){
1125
1182
            break;
1126
1183
          }
1127
 
          if(direntry->d_type == DT_REG){
1128
 
            char *fullname = NULL;
1129
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1130
 
                           direntry->d_name);
1131
 
            if(ret < 0){
1132
 
              perror("asprintf");
1133
 
              continue;
1134
 
            }
1135
 
            ret = unlink(fullname);
1136
 
            if(ret == -1){
1137
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1138
 
                      fullname, strerror(errno));
1139
 
            }
1140
 
            free(fullname);
1141
 
          }
 
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);
1142
1204
        }
1143
1205
        closedir(d);
1144
1206
      }
1147
1209
        perror("rmdir");
1148
1210
      }
1149
1211
    }
1150
 
          
 
1212
    
1151
1213
    return exitcode;
1152
1214
}