/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:
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), remove() */
 
39
                                   stdout, ferror(), sscanf(),
 
40
                                   remove() */
40
41
#include <stdint.h>             /* uint16_t, uint32_t */
41
42
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
43
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
43
 
                                   srand(), strtof() */
 
44
                                   srand() */
44
45
#include <stdbool.h>            /* bool, false, true */
45
46
#include <string.h>             /* memset(), strcmp(), strlen(),
46
47
                                   strerror(), asprintf(), strcpy() */
55
56
#include <fcntl.h>              /* open() */
56
57
#include <dirent.h>             /* opendir(), struct dirent, readdir()
57
58
                                 */
58
 
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
59
 
                                   strtoimax() */
 
59
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
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(),
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
 
894
880
  int error;
895
881
  int ret;
896
882
  intmax_t tmpmax;
897
 
  char *tmp;
 
883
  int numchars;
898
884
  int exitcode = EXIT_SUCCESS;
899
885
  const char *interface = "eth0";
900
886
  struct ifreq network;
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
 
  float delay = 2.5f;
918
 
  
 
899
  double delay = 2.5;
 
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[] = {
975
962
        pubkey = arg;
976
963
        break;
977
964
      case 129:                 /* --dh-bits */
978
 
        errno = 0;
979
 
        tmpmax = strtoimax(arg, &tmp, 10);
980
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
981
 
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
 
965
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
966
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
 
967
           or arg[numchars] != '\0'){
982
968
          fprintf(stderr, "Bad number of DH bits\n");
983
969
          exit(EXIT_FAILURE);
984
970
        }
988
974
        mc.priority = arg;
989
975
        break;
990
976
      case 131:                 /* --delay */
991
 
        errno = 0;
992
 
        delay = strtof(arg, &tmp);
993
 
        if(errno != 0 or tmp == arg or *tmp != '\0'){
 
977
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
 
978
        if(ret < 1 or arg[numchars] != '\0'){
994
979
          fprintf(stderr, "Bad delay\n");
995
980
          exit(EXIT_FAILURE);
996
981
        }
1017
1002
    }
1018
1003
  }
1019
1004
  
1020
 
  if(not debug){
1021
 
    avahi_set_log_function(empty_log);
1022
 
  }
1023
 
  
1024
 
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1025
 
     from the signal handler */
1026
 
  /* Initialize the pseudo-RNG for Avahi */
1027
 
  srand((unsigned int) time(NULL));
1028
 
  mc.simple_poll = avahi_simple_poll_new();
1029
 
  if(mc.simple_poll == NULL){
1030
 
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1031
 
    exitcode = EXIT_FAILURE;
1032
 
    goto end;
1033
 
  }
1034
 
  
1035
 
  sigemptyset(&sigterm_action.sa_mask);
1036
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1037
 
  if(ret == -1){
1038
 
    perror("sigaddset");
1039
 
    exitcode = EXIT_FAILURE;
1040
 
    goto end;
1041
 
  }
1042
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1043
 
  if(ret == -1){
1044
 
    perror("sigaddset");
1045
 
    exitcode = EXIT_FAILURE;
1046
 
    goto end;
1047
 
  }
1048
 
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1049
 
  if(ret == -1){
1050
 
    perror("sigaddset");
1051
 
    exitcode = EXIT_FAILURE;
1052
 
    goto end;
1053
 
  }
1054
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1055
 
  if(ret == -1){
1056
 
    perror("sigaction");
1057
 
    exitcode = EXIT_FAILURE;
1058
 
    goto end;
1059
 
  }  
1060
 
  
1061
1005
  /* If the interface is down, bring it up */
1062
1006
  if(interface[0] != '\0'){
1063
1007
#ifdef __linux__
1069
1013
      restore_loglevel = false;
1070
1014
      perror("klogctl");
1071
1015
    }
1072
 
#endif  /* __linux__ */
 
1016
#endif
1073
1017
    
1074
1018
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1075
1019
    if(sd < 0){
1082
1026
          perror("klogctl");
1083
1027
        }
1084
1028
      }
1085
 
#endif  /* __linux__ */
 
1029
#endif
1086
1030
      goto end;
1087
1031
    }
1088
1032
    strcpy(network.ifr_name, interface);
1096
1040
          perror("klogctl");
1097
1041
        }
1098
1042
      }
1099
 
#endif  /* __linux__ */
 
1043
#endif
1100
1044
      exitcode = EXIT_FAILURE;
1101
1045
      goto end;
1102
1046
    }
1113
1057
            perror("klogctl");
1114
1058
          }
1115
1059
        }
1116
 
#endif  /* __linux__ */
 
1060
#endif
1117
1061
        goto end;
1118
1062
      }
1119
1063
    }
1143
1087
        perror("klogctl");
1144
1088
      }
1145
1089
    }
1146
 
#endif  /* __linux__ */
 
1090
#endif
1147
1091
  }
1148
1092
  
1149
1093
  uid = getuid();
1202
1146
      goto end;
1203
1147
    }
1204
1148
    uint16_t port;
1205
 
    errno = 0;
1206
 
    tmpmax = strtoimax(address+1, &tmp, 10);
1207
 
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1208
 
       or tmpmax != (uint16_t)tmpmax){
 
1149
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
 
1150
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
 
1151
       or address[numchars+1] != '\0'){
1209
1152
      fprintf(stderr, "Bad port number\n");
1210
1153
      exitcode = EXIT_FAILURE;
1211
1154
      goto end;
1220
1163
    } else {
1221
1164
      af = AF_INET;
1222
1165
    }
1223
 
    ret = start_mandos_communication(address, port, if_index, af);
 
1166
    ret = start_mandos_communication(address, port, if_index,
 
1167
                                     af);
1224
1168
    if(ret < 0){
1225
1169
      exitcode = EXIT_FAILURE;
1226
1170
    } else {
1228
1172
    }
1229
1173
    goto end;
1230
1174
  }
1231
 
    
 
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
  
1232
1191
  {
1233
1192
    AvahiServerConfig config;
1234
1193
    /* Do not publish any local Zeroconf records */
1265
1224
    exitcode = EXIT_FAILURE;
1266
1225
    goto end;
1267
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
  }  
1268
1241
  
1269
1242
  /* Run the main loop */
1270
1243