/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2009-02-09 05:09:39 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: belorn@braxen-20090209050939-ml95liuwtbn9kjtb
plugin-runner: Added support for empty string arguments
               Fixed bug where IP:port argument failed
mandos-client: Fixed bug where files wasnt removed after sigterm signal

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(),
79
 
                                   sigaction(), SIGTERM, sigaction,
80
 
                                   sig_atomic_t */
81
 
 
 
78
#include <signal.h>             /* sigemptyset(), sigaddset(), sigaction(), SIGTERM, sigaction */
82
79
#ifdef __linux__
83
80
#include <sys/klog.h>           /* klogctl() */
84
 
#endif  /* __linux__ */
 
81
#endif
85
82
 
86
83
/* Avahi */
87
84
/* All Avahi types, constants and functions
134
131
} mandos_context;
135
132
 
136
133
/* global context so signal handler can reach it*/
137
 
mandos_context mc = { .simple_poll = NULL, .server = NULL,
138
 
                      .dh_bits = 1024, .priority = "SECURE256"
139
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
134
mandos_context mc;
140
135
 
141
136
/*
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.
 
137
 * Make additional room in "buffer" for at least BUFFER_SIZE
 
138
 * additional bytes. "buffer_capacity" is how much is currently
 
139
 * allocated, "buffer_length" is how much is already used.
145
140
 */
146
141
size_t incbuffer(char **buffer, size_t buffer_length,
147
142
                  size_t buffer_capacity){
201
196
  }
202
197
  
203
198
  if(debug){
204
 
    fprintf(stderr, "Initializing GPGME\n");
 
199
    fprintf(stderr, "Initialize gpgme\n");
205
200
  }
206
201
  
207
202
  /* Init GPGME */
791
786
                             AVAHI_GCC_UNUSED AvahiStringList *txt,
792
787
                             AVAHI_GCC_UNUSED AvahiLookupResultFlags
793
788
                             flags,
794
 
                             AVAHI_GCC_UNUSED void* userdata){
 
789
                             __attribute__((unused)) void* userdata){
795
790
  assert(r);
796
791
  
797
792
  /* Called whenever a service has been resolved successfully or
833
828
                            const char *domain,
834
829
                            AVAHI_GCC_UNUSED AvahiLookupResultFlags
835
830
                            flags,
836
 
                            AVAHI_GCC_UNUSED void* userdata){
 
831
                            __attribute__((unused)) void* userdata){
837
832
  assert(b);
838
833
  
839
834
  /* Called whenever a new services becomes available on the LAN or
874
869
  }
875
870
}
876
871
 
877
 
sig_atomic_t quit_now = 0;
878
 
 
879
 
/* stop main loop after sigterm has been called */
880
872
static void handle_sigterm(__attribute__((unused)) int sig){
881
 
  if(quit_now){
882
 
    return;
883
 
  }
884
 
  quit_now = 1;
885
873
  int old_errno = errno;
886
 
  if(mc.simple_poll != NULL){
887
 
    avahi_simple_poll_quit(mc.simple_poll);
888
 
  }
 
874
  avahi_simple_poll_quit(mc.simple_poll);
889
875
  errno = old_errno;
890
876
}
891
877
 
908
894
  const char *seckey = PATHDIR "/" SECKEY;
909
895
  const char *pubkey = PATHDIR "/" PUBKEY;
910
896
  
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" };
915
897
  bool gnutls_initialized = false;
916
898
  bool gpgme_initialized = false;
917
899
  double delay = 2.5;
918
900
 
919
901
  struct sigaction old_sigterm_action;
920
902
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
 
903
 
 
904
  /* Initialize mandos context */
 
905
  mc = (mandos_context){ .simple_poll = NULL, .server = NULL,
 
906
                         .dh_bits = 1024, .priority = "SECURE256"
 
907
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
921
908
  
922
909
  {
923
910
    struct argp_option options[] = {
1015
1002
    }
1016
1003
  }
1017
1004
  
1018
 
  if(not debug){
1019
 
    avahi_set_log_function(empty_log);
1020
 
  }
1021
 
  
1022
 
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1023
 
     from the signal handler */
1024
 
  /* Initialize the pseudo-RNG for Avahi */
1025
 
  srand((unsigned int) time(NULL));
1026
 
  mc.simple_poll = avahi_simple_poll_new();
1027
 
  if(mc.simple_poll == NULL){
1028
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1029
 
    exitcode = EXIT_FAILURE;
1030
 
    goto end;
1031
 
  }
1032
 
  
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
 
  }
1046
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1047
 
  if(ret == -1){
1048
 
    perror("sigaddset");
1049
 
    exitcode = EXIT_FAILURE;
1050
 
    goto end;
1051
 
  }
1052
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1053
 
  if(ret == -1){
1054
 
    perror("sigaction");
1055
 
    exitcode = EXIT_FAILURE;
1056
 
    goto end;
1057
 
  }  
1058
 
  
1059
1005
  /* If the interface is down, bring it up */
1060
1006
  if(interface[0] != '\0'){
1061
1007
#ifdef __linux__
1067
1013
      restore_loglevel = false;
1068
1014
      perror("klogctl");
1069
1015
    }
1070
 
#endif  /* __linux__ */
 
1016
#endif
1071
1017
    
1072
1018
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1073
1019
    if(sd < 0){
1080
1026
          perror("klogctl");
1081
1027
        }
1082
1028
      }
1083
 
#endif  /* __linux__ */
 
1029
#endif
1084
1030
      goto end;
1085
1031
    }
1086
1032
    strcpy(network.ifr_name, interface);
1094
1040
          perror("klogctl");
1095
1041
        }
1096
1042
      }
1097
 
#endif  /* __linux__ */
 
1043
#endif
1098
1044
      exitcode = EXIT_FAILURE;
1099
1045
      goto end;
1100
1046
    }
1111
1057
            perror("klogctl");
1112
1058
          }
1113
1059
        }
1114
 
#endif  /* __linux__ */
 
1060
#endif
1115
1061
        goto end;
1116
1062
      }
1117
1063
    }
1141
1087
        perror("klogctl");
1142
1088
      }
1143
1089
    }
1144
 
#endif  /* __linux__ */
 
1090
#endif
1145
1091
  }
1146
1092
  
1147
1093
  uid = getuid();
1217
1163
    } else {
1218
1164
      af = AF_INET;
1219
1165
    }
1220
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1166
    ret = start_mandos_communication(address, port, if_index,
 
1167
                                     af);
1221
1168
    if(ret < 0){
1222
1169
      exitcode = EXIT_FAILURE;
1223
1170
    } else {
1225
1172
    }
1226
1173
    goto end;
1227
1174
  }
1228
 
    
 
1175
  
 
1176
  if(not debug){
 
1177
    avahi_set_log_function(empty_log);
 
1178
  }
 
1179
  
 
1180
  /* Initialize the pseudo-RNG for Avahi */
 
1181
  srand((unsigned int) time(NULL));
 
1182
  
 
1183
  /* Allocate main Avahi loop object */
 
1184
  mc.simple_poll = avahi_simple_poll_new();
 
1185
  if(mc.simple_poll == NULL){
 
1186
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
 
1187
    exitcode = EXIT_FAILURE;
 
1188
    goto end;
 
1189
  }
 
1190
  
1229
1191
  {
1230
1192
    AvahiServerConfig config;
1231
1193
    /* Do not publish any local Zeroconf records */
1262
1224
    exitcode = EXIT_FAILURE;
1263
1225
    goto end;
1264
1226
  }
 
1227
 
 
1228
  sigemptyset(&sigterm_action.sa_mask);
 
1229
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
 
1230
  if(ret == -1){
 
1231
    perror("sigaddset");
 
1232
    exitcode = EXIT_FAILURE;
 
1233
    goto end;
 
1234
  }
 
1235
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
 
1236
  if(ret == -1){
 
1237
    perror("sigaction");
 
1238
    exitcode = EXIT_FAILURE;
 
1239
    goto end;
 
1240
  }  
1265
1241
  
1266
1242
  /* Run the main loop */
1267
1243