/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

Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
// getopt long
69
69
#include <getopt.h>
70
70
 
71
 
#ifndef CERT_ROOT
72
 
#define CERT_ROOT "/conf/conf.d/cryptkeyreq/"
73
 
#endif
74
 
#define CERTFILE CERT_ROOT "openpgp-client.txt"
75
 
#define KEYFILE CERT_ROOT "openpgp-client-key.txt"
76
71
#define BUFFER_SIZE 256
77
72
#define DH_BITS 1024
78
73
 
 
74
const char *certdir = "/conf/conf.d/cryptkeyreq/";
 
75
const char *certfile = "openpgp-client.txt";
 
76
const char *certkey = "openpgp-client-key.txt";
 
77
 
79
78
bool debug = false;
80
79
 
81
80
typedef struct {
101
100
  
102
101
  /* Init GPGME */
103
102
  gpgme_check_version(NULL);
104
 
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
103
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
 
104
  if (rc != GPG_ERR_NO_ERROR){
 
105
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
 
106
            gpgme_strsource(rc), gpgme_strerror(rc));
 
107
    return -1;
 
108
  }
105
109
  
106
110
  /* Set GPGME home directory */
107
111
  rc = gpgme_get_engine_info (&engine_info);
253
257
  if(debug){
254
258
    fprintf(stderr, "Initializing GnuTLS\n");
255
259
  }
256
 
  
 
260
 
257
261
  if ((ret = gnutls_global_init ())
258
262
      != GNUTLS_E_SUCCESS) {
259
263
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
275
279
  
276
280
  if(debug){
277
281
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
278
 
            " and keyfile %s as GnuTLS credentials\n", CERTFILE,
279
 
            KEYFILE);
 
282
            " and keyfile %s as GnuTLS credentials\n", certfile,
 
283
            certkey);
280
284
  }
281
285
  
282
286
  ret = gnutls_certificate_set_openpgp_key_file
283
 
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
 
287
    (es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
284
288
  if (ret != GNUTLS_E_SUCCESS) {
285
289
    fprintf
286
290
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
287
291
       " '%s')\n",
288
 
       ret, CERTFILE, KEYFILE);
 
292
       ret, certfile, certkey);
289
293
    fprintf(stdout, "The Error is: %s\n",
290
294
            safer_gnutls_strerror(ret));
291
295
    return -1;
479
483
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
480
484
                                               buffer_length,
481
485
                                               &decrypted_buffer,
482
 
                                               CERT_ROOT);
 
486
                                               certdir);
483
487
    if (decrypted_buffer_size >= 0){
484
488
      while(written < decrypted_buffer_size){
485
489
        ret = (int)fwrite (decrypted_buffer + written, 1,
615
619
    }
616
620
}
617
621
 
 
622
/* combinds two strings and returns the malloced new string. som sane checks could/should be added */
 
623
const char *combinestrings(const char *first, const char *second){
 
624
  char *tmp;
 
625
  tmp = malloc(strlen(first) + strlen(second));
 
626
  if (tmp == NULL){
 
627
    perror("malloc");
 
628
    return NULL;
 
629
  }
 
630
  strcpy(tmp, first);
 
631
  strcat(tmp, second);
 
632
  return tmp;
 
633
}
 
634
 
 
635
 
618
636
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
619
637
    AvahiServerConfig config;
620
638
    AvahiSServiceBrowser *sb = NULL;
627
645
      static struct option long_options[] = {
628
646
        {"debug", no_argument, (int *)&debug, 1},
629
647
        {"interface", required_argument, 0, 'i'},
 
648
        {"certdir", required_argument, 0, 'd'},
 
649
        {"certkey", required_argument, 0, 'c'},
 
650
        {"certfile", required_argument, 0, 'k'},
630
651
        {0, 0, 0, 0} };
631
652
      
632
653
      int option_index = 0;
643
664
      case 'i':
644
665
        interface = optarg;
645
666
        break;
 
667
      case 'd':
 
668
        certdir = optarg;
 
669
        break;
 
670
      case 'c':
 
671
        certfile = optarg;
 
672
        break;
 
673
      case 'k':
 
674
        certkey = optarg;
 
675
        break;
646
676
      default:
647
677
        exit(EXIT_FAILURE);
648
678
      }
649
679
    }
 
680
 
 
681
    certfile = combinestrings(certdir, certfile);
 
682
    if (certfile == NULL){
 
683
      goto exit;
 
684
    }
650
685
    
 
686
    certkey = combinestrings(certdir, certkey);
 
687
    if (certkey == NULL){
 
688
      goto exit;
 
689
    }
 
690
        
651
691
    if (not debug){
652
692
      avahi_set_log_function(empty_log);
653
693
    }