/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

  • Committer: Teddy Hogeborn
  • Date: 2009-01-13 04:35:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113043519-0yr39snphoegq8l1
* plugin-runner.c: Only space changes.
* plugins.d/mandos-client.c: - '' -
* plugins.d/password-prompt.c: - '' -

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,
58
57
#include <fcntl.h>              /* open() */
59
58
#include <dirent.h>             /* opendir(), struct dirent, readdir()
60
59
                                 */
61
 
#include <inttypes.h>           /* PRIu16, intmax_t, SCNdMAX */
 
60
#include <inttypes.h>           /* PRIu16, SCNu16 */
62
61
#include <assert.h>             /* assert() */
63
62
#include <errno.h>              /* perror(), errno */
64
63
#include <time.h>               /* time() */
366
365
}
367
366
 
368
367
static const char * safer_gnutls_strerror(int value) {
369
 
  const char *ret = gnutls_strerror(value); /* Spurious warning from
370
 
                                               -Wunreachable-code */
 
368
  const char *ret = gnutls_strerror(value); /* Spurious warning */
371
369
  if(ret == NULL)
372
370
    ret = "(unknown)";
373
371
  return ret;
406
404
  /* OpenPGP credentials */
407
405
  gnutls_certificate_allocate_credentials(&mc->cred);
408
406
  if(ret != GNUTLS_E_SUCCESS){
409
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
410
 
                                                  * from
411
 
                                                  * -Wunreachable-code
412
 
                                                  */
 
407
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious
 
408
                                                    warning */
413
409
            safer_gnutls_strerror(ret));
414
410
    gnutls_global_deinit();
415
411
    return -1;
557
553
    fprintf(stderr, "Bad address: %s\n", ip);
558
554
    return -1;
559
555
  }
560
 
  to.in6.sin6_port = htons(port); /* Spurious warnings from
561
 
                                     -Wconversion and
562
 
                                     -Wunreachable-code */
 
556
  to.in6.sin6_port = htons(port); /* Spurious warning */
563
557
  
564
558
  to.in6.sin6_scope_id = (uint32_t)if_index;
565
559
  
755
749
      avahi_address_snprint(ip, sizeof(ip), address);
756
750
      if(debug){
757
751
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
758
 
                PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
759
 
                ip, (intmax_t)interface, port);
 
752
                PRIu16 ") on port %d\n", name, host_name, ip,
 
753
                interface, port);
760
754
      }
761
755
      int ret = start_mandos_communication(ip, port, interface, mc);
762
756
      if(ret == 0){
822
816
    AvahiSServiceBrowser *sb = NULL;
823
817
    int error;
824
818
    int ret;
825
 
    intmax_t tmpmax;
826
 
    int numchars;
827
819
    int exitcode = EXIT_SUCCESS;
828
820
    const char *interface = "eth0";
829
821
    struct ifreq network;
839
831
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
840
832
                          .dh_bits = 1024, .priority = "SECURE256"
841
833
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
842
 
    bool gnutls_initialized = false;
843
 
    bool gpgme_initialized = false;
 
834
    bool gnutls_initalized = false;
 
835
    bool gpgme_initalized = false;
844
836
    
845
837
    {
846
838
      struct argp_option options[] = {
894
886
          pubkey = arg;
895
887
          break;
896
888
        case 129:               /* --dh-bits */
897
 
          ret = sscanf(arg, "%" SCNdMAX "%n", &tmpmax, &numchars);
898
 
          if(ret < 1 or tmpmax != (typeof(mc.dh_bits))tmpmax
899
 
             or arg[numchars] != '\0'){
 
889
          ret = sscanf(arg, "%u", &mc.dh_bits);
 
890
          if(ret != 1){
900
891
            fprintf(stderr, "Bad number of DH bits\n");
901
892
            exit(EXIT_FAILURE);
902
893
          }
903
 
          mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
904
894
          break;
905
895
        case 130:               /* --priority */
906
896
          mc.priority = arg;
976
966
      exitcode = EXIT_FAILURE;
977
967
      goto end;
978
968
    } else {
979
 
      gnutls_initialized = true;
 
969
      gnutls_initalized = true;
980
970
    }
981
971
    
982
972
    if(mkdtemp(tempdir) == NULL){
986
976
    }
987
977
    
988
978
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
989
 
      fprintf(stderr, "init_gpgme failed\n");
 
979
      fprintf(stderr, "gpgme_initalized failed\n");
990
980
      exitcode = EXIT_FAILURE;
991
981
      goto end;
992
982
    } else {
993
 
      gpgme_initialized = true;
 
983
      gpgme_initalized = true;
994
984
    }
995
985
    
996
986
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1009
999
        goto end;
1010
1000
      }
1011
1001
      uint16_t port;
1012
 
      ret = sscanf(address+1, "%" SCNdMAX "%n", &tmpmax, &numchars);
1013
 
      if(ret < 1 or tmpmax != (uint16_t)tmpmax
1014
 
         or address[numchars+1] != '\0'){
 
1002
      ret = sscanf(address+1, "%" SCNu16, &port);
 
1003
      if(ret != 1){
1015
1004
        fprintf(stderr, "Bad port number\n");
1016
1005
        exitcode = EXIT_FAILURE;
1017
1006
        goto end;
1018
1007
      }
1019
 
      port = (uint16_t)tmpmax;
1020
1008
      *address = '\0';
1021
1009
      address = connect_to;
1022
1010
      ret = start_mandos_communication(address, port, if_index, &mc);
1106
1094
    if(mc.simple_poll != NULL)
1107
1095
        avahi_simple_poll_free(mc.simple_poll);
1108
1096
    
1109
 
    if(gnutls_initialized){
 
1097
    if(gnutls_initalized){
1110
1098
      gnutls_certificate_free_credentials(mc.cred);
1111
1099
      gnutls_global_deinit();
1112
1100
      gnutls_dh_params_deinit(mc.dh_params);
1113
1101
    }
1114
1102
    
1115
 
    if(gpgme_initialized){
 
1103
    if(gpgme_initalized){
1116
1104
      gpgme_release(mc.ctx);
1117
1105
    }
1118
1106
    
1131
1119
          if(direntry == NULL){
1132
1120
            break;
1133
1121
          }
1134
 
          /* Skip "." and ".." */
1135
 
          if(direntry->d_name[0] == '.'
1136
 
             and (direntry->d_name[1] == '\0'
1137
 
                  or (direntry->d_name[1] == '.'
1138
 
                      and direntry->d_name[2] == '\0'))){
1139
 
            continue;
1140
 
          }
1141
 
          char *fullname = NULL;
1142
 
          ret = asprintf(&fullname, "%s/%s", tempdir,
1143
 
                         direntry->d_name);
1144
 
          if(ret < 0){
1145
 
            perror("asprintf");
1146
 
            continue;
1147
 
          }
1148
 
          ret = remove(fullname);
1149
 
          if(ret == -1){
1150
 
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1151
 
                    strerror(errno));
1152
 
          }
1153
 
          free(fullname);
 
1122
          if(direntry->d_type == DT_REG){
 
1123
            char *fullname = NULL;
 
1124
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1125
                           direntry->d_name);
 
1126
            if(ret < 0){
 
1127
              perror("asprintf");
 
1128
              continue;
 
1129
            }
 
1130
            ret = unlink(fullname);
 
1131
            if(ret == -1){
 
1132
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1133
                      fullname, strerror(errno));
 
1134
            }
 
1135
            free(fullname);
 
1136
          }
1154
1137
        }
1155
1138
        closedir(d);
1156
1139
      }
1159
1142
        perror("rmdir");
1160
1143
      }
1161
1144
    }
1162
 
    
 
1145
          
1163
1146
    return exitcode;
1164
1147
}