/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-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() */
66
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
76
#define DH_BITS 1024
71
77
 
72
 
const char *certdir = "/conf/conf.d/cryptkeyreq/";
73
 
const char *certfile = "openpgp-client.txt";
74
 
const char *certkey = "openpgp-client-key.txt";
75
 
 
76
78
bool debug = false;
77
79
 
78
80
typedef struct {
98
100
  
99
101
  /* Init GPGME */
100
102
  gpgme_check_version(NULL);
101
 
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
102
 
  if (rc != GPG_ERR_NO_ERROR){
103
 
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
104
 
            gpgme_strsource(rc), gpgme_strerror(rc));
105
 
    return -1;
106
 
  }
 
103
  gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
107
104
  
108
105
  /* Set GPGME home directory */
109
106
  rc = gpgme_get_engine_info (&engine_info);
195
192
  gpgme_data_release(dh_crypto);
196
193
  
197
194
  /* Seek back to the beginning of the GPGME plaintext data buffer */
198
 
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
199
 
    perror("pgpme_data_seek");
200
 
  }
201
 
  
 
195
  gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET);
 
196
 
202
197
  *new_packet = 0;
203
198
  while(true){
204
199
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
257
252
  if(debug){
258
253
    fprintf(stderr, "Initializing GnuTLS\n");
259
254
  }
260
 
 
 
255
  
261
256
  if ((ret = gnutls_global_init ())
262
257
      != GNUTLS_E_SUCCESS) {
263
258
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
279
274
  
280
275
  if(debug){
281
276
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
282
 
            " and keyfile %s as GnuTLS credentials\n", certfile,
283
 
            certkey);
 
277
            " and keyfile %s as GnuTLS credentials\n", CERTFILE,
 
278
            KEYFILE);
284
279
  }
285
280
  
286
281
  ret = gnutls_certificate_set_openpgp_key_file
287
 
    (es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
 
282
    (es->cred, CERTFILE, KEYFILE, GNUTLS_OPENPGP_FMT_BASE64);
288
283
  if (ret != GNUTLS_E_SUCCESS) {
289
284
    fprintf
290
285
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
291
286
       " '%s')\n",
292
 
       ret, certfile, certkey);
 
287
       ret, CERTFILE, KEYFILE);
293
288
    fprintf(stdout, "The Error is: %s\n",
294
289
            safer_gnutls_strerror(ret));
295
290
    return -1;
348
343
               __attribute__((unused)) const char *txt){}
349
344
 
350
345
int start_mandos_communication(const char *ip, uint16_t port,
351
 
                               AvahiIfIndex if_index){
 
346
                               unsigned int if_index){
352
347
  int ret, tcp_sd;
353
348
  struct sockaddr_in6 to;
354
349
  encrypted_session es;
372
367
    return -1;
373
368
  }
374
369
  
375
 
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
 
370
  if(if_indextoname(if_index, interface) == NULL){
376
371
    if(debug){
377
372
      perror("if_indextoname");
378
373
    }
492
487
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
493
488
                                               buffer_length,
494
489
                                               &decrypted_buffer,
495
 
                                               certdir);
 
490
                                               CERT_ROOT);
496
491
    if (decrypted_buffer_size >= 0){
497
492
      while(written < (size_t) decrypted_buffer_size){
498
493
        ret = (int)fwrite (decrypted_buffer + written, 1,
569
564
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
570
565
                " port %d\n", name, host_name, ip, port);
571
566
      }
572
 
      int ret = start_mandos_communication(ip, port, interface);
 
567
      int ret = start_mandos_communication(ip, port,
 
568
                                           (unsigned int) interface);
573
569
      if (ret == 0){
574
570
        exit(EXIT_SUCCESS);
575
571
      }
627
623
    }
628
624
}
629
625
 
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);
634
 
  if (tmp == NULL){
635
 
    perror("malloc");
636
 
    return NULL;
637
 
  }
638
 
  strcpy(tmp, first);
639
 
  if (first[0] != '\0' and first[strlen(first) - 1] != '/'){
640
 
    strcat(tmp, "/");
641
 
  }
642
 
  strcat(tmp, second);
643
 
  return tmp;
644
 
}
645
 
 
646
 
 
647
626
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
648
627
    AvahiServerConfig config;
649
628
    AvahiSServiceBrowser *sb = NULL;
650
629
    int error;
651
630
    int ret;
652
631
    int returncode = EXIT_SUCCESS;
653
 
    const char *interface = NULL;
654
 
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
 
632
    const char *interface = "eth0";
 
633
    unsigned int if_index;
655
634
    char *connect_to = NULL;
656
635
    
657
636
    while (true){
658
637
      static struct option long_options[] = {
659
638
        {"debug", no_argument, (int *)&debug, 1},
660
 
        {"connect", required_argument, 0, 'C'},
 
639
        {"connect", required_argument, 0, 'c'},
661
640
        {"interface", required_argument, 0, 'i'},
662
 
        {"certdir", required_argument, 0, 'd'},
663
 
        {"certkey", required_argument, 0, 'c'},
664
 
        {"certfile", required_argument, 0, 'k'},
665
641
        {0, 0, 0, 0} };
666
642
      
667
643
      int option_index = 0;
678
654
      case 'i':
679
655
        interface = optarg;
680
656
        break;
681
 
      case 'C':
 
657
      case 'c':
682
658
        connect_to = optarg;
683
659
        break;
684
 
      case 'd':
685
 
        certdir = optarg;
686
 
        break;
687
 
      case 'c':
688
 
        certfile = optarg;
689
 
        break;
690
 
      case 'k':
691
 
        certkey = optarg;
692
 
        break;
693
660
      default:
694
661
        exit(EXIT_FAILURE);
695
662
      }
696
663
    }
697
 
 
698
 
    certfile = combinepath(certdir, certfile);
699
 
    if (certfile == NULL){
700
 
      goto exit;
701
 
    }
702
664
    
703
 
    if(interface != NULL){
704
 
      if_index = (AvahiIfIndex) if_nametoindex(interface);
705
 
      if(if_index == 0){
706
 
        fprintf(stderr, "No such interface: \"%s\"\n", interface);
707
 
        exit(EXIT_FAILURE);
708
 
      }
 
665
    if_index = if_nametoindex(interface);
 
666
    if(if_index == 0){
 
667
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
668
      exit(EXIT_FAILURE);
709
669
    }
710
670
    
711
671
    if(connect_to != NULL){
732
692
      }
733
693
    }
734
694
    
735
 
    certkey = combinepath(certdir, certkey);
736
 
    if (certkey == NULL){
737
 
      goto exit;
738
 
    }
739
 
    
740
695
    if (not debug){
741
696
      avahi_set_log_function(empty_log);
742
697
    }
774
729
    }
775
730
    
776
731
    /* Create the service browser */
777
 
    sb = avahi_s_service_browser_new(server, if_index,
 
732
    sb = avahi_s_service_browser_new(server, (AvahiIfIndex)if_index,
778
733
                                     AVAHI_PROTO_INET6,
779
734
                                     "_mandos._tcp", NULL, 0,
780
735
                                     browse_callback, server);
808
763
 
809
764
    if (simple_poll)
810
765
        avahi_simple_poll_free(simple_poll);
811
 
    free(certfile);
812
 
    free(certkey);
813
 
    
 
766
 
814
767
    return returncode;
815
768
}