/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/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2008-12-10 01:26:02 UTC
  • mfrom: (237.1.2 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20081210012602-vhz3h75xkj24t340
First version of a somewhat complete D-Bus server interface.  Also
change user/group name to "_mandos".

* debian/mandos.postinst: Rename old "mandos" user and group to
                          "_mandos"; create "_mandos" user and group
                          if none exist.
* debian/mandos-client.postinst: - '' -

* initramfs-tools-hook: Try "_mandos" before "mandos" as user and
                        group name.

* mandos (_datetime_to_dbus_struct): New; was previously local.
  (Client.started): Renamed to "last_started".  All users changed.
  (Client.started): New; boolean.
  (Client.dbus_object_path): New.
  (Client.check_command): Renamed to "checker_command".  All users
                          changed.
  (Client.__init__): Set and use "self.dbus_object_path".  Set
                     "self.started".
  (Client.start): Update "self.started".  Emit "self.PropertyChanged"
                  signals for both "started" and "last_started".
  (Client.stop): Update "self.started".  Emit "self.PropertyChanged"
                 signal for "started".
  (Client.checker_callback): Take additional "command" argument.  All
                             callers changed. Emit
                             "self.PropertyChanged" signal.
  (Client.bump_timeout): Emit "self.PropertyChanged" signal for
                         "last_checked_ok".
  (Client.start_checker): Emit "self.PropertyChanged" signal for
                          "checker_running".
  (Client.stop_checker): Emit "self.PropertyChanged" signal for
                         "checker_running".
  (Client.still_valid): Bug fix: use "getattr(self, started, False)"
                        instead of "self.started" in case this client
                        object is so new that the "started" attribute
                        has not been created yet.
  (Client.IntervalChanged, Client.CheckerIsRunning, Client.GetChecker,
  Client.GetCreated, Client.GetFingerprint, Client.GetHost,
  Client.GetInterval, Client.GetName, Client.GetStarted,
  Client.GetTimeout, Client.StateChanged, Client.TimeoutChanged):
  Removed; all callers changed.
  (Client.CheckerCompleted): Add "condition" and "command" arguments.
                             All callers changed.
  (Client.GetAllProperties, Client.PropertyChanged): New.
  (Client.StillValid): Renamed to "IsStillValid".
  (Client.StartChecker): Changed to its own function to avoid the
                         return value from "Client.start_checker()".
  (Client.Stop): Changed to its own function to avoid the return value
                 from "Client.stop()".
  (main): Try "_mandos" before "mandos" as user and group name.
          Removed inner function "remove_from_clients".  New inner
          class "MandosServer".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Mandos-client - get and decrypt data from a Mandos server
 
3
 * Mandos client - get and decrypt data from a Mandos server
4
4
 *
5
5
 * This program is partly derived from an example program for an Avahi
6
6
 * service browser, downloaded from
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008,2009 Teddy Hogeborn
13
 
 * Copyright © 2008,2009 Björn Påhlsson
 
12
 * Copyright © 2008 Teddy Hogeborn
 
13
 * Copyright © 2008 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf */
 
39
                                   stdout, ferror() */
40
40
#include <stdint.h>             /* uint16_t, uint32_t */
41
41
#include <stddef.h>             /* NULL, size_t, ssize_t */
42
42
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
55
55
                                   connect() */
56
56
#include <fcntl.h>              /* open() */
57
57
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
58
 
#include <inttypes.h>           /* PRIu16, SCNu16 */
 
58
#include <inttypes.h>           /* PRIu16 */
59
59
#include <assert.h>             /* assert() */
60
60
#include <errno.h>              /* perror(), errno */
61
61
#include <time.h>               /* time() */
67
67
                                   getuid(), getgid(), setuid(),
68
68
                                   setgid() */
69
69
#include <arpa/inet.h>          /* inet_pton(), htons */
70
 
#include <iso646.h>             /* not, and, or */
 
70
#include <iso646.h>             /* not, and */
71
71
#include <argp.h>               /* struct argp_option, error_t, struct
72
72
                                   argp_state, struct argp,
73
73
                                   argp_parse(), ARGP_KEY_ARG,
156
156
    int fd;
157
157
    gpgme_data_t pgp_data;
158
158
    
159
 
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
 
159
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
160
160
    if(fd == -1){
161
161
      perror("open");
162
162
      return false;
176
176
      return false;
177
177
    }
178
178
    
179
 
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
 
179
    ret = TEMP_FAILURE_RETRY(close(fd));
180
180
    if(ret == -1){
181
181
      perror("close");
182
182
    }
501
501
                                      AvahiIfIndex if_index,
502
502
                                      mandos_context *mc){
503
503
  int ret, tcp_sd;
504
 
  ssize_t sret;
505
504
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
506
505
  char *buffer = NULL;
507
506
  char *decrypted_buffer;
578
577
  written = 0;
579
578
  while (true){
580
579
    size_t out_size = strlen(out);
581
 
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
 
580
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
582
581
                                   out_size - written));
583
582
    if (ret == -1){
584
583
      perror("write");
633
632
      goto mandos_end;
634
633
    }
635
634
    
636
 
    sret = gnutls_record_recv(session, buffer+buffer_length,
637
 
                              BUFFER_SIZE);
638
 
    if (sret == 0){
 
635
    ret = gnutls_record_recv(session, buffer+buffer_length,
 
636
                             BUFFER_SIZE);
 
637
    if (ret == 0){
639
638
      break;
640
639
    }
641
 
    if (sret < 0){
642
 
      switch(sret){
 
640
    if (ret < 0){
 
641
      switch(ret){
643
642
      case GNUTLS_E_INTERRUPTED:
644
643
      case GNUTLS_E_AGAIN:
645
644
        break;
662
661
        goto mandos_end;
663
662
      }
664
663
    } else {
665
 
      buffer_length += (size_t) sret;
 
664
      buffer_length += (size_t) ret;
666
665
    }
667
666
  }
668
667
  
704
703
  
705
704
 mandos_end:
706
705
  free(buffer);
707
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
706
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
708
707
  if(ret == -1){
709
708
    perror("close");
710
709
  }
866
865
      
867
866
      error_t parse_opt (int key, char *arg,
868
867
                         struct argp_state *state) {
 
868
        /* Get the INPUT argument from `argp_parse', which we know is
 
869
           a pointer to our plugin list pointer. */
869
870
        switch (key) {
870
871
        case 128:               /* --debug */
871
872
          debug = true;
883
884
          pubkey = arg;
884
885
          break;
885
886
        case 129:               /* --dh-bits */
886
 
          ret = sscanf(arg, "%u", &mc.dh_bits);
887
 
          if(ret == 0 or mc.dh_bits == 0){
888
 
            fprintf(stderr, "Bad number of DH bits\n");
 
887
          errno = 0;
 
888
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
 
889
          if (errno){
 
890
            perror("strtol");
889
891
            exit(EXIT_FAILURE);
890
892
          }
891
893
          break;
938
940
          goto end;
939
941
        }
940
942
      }
941
 
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
943
      ret = TEMP_FAILURE_RETRY(close(sd));
942
944
      if(ret == -1){
943
945
        perror("close");
944
946
      }
995
997
        exitcode = EXIT_FAILURE;
996
998
        goto end;
997
999
      }
998
 
      uint16_t port;
999
 
      ret = sscanf(address+1, "%" SCNu16, &port);
1000
 
      if(ret == 0 or port == 0){
1001
 
        fprintf(stderr, "Bad port number\n");
 
1000
      errno = 0;
 
1001
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
 
1002
      if(errno){
 
1003
        perror("Bad port number");
1002
1004
        exitcode = EXIT_FAILURE;
1003
1005
        goto end;
1004
1006
      }
1107
1109
      struct dirent *direntry;
1108
1110
      d = opendir(tempdir);
1109
1111
      if(d == NULL){
1110
 
        if(errno != ENOENT){
1111
 
          perror("opendir");
1112
 
        }
 
1112
        perror("opendir");
1113
1113
      } else {
1114
1114
        while(true){
1115
1115
          direntry = readdir(d);
1135
1135
        closedir(d);
1136
1136
      }
1137
1137
      ret = rmdir(tempdir);
1138
 
      if(ret == -1 and errno != ENOENT){
 
1138
      if(ret == -1){
1139
1139
        perror("rmdir");
1140
1140
      }
1141
1141
    }