/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-27 00:07:26 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090127000726-4cafcfy35g8kh2b8
* INSTALL (Prerequisites/Mandos Server): Added "PyGObject".

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,
60
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(), nanosleep() */
 
64
#include <time.h>               /* 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 */
77
 
#include <sys/klog.h>           /* klogctl() */
78
78
 
79
79
/* Avahi */
80
80
/* All Avahi types, constants and functions
839
839
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
840
840
                          .dh_bits = 1024, .priority = "SECURE256"
841
841
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
842
 
    bool gnutls_initalized = false;
843
 
    bool gpgme_initalized = false;
844
 
    double delay = 2.5;
 
842
    bool gnutls_initialized = false;
 
843
    bool gpgme_initialized = false;
845
844
    
846
845
    {
847
846
      struct argp_option options[] = {
873
872
          .arg = "STRING",
874
873
          .doc = "GnuTLS priority string for the TLS handshake",
875
874
          .group = 1 },
876
 
        { .name = "delay", .key = 131,
877
 
          .arg = "SECONDS",
878
 
          .doc = "Maximum delay to wait for interface startup",
879
 
          .group = 2 },
880
875
        { .name = NULL }
881
876
      };
882
877
      
910
905
        case 130:               /* --priority */
911
906
          mc.priority = arg;
912
907
          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;
920
908
        case ARGP_KEY_ARG:
921
909
          argp_usage(state);
922
910
        case ARGP_KEY_END:
941
929
    
942
930
    /* If the interface is down, bring it up */
943
931
    {
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
 
 
951
932
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
952
933
      if(sd < 0) {
953
934
        perror("socket");
954
935
        exitcode = EXIT_FAILURE;
955
 
        ret = klogctl(7, NULL, 0);
956
 
        if(ret == -1){
957
 
          perror("klogctl");
958
 
        }
959
936
        goto end;
960
937
      }
961
938
      strcpy(network.ifr_name, interface);
962
939
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
963
940
      if(ret == -1){
964
941
        perror("ioctl SIOCGIFFLAGS");
965
 
        ret = klogctl(7, NULL, 0);
966
 
        if(ret == -1){
967
 
          perror("klogctl");
968
 
        }
969
942
        exitcode = EXIT_FAILURE;
970
943
        goto end;
971
944
      }
975
948
        if(ret == -1){
976
949
          perror("ioctl SIOCSIFFLAGS");
977
950
          exitcode = EXIT_FAILURE;
978
 
          ret = klogctl(7, NULL, 0);
979
 
          if(ret == -1){
980
 
            perror("klogctl");
981
 
          }
982
951
          goto end;
983
952
        }
984
953
      }
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
 
      }
996
954
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
997
955
      if(ret == -1){
998
956
        perror("close");
999
957
      }
1000
 
      // Restores kernel loglevel to default
1001
 
      ret = klogctl(7, NULL, 0);
1002
 
      if(ret == -1){
1003
 
        perror("klogctl");
1004
 
      }
1005
958
    }
1006
959
    
1007
960
    uid = getuid();
1023
976
      exitcode = EXIT_FAILURE;
1024
977
      goto end;
1025
978
    } else {
1026
 
      gnutls_initalized = true;
 
979
      gnutls_initialized = true;
1027
980
    }
1028
981
    
1029
982
    if(mkdtemp(tempdir) == NULL){
1033
986
    }
1034
987
    
1035
988
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1036
 
      fprintf(stderr, "gpgme_initalized failed\n");
 
989
      fprintf(stderr, "init_gpgme failed\n");
1037
990
      exitcode = EXIT_FAILURE;
1038
991
      goto end;
1039
992
    } else {
1040
 
      gpgme_initalized = true;
 
993
      gpgme_initialized = true;
1041
994
    }
1042
995
    
1043
996
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1134
1087
    if(debug){
1135
1088
      fprintf(stderr, "Starting Avahi loop search\n");
1136
1089
    }
1137
 
 
 
1090
    
1138
1091
    avahi_simple_poll_loop(mc.simple_poll);
1139
1092
    
1140
1093
 end:
1153
1106
    if(mc.simple_poll != NULL)
1154
1107
        avahi_simple_poll_free(mc.simple_poll);
1155
1108
    
1156
 
    if(gnutls_initalized){
 
1109
    if(gnutls_initialized){
1157
1110
      gnutls_certificate_free_credentials(mc.cred);
1158
1111
      gnutls_global_deinit();
1159
1112
      gnutls_dh_params_deinit(mc.dh_params);
1160
1113
    }
1161
1114
    
1162
 
    if(gpgme_initalized){
 
1115
    if(gpgme_initialized){
1163
1116
      gpgme_release(mc.ctx);
1164
1117
    }
1165
1118
    
1178
1131
          if(direntry == NULL){
1179
1132
            break;
1180
1133
          }
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
 
          }
 
1134
          /* Skip "." and ".." */
 
1135
          if(direntry->d_name[0] == '.'
 
1136
             and (direntry->d_name[1] == '\0'
 
1137
                  or (direntry->d_name[1] == '.'
 
1138
                      and direntry->d_name[2] == '\0'))){
 
1139
            continue;
 
1140
          }
 
1141
          char *fullname = NULL;
 
1142
          ret = asprintf(&fullname, "%s/%s", tempdir,
 
1143
                         direntry->d_name);
 
1144
          if(ret < 0){
 
1145
            perror("asprintf");
 
1146
            continue;
 
1147
          }
 
1148
          ret = remove(fullname);
 
1149
          if(ret == -1){
 
1150
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
 
1151
                    strerror(errno));
 
1152
          }
 
1153
          free(fullname);
1196
1154
        }
1197
1155
        closedir(d);
1198
1156
      }
1201
1159
        perror("rmdir");
1202
1160
      }
1203
1161
    }
1204
 
          
 
1162
    
1205
1163
    return exitcode;
1206
1164
}