/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

merge

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(),
40
 
                                   remove() */
 
39
                                   stdout, ferror(), sscanf */
41
40
#include <stdint.h>             /* uint16_t, uint32_t */
42
41
#include <stddef.h>             /* NULL, size_t, ssize_t */
43
42
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
61
60
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
62
61
#include <assert.h>             /* assert() */
63
62
#include <errno.h>              /* perror(), errno */
64
 
#include <time.h>               /* time() */
 
63
#include <time.h>               /* time(), nanosleep() */
65
64
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
65
                                   SIOCSIFFLAGS, if_indextoname(),
67
66
                                   if_nametoindex(), IF_NAMESIZE */
75
74
                                   argp_state, struct argp,
76
75
                                   argp_parse(), ARGP_KEY_ARG,
77
76
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
 
77
#include <sys/klog.h>           /* klogctl() */
78
78
 
79
79
/* Avahi */
80
80
/* All Avahi types, constants and functions
154
154
  
155
155
  
156
156
  /*
157
 
   * Helper function to insert pub and seckey to the engine keyring.
 
157
   * Helper function to insert pub and seckey to the enigne keyring.
158
158
   */
159
159
  bool import_key(const char *filename){
160
160
    int fd;
832
832
    gid_t gid;
833
833
    char *connect_to = NULL;
834
834
    char tempdir[] = "/tmp/mandosXXXXXX";
835
 
    bool tempdir_created = false;
836
835
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
837
836
    const char *seckey = PATHDIR "/" SECKEY;
838
837
    const char *pubkey = PATHDIR "/" PUBKEY;
840
839
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
841
840
                          .dh_bits = 1024, .priority = "SECURE256"
842
841
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
843
 
    bool gnutls_initialized = false;
844
 
    bool gpgme_initialized = false;
 
842
    bool gnutls_initalized = false;
 
843
    bool gpgme_initalized = false;
 
844
    double delay = 2.5;
845
845
    
846
846
    {
847
847
      struct argp_option options[] = {
873
873
          .arg = "STRING",
874
874
          .doc = "GnuTLS priority string for the TLS handshake",
875
875
          .group = 1 },
 
876
        { .name = "delay", .key = 131,
 
877
          .arg = "SECONDS",
 
878
          .doc = "Maximum delay to wait for interface startup",
 
879
          .group = 2 },
876
880
        { .name = NULL }
877
881
      };
878
882
      
906
910
        case 130:               /* --priority */
907
911
          mc.priority = arg;
908
912
          break;
 
913
        case 131:               /* --delay */
 
914
          ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
915
          if(ret < 1 or arg[numchars] != '\0'){
 
916
            fprintf(stderr, "Bad delay\n");
 
917
            exit(EXIT_FAILURE);
 
918
          }
 
919
          break;
909
920
        case ARGP_KEY_ARG:
910
921
          argp_usage(state);
911
922
        case ARGP_KEY_END:
930
941
    
931
942
    /* If the interface is down, bring it up */
932
943
    {
 
944
      // Lower kernel loglevel to KERN_NOTICE to avoid
 
945
      // KERN_INFO messages to mess up the prompt
 
946
      ret = klogctl(8, NULL, 5);
 
947
      if(ret == -1){
 
948
        perror("klogctl");
 
949
      }
 
950
 
933
951
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
934
952
      if(sd < 0) {
935
953
        perror("socket");
936
954
        exitcode = EXIT_FAILURE;
 
955
        ret = klogctl(7, NULL, 0);
 
956
        if(ret == -1){
 
957
          perror("klogctl");
 
958
        }
937
959
        goto end;
938
960
      }
939
961
      strcpy(network.ifr_name, interface);
940
962
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
941
963
      if(ret == -1){
942
964
        perror("ioctl SIOCGIFFLAGS");
 
965
        ret = klogctl(7, NULL, 0);
 
966
        if(ret == -1){
 
967
          perror("klogctl");
 
968
        }
943
969
        exitcode = EXIT_FAILURE;
944
970
        goto end;
945
971
      }
949
975
        if(ret == -1){
950
976
          perror("ioctl SIOCSIFFLAGS");
951
977
          exitcode = EXIT_FAILURE;
 
978
          ret = klogctl(7, NULL, 0);
 
979
          if(ret == -1){
 
980
            perror("klogctl");
 
981
          }
952
982
          goto end;
953
983
        }
954
984
      }
 
985
      // sleep checking until interface is running
 
986
      for(int i=0; i < delay * 4; i++){
 
987
        ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
988
        if(ret == -1){
 
989
          perror("ioctl SIOCGIFFLAGS");
 
990
        } else if(network.ifr_flags & IFF_RUNNING){
 
991
          break;
 
992
        }
 
993
        struct timespec sleeptime = { .tv_nsec = 250000000 };
 
994
        nanosleep(&sleeptime, NULL);
 
995
      }
955
996
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
956
997
      if(ret == -1){
957
998
        perror("close");
958
999
      }
 
1000
      // Restores kernel loglevel to default
 
1001
      ret = klogctl(7, NULL, 0);
 
1002
      if(ret == -1){
 
1003
        perror("klogctl");
 
1004
      }
959
1005
    }
960
1006
    
961
1007
    uid = getuid();
977
1023
      exitcode = EXIT_FAILURE;
978
1024
      goto end;
979
1025
    } else {
980
 
      gnutls_initialized = true;
 
1026
      gnutls_initalized = true;
981
1027
    }
982
1028
    
983
1029
    if(mkdtemp(tempdir) == NULL){
984
1030
      perror("mkdtemp");
 
1031
      tempdir[0] = '\0';
985
1032
      goto end;
986
1033
    }
987
 
    tempdir_created = true;
988
1034
    
989
1035
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
990
 
      fprintf(stderr, "init_gpgme failed\n");
 
1036
      fprintf(stderr, "gpgme_initalized failed\n");
991
1037
      exitcode = EXIT_FAILURE;
992
1038
      goto end;
993
1039
    } else {
994
 
      gpgme_initialized = true;
 
1040
      gpgme_initalized = true;
995
1041
    }
996
1042
    
997
1043
    if_index = (AvahiIfIndex) if_nametoindex(interface);
998
1044
    if(if_index == 0){
999
1045
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1000
 
      exitcode = EXIT_FAILURE;
1001
 
      goto end;
 
1046
      exit(EXIT_FAILURE);
1002
1047
    }
1003
1048
    
1004
1049
    if(connect_to != NULL){
1089
1134
    if(debug){
1090
1135
      fprintf(stderr, "Starting Avahi loop search\n");
1091
1136
    }
1092
 
    
 
1137
 
1093
1138
    avahi_simple_poll_loop(mc.simple_poll);
1094
1139
    
1095
1140
 end:
1108
1153
    if(mc.simple_poll != NULL)
1109
1154
        avahi_simple_poll_free(mc.simple_poll);
1110
1155
    
1111
 
    if(gnutls_initialized){
 
1156
    if(gnutls_initalized){
1112
1157
      gnutls_certificate_free_credentials(mc.cred);
1113
1158
      gnutls_global_deinit();
1114
1159
      gnutls_dh_params_deinit(mc.dh_params);
1115
1160
    }
1116
1161
    
1117
 
    if(gpgme_initialized){
 
1162
    if(gpgme_initalized){
1118
1163
      gpgme_release(mc.ctx);
1119
1164
    }
1120
1165
    
1121
1166
    /* Removes the temp directory used by GPGME */
1122
 
    if(tempdir_created){
 
1167
    if(tempdir[0] != '\0'){
1123
1168
      DIR *d;
1124
1169
      struct dirent *direntry;
1125
1170
      d = opendir(tempdir);
1133
1178
          if(direntry == NULL){
1134
1179
            break;
1135
1180
          }
1136
 
          /* Skip "." and ".." */
1137
 
          if(direntry->d_name[0] == '.'
1138
 
             and (direntry->d_name[1] == '\0'
1139
 
                  or (direntry->d_name[1] == '.'
1140
 
                      and direntry->d_name[2] == '\0'))){
1141
 
            continue;
1142
 
          }
1143
 
          char *fullname = NULL;
1144
 
          ret = asprintf(&fullname, "%s/%s", tempdir,
1145
 
                         direntry->d_name);
1146
 
          if(ret < 0){
1147
 
            perror("asprintf");
1148
 
            continue;
1149
 
          }
1150
 
          ret = remove(fullname);
1151
 
          if(ret == -1){
1152
 
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1153
 
                    strerror(errno));
1154
 
          }
1155
 
          free(fullname);
 
1181
          if(direntry->d_type == DT_REG){
 
1182
            char *fullname = NULL;
 
1183
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1184
                           direntry->d_name);
 
1185
            if(ret < 0){
 
1186
              perror("asprintf");
 
1187
              continue;
 
1188
            }
 
1189
            ret = unlink(fullname);
 
1190
            if(ret == -1){
 
1191
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1192
                      fullname, strerror(errno));
 
1193
            }
 
1194
            free(fullname);
 
1195
          }
1156
1196
        }
1157
1197
        closedir(d);
1158
1198
      }
1161
1201
        perror("rmdir");
1162
1202
      }
1163
1203
    }
1164
 
    
 
1204
          
1165
1205
    return exitcode;
1166
1206
}