/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-07-29 03:35:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080729033539-08zecoj3jwlkpjhw
* server.conf: New file.

* mandos-clients.conf: Renamed to clients.conf.

* Makefile (FORTIFY): New.
  (CFLAGS): Include $(FORTIFY).

* plugins.d/mandosclient.c (main): New "if_index" variable.  Bug fix:
                                   check if interface exists.  New
                                   "--connect" option.

* server.py (serviceInterface): Removed; replaced by
                                "AvahiService.interface".  All users
                                changed.
  (AvahiError, AvahiServiceError, AvahiGroupError): New exception
                                                    classes.
  (AvahiService): New class.
  (serviceName): Removed; replaced by "AvahiService.name".  All users
                 changed.
  (serviceType): Removed; replaced by "AvahiService.type".  All users
                 changed.
  (servicePort): Removed; replaced by "AvahiService.port".  All users
                 changed.
  (serviceTXT): Removed; replaced by "AvahiService.TXT".  All users
                changed.
  (domain): Removed; replaced by "AvahiService.domain".  All users
            changed.
  (host): Removed; replaced by "AvahiService.host".  All users
          changed.
  (rename_count): Removed; replaced by "AvahiService.rename_count" and
                 "AvahiService.max_renames".  All users changed.
  (Client.__init__): If no secret or secfile, raise TypeError instead
                     of RuntimeError.
  (Client.last_seen): Renamed to "Client.last_checked_ok".  All users
                      changed.
  (Client.stop, Client.stop_checker): Use "getattr" with default value
                                      instead of "hasattr".
  (Client.still_valid): Removed "now" argument.
  (Client.handle): Separate the "no client found" and "client invalid"
                   cases for clearer code.
  (IPv6_TCPServer.__init__): "options" argument replaced by
                             "settings".  All callers changed.
  (IPv6_TCPServer.options): Replaced by "IPv6_TCPServer.settings".
                            All users changed.
  (IPv6_TCPServer.server_bind): Use getattr instead of hasattr.
  (add_service): Removed; replaced by "AvahiService.add".  All callers
                 changed.
  (remove_service): Removed; replaced by "AvahiService.remove".  All
                    callers changed.
  (entry_group_state_changed): On entry group collision, call the new
                               AvahiService.rename method.  Raise
                               AvahiGroupError on group error.
  (if_nametoindex): Use ctypes.utils.find_library to locate the C
                    library.  Cache the result.  Loop on EINTR.
  (daemon): Use os.path.devnull to locate "/dev/null".
  (killme): Removed.  All callers changed to do "sys.exit()" instead,
            except where stated otherwise.
  (main): Removed "exitstatus".  Removed all default values from all
          non-bool options.  New option "--configdir".  New variables
          "server_defaults" and "server_settings", read from
          "%(configdir)s/server.conf".  Let any supplied command line
          options override server settings.   Variable "defaults"
          renamed to "client_defaults", which is read from
          "clients.conf" instead of "mandos-clients.conf".  New global
          AvahiService object "service" replaces old global variables.
          Catch AvahiError and exit with error if caught.

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 <mandos@fukt.bsnet.se>.
 
28
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
29
 * <https://www.fukt.bsnet.se/~teddy/>.
29
30
 */
30
31
 
31
32
/* Needed by GPGME, specifically gpgme_data_seek() */
63
64
#include <errno.h>              /* perror() */
64
65
#include <gpgme.h>
65
66
 
66
 
// getopt_long
 
67
// getopt long
67
68
#include <getopt.h>
68
69
 
 
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"
69
75
#define BUFFER_SIZE 256
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";
 
76
#define DH_BITS 1024
76
77
 
77
78
bool debug = false;
78
79
 
79
 
/* Used for  */
80
80
typedef struct {
81
81
  gnutls_session_t session;
82
82
  gnutls_certificate_credentials_t cred;
84
84
} encrypted_session;
85
85
 
86
86
 
87
 
static ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
88
 
                                   char **new_packet,
89
 
                                   const char *homedir){
 
87
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
 
88
                            char **new_packet, const char *homedir){
90
89
  gpgme_data_t dh_crypto, dh_plain;
91
90
  gpgme_ctx_t ctx;
92
91
  gpgme_error_t rc;
101
100
  
102
101
  /* Init GPGME */
103
102
  gpgme_check_version(NULL);
104
 
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
105
 
  if (rc != GPG_ERR_NO_ERROR){
106
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
107
 
            gpgme_strsource(rc), gpgme_strerror(rc));
108
 
    return -1;
109
 
  }
 
103
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
110
104
  
111
105
  /* Set GPGME home directory */
112
106
  rc = gpgme_get_engine_info (&engine_info);
198
192
  gpgme_data_release(dh_crypto);
199
193
  
200
194
  /* Seek back to the beginning of the GPGME plaintext data buffer */
201
 
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
202
 
    perror("pgpme_data_seek");
203
 
  }
204
 
  
 
195
  gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
 
196
 
205
197
  *new_packet = 0;
206
198
  while(true){
207
199
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
248
240
  return ret;
249
241
}
250
242
 
251
 
static void debuggnutls(__attribute__((unused)) int level,
252
 
                        const char* string){
 
243
void debuggnutls(__attribute__((unused)) int level,
 
244
                 const char* string){
253
245
  fprintf(stderr, "%s", string);
254
246
}
255
247
 
256
 
static int initgnutls(encrypted_session *es){
 
248
int initgnutls(encrypted_session *es){
257
249
  const char *err;
258
250
  int ret;
259
251
  
260
252
  if(debug){
261
253
    fprintf(stderr, "Initializing GnuTLS\n");
262
254
  }
263
 
 
 
255
  
264
256
  if ((ret = gnutls_global_init ())
265
257
      != GNUTLS_E_SUCCESS) {
266
258
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
282
274
  
283
275
  if(debug){
284
276
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
285
 
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
286
 
            seckeyfile);
 
277
            " and keyfile %s as GnuTLS credentials\n", CERTFILE,
 
278
            KEYFILE);
287
279
  }
288
280
  
289
281
  ret = gnutls_certificate_set_openpgp_key_file
290
 
    (es->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
 
282
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
291
283
  if (ret != GNUTLS_E_SUCCESS) {
292
284
    fprintf
293
285
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
294
286
       " '%s')\n",
295
 
       ret, pubkeyfile, seckeyfile);
 
287
       ret, CERTFILE, KEYFILE);
296
288
    fprintf(stdout, "The Error is: %s\n",
297
289
            safer_gnutls_strerror(ret));
298
290
    return -1;
306
298
    return -1;
307
299
  }
308
300
  
309
 
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, dh_bits))
 
301
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
310
302
      != GNUTLS_E_SUCCESS) {
311
303
    fprintf (stderr, "Error in prime generation: %s\n",
312
304
             safer_gnutls_strerror(ret));
342
334
  gnutls_certificate_server_set_request (es->session,
343
335
                                         GNUTLS_CERT_IGNORE);
344
336
  
345
 
  gnutls_dh_set_prime_bits (es->session, dh_bits);
 
337
  gnutls_dh_set_prime_bits (es->session, DH_BITS);
346
338
  
347
339
  return 0;
348
340
}
349
341
 
350
 
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
351
 
                      __attribute__((unused)) const char *txt){}
 
342
void empty_log(__attribute__((unused)) AvahiLogLevel level,
 
343
               __attribute__((unused)) const char *txt){}
352
344
 
353
 
static int start_mandos_communication(const char *ip, uint16_t port,
354
 
                                      AvahiIfIndex if_index){
 
345
int start_mandos_communication(const char *ip, uint16_t port,
 
346
                               unsigned int if_index){
355
347
  int ret, tcp_sd;
356
348
  struct sockaddr_in6 to;
357
349
  encrypted_session es;
375
367
    return -1;
376
368
  }
377
369
  
378
 
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
370
  if(if_indextoname(if_index, interface) == NULL){
379
371
    if(debug){
380
372
      perror("if_indextoname");
381
373
    }
403
395
  
404
396
  if(debug){
405
397
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
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
 
    }
 
398
/*     char addrstr[INET6_ADDRSTRLEN]; */
 
399
/*     if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
 
400
/*               sizeof(addrstr)) == NULL){ */
 
401
/*       perror("inet_ntop"); */
 
402
/*     } else { */
 
403
/*       fprintf(stderr, "Really connecting to: %s, port %d\n", */
 
404
/*            addrstr, ntohs(to.sin6_port)); */
 
405
/*     } */
416
406
  }
417
407
  
418
408
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
497
487
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
498
488
                                               buffer_length,
499
489
                                               &decrypted_buffer,
500
 
                                               keydir);
 
490
                                               CERT_ROOT);
501
491
    if (decrypted_buffer_size >= 0){
502
492
      while(written < (size_t) decrypted_buffer_size){
503
493
        ret = (int)fwrite (decrypted_buffer + written, 1,
574
564
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
575
565
                " port %d\n", name, host_name, ip, port);
576
566
      }
577
 
      int ret = start_mandos_communication(ip, port, interface);
 
567
      int ret = start_mandos_communication(ip, port,
 
568
                                           (unsigned int) interface);
578
569
      if (ret == 0){
579
570
        exit(EXIT_SUCCESS);
580
571
      }
632
623
    }
633
624
}
634
625
 
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);
641
 
  if (tmp == NULL){
642
 
    return NULL;
643
 
  }
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';
652
 
  return tmp;
653
 
}
654
 
 
655
 
 
656
626
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
657
627
    AvahiServerConfig config;
658
628
    AvahiSServiceBrowser *sb = NULL;
659
629
    int error;
660
630
    int ret;
661
 
    int debug_int = 0;
662
631
    int returncode = EXIT_SUCCESS;
663
 
    const char *interface = NULL;
664
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
632
    const char *interface = "eth0";
 
633
    unsigned int if_index;
665
634
    char *connect_to = NULL;
666
635
    
667
 
    debug_int = debug ? 1 : 0;
668
636
    while (true){
669
637
      static struct option long_options[] = {
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'},
 
638
        {"debug", no_argument, (int *)&debug, 1},
 
639
        {"connect", required_argument, 0, 'c'},
 
640
        {"interface", required_argument, 0, 'i'},
677
641
        {0, 0, 0, 0} };
678
642
      
679
643
      int option_index = 0;
690
654
      case 'i':
691
655
        interface = optarg;
692
656
        break;
693
 
      case 'C':
 
657
      case 'c':
694
658
        connect_to = optarg;
695
659
        break;
696
 
      case 'd':
697
 
        keydir = optarg;
698
 
        break;
699
 
      case 'c':
700
 
        pubkeyfile = optarg;
701
 
        break;
702
 
      case 'k':
703
 
        seckeyfile = optarg;
704
 
        break;
705
 
      case 'D':
706
 
        dh_bits = atoi(optarg);
707
 
        break;
708
 
      case '?':
709
 
        break
710
660
      default:
711
661
        exit(EXIT_FAILURE);
712
662
      }
713
663
    }
714
 
    debug = debug_int ? true : false;
715
 
    
716
 
    pubkeyfile = combinepath(keydir, pubkeyfile);
717
 
    if (pubkeyfile == NULL){
718
 
      perror("combinepath");
719
 
      goto exit;
720
 
    }
721
 
    
722
 
    if(interface != NULL){
723
 
      if_index = (AvahiIfIndex) if_nametoindex(interface);
724
 
      if(if_index == 0){
725
 
        fprintf(stderr, "No such interface: \"%s\"\n", interface);
726
 
        exit(EXIT_FAILURE);
727
 
      }
 
664
    
 
665
    if_index = if_nametoindex(interface);
 
666
    if(if_index == 0){
 
667
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
668
      exit(EXIT_FAILURE);
728
669
    }
729
670
    
730
671
    if(connect_to != NULL){
751
692
      }
752
693
    }
753
694
    
754
 
    seckeyfile = combinepath(keydir, seckeyfile);
755
 
    if (seckeyfile == NULL){
756
 
      perror("combinepath");
757
 
      goto exit;
758
 
    }
759
 
    
760
695
    if (not debug){
761
696
      avahi_set_log_function(empty_log);
762
697
    }
794
729
    }
795
730
    
796
731
    /* Create the service browser */
797
 
    sb = avahi_s_service_browser_new(server, if_index,
 
732
    sb = avahi_s_service_browser_new(server, (AvahiIfIndex)if_index,
798
733
                                     AVAHI_PROTO_INET6,
799
734
                                     "_mandos._tcp", NULL, 0,
800
735
                                     browse_callback, server);
828
763
 
829
764
    if (simple_poll)
830
765
        avahi_simple_poll_free(simple_poll);
831
 
    free(pubkeyfile);
832
 
    free(seckeyfile);
833
 
    
 
766
 
834
767
    return returncode;
835
768
}