/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: Teddy Hogeborn
  • Date: 2008-09-13 15:36:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080913153618-atp386q2bqj0ku99
* Makefile (install-client-nokey): Do "&&" instead of ";" to catch
                                   errors.

* README: Kill the straight quotes.  Add copyright notice.

* overview.xml: Improved wording.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Mandos-client - get and decrypt data from a Mandos server
 
3
 * Mandos client - get and decrypt data from a Mandos server
4
4
 *
5
5
 * This program is partly derived from an example program for an Avahi
6
6
 * service browser, downloaded from
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
14
13
 * 
15
14
 * This program is free software: you can redistribute it and/or
16
15
 * modify it under the terms of the GNU General Public License as
36
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
36
 
38
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
38
                                   stdout, ferror() */
40
39
#include <stdint.h>             /* uint16_t, uint32_t */
41
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
55
54
                                   connect() */
56
55
#include <fcntl.h>              /* open() */
57
56
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
58
 
#include <inttypes.h>           /* PRIu16, SCNu16 */
 
57
#include <inttypes.h>           /* PRIu16 */
59
58
#include <assert.h>             /* assert() */
60
59
#include <errno.h>              /* perror(), errno */
61
60
#include <time.h>               /* time() */
67
66
                                   getuid(), getgid(), setuid(),
68
67
                                   setgid() */
69
68
#include <arpa/inet.h>          /* inet_pton(), htons */
70
 
#include <iso646.h>             /* not, and, or */
 
69
#include <iso646.h>             /* not, and */
71
70
#include <argp.h>               /* struct argp_option, error_t, struct
72
71
                                   argp_state, struct argp,
73
72
                                   argp_parse(), ARGP_KEY_ARG,
102
101
 
103
102
#define BUFFER_SIZE 256
104
103
 
 
104
/*
 
105
  #define PATHDIR "/conf/conf.d/mandos"
 
106
*/
 
107
 
105
108
#define PATHDIR "/conf/conf.d/mandos"
106
109
#define SECKEY "seckey.txt"
107
110
#define PUBKEY "pubkey.txt"
108
111
 
109
112
bool debug = false;
110
113
static const char mandos_protocol_version[] = "1";
111
 
const char *argp_program_version = "mandos-client " VERSION;
 
114
const char *argp_program_version = "mandos-client 1.0";
112
115
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
113
116
 
114
117
/* Used for passing in values through the Avahi callback functions */
156
159
    int fd;
157
160
    gpgme_data_t pgp_data;
158
161
    
159
 
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
 
162
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
160
163
    if(fd == -1){
161
164
      perror("open");
162
165
      return false;
176
179
      return false;
177
180
    }
178
181
    
179
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
182
    ret = TEMP_FAILURE_RETRY(close(fd));
180
183
    if(ret == -1){
181
184
      perror("close");
182
185
    }
501
504
                                      AvahiIfIndex if_index,
502
505
                                      mandos_context *mc){
503
506
  int ret, tcp_sd;
504
 
  ssize_t sret;
505
507
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
506
508
  char *buffer = NULL;
507
509
  char *decrypted_buffer;
578
580
  written = 0;
579
581
  while (true){
580
582
    size_t out_size = strlen(out);
581
 
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
583
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
582
584
                                   out_size - written));
583
585
    if (ret == -1){
584
586
      perror("write");
633
635
      goto mandos_end;
634
636
    }
635
637
    
636
 
    sret = gnutls_record_recv(session, buffer+buffer_length,
637
 
                              BUFFER_SIZE);
638
 
    if (sret == 0){
 
638
    ret = gnutls_record_recv(session, buffer+buffer_length,
 
639
                             BUFFER_SIZE);
 
640
    if (ret == 0){
639
641
      break;
640
642
    }
641
 
    if (sret < 0){
642
 
      switch(sret){
 
643
    if (ret < 0){
 
644
      switch(ret){
643
645
      case GNUTLS_E_INTERRUPTED:
644
646
      case GNUTLS_E_AGAIN:
645
647
        break;
662
664
        goto mandos_end;
663
665
      }
664
666
    } else {
665
 
      buffer_length += (size_t) sret;
 
667
      buffer_length += (size_t) ret;
666
668
    }
667
669
  }
668
670
  
704
706
  
705
707
 mandos_end:
706
708
  free(buffer);
707
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
709
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
708
710
  if(ret == -1){
709
711
    perror("close");
710
712
  }
866
868
      
867
869
      error_t parse_opt (int key, char *arg,
868
870
                         struct argp_state *state) {
 
871
        /* Get the INPUT argument from `argp_parse', which we know is
 
872
           a pointer to our plugin list pointer. */
869
873
        switch (key) {
870
874
        case 128:               /* --debug */
871
875
          debug = true;
883
887
          pubkey = arg;
884
888
          break;
885
889
        case 129:               /* --dh-bits */
886
 
          ret = sscanf(arg, "%u", &mc.dh_bits);
887
 
          if(ret == 0 or mc.dh_bits == 0){
888
 
            fprintf(stderr, "Bad number of DH bits\n");
 
890
          errno = 0;
 
891
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
892
          if (errno){
 
893
            perror("strtol");
889
894
            exit(EXIT_FAILURE);
890
895
          }
891
896
          break;
938
943
          goto end;
939
944
        }
940
945
      }
941
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
946
      ret = TEMP_FAILURE_RETRY(close(sd));
942
947
      if(ret == -1){
943
948
        perror("close");
944
949
      }
995
1000
        exitcode = EXIT_FAILURE;
996
1001
        goto end;
997
1002
      }
998
 
      uint16_t port;
999
 
      ret = sscanf(address+1, "%" SCNu16, &port);
1000
 
      if(ret == 0 or port == 0){
1001
 
        fprintf(stderr, "Bad port number\n");
 
1003
      errno = 0;
 
1004
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
 
1005
      if(errno){
 
1006
        perror("Bad port number");
1002
1007
        exitcode = EXIT_FAILURE;
1003
1008
        goto end;
1004
1009
      }
1107
1112
      struct dirent *direntry;
1108
1113
      d = opendir(tempdir);
1109
1114
      if(d == NULL){
1110
 
        if(errno != ENOENT){
1111
 
          perror("opendir");
1112
 
        }
 
1115
        perror("opendir");
1113
1116
      } else {
1114
1117
        while(true){
1115
1118
          direntry = readdir(d);
1135
1138
        closedir(d);
1136
1139
      }
1137
1140
      ret = rmdir(tempdir);
1138
 
      if(ret == -1 and errno != ENOENT){
 
1141
      if(ret == -1){
1139
1142
        perror("rmdir");
1140
1143
      }
1141
1144
    }