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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-02 21:06:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080802210612-4c1waup4z0f66ya7
Non-tested commit for merge purposes.

* plugbasedclient.c (getplugin, addargument, set_cloexec): Made static.

* plugins.d/passprompt.c (termination_handler): Made static.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
#include <errno.h>              /* perror() */
64
64
#include <gpgme.h>
65
65
 
66
 
// getopt long
 
66
// getopt_long
67
67
#include <getopt.h>
68
68
 
69
69
#define BUFFER_SIZE 256
70
 
#define DH_BITS 1024
71
 
 
72
 
const char *certdir = "/conf/conf.d/cryptkeyreq/";
73
 
const char *certfile = "openpgp-client.txt";
74
 
const char *certkey = "openpgp-client-key.txt";
 
70
 
 
71
static int dh_bits = 1024;
 
72
 
 
73
static const char *keydir = "/conf/conf.d/mandos";
 
74
static const char *pubkeyfile = "pubkey.txt";
 
75
static const char *seckeyfile = "seckey.txt";
75
76
 
76
77
bool debug = false;
77
78
 
 
79
/* Used for  */
78
80
typedef struct {
79
81
  gnutls_session_t session;
80
82
  gnutls_certificate_credentials_t cred;
82
84
} encrypted_session;
83
85
 
84
86
 
85
 
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
86
 
                            char **new_packet, const char *homedir){
 
87
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
 
88
                                   char **new_packet,
 
89
                                   const char *homedir){
87
90
  gpgme_data_t dh_crypto, dh_plain;
88
91
  gpgme_ctx_t ctx;
89
92
  gpgme_error_t rc;
245
248
  return ret;
246
249
}
247
250
 
248
 
void debuggnutls(__attribute__((unused)) int level,
249
 
                 const char* string){
 
251
static void debuggnutls(__attribute__((unused)) int level,
 
252
                        const char* string){
250
253
  fprintf(stderr, "%s", string);
251
254
}
252
255
 
253
 
int initgnutls(encrypted_session *es){
 
256
static int initgnutls(encrypted_session *es){
254
257
  const char *err;
255
258
  int ret;
256
259
  
279
282
  
280
283
  if(debug){
281
284
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
282
 
            " and keyfile %s as GnuTLS credentials\n", certfile,
283
 
            certkey);
 
285
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
286
            seckeyfile);
284
287
  }
285
288
  
286
289
  ret = gnutls_certificate_set_openpgp_key_file
287
 
    (es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
 
290
    (es->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
288
291
  if (ret != GNUTLS_E_SUCCESS) {
289
292
    fprintf
290
293
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
291
294
       " '%s')\n",
292
 
       ret, certfile, certkey);
 
295
       ret, pubkeyfile, seckeyfile);
293
296
    fprintf(stdout, "The Error is: %s\n",
294
297
            safer_gnutls_strerror(ret));
295
298
    return -1;
303
306
    return -1;
304
307
  }
305
308
  
306
 
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
 
309
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, dh_bits))
307
310
      != GNUTLS_E_SUCCESS) {
308
311
    fprintf (stderr, "Error in prime generation: %s\n",
309
312
             safer_gnutls_strerror(ret));
339
342
  gnutls_certificate_server_set_request (es->session,
340
343
                                         GNUTLS_CERT_IGNORE);
341
344
  
342
 
  gnutls_dh_set_prime_bits (es->session, DH_BITS);
 
345
  gnutls_dh_set_prime_bits (es->session, dh_bits);
343
346
  
344
347
  return 0;
345
348
}
346
349
 
347
 
void empty_log(__attribute__((unused)) AvahiLogLevel level,
348
 
               __attribute__((unused)) const char *txt){}
 
350
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
351
                      __attribute__((unused)) const char *txt){}
349
352
 
350
 
int start_mandos_communication(const char *ip, uint16_t port,
351
 
                               AvahiIfIndex if_index){
 
353
static int start_mandos_communication(const char *ip, uint16_t port,
 
354
                                      AvahiIfIndex if_index){
352
355
  int ret, tcp_sd;
353
356
  struct sockaddr_in6 to;
354
357
  encrypted_session es;
400
403
  
401
404
  if(debug){
402
405
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
403
 
/*     char addrstr[INET6_ADDRSTRLEN]; */
404
 
/*     if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
405
 
/*               sizeof(addrstr)) == NULL){ */
406
 
/*       perror("inet_ntop"); */
407
 
/*     } else { */
408
 
/*       fprintf(stderr, "Really connecting to: %s, port %d\n", */
409
 
/*            addrstr, ntohs(to.sin6_port)); */
410
 
/*     } */
 
406
    char addrstr[INET6_ADDRSTRLEN] = "";
 
407
    if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr,
 
408
                 sizeof(addrstr)) == NULL){
 
409
      perror("inet_ntop");
 
410
    } else {
 
411
      if(strcmp(addrstr, ip) != 0){
 
412
        fprintf(stderr, "Canonical address form: %s\n",
 
413
                addrstr, ntohs(to.sin6_port));
 
414
      }
 
415
    }
411
416
  }
412
417
  
413
418
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
492
497
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
493
498
                                               buffer_length,
494
499
                                               &decrypted_buffer,
495
 
                                               certdir);
 
500
                                               keydir);
496
501
    if (decrypted_buffer_size >= 0){
497
502
      while(written < (size_t) decrypted_buffer_size){
498
503
        ret = (int)fwrite (decrypted_buffer + written, 1,
627
632
    }
628
633
}
629
634
 
630
 
/* combinds file name and path and returns the malloced new string. som sane checks could/should be added */
631
 
const char *combinepath(const char *first, const char *second){
632
 
  char *tmp;
633
 
  tmp = malloc(strlen(first) + strlen(second) + 2);
 
635
/* Combines file name and path and returns the malloced new
 
636
   string. some sane checks could/should be added */
 
637
static const char *combinepath(const char *first, const char *second){
 
638
  size_t f_len = strlen(first);
 
639
  size_t s_len = strlen(second);
 
640
  char *tmp = malloc(f_len + s_len + 2);
634
641
  if (tmp == NULL){
635
 
    perror("malloc");
636
642
    return NULL;
637
643
  }
638
 
  strcpy(tmp, first);
639
 
  if (first[0] != '\0' and first[strlen(first) - 1] != '/'){
640
 
    strcat(tmp, "/");
641
 
  }
642
 
  strcat(tmp, second);
 
644
  if(f_len > 0){
 
645
    memcpy(tmp, first, f_len);
 
646
  }
 
647
  tmp[f_len] = '/';
 
648
  if(s_len > 0){
 
649
    memcpy(tmp + f_len + 1, second, s_len);
 
650
  }
 
651
  tmp[f_len + 1 + s_len] = '\0';
643
652
  return tmp;
644
653
}
645
654
 
649
658
    AvahiSServiceBrowser *sb = NULL;
650
659
    int error;
651
660
    int ret;
 
661
    int debug_int = 0;
652
662
    int returncode = EXIT_SUCCESS;
653
663
    const char *interface = NULL;
654
664
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
655
665
    char *connect_to = NULL;
656
666
    
 
667
    debug_int = debug ? 1 : 0;
657
668
    while (true){
658
669
      static struct option long_options[] = {
659
 
        {"debug", no_argument, (int *)&debug, 1},
660
 
        {"connect", required_argument, 0, 'C'},
661
 
        {"interface", required_argument, 0, 'i'},
662
 
        {"certdir", required_argument, 0, 'd'},
663
 
        {"certkey", required_argument, 0, 'c'},
664
 
        {"certfile", required_argument, 0, 'k'},
 
670
        {"debug", no_argument, &debug_int, 1},
 
671
        {"connect", required_argument, NULL, 'C'},
 
672
        {"interface", required_argument, NULL, 'i'},
 
673
        {"keydir", required_argument, NULL, 'd'},
 
674
        {"seckey", required_argument, NULL, 'c'},
 
675
        {"pubkey", required_argument, NULL, 'k'},
 
676
        {"dh-bits", required_argument, NULL, 'D'},
665
677
        {0, 0, 0, 0} };
666
678
      
667
679
      int option_index = 0;
682
694
        connect_to = optarg;
683
695
        break;
684
696
      case 'd':
685
 
        certdir = optarg;
 
697
        keydir = optarg;
686
698
        break;
687
699
      case 'c':
688
 
        certfile = optarg;
 
700
        pubkeyfile = optarg;
689
701
        break;
690
702
      case 'k':
691
 
        certkey = optarg;
692
 
        break;
 
703
        seckeyfile = optarg;
 
704
        break;
 
705
      case 'D':
 
706
        dh_bits = atoi(optarg);
 
707
        break;
 
708
      case '?':
 
709
        break
693
710
      default:
694
711
        exit(EXIT_FAILURE);
695
712
      }
696
713
    }
697
 
 
698
 
    certfile = combinepath(certdir, certfile);
699
 
    if (certfile == NULL){
 
714
    debug = debug_int ? true : false;
 
715
    
 
716
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
717
    if (pubkeyfile == NULL){
 
718
      perror("combinepath");
700
719
      goto exit;
701
720
    }
702
721
    
732
751
      }
733
752
    }
734
753
    
735
 
    certkey = combinepath(certdir, certkey);
736
 
    if (certkey == NULL){
 
754
    seckeyfile = combinepath(keydir, seckeyfile);
 
755
    if (seckeyfile == NULL){
 
756
      perror("combinepath");
737
757
      goto exit;
738
758
    }
739
759
    
808
828
 
809
829
    if (simple_poll)
810
830
        avahi_simple_poll_free(simple_poll);
811
 
    free(certfile);
812
 
    free(certkey);
 
831
    free(pubkeyfile);
 
832
    free(seckeyfile);
813
833
    
814
834
    return returncode;
815
835
}