/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: Björn Påhlsson
  • Date: 2009-01-29 22:01:00 UTC
  • Revision ID: belorn@nexus-20090129220100-8wzzh4q1b1ka56a2
* mandos (main): Catch SIGSEGV and raise RuntimeError.

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
62
62
#include <assert.h>             /* assert() */
63
63
#include <errno.h>              /* perror(), errno */
64
 
#include <time.h>               /* nanosleep(), time() */
 
64
#include <time.h>               /* time() */
65
65
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
66
                                   SIOCSIFFLAGS, if_indextoname(),
67
67
                                   if_nametoindex(), IF_NAMESIZE */
75
75
                                   argp_state, struct argp,
76
76
                                   argp_parse(), ARGP_KEY_ARG,
77
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
 
#include <sys/klog.h>           /* klogctl() */
79
78
 
80
79
/* Avahi */
81
80
/* All Avahi types, constants and functions
843
842
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
843
    bool gnutls_initialized = false;
845
844
    bool gpgme_initialized = false;
846
 
    double delay = 2.5;
847
845
    
848
846
    {
849
847
      struct argp_option options[] = {
875
873
          .arg = "STRING",
876
874
          .doc = "GnuTLS priority string for the TLS handshake",
877
875
          .group = 1 },
878
 
        { .name = "delay", .key = 131,
879
 
          .arg = "SECONDS",
880
 
          .doc = "Maximum delay to wait for interface startup",
881
 
          .group = 2 },
882
876
        { .name = NULL }
883
877
      };
884
878
      
912
906
        case 130:               /* --priority */
913
907
          mc.priority = arg;
914
908
          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
909
        case ARGP_KEY_ARG:
923
910
          argp_usage(state);
924
911
        case ARGP_KEY_END:
943
930
    
944
931
    /* If the interface is down, bring it up */
945
932
    {
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
933
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
954
934
      if(sd < 0) {
955
935
        perror("socket");
956
936
        exitcode = EXIT_FAILURE;
957
 
        ret = klogctl(7, NULL, 0);
958
 
        if(ret == -1){
959
 
          perror("klogctl");
960
 
        }
961
937
        goto end;
962
938
      }
963
939
      strcpy(network.ifr_name, interface);
964
940
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
965
941
      if(ret == -1){
966
942
        perror("ioctl SIOCGIFFLAGS");
967
 
        ret = klogctl(7, NULL, 0);
968
 
        if(ret == -1){
969
 
          perror("klogctl");
970
 
        }
971
943
        exitcode = EXIT_FAILURE;
972
944
        goto end;
973
945
      }
977
949
        if(ret == -1){
978
950
          perror("ioctl SIOCSIFFLAGS");
979
951
          exitcode = EXIT_FAILURE;
980
 
          ret = klogctl(7, NULL, 0);
981
 
          if(ret == -1){
982
 
            perror("klogctl");
983
 
          }
984
952
          goto end;
985
953
        }
986
954
      }
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
955
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
999
956
      if(ret == -1){
1000
957
        perror("close");
1001
958
      }
1002
 
      /* Restores kernel loglevel to default */
1003
 
      ret = klogctl(7, NULL, 0);
1004
 
      if(ret == -1){
1005
 
        perror("klogctl");
1006
 
      }
1007
959
    }
1008
960
    
1009
961
    uid = getuid();
1010
962
    gid = getgid();
1011
963
    
 
964
    ret = setuid(uid);
 
965
    if(ret == -1){
 
966
      perror("setuid");
 
967
    }
 
968
    
1012
969
    setgid(gid);
1013
970
    if(ret == -1){
1014
971
      perror("setgid");
1015
972
    }
1016
973
    
1017
 
    ret = setuid(uid);
1018
 
    if(ret == -1){
1019
 
      perror("setuid");
1020
 
    }
1021
 
    
1022
974
    ret = init_gnutls_global(&mc, pubkey, seckey);
1023
975
    if(ret == -1){
1024
976
      fprintf(stderr, "init_gnutls_global failed\n");
1137
1089
    if(debug){
1138
1090
      fprintf(stderr, "Starting Avahi loop search\n");
1139
1091
    }
1140
 
 
 
1092
    
1141
1093
    avahi_simple_poll_loop(mc.simple_poll);
1142
1094
    
1143
1095
 end: