/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-02-13 05:38:21 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090213053821-03e696gckk4nbjps
Support not using IPv6 in server:

* mandos (AvahiService.__init__): Take new "protocol" parameter.  All
                                  callers changed.
  (IPv6_TCPServer.__init__): Take new "use_ipv6" parameter.  All
                             callers changed.
  (IPv6_TCPServer.server_bind): Create IPv4 socket if not using IPv6.
  (main): New "--no-ipv6" command line option.  New "use_ipv6" config
          option.
* mandos-options.xml ([@id="address"]): Document conditional IPv4
                                        address support.
  ([@id="ipv6"]): New paragraph.
* mandos.conf (use_ipv6): New config option.
* mandos.conf.xml (OPTIONS): Document new "use_dbus" option.
  (EXAMPLE): Changed to use IPv6 link-local address.  Added "use_ipv6"
             option.
* mandos.xml (SYNOPSIS): New "--no-ipv6" option.
  (OPTIONS): Document new "--no-ipv6" option.

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(), remove() */
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,
44
 
                                   srand() */
 
43
                                   srand(), strtof() */
45
44
#include <stdbool.h>            /* bool, false, true */
46
45
#include <string.h>             /* memset(), strcmp(), strlen(),
47
46
                                   strerror(), asprintf(), strcpy() */
56
55
#include <fcntl.h>              /* open() */
57
56
#include <dirent.h>             /* opendir(), struct dirent, readdir()
58
57
                                 */
59
 
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
 
58
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
 
59
                                   strtoimax() */
60
60
#include <assert.h>             /* assert() */
61
61
#include <errno.h>              /* perror(), errno */
62
62
#include <time.h>               /* nanosleep(), time() */
75
75
                                   argp_state, struct argp,
76
76
                                   argp_parse(), ARGP_KEY_ARG,
77
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
 
#include <signal.h>             /* sigemptyset(), sigaddset(), sigaction(), SIGTERM, sigaction */
 
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
 
79
                                   sigaction(), SIGTERM, sigaction,
 
80
                                   sig_atomic_t */
 
81
 
79
82
#ifdef __linux__
80
83
#include <sys/klog.h>           /* klogctl() */
81
 
#endif
 
84
#endif  /* __linux__ */
82
85
 
83
86
/* Avahi */
84
87
/* All Avahi types, constants and functions
136
139
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
137
140
 
138
141
/*
139
 
 * Make additional room in "buffer" for at least BUFFER_SIZE
140
 
 * additional bytes. "buffer_capacity" is how much is currently
141
 
 * allocated, "buffer_length" is how much is already used.
 
142
 * Make additional room in "buffer" for at least BUFFER_SIZE more
 
143
 * bytes. "buffer_capacity" is how much is currently allocated,
 
144
 * "buffer_length" is how much is already used.
142
145
 */
143
146
size_t incbuffer(char **buffer, size_t buffer_length,
144
147
                  size_t buffer_capacity){
198
201
  }
199
202
  
200
203
  if(debug){
201
 
    fprintf(stderr, "Initialize gpgme\n");
 
204
    fprintf(stderr, "Initializing GPGME\n");
202
205
  }
203
206
  
204
207
  /* Init GPGME */
788
791
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
789
792
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
790
793
                             flags,
791
 
                             __attribute__((unused)) void* userdata){
 
794
                             AVAHI_GCC_UNUSED void* userdata){
792
795
  assert(r);
793
796
  
794
797
  /* Called whenever a service has been resolved successfully or
830
833
                            const char *domain,
831
834
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
832
835
                            flags,
833
 
                            __attribute__((unused)) void* userdata){
 
836
                            AVAHI_GCC_UNUSED void* userdata){
834
837
  assert(b);
835
838
  
836
839
  /* Called whenever a new services becomes available on the LAN or
851
854
       the callback function is called the Avahi server will free the
852
855
       resolver for us. */
853
856
    
854
 
    if(!(avahi_s_service_resolver_new(mc.server, interface,
855
 
                                       protocol, name, type, domain,
856
 
                                       AVAHI_PROTO_INET6, 0,
857
 
                                       resolve_callback, NULL)))
 
857
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
 
858
                                    name, type, domain, protocol, 0,
 
859
                                    resolve_callback, NULL) == NULL)
858
860
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
859
861
              name, avahi_strerror(avahi_server_errno(mc.server)));
860
862
    break;
871
873
  }
872
874
}
873
875
 
 
876
sig_atomic_t quit_now = 0;
 
877
 
874
878
/* stop main loop after sigterm has been called */
875
879
static void handle_sigterm(__attribute__((unused)) int sig){
 
880
  if(quit_now){
 
881
    return;
 
882
  }
 
883
  quit_now = 1;
876
884
  int old_errno = errno;
877
 
  avahi_simple_poll_quit(mc.simple_poll);
 
885
  if(mc.simple_poll != NULL){
 
886
    avahi_simple_poll_quit(mc.simple_poll);
 
887
  }
878
888
  errno = old_errno;
879
889
}
880
890
 
883
893
  int error;
884
894
  int ret;
885
895
  intmax_t tmpmax;
886
 
  int numchars;
 
896
  char *tmp;
887
897
  int exitcode = EXIT_SUCCESS;
888
898
  const char *interface = "eth0";
889
899
  struct ifreq network;
899
909
  
900
910
  bool gnutls_initialized = false;
901
911
  bool gpgme_initialized = false;
902
 
  double delay = 2.5;
903
 
 
 
912
  float delay = 2.5f;
 
913
  
904
914
  struct sigaction old_sigterm_action;
905
915
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
906
 
 
 
916
  
907
917
  {
908
918
    struct argp_option options[] = {
909
919
      { .name = "debug", .key = 128,
960
970
        pubkey = arg;
961
971
        break;
962
972
      case 129:                 /* --dh-bits */
963
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
964
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
965
 
           or arg[numchars] != '\0'){
 
973
        errno = 0;
 
974
        tmpmax = strtoimax(arg, &tmp, 10);
 
975
        if(errno != 0 or tmp == arg or *tmp != '\0'
 
976
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
966
977
          fprintf(stderr, "Bad number of DH bits\n");
967
978
          exit(EXIT_FAILURE);
968
979
        }
972
983
        mc.priority = arg;
973
984
        break;
974
985
      case 131:                 /* --delay */
975
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
976
 
        if(ret < 1 or arg[numchars] != '\0'){
 
986
        errno = 0;
 
987
        delay = strtof(arg, &tmp);
 
988
        if(errno != 0 or tmp == arg or *tmp != '\0'){
977
989
          fprintf(stderr, "Bad delay\n");
978
990
          exit(EXIT_FAILURE);
979
991
        }
999
1011
      goto end;
1000
1012
    }
1001
1013
  }
1002
 
 
 
1014
  
1003
1015
  if(not debug){
1004
1016
    avahi_set_log_function(empty_log);
1005
1017
  }
1006
 
 
 
1018
  
1007
1019
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1008
1020
     from the signal handler */
1009
1021
  /* Initialize the pseudo-RNG for Avahi */
1014
1026
    exitcode = EXIT_FAILURE;
1015
1027
    goto end;
1016
1028
  }
1017
 
 
 
1029
  
1018
1030
  sigemptyset(&sigterm_action.sa_mask);
 
1031
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1032
  if(ret == -1){
 
1033
    perror("sigaddset");
 
1034
    exitcode = EXIT_FAILURE;
 
1035
    goto end;
 
1036
  }
 
1037
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1038
  if(ret == -1){
 
1039
    perror("sigaddset");
 
1040
    exitcode = EXIT_FAILURE;
 
1041
    goto end;
 
1042
  }
1019
1043
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1020
1044
  if(ret == -1){
1021
1045
    perror("sigaddset");
1028
1052
    exitcode = EXIT_FAILURE;
1029
1053
    goto end;
1030
1054
  }  
1031
 
 
1032
1055
  
1033
1056
  /* If the interface is down, bring it up */
1034
1057
  if(interface[0] != '\0'){
1041
1064
      restore_loglevel = false;
1042
1065
      perror("klogctl");
1043
1066
    }
1044
 
#endif
 
1067
#endif  /* __linux__ */
1045
1068
    
1046
1069
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1047
1070
    if(sd < 0){
1054
1077
          perror("klogctl");
1055
1078
        }
1056
1079
      }
1057
 
#endif
 
1080
#endif  /* __linux__ */
1058
1081
      goto end;
1059
1082
    }
1060
1083
    strcpy(network.ifr_name, interface);
1068
1091
          perror("klogctl");
1069
1092
        }
1070
1093
      }
1071
 
#endif
 
1094
#endif  /* __linux__ */
1072
1095
      exitcode = EXIT_FAILURE;
1073
1096
      goto end;
1074
1097
    }
1085
1108
            perror("klogctl");
1086
1109
          }
1087
1110
        }
1088
 
#endif
 
1111
#endif  /* __linux__ */
1089
1112
        goto end;
1090
1113
      }
1091
1114
    }
1115
1138
        perror("klogctl");
1116
1139
      }
1117
1140
    }
1118
 
#endif
 
1141
#endif  /* __linux__ */
1119
1142
  }
1120
1143
  
1121
1144
  uid = getuid();
1174
1197
      goto end;
1175
1198
    }
1176
1199
    uint16_t port;
1177
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1178
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1179
 
       or address[numchars+1] != '\0'){
 
1200
    errno = 0;
 
1201
    tmpmax = strtoimax(address+1, &tmp, 10);
 
1202
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
 
1203
       or tmpmax != (uint16_t)tmpmax){
1180
1204
      fprintf(stderr, "Bad port number\n");
1181
1205
      exitcode = EXIT_FAILURE;
1182
1206
      goto end;
1191
1215
    } else {
1192
1216
      af = AF_INET;
1193
1217
    }
1194
 
    ret = start_mandos_communication(address, port, if_index,
1195
 
                                     af);
 
1218
    ret = start_mandos_communication(address, port, if_index, af);
1196
1219
    if(ret < 0){
1197
1220
      exitcode = EXIT_FAILURE;
1198
1221
    } else {
1229
1252
  
1230
1253
  /* Create the Avahi service browser */
1231
1254
  sb = avahi_s_service_browser_new(mc.server, if_index,
1232
 
                                   AVAHI_PROTO_INET6, "_mandos._tcp",
 
1255
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1233
1256
                                   NULL, 0, browse_callback, NULL);
1234
1257
  if(sb == NULL){
1235
1258
    fprintf(stderr, "Failed to create service browser: %s\n",