/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/mandosclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-02 14:33:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080802143347-u1m4zs1q0feu72ei
* TODO: Converted to org-mode style

* plugins.d/mandosclient.c (certdir): Changed to "/conf/conf.d/mandos/".
  (certdir, certfile, certkey, pgp_packet_decrypt, debuggnutls,
  initgnutls, empty_log, combinepath): Made static.
  (combinepath): Rewritten to only get the argument string lengths
                 once.  Do not print error message; leave that to
                 caller.  All callers changed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 * along with this program.  If not, see
26
26
 * <http://www.gnu.org/licenses/>.
27
27
 * 
28
 
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
29
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
28
 * Contact the authors at <mandos@fukt.bsnet.se>.
30
29
 */
31
30
 
32
31
/* Needed by GPGME, specifically gpgme_data_seek() */
67
66
// getopt long
68
67
#include <getopt.h>
69
68
 
70
 
#ifndef CERT_ROOT
71
 
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
72
 
#endif
73
 
#define CERTFILE CERT_ROOT "openpgp-client.txt"
74
 
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
75
69
#define BUFFER_SIZE 256
76
70
#define DH_BITS 1024
77
71
 
 
72
static const char *certdir = "/conf/conf.d/mandos";
 
73
static const char *certfile = "openpgp-client.txt";
 
74
static const char *certkey = "openpgp-client-key.txt";
 
75
 
78
76
bool debug = false;
79
77
 
80
78
typedef struct {
84
82
} encrypted_session;
85
83
 
86
84
 
87
 
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
88
 
                            char **new_packet, const char *homedir){
 
85
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
 
86
                                   char **new_packet,
 
87
                                   const char *homedir){
89
88
  gpgme_data_t dh_crypto, dh_plain;
90
89
  gpgme_ctx_t ctx;
91
90
  gpgme_error_t rc;
100
99
  
101
100
  /* Init GPGME */
102
101
  gpgme_check_version(NULL);
103
 
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
102
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
103
  if (rc != GPG_ERR_NO_ERROR){
 
104
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
105
            gpgme_strsource(rc), gpgme_strerror(rc));
 
106
    return -1;
 
107
  }
104
108
  
105
109
  /* Set GPGME home directory */
106
110
  rc = gpgme_get_engine_info (&engine_info);
192
196
  gpgme_data_release(dh_crypto);
193
197
  
194
198
  /* Seek back to the beginning of the GPGME plaintext data buffer */
195
 
  gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
196
 
 
 
199
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
 
200
    perror("pgpme_data_seek");
 
201
  }
 
202
  
197
203
  *new_packet = 0;
198
204
  while(true){
199
205
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
240
246
  return ret;
241
247
}
242
248
 
243
 
void debuggnutls(__attribute__((unused)) int level,
244
 
                 const char* string){
 
249
static void debuggnutls(__attribute__((unused)) int level,
 
250
                        const char* string){
245
251
  fprintf(stderr, "%s", string);
246
252
}
247
253
 
248
 
int initgnutls(encrypted_session *es){
 
254
static int initgnutls(encrypted_session *es){
249
255
  const char *err;
250
256
  int ret;
251
257
  
252
258
  if(debug){
253
259
    fprintf(stderr, "Initializing GnuTLS\n");
254
260
  }
255
 
  
 
261
 
256
262
  if ((ret = gnutls_global_init ())
257
263
      != GNUTLS_E_SUCCESS) {
258
264
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
274
280
  
275
281
  if(debug){
276
282
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
277
 
            " and keyfile %s as GnuTLS credentials\n", CERTFILE,
278
 
            KEYFILE);
 
283
            " and keyfile %s as GnuTLS credentials\n", certfile,
 
284
            certkey);
279
285
  }
280
286
  
281
287
  ret = gnutls_certificate_set_openpgp_key_file
282
 
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
 
288
    (es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
283
289
  if (ret != GNUTLS_E_SUCCESS) {
284
290
    fprintf
285
291
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
286
292
       " '%s')\n",
287
 
       ret, CERTFILE, KEYFILE);
 
293
       ret, certfile, certkey);
288
294
    fprintf(stdout, "The Error is: %s\n",
289
295
            safer_gnutls_strerror(ret));
290
296
    return -1;
339
345
  return 0;
340
346
}
341
347
 
342
 
void empty_log(__attribute__((unused)) AvahiLogLevel level,
343
 
               __attribute__((unused)) const char *txt){}
 
348
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
349
                      __attribute__((unused)) const char *txt){}
344
350
 
345
 
int start_mandos_communication(const char *ip, uint16_t port,
346
 
                               AvahiIfIndex if_index){
 
351
static int start_mandos_communication(const char *ip, uint16_t port,
 
352
                                      AvahiIfIndex if_index){
347
353
  int ret, tcp_sd;
348
354
  struct sockaddr_in6 to;
349
355
  encrypted_session es;
487
493
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
488
494
                                               buffer_length,
489
495
                                               &decrypted_buffer,
490
 
                                               CERT_ROOT);
 
496
                                               certdir);
491
497
    if (decrypted_buffer_size >= 0){
492
498
      while(written < (size_t) decrypted_buffer_size){
493
499
        ret = (int)fwrite (decrypted_buffer + written, 1,
622
628
    }
623
629
}
624
630
 
 
631
/* Combines file name and path and returns the malloced new
 
632
   string. some sane checks could/should be added */
 
633
static const char *combinepath(const char *first, const char *second){
 
634
  size_t f_len = strlen(first);
 
635
  size_t s_len = strlen(second);
 
636
  char *tmp = malloc(f_len + s_len + 2);
 
637
  if (tmp == NULL){
 
638
    return NULL;
 
639
  }
 
640
  if(f_len > 0){
 
641
    memcpy(tmp, first, f_len);
 
642
  }
 
643
  tmp[f_len] = '/';
 
644
  if(s_len > 0){
 
645
    memcpy(tmp + f_len + 1, second, s_len);
 
646
  }
 
647
  tmp[f_len + 1 + s_len] = '\0';
 
648
  return tmp;
 
649
}
 
650
 
 
651
 
625
652
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
626
653
    AvahiServerConfig config;
627
654
    AvahiSServiceBrowser *sb = NULL;
635
662
    while (true){
636
663
      static struct option long_options[] = {
637
664
        {"debug", no_argument, (int *)&debug, 1},
638
 
        {"connect", required_argument, 0, 'c'},
 
665
        {"connect", required_argument, 0, 'C'},
639
666
        {"interface", required_argument, 0, 'i'},
 
667
        {"certdir", required_argument, 0, 'd'},
 
668
        {"certkey", required_argument, 0, 'c'},
 
669
        {"certfile", required_argument, 0, 'k'},
640
670
        {0, 0, 0, 0} };
641
671
      
642
672
      int option_index = 0;
653
683
      case 'i':
654
684
        interface = optarg;
655
685
        break;
 
686
      case 'C':
 
687
        connect_to = optarg;
 
688
        break;
 
689
      case 'd':
 
690
        certdir = optarg;
 
691
        break;
656
692
      case 'c':
657
 
        connect_to = optarg;
 
693
        certfile = optarg;
 
694
        break;
 
695
      case 'k':
 
696
        certkey = optarg;
658
697
        break;
659
698
      default:
660
699
        exit(EXIT_FAILURE);
661
700
      }
662
701
    }
663
702
    
 
703
    certfile = combinepath(certdir, certfile);
 
704
    if (certfile == NULL){
 
705
      perror("combinepath");
 
706
      goto exit;
 
707
    }
 
708
    
664
709
    if(interface != NULL){
665
710
      if_index = (AvahiIfIndex) if_nametoindex(interface);
666
711
      if(if_index == 0){
693
738
      }
694
739
    }
695
740
    
 
741
    certkey = combinepath(certdir, certkey);
 
742
    if (certkey == NULL){
 
743
      perror("combinepath");
 
744
      goto exit;
 
745
    }
 
746
    
696
747
    if (not debug){
697
748
      avahi_set_log_function(empty_log);
698
749
    }
764
815
 
765
816
    if (simple_poll)
766
817
        avahi_simple_poll_free(simple_poll);
767
 
 
 
818
    free(certfile);
 
819
    free(certkey);
 
820
    
768
821
    return returncode;
769
822
}