/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

Overflows are not detected by sscanf(), so stop using it:

* plugin-runner.c (main/parse_opt): Change from using "sscanf()" to
                                    "strtoimax()".
* plugins.d/mandos-client.c (main/parse_opt, main): Change from using
                                                    "sscanf()" to
                                                    "strtoimax()" and
                                                    "strtof()".
* splashy.c (main): Change from using "sscanf()" to "strtoimax()".
* usplash.c (main): - '' -

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() */
76
76
                                   argp_parse(), ARGP_KEY_ARG,
77
77
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
78
78
#include <signal.h>             /* sigemptyset(), sigaddset(),
79
 
                                   sigaction(), SIGTERM, sigaction */
 
79
                                   sigaction(), SIGTERM, sigaction,
 
80
                                   sig_atomic_t */
80
81
 
81
82
#ifdef __linux__
82
83
#include <sys/klog.h>           /* klogctl() */
83
 
#endif
 
84
#endif  /* __linux__ */
84
85
 
85
86
/* Avahi */
86
87
/* All Avahi types, constants and functions
133
134
} mandos_context;
134
135
 
135
136
/* global context so signal handler can reach it*/
136
 
mandos_context mc;
 
137
mandos_context mc = { .simple_poll = NULL, .server = NULL,
 
138
                      .dh_bits = 1024, .priority = "SECURE256"
 
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){
871
874
  }
872
875
}
873
876
 
 
877
sig_atomic_t quit_now = 0;
 
878
 
 
879
/* stop main loop after sigterm has been called */
874
880
static void handle_sigterm(__attribute__((unused)) int sig){
 
881
  if(quit_now){
 
882
    return;
 
883
  }
 
884
  quit_now = 1;
875
885
  int old_errno = errno;
876
 
  avahi_simple_poll_quit(mc.simple_poll);
 
886
  if(mc.simple_poll != NULL){
 
887
    avahi_simple_poll_quit(mc.simple_poll);
 
888
  }
877
889
  errno = old_errno;
878
890
}
879
891
 
882
894
  int error;
883
895
  int ret;
884
896
  intmax_t tmpmax;
885
 
  int numchars;
 
897
  char *tmp;
886
898
  int exitcode = EXIT_SUCCESS;
887
899
  const char *interface = "eth0";
888
900
  struct ifreq network;
902
914
                         ":!CTYPE-X.509:+CTYPE-OPENPGP" };
903
915
  bool gnutls_initialized = false;
904
916
  bool gpgme_initialized = false;
905
 
  double delay = 2.5;
906
 
 
 
917
  float delay = 2.5f;
 
918
  
907
919
  struct sigaction old_sigterm_action;
908
920
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
909
921
  
963
975
        pubkey = arg;
964
976
        break;
965
977
      case 129:                 /* --dh-bits */
966
 
        ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
967
 
        if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
968
 
           or arg[numchars] != '\0'){
 
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){
969
982
          fprintf(stderr, "Bad number of DH bits\n");
970
983
          exit(EXIT_FAILURE);
971
984
        }
975
988
        mc.priority = arg;
976
989
        break;
977
990
      case 131:                 /* --delay */
978
 
        ret = sscanf(arg, "%lf%n", &delay, &numchars);
979
 
        if(ret < 1 or arg[numchars] != '\0'){
 
991
        errno = 0;
 
992
        delay = strtof(arg, &tmp);
 
993
        if(errno != 0 or tmp == arg or *tmp != '\0'){
980
994
          fprintf(stderr, "Bad delay\n");
981
995
          exit(EXIT_FAILURE);
982
996
        }
1003
1017
    }
1004
1018
  }
1005
1019
  
 
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
  
1006
1061
  /* If the interface is down, bring it up */
1007
1062
  if(interface[0] != '\0'){
1008
1063
#ifdef __linux__
1014
1069
      restore_loglevel = false;
1015
1070
      perror("klogctl");
1016
1071
    }
1017
 
#endif
 
1072
#endif  /* __linux__ */
1018
1073
    
1019
1074
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1020
1075
    if(sd < 0){
1027
1082
          perror("klogctl");
1028
1083
        }
1029
1084
      }
1030
 
#endif
 
1085
#endif  /* __linux__ */
1031
1086
      goto end;
1032
1087
    }
1033
1088
    strcpy(network.ifr_name, interface);
1041
1096
          perror("klogctl");
1042
1097
        }
1043
1098
      }
1044
 
#endif
 
1099
#endif  /* __linux__ */
1045
1100
      exitcode = EXIT_FAILURE;
1046
1101
      goto end;
1047
1102
    }
1058
1113
            perror("klogctl");
1059
1114
          }
1060
1115
        }
1061
 
#endif
 
1116
#endif  /* __linux__ */
1062
1117
        goto end;
1063
1118
      }
1064
1119
    }
1088
1143
        perror("klogctl");
1089
1144
      }
1090
1145
    }
1091
 
#endif
 
1146
#endif  /* __linux__ */
1092
1147
  }
1093
1148
  
1094
1149
  uid = getuid();
1147
1202
      goto end;
1148
1203
    }
1149
1204
    uint16_t port;
1150
 
    ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1151
 
    if(ret < 1 or tmpmax != (uint16_t)tmpmax
1152
 
       or address[numchars+1] != '\0'){
 
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){
1153
1209
      fprintf(stderr, "Bad port number\n");
1154
1210
      exitcode = EXIT_FAILURE;
1155
1211
      goto end;
1172
1228
    }
1173
1229
    goto end;
1174
1230
  }
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
 
  
 
1231
    
1191
1232
  {
1192
1233
    AvahiServerConfig config;
1193
1234
    /* Do not publish any local Zeroconf records */
1225
1266
    goto end;
1226
1267
  }
1227
1268
  
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
 
  }  
1241
 
  
1242
1269
  /* Run the main loop */
1243
1270
  
1244
1271
  if(debug){