/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

Added lower kernel loglevel to reduce clutter on system console.

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(), sscanf */
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,
61
60
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
62
61
#include <assert.h>             /* assert() */
63
62
#include <errno.h>              /* perror(), errno */
64
 
#include <time.h>               /* nanosleep(), time() */
 
63
#include <time.h>               /* time(), nanosleep() */
65
64
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
66
65
                                   SIOCSIFFLAGS, if_indextoname(),
67
66
                                   if_nametoindex(), IF_NAMESIZE */
155
154
  
156
155
  
157
156
  /*
158
 
   * Helper function to insert pub and seckey to the engine keyring.
 
157
   * Helper function to insert pub and seckey to the enigne keyring.
159
158
   */
160
159
  bool import_key(const char *filename){
161
160
    int fd;
833
832
    gid_t gid;
834
833
    char *connect_to = NULL;
835
834
    char tempdir[] = "/tmp/mandosXXXXXX";
836
 
    bool tempdir_created = false;
837
835
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
838
836
    const char *seckey = PATHDIR "/" SECKEY;
839
837
    const char *pubkey = PATHDIR "/" PUBKEY;
841
839
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
842
840
                          .dh_bits = 1024, .priority = "SECURE256"
843
841
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
844
 
    bool gnutls_initialized = false;
845
 
    bool gpgme_initialized = false;
 
842
    bool gnutls_initalized = false;
 
843
    bool gpgme_initalized = false;
846
844
    double delay = 2.5;
847
845
    
848
846
    {
943
941
    
944
942
    /* If the interface is down, bring it up */
945
943
    {
946
 
      /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
947
 
         messages to mess up the prompt */
 
944
      // Lower kernel loglevel to KERN_NOTICE to avoid
 
945
      // KERN_INFO messages to mess up the prompt
948
946
      ret = klogctl(8, NULL, 5);
949
947
      if(ret == -1){
950
948
        perror("klogctl");
951
949
      }
952
 
      
 
950
 
953
951
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
954
952
      if(sd < 0) {
955
953
        perror("socket");
984
982
          goto end;
985
983
        }
986
984
      }
987
 
      /* sleep checking until interface is running */
 
985
      // sleep checking until interface is running
988
986
      for(int i=0; i < delay * 4; i++){
989
987
        ret = ioctl(sd, SIOCGIFFLAGS, &network);
990
988
        if(ret == -1){
999
997
      if(ret == -1){
1000
998
        perror("close");
1001
999
      }
1002
 
      /* Restores kernel loglevel to default */
 
1000
      // Restores kernel loglevel to default
1003
1001
      ret = klogctl(7, NULL, 0);
1004
1002
      if(ret == -1){
1005
1003
        perror("klogctl");
1009
1007
    uid = getuid();
1010
1008
    gid = getgid();
1011
1009
    
 
1010
    ret = setuid(uid);
 
1011
    if(ret == -1){
 
1012
      perror("setuid");
 
1013
    }
 
1014
    
1012
1015
    setgid(gid);
1013
1016
    if(ret == -1){
1014
1017
      perror("setgid");
1015
1018
    }
1016
1019
    
1017
 
    ret = setuid(uid);
1018
 
    if(ret == -1){
1019
 
      perror("setuid");
1020
 
    }
1021
 
    
1022
1020
    ret = init_gnutls_global(&mc, pubkey, seckey);
1023
1021
    if(ret == -1){
1024
1022
      fprintf(stderr, "init_gnutls_global failed\n");
1025
1023
      exitcode = EXIT_FAILURE;
1026
1024
      goto end;
1027
1025
    } else {
1028
 
      gnutls_initialized = true;
 
1026
      gnutls_initalized = true;
1029
1027
    }
1030
1028
    
1031
1029
    if(mkdtemp(tempdir) == NULL){
1032
1030
      perror("mkdtemp");
 
1031
      tempdir[0] = '\0';
1033
1032
      goto end;
1034
1033
    }
1035
 
    tempdir_created = true;
1036
1034
    
1037
1035
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
1038
 
      fprintf(stderr, "init_gpgme failed\n");
 
1036
      fprintf(stderr, "gpgme_initalized failed\n");
1039
1037
      exitcode = EXIT_FAILURE;
1040
1038
      goto end;
1041
1039
    } else {
1042
 
      gpgme_initialized = true;
 
1040
      gpgme_initalized = true;
1043
1041
    }
1044
1042
    
1045
1043
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1046
1044
    if(if_index == 0){
1047
1045
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1048
 
      exitcode = EXIT_FAILURE;
1049
 
      goto end;
 
1046
      exit(EXIT_FAILURE);
1050
1047
    }
1051
1048
    
1052
1049
    if(connect_to != NULL){
1156
1153
    if(mc.simple_poll != NULL)
1157
1154
        avahi_simple_poll_free(mc.simple_poll);
1158
1155
    
1159
 
    if(gnutls_initialized){
 
1156
    if(gnutls_initalized){
1160
1157
      gnutls_certificate_free_credentials(mc.cred);
1161
1158
      gnutls_global_deinit();
1162
1159
      gnutls_dh_params_deinit(mc.dh_params);
1163
1160
    }
1164
1161
    
1165
 
    if(gpgme_initialized){
 
1162
    if(gpgme_initalized){
1166
1163
      gpgme_release(mc.ctx);
1167
1164
    }
1168
1165
    
1169
1166
    /* Removes the temp directory used by GPGME */
1170
 
    if(tempdir_created){
 
1167
    if(tempdir[0] != '\0'){
1171
1168
      DIR *d;
1172
1169
      struct dirent *direntry;
1173
1170
      d = opendir(tempdir);
1181
1178
          if(direntry == NULL){
1182
1179
            break;
1183
1180
          }
1184
 
          /* Skip "." and ".." */
1185
 
          if(direntry->d_name[0] == '.'
1186
 
             and (direntry->d_name[1] == '\0'
1187
 
                  or (direntry->d_name[1] == '.'
1188
 
                      and direntry->d_name[2] == '\0'))){
1189
 
            continue;
1190
 
          }
1191
 
          char *fullname = NULL;
1192
 
          ret = asprintf(&fullname, "%s/%s", tempdir,
1193
 
                         direntry->d_name);
1194
 
          if(ret < 0){
1195
 
            perror("asprintf");
1196
 
            continue;
1197
 
          }
1198
 
          ret = remove(fullname);
1199
 
          if(ret == -1){
1200
 
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1201
 
                    strerror(errno));
1202
 
          }
1203
 
          free(fullname);
 
1181
          if(direntry->d_type == DT_REG){
 
1182
            char *fullname = NULL;
 
1183
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1184
                           direntry->d_name);
 
1185
            if(ret < 0){
 
1186
              perror("asprintf");
 
1187
              continue;
 
1188
            }
 
1189
            ret = unlink(fullname);
 
1190
            if(ret == -1){
 
1191
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1192
                      fullname, strerror(errno));
 
1193
            }
 
1194
            free(fullname);
 
1195
          }
1204
1196
        }
1205
1197
        closedir(d);
1206
1198
      }
1209
1201
        perror("rmdir");
1210
1202
      }
1211
1203
    }
1212
 
    
 
1204
          
1213
1205
    return exitcode;
1214
1206
}