/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

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
871
874
  }
872
875
}
873
876
 
 
877
sig_atomic_t quit_now = 0;
 
878
 
874
879
/* stop main loop after sigterm has been called */
875
880
static void handle_sigterm(__attribute__((unused)) int sig){
 
881
  if(quit_now){
 
882
    return;
 
883
  }
 
884
  quit_now = 1;
876
885
  int old_errno = errno;
877
 
  avahi_simple_poll_quit(mc.simple_poll);
 
886
  if(mc.simple_poll != NULL){
 
887
    avahi_simple_poll_quit(mc.simple_poll);
 
888
  }
878
889
  errno = old_errno;
879
890
}
880
891
 
897
908
  const char *seckey = PATHDIR "/" SECKEY;
898
909
  const char *pubkey = PATHDIR "/" PUBKEY;
899
910
  
 
911
  /* Initialize Mandos context */
 
912
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
913
                         .dh_bits = 1024, .priority = "SECURE256"
 
914
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
900
915
  bool gnutls_initialized = false;
901
916
  bool gpgme_initialized = false;
902
917
  double delay = 2.5;
903
918
 
904
919
  struct sigaction old_sigterm_action;
905
920
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
906
 
 
 
921
  
907
922
  {
908
923
    struct argp_option options[] = {
909
924
      { .name = "debug", .key = 128,
999
1014
      goto end;
1000
1015
    }
1001
1016
  }
1002
 
 
 
1017
  
1003
1018
  if(not debug){
1004
1019
    avahi_set_log_function(empty_log);
1005
1020
  }
1006
 
 
 
1021
  
1007
1022
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1008
1023
     from the signal handler */
1009
1024
  /* Initialize the pseudo-RNG for Avahi */
1014
1029
    exitcode = EXIT_FAILURE;
1015
1030
    goto end;
1016
1031
  }
1017
 
 
 
1032
  
1018
1033
  sigemptyset(&sigterm_action.sa_mask);
 
1034
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
 
1035
  if(ret == -1){
 
1036
    perror("sigaddset");
 
1037
    exitcode = EXIT_FAILURE;
 
1038
    goto end;
 
1039
  }
 
1040
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
 
1041
  if(ret == -1){
 
1042
    perror("sigaddset");
 
1043
    exitcode = EXIT_FAILURE;
 
1044
    goto end;
 
1045
  }
1019
1046
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1020
1047
  if(ret == -1){
1021
1048
    perror("sigaddset");
1028
1055
    exitcode = EXIT_FAILURE;
1029
1056
    goto end;
1030
1057
  }  
1031
 
 
1032
1058
  
1033
1059
  /* If the interface is down, bring it up */
1034
1060
  if(interface[0] != '\0'){
1041
1067
      restore_loglevel = false;
1042
1068
      perror("klogctl");
1043
1069
    }
1044
 
#endif
 
1070
#endif  /* __linux__ */
1045
1071
    
1046
1072
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1047
1073
    if(sd < 0){
1054
1080
          perror("klogctl");
1055
1081
        }
1056
1082
      }
1057
 
#endif
 
1083
#endif  /* __linux__ */
1058
1084
      goto end;
1059
1085
    }
1060
1086
    strcpy(network.ifr_name, interface);
1068
1094
          perror("klogctl");
1069
1095
        }
1070
1096
      }
1071
 
#endif
 
1097
#endif  /* __linux__ */
1072
1098
      exitcode = EXIT_FAILURE;
1073
1099
      goto end;
1074
1100
    }
1085
1111
            perror("klogctl");
1086
1112
          }
1087
1113
        }
1088
 
#endif
 
1114
#endif  /* __linux__ */
1089
1115
        goto end;
1090
1116
      }
1091
1117
    }
1115
1141
        perror("klogctl");
1116
1142
      }
1117
1143
    }
1118
 
#endif
 
1144
#endif  /* __linux__ */
1119
1145
  }
1120
1146
  
1121
1147
  uid = getuid();
1191
1217
    } else {
1192
1218
      af = AF_INET;
1193
1219
    }
1194
 
    ret = start_mandos_communication(address, port, if_index,
1195
 
                                     af);
 
1220
    ret = start_mandos_communication(address, port, if_index, af);
1196
1221
    if(ret < 0){
1197
1222
      exitcode = EXIT_FAILURE;
1198
1223
    } else {