/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 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
 
                               unsigned int 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;
367
373
    return -1;
368
374
  }
369
375
  
370
 
  if(if_indextoname(if_index, interface) == NULL){
 
376
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
371
377
    if(debug){
372
378
      perror("if_indextoname");
373
379
    }
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,
564
570
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
565
571
                " port %d\n", name, host_name, ip, port);
566
572
      }
567
 
      int ret = start_mandos_communication(ip, port,
568
 
                                           (unsigned int) interface);
 
573
      int ret = start_mandos_communication(ip, port, interface);
569
574
      if (ret == 0){
570
575
        exit(EXIT_SUCCESS);
571
576
      }
623
628
    }
624
629
}
625
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
 
626
652
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
627
653
    AvahiServerConfig config;
628
654
    AvahiSServiceBrowser *sb = NULL;
629
655
    int error;
630
656
    int ret;
631
657
    int returncode = EXIT_SUCCESS;
632
 
    const char *interface = "eth0";
633
 
    unsigned int if_index;
 
658
    const char *interface = NULL;
 
659
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
634
660
    char *connect_to = NULL;
635
661
    
636
662
    while (true){
637
663
      static struct option long_options[] = {
638
664
        {"debug", no_argument, (int *)&debug, 1},
639
 
        {"connect", required_argument, 0, 'c'},
 
665
        {"connect", required_argument, 0, 'C'},
640
666
        {"interface", required_argument, 0, 'i'},
 
667
        {"certdir", required_argument, 0, 'd'},
 
668
        {"certkey", required_argument, 0, 'c'},
 
669
        {"certfile", required_argument, 0, 'k'},
641
670
        {0, 0, 0, 0} };
642
671
      
643
672
      int option_index = 0;
654
683
      case 'i':
655
684
        interface = optarg;
656
685
        break;
 
686
      case 'C':
 
687
        connect_to = optarg;
 
688
        break;
 
689
      case 'd':
 
690
        certdir = optarg;
 
691
        break;
657
692
      case 'c':
658
 
        connect_to = optarg;
 
693
        certfile = optarg;
 
694
        break;
 
695
      case 'k':
 
696
        certkey = optarg;
659
697
        break;
660
698
      default:
661
699
        exit(EXIT_FAILURE);
662
700
      }
663
701
    }
664
702
    
665
 
    if_index = if_nametoindex(interface);
666
 
    if(if_index == 0){
667
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
668
 
      exit(EXIT_FAILURE);
 
703
    certfile = combinepath(certdir, certfile);
 
704
    if (certfile == NULL){
 
705
      perror("combinepath");
 
706
      goto exit;
 
707
    }
 
708
    
 
709
    if(interface != NULL){
 
710
      if_index = (AvahiIfIndex) if_nametoindex(interface);
 
711
      if(if_index == 0){
 
712
        fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
713
        exit(EXIT_FAILURE);
 
714
      }
669
715
    }
670
716
    
671
717
    if(connect_to != NULL){
692
738
      }
693
739
    }
694
740
    
 
741
    certkey = combinepath(certdir, certkey);
 
742
    if (certkey == NULL){
 
743
      perror("combinepath");
 
744
      goto exit;
 
745
    }
 
746
    
695
747
    if (not debug){
696
748
      avahi_set_log_function(empty_log);
697
749
    }
729
781
    }
730
782
    
731
783
    /* Create the service browser */
732
 
    sb = avahi_s_service_browser_new(server, (AvahiIfIndex)if_index,
 
784
    sb = avahi_s_service_browser_new(server, if_index,
733
785
                                     AVAHI_PROTO_INET6,
734
786
                                     "_mandos._tcp", NULL, 0,
735
787
                                     browse_callback, server);
763
815
 
764
816
    if (simple_poll)
765
817
        avahi_simple_poll_free(simple_poll);
766
 
 
 
818
    free(certfile);
 
819
    free(certkey);
 
820
    
767
821
    return returncode;
768
822
}