/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-15 02:52:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090115025202-utyjssfivzumvw7m
* debian/watch: New file.

* debian/mandos-client.README.Debian (Emergency Escape): New section;
                                                         document the
                                                         "mandos=off"
                                                         kernel
                                                         parameter.
* initramfs-tools-script: Exit if kernel has parameter "mandos=off".

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>               /* nanosleep(), time() */
 
63
#include <time.h>               /* time() */
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 */
78
 
#include <sys/klog.h>           /* klogctl() */
79
77
 
80
78
/* Avahi */
81
79
/* All Avahi types, constants and functions
155
153
  
156
154
  
157
155
  /*
158
 
   * Helper function to insert pub and seckey to the engine keyring.
 
156
   * Helper function to insert pub and seckey to the enigne keyring.
159
157
   */
160
158
  bool import_key(const char *filename){
161
159
    int fd;
833
831
    gid_t gid;
834
832
    char *connect_to = NULL;
835
833
    char tempdir[] = "/tmp/mandosXXXXXX";
836
 
    bool tempdir_created = false;
837
834
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
835
    const char *seckey = PATHDIR "/" SECKEY;
839
836
    const char *pubkey = PATHDIR "/" PUBKEY;
841
838
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
839
                          .dh_bits = 1024, .priority = "SECURE256"
843
840
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
 
    bool gnutls_initialized = false;
845
 
    bool gpgme_initialized = false;
846
 
    double delay = 2.5;
 
841
    bool gnutls_initalized = false;
 
842
    bool gpgme_initalized = false;
847
843
    
848
844
    {
849
845
      struct argp_option options[] = {
875
871
          .arg = "STRING",
876
872
          .doc = "GnuTLS priority string for the TLS handshake",
877
873
          .group = 1 },
878
 
        { .name = "delay", .key = 131,
879
 
          .arg = "SECONDS",
880
 
          .doc = "Maximum delay to wait for interface startup",
881
 
          .group = 2 },
882
874
        { .name = NULL }
883
875
      };
884
876
      
912
904
        case 130:               /* --priority */
913
905
          mc.priority = arg;
914
906
          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;
922
907
        case ARGP_KEY_ARG:
923
908
          argp_usage(state);
924
909
        case ARGP_KEY_END:
943
928
    
944
929
    /* If the interface is down, bring it up */
945
930
    {
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
 
      
953
931
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
954
932
      if(sd < 0) {
955
933
        perror("socket");
956
934
        exitcode = EXIT_FAILURE;
957
 
        ret = klogctl(7, NULL, 0);
958
 
        if(ret == -1){
959
 
          perror("klogctl");
960
 
        }
961
935
        goto end;
962
936
      }
963
937
      strcpy(network.ifr_name, interface);
964
938
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
965
939
      if(ret == -1){
966
940
        perror("ioctl SIOCGIFFLAGS");
967
 
        ret = klogctl(7, NULL, 0);
968
 
        if(ret == -1){
969
 
          perror("klogctl");
970
 
        }
971
941
        exitcode = EXIT_FAILURE;
972
942
        goto end;
973
943
      }
977
947
        if(ret == -1){
978
948
          perror("ioctl SIOCSIFFLAGS");
979
949
          exitcode = EXIT_FAILURE;
980
 
          ret = klogctl(7, NULL, 0);
981
 
          if(ret == -1){
982
 
            perror("klogctl");
983
 
          }
984
950
          goto end;
985
951
        }
986
952
      }
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
 
      }
998
953
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
999
954
      if(ret == -1){
1000
955
        perror("close");
1001
956
      }
1002
 
      /* Restores kernel loglevel to default */
1003
 
      ret = klogctl(7, NULL, 0);
1004
 
      if(ret == -1){
1005
 
        perror("klogctl");
1006
 
      }
1007
957
    }
1008
958
    
1009
959
    uid = getuid();
1010
960
    gid = getgid();
1011
961
    
 
962
    ret = setuid(uid);
 
963
    if(ret == -1){
 
964
      perror("setuid");
 
965
    }
 
966
    
1012
967
    setgid(gid);
1013
968
    if(ret == -1){
1014
969
      perror("setgid");
1015
970
    }
1016
971
    
1017
 
    ret = setuid(uid);
1018
 
    if(ret == -1){
1019
 
      perror("setuid");
1020
 
    }
1021
 
    
1022
972
    ret = init_gnutls_global(&mc, pubkey, seckey);
1023
973
    if(ret == -1){
1024
974
      fprintf(stderr, "init_gnutls_global failed\n");
1025
975
      exitcode = EXIT_FAILURE;
1026
976
      goto end;
1027
977
    } else {
1028
 
      gnutls_initialized = true;
 
978
      gnutls_initalized = true;
1029
979
    }
1030
980
    
1031
981
    if(mkdtemp(tempdir) == NULL){
1032
982
      perror("mkdtemp");
 
983
      tempdir[0] = '\0';
1033
984
      goto end;
1034
985
    }
1035
 
    tempdir_created = true;
1036
986
    
1037
987
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
 
      fprintf(stderr, "init_gpgme failed\n");
 
988
      fprintf(stderr, "gpgme_initalized failed\n");
1039
989
      exitcode = EXIT_FAILURE;
1040
990
      goto end;
1041
991
    } else {
1042
 
      gpgme_initialized = true;
 
992
      gpgme_initalized = true;
1043
993
    }
1044
994
    
1045
995
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
996
    if(if_index == 0){
1047
997
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
 
      exitcode = EXIT_FAILURE;
1049
 
      goto end;
 
998
      exit(EXIT_FAILURE);
1050
999
    }
1051
1000
    
1052
1001
    if(connect_to != NULL){
1137
1086
    if(debug){
1138
1087
      fprintf(stderr, "Starting Avahi loop search\n");
1139
1088
    }
1140
 
 
 
1089
    
1141
1090
    avahi_simple_poll_loop(mc.simple_poll);
1142
1091
    
1143
1092
 end:
1156
1105
    if(mc.simple_poll != NULL)
1157
1106
        avahi_simple_poll_free(mc.simple_poll);
1158
1107
    
1159
 
    if(gnutls_initialized){
 
1108
    if(gnutls_initalized){
1160
1109
      gnutls_certificate_free_credentials(mc.cred);
1161
1110
      gnutls_global_deinit();
1162
1111
      gnutls_dh_params_deinit(mc.dh_params);
1163
1112
    }
1164
1113
    
1165
 
    if(gpgme_initialized){
 
1114
    if(gpgme_initalized){
1166
1115
      gpgme_release(mc.ctx);
1167
1116
    }
1168
1117
    
1169
1118
    /* Removes the temp directory used by GPGME */
1170
 
    if(tempdir_created){
 
1119
    if(tempdir[0] != '\0'){
1171
1120
      DIR *d;
1172
1121
      struct dirent *direntry;
1173
1122
      d = opendir(tempdir);
1181
1130
          if(direntry == NULL){
1182
1131
            break;
1183
1132
          }
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);
 
1133
          if(direntry->d_type == DT_REG){
 
1134
            char *fullname = NULL;
 
1135
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1136
                           direntry->d_name);
 
1137
            if(ret < 0){
 
1138
              perror("asprintf");
 
1139
              continue;
 
1140
            }
 
1141
            ret = unlink(fullname);
 
1142
            if(ret == -1){
 
1143
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1144
                      fullname, strerror(errno));
 
1145
            }
 
1146
            free(fullname);
 
1147
          }
1204
1148
        }
1205
1149
        closedir(d);
1206
1150
      }
1209
1153
        perror("rmdir");
1210
1154
      }
1211
1155
    }
1212
 
    
 
1156
          
1213
1157
    return exitcode;
1214
1158
}