/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:
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>               /* time() */
 
64
#include <time.h>               /* nanosleep(), 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() */
78
79
 
79
80
/* Avahi */
80
81
/* All Avahi types, constants and functions
842
843
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
843
844
    bool gnutls_initialized = false;
844
845
    bool gpgme_initialized = false;
 
846
    double delay = 2.5;
845
847
    
846
848
    {
847
849
      struct argp_option options[] = {
873
875
          .arg = "STRING",
874
876
          .doc = "GnuTLS priority string for the TLS handshake",
875
877
          .group = 1 },
 
878
        { .name = "delay", .key = 131,
 
879
          .arg = "SECONDS",
 
880
          .doc = "Maximum delay to wait for interface startup",
 
881
          .group = 2 },
876
882
        { .name = NULL }
877
883
      };
878
884
      
906
912
        case 130:               /* --priority */
907
913
          mc.priority = arg;
908
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;
909
922
        case ARGP_KEY_ARG:
910
923
          argp_usage(state);
911
924
        case ARGP_KEY_END:
930
943
    
931
944
    /* If the interface is down, bring it up */
932
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
      
933
953
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
934
954
      if(sd < 0) {
935
955
        perror("socket");
936
956
        exitcode = EXIT_FAILURE;
 
957
        ret = klogctl(7, NULL, 0);
 
958
        if(ret == -1){
 
959
          perror("klogctl");
 
960
        }
937
961
        goto end;
938
962
      }
939
963
      strcpy(network.ifr_name, interface);
940
964
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
941
965
      if(ret == -1){
942
966
        perror("ioctl SIOCGIFFLAGS");
 
967
        ret = klogctl(7, NULL, 0);
 
968
        if(ret == -1){
 
969
          perror("klogctl");
 
970
        }
943
971
        exitcode = EXIT_FAILURE;
944
972
        goto end;
945
973
      }
949
977
        if(ret == -1){
950
978
          perror("ioctl SIOCSIFFLAGS");
951
979
          exitcode = EXIT_FAILURE;
 
980
          ret = klogctl(7, NULL, 0);
 
981
          if(ret == -1){
 
982
            perror("klogctl");
 
983
          }
952
984
          goto end;
953
985
        }
954
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
      }
955
998
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
956
999
      if(ret == -1){
957
1000
        perror("close");
958
1001
      }
 
1002
      /* Restores kernel loglevel to default */
 
1003
      ret = klogctl(7, NULL, 0);
 
1004
      if(ret == -1){
 
1005
        perror("klogctl");
 
1006
      }
959
1007
    }
960
1008
    
961
1009
    uid = getuid();
1089
1137
    if(debug){
1090
1138
      fprintf(stderr, "Starting Avahi loop search\n");
1091
1139
    }
1092
 
    
 
1140
 
1093
1141
    avahi_simple_poll_loop(mc.simple_poll);
1094
1142
    
1095
1143
 end: