/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/password-request.c

updated mandos-client sections and added see also stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
 
12
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
13
13
 * 
14
14
 * This program is free software: you can redistribute it and/or
15
15
 * modify it under the terms of the GNU General Public License as
32
32
#define _LARGEFILE_SOURCE
33
33
#define _FILE_OFFSET_BITS 64
34
34
 
35
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
35
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
36
36
 
37
 
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
38
 
                                   stdout, ferror() */
 
37
#include <stdio.h>              /* fprintf(), stderr, fwrite(), stdout,
 
38
                                   ferror() */
39
39
#include <stdint.h>             /* uint16_t, uint32_t */
40
40
#include <stddef.h>             /* NULL, size_t, ssize_t */
41
41
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
42
                                   srand() */
43
43
#include <stdbool.h>            /* bool, true */
44
44
#include <string.h>             /* memset(), strcmp(), strlen(),
45
 
                                   strerror(), asprintf(), strcpy() */
 
45
                                   strerror(), memcpy(), strcpy() */
46
46
#include <sys/ioctl.h>          /* ioctl */
 
47
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
 
48
                                   IFF_UP */
47
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
50
                                   sockaddr_in6, PF_INET6,
49
51
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
 
                                   uid_t, gid_t, open(), opendir(), DIR */
51
 
#include <sys/stat.h>           /* open() */
 
52
                                   uid_t, gid_t */
 
53
#include <inttypes.h>           /* PRIu16 */
52
54
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
55
                                   struct in6_addr, inet_pton(),
54
56
                                   connect() */
55
 
#include <fcntl.h>              /* open() */
56
 
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
57
 
#include <inttypes.h>           /* PRIu16 */
58
57
#include <assert.h>             /* assert() */
59
58
#include <errno.h>              /* perror(), errno */
60
59
#include <time.h>               /* time() */
61
60
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
62
61
                                   SIOCSIFFLAGS, if_indextoname(),
63
62
                                   if_nametoindex(), IF_NAMESIZE */
64
 
#include <netinet/in.h>
65
63
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
66
64
                                   getuid(), getgid(), setuid(),
67
65
                                   setgid() */
 
66
#include <netinet/in.h>
68
67
#include <arpa/inet.h>          /* inet_pton(), htons */
69
68
#include <iso646.h>             /* not, and */
70
69
#include <argp.h>               /* struct argp_option, error_t, struct
84
83
#include <avahi-common/error.h>
85
84
 
86
85
/* GnuTLS */
87
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
88
 
                                   functions:
 
86
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
89
87
                                   gnutls_*
90
88
                                   init_gnutls_session(),
91
89
                                   GNUTLS_* */
93
91
                                   GNUTLS_OPENPGP_FMT_BASE64 */
94
92
 
95
93
/* GPGME */
96
 
#include <gpgme.h>              /* All GPGME types, constants and
97
 
                                   functions:
 
94
#include <gpgme.h>              /* All GPGME types, constants and functions
98
95
                                   gpgme_*
99
96
                                   GPGME_PROTOCOL_OpenPGP,
100
97
                                   GPG_ERR_NO_* */
101
98
 
102
99
#define BUFFER_SIZE 256
103
100
 
104
 
#define PATHDIR "/conf/conf.d/mandos"
105
 
#define SECKEY "seckey.txt"
106
 
#define PUBKEY "pubkey.txt"
107
 
 
108
101
bool debug = false;
 
102
static const char *keydir = "/conf/conf.d/mandos";
109
103
static const char mandos_protocol_version[] = "1";
110
 
const char *argp_program_version = "mandos-client " VERSION;
 
104
const char *argp_program_version = "password-request 1.0";
111
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
112
106
 
113
107
/* Used for passing in values through the Avahi callback functions */
118
112
  unsigned int dh_bits;
119
113
  gnutls_dh_params_t dh_params;
120
114
  const char *priority;
121
 
  gpgme_ctx_t ctx;
122
115
} mandos_context;
123
116
 
124
117
/*
139
132
}
140
133
 
141
134
/* 
142
 
 * Initialize GPGME.
 
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
136
 * Returns -1 on error
143
137
 */
144
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
145
 
                       const char *pubkey, const char *tempdir){
146
 
  int ret;
 
138
static ssize_t pgp_packet_decrypt (const char *cryptotext,
 
139
                                   size_t crypto_size,
 
140
                                   char **plaintext,
 
141
                                   const char *homedir){
 
142
  gpgme_data_t dh_crypto, dh_plain;
 
143
  gpgme_ctx_t ctx;
147
144
  gpgme_error_t rc;
 
145
  ssize_t ret;
 
146
  size_t plaintext_capacity = 0;
 
147
  ssize_t plaintext_length = 0;
148
148
  gpgme_engine_info_t engine_info;
149
149
  
150
 
  
151
 
  /*
152
 
   * Helper function to insert pub and seckey to the enigne keyring.
153
 
   */
154
 
  bool import_key(const char *filename){
155
 
    int fd;
156
 
    gpgme_data_t pgp_data;
157
 
    
158
 
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
159
 
    if(fd == -1){
160
 
      perror("open");
161
 
      return false;
162
 
    }
163
 
    
164
 
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
165
 
    if (rc != GPG_ERR_NO_ERROR){
166
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
167
 
              gpgme_strsource(rc), gpgme_strerror(rc));
168
 
      return false;
169
 
    }
170
 
    
171
 
    rc = gpgme_op_import(mc->ctx, pgp_data);
172
 
    if (rc != GPG_ERR_NO_ERROR){
173
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
174
 
              gpgme_strsource(rc), gpgme_strerror(rc));
175
 
      return false;
176
 
    }
177
 
    
178
 
    ret = TEMP_FAILURE_RETRY(close(fd));
179
 
    if(ret == -1){
180
 
      perror("close");
181
 
    }
182
 
    gpgme_data_release(pgp_data);
183
 
    return true;
184
 
  }
185
 
  
186
150
  if (debug){
187
 
    fprintf(stderr, "Initialize gpgme\n");
 
151
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
188
152
  }
189
153
  
190
154
  /* Init GPGME */
193
157
  if (rc != GPG_ERR_NO_ERROR){
194
158
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
159
            gpgme_strsource(rc), gpgme_strerror(rc));
196
 
    return false;
 
160
    return -1;
197
161
  }
198
162
  
199
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
163
  /* Set GPGME home directory for the OpenPGP engine only */
200
164
  rc = gpgme_get_engine_info (&engine_info);
201
165
  if (rc != GPG_ERR_NO_ERROR){
202
166
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
167
            gpgme_strsource(rc), gpgme_strerror(rc));
204
 
    return false;
 
168
    return -1;
205
169
  }
206
170
  while(engine_info != NULL){
207
171
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
208
172
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
209
 
                            engine_info->file_name, tempdir);
 
173
                            engine_info->file_name, homedir);
210
174
      break;
211
175
    }
212
176
    engine_info = engine_info->next;
213
177
  }
214
178
  if(engine_info == NULL){
215
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
216
 
    return false;
217
 
  }
218
 
  
219
 
  /* Create new GPGME "context" */
220
 
  rc = gpgme_new(&(mc->ctx));
221
 
  if (rc != GPG_ERR_NO_ERROR){
222
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
223
 
            gpgme_strsource(rc), gpgme_strerror(rc));
224
 
    return false;
225
 
  }
226
 
  
227
 
  if (not import_key(pubkey) or not import_key(seckey)){
228
 
    return false;
229
 
  }
230
 
  
231
 
  return true; 
232
 
}
233
 
 
234
 
/* 
235
 
 * Decrypt OpenPGP data.
236
 
 * Returns -1 on error
237
 
 */
238
 
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
239
 
                                   const char *cryptotext,
240
 
                                   size_t crypto_size,
241
 
                                   char **plaintext){
242
 
  gpgme_data_t dh_crypto, dh_plain;
243
 
  gpgme_error_t rc;
244
 
  ssize_t ret;
245
 
  size_t plaintext_capacity = 0;
246
 
  ssize_t plaintext_length = 0;
247
 
  
248
 
  if (debug){
249
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
179
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
180
    return -1;
250
181
  }
251
182
  
252
183
  /* Create new GPGME data buffer from memory cryptotext */
267
198
    return -1;
268
199
  }
269
200
  
 
201
  /* Create new GPGME "context" */
 
202
  rc = gpgme_new(&ctx);
 
203
  if (rc != GPG_ERR_NO_ERROR){
 
204
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
205
            gpgme_strsource(rc), gpgme_strerror(rc));
 
206
    plaintext_length = -1;
 
207
    goto decrypt_end;
 
208
  }
 
209
  
270
210
  /* Decrypt data from the cryptotext data buffer to the plaintext
271
211
     data buffer */
272
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
212
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
273
213
  if (rc != GPG_ERR_NO_ERROR){
274
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
215
            gpgme_strsource(rc), gpgme_strerror(rc));
276
216
    plaintext_length = -1;
277
 
    if (debug){
278
 
      gpgme_decrypt_result_t result;
279
 
      result = gpgme_op_decrypt_result(mc->ctx);
280
 
      if (result == NULL){
281
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
282
 
      } else {
283
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
284
 
                result->unsupported_algorithm);
285
 
        fprintf(stderr, "Wrong key usage: %u\n",
286
 
                result->wrong_key_usage);
287
 
        if(result->file_name != NULL){
288
 
          fprintf(stderr, "File name: %s\n", result->file_name);
289
 
        }
290
 
        gpgme_recipient_t recipient;
291
 
        recipient = result->recipients;
292
 
        if(recipient){
293
 
          while(recipient != NULL){
294
 
            fprintf(stderr, "Public key algorithm: %s\n",
295
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
296
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
297
 
            fprintf(stderr, "Secret key available: %s\n",
298
 
                    recipient->status == GPG_ERR_NO_SECKEY
299
 
                    ? "No" : "Yes");
300
 
            recipient = recipient->next;
301
 
          }
302
 
        }
303
 
      }
304
 
    }
305
217
    goto decrypt_end;
306
218
  }
307
219
  
309
221
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
310
222
  }
311
223
  
 
224
  if (debug){
 
225
    gpgme_decrypt_result_t result;
 
226
    result = gpgme_op_decrypt_result(ctx);
 
227
    if (result == NULL){
 
228
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
229
    } else {
 
230
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
231
              result->unsupported_algorithm);
 
232
      fprintf(stderr, "Wrong key usage: %u\n",
 
233
              result->wrong_key_usage);
 
234
      if(result->file_name != NULL){
 
235
        fprintf(stderr, "File name: %s\n", result->file_name);
 
236
      }
 
237
      gpgme_recipient_t recipient;
 
238
      recipient = result->recipients;
 
239
      if(recipient){
 
240
        while(recipient != NULL){
 
241
          fprintf(stderr, "Public key algorithm: %s\n",
 
242
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
243
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
244
          fprintf(stderr, "Secret key available: %s\n",
 
245
                  recipient->status == GPG_ERR_NO_SECKEY
 
246
                  ? "No" : "Yes");
 
247
          recipient = recipient->next;
 
248
        }
 
249
      }
 
250
    }
 
251
  }
 
252
  
312
253
  /* Seek back to the beginning of the GPGME plaintext data buffer */
313
254
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
314
 
    perror("gpgme_data_seek");
 
255
    perror("pgpme_data_seek");
315
256
    plaintext_length = -1;
316
257
    goto decrypt_end;
317
258
  }
341
282
    }
342
283
    plaintext_length += ret;
343
284
  }
344
 
  
 
285
 
345
286
  if(debug){
346
287
    fprintf(stderr, "Decrypted password is: ");
347
288
    for(ssize_t i = 0; i < plaintext_length; i++){
361
302
}
362
303
 
363
304
static const char * safer_gnutls_strerror (int value) {
364
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
305
  const char *ret = gnutls_strerror (value);
365
306
  if (ret == NULL)
366
307
    ret = "(unknown)";
367
308
  return ret;
374
315
}
375
316
 
376
317
static int init_gnutls_global(mandos_context *mc,
377
 
                              const char *pubkeyfilename,
378
 
                              const char *seckeyfilename){
 
318
                              const char *pubkeyfile,
 
319
                              const char *seckeyfile){
379
320
  int ret;
380
321
  
381
322
  if(debug){
400
341
  /* OpenPGP credentials */
401
342
  gnutls_certificate_allocate_credentials(&mc->cred);
402
343
  if (ret != GNUTLS_E_SUCCESS){
403
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
404
 
                                                     warning */
 
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
405
345
             safer_gnutls_strerror(ret));
406
346
    gnutls_global_deinit ();
407
347
    return -1;
408
348
  }
409
349
  
410
350
  if(debug){
411
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
412
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
413
 
            seckeyfilename);
 
351
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
352
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
353
            seckeyfile);
414
354
  }
415
355
  
416
356
  ret = gnutls_certificate_set_openpgp_key_file
417
 
    (mc->cred, pubkeyfilename, seckeyfilename,
418
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
419
358
  if (ret != GNUTLS_E_SUCCESS) {
420
359
    fprintf(stderr,
421
360
            "Error[%d] while reading the OpenPGP key pair ('%s',"
422
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
423
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
361
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
362
    fprintf(stdout, "The GnuTLS error is: %s\n",
424
363
            safer_gnutls_strerror(ret));
425
364
    goto globalfail;
426
365
  }
440
379
  }
441
380
  
442
381
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
443
 
  
 
382
 
444
383
  return 0;
445
 
  
 
384
 
446
385
 globalfail:
447
 
  
 
386
 
448
387
  gnutls_certificate_free_credentials(mc->cred);
449
388
  gnutls_global_deinit();
450
 
  gnutls_dh_params_deinit(mc->dh_params);
451
389
  return -1;
 
390
 
452
391
}
453
392
 
454
393
static int init_gnutls_session(mandos_context *mc,
526
465
    perror("socket");
527
466
    return -1;
528
467
  }
529
 
  
 
468
 
530
469
  if(debug){
531
470
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
532
471
      perror("if_indextoname");
535
474
    fprintf(stderr, "Binding to interface %s\n", interface);
536
475
  }
537
476
  
538
 
  memset(&to, 0, sizeof(to));
 
477
  memset(&to,0,sizeof(to));     /* Spurious warning */
539
478
  to.in6.sin6_family = AF_INET6;
540
479
  /* It would be nice to have a way to detect if we were passed an
541
480
     IPv4 address here.   Now we assume an IPv6 address. */
548
487
    fprintf(stderr, "Bad address: %s\n", ip);
549
488
    return -1;
550
489
  }
551
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port);       /* Spurious warning */
552
491
  
553
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
554
493
  
571
510
    perror("connect");
572
511
    return -1;
573
512
  }
574
 
  
 
513
 
575
514
  const char *out = mandos_protocol_version;
576
515
  written = 0;
577
516
  while (true){
595
534
      }
596
535
    }
597
536
  }
598
 
  
 
537
 
599
538
  if(debug){
600
539
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
601
540
  }
602
541
  
603
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
604
 
  
 
543
 
605
544
  do{
606
545
    ret = gnutls_handshake (session);
607
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
621
560
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
622
561
            ip);
623
562
  }
624
 
  
 
563
 
625
564
  while(true){
626
565
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
627
566
                                   buffer_capacity);
671
610
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
672
611
  
673
612
  if (buffer_length > 0){
674
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
613
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
675
614
                                               buffer_length,
676
 
                                               &decrypted_buffer);
 
615
                                               &decrypted_buffer,
 
616
                                               keydir);
677
617
    if (decrypted_buffer_size >= 0){
678
618
      written = 0;
679
619
      while(written < (size_t) decrypted_buffer_size){
694
634
    } else {
695
635
      retval = -1;
696
636
    }
697
 
  } else {
698
 
    retval = -1;
699
637
  }
700
638
  
701
639
  /* Shutdown procedure */
702
640
  
703
641
 mandos_end:
704
642
  free(buffer);
705
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
706
 
  if(ret == -1){
707
 
    perror("close");
708
 
  }
 
643
  close(tcp_sd);
709
644
  gnutls_deinit (session);
710
645
  return retval;
711
646
}
725
660
                             flags,
726
661
                             void* userdata) {
727
662
  mandos_context *mc = userdata;
728
 
  assert(r);
 
663
  assert(r);                    /* Spurious warning */
729
664
  
730
665
  /* Called whenever a service has been resolved successfully or
731
666
     timed out */
749
684
      }
750
685
      int ret = start_mandos_communication(ip, port, interface, mc);
751
686
      if (ret == 0){
752
 
        avahi_simple_poll_quit(mc->simple_poll);
 
687
        exit(EXIT_SUCCESS);
753
688
      }
754
689
    }
755
690
  }
767
702
                             flags,
768
703
                             void* userdata) {
769
704
  mandos_context *mc = userdata;
770
 
  assert(b);
 
705
  assert(b);                    /* Spurious warning */
771
706
  
772
707
  /* Called whenever a new services becomes available on the LAN or
773
708
     is removed from the LAN */
807
742
  }
808
743
}
809
744
 
 
745
/* Combines file name and path and returns the malloced new
 
746
   string. some sane checks could/should be added */
 
747
static const char *combinepath(const char *first, const char *second){
 
748
  size_t f_len = strlen(first);
 
749
  size_t s_len = strlen(second);
 
750
  char *tmp = malloc(f_len + s_len + 2);
 
751
  if (tmp == NULL){
 
752
    return NULL;
 
753
  }
 
754
  if(f_len > 0){
 
755
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
756
  }
 
757
  tmp[f_len] = '/';
 
758
  if(s_len > 0){
 
759
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
760
  }
 
761
  tmp[f_len + 1 + s_len] = '\0';
 
762
  return tmp;
 
763
}
 
764
 
 
765
 
810
766
int main(int argc, char *argv[]){
811
767
    AvahiSServiceBrowser *sb = NULL;
812
768
    int error;
818
774
    uid_t uid;
819
775
    gid_t gid;
820
776
    char *connect_to = NULL;
821
 
    char tempdir[] = "/tmp/mandosXXXXXX";
822
777
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
823
 
    const char *seckey = PATHDIR "/" SECKEY;
824
 
    const char *pubkey = PATHDIR "/" PUBKEY;
825
 
    
 
778
    const char *pubkeyfile = "pubkey.txt";
 
779
    const char *seckeyfile = "seckey.txt";
826
780
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
827
 
                          .dh_bits = 1024, .priority = "SECURE256"
828
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
781
                          .dh_bits = 1024, .priority = "SECURE256"};
829
782
    bool gnutls_initalized = false;
830
 
    bool gpgme_initalized = false;
831
783
    
832
784
    {
833
785
      struct argp_option options[] = {
834
786
        { .name = "debug", .key = 128,
835
787
          .doc = "Debug mode", .group = 3 },
836
788
        { .name = "connect", .key = 'c',
837
 
          .arg = "ADDRESS:PORT",
838
 
          .doc = "Connect directly to a specific Mandos server",
 
789
          .arg = "IP",
 
790
          .doc = "Connect directly to a sepcified mandos server",
839
791
          .group = 1 },
840
792
        { .name = "interface", .key = 'i',
841
 
          .arg = "NAME",
842
 
          .doc = "Interface that will be used to search for Mandos"
843
 
          " servers",
 
793
          .arg = "INTERFACE",
 
794
          .doc = "Interface that Avahi will conntect through",
 
795
          .group = 1 },
 
796
        { .name = "keydir", .key = 'd',
 
797
          .arg = "KEYDIR",
 
798
          .doc = "Directory where the openpgp keyring is",
844
799
          .group = 1 },
845
800
        { .name = "seckey", .key = 's',
846
 
          .arg = "FILE",
847
 
          .doc = "OpenPGP secret key file base name",
 
801
          .arg = "SECKEY",
 
802
          .doc = "Secret openpgp key for gnutls authentication",
848
803
          .group = 1 },
849
804
        { .name = "pubkey", .key = 'p',
850
 
          .arg = "FILE",
851
 
          .doc = "OpenPGP public key file base name",
 
805
          .arg = "PUBKEY",
 
806
          .doc = "Public openpgp key for gnutls authentication",
852
807
          .group = 2 },
853
808
        { .name = "dh-bits", .key = 129,
854
809
          .arg = "BITS",
855
 
          .doc = "Bit length of the prime number used in the"
856
 
          " Diffie-Hellman key exchange",
 
810
          .doc = "dh-bits to use in gnutls communication",
857
811
          .group = 2 },
858
812
        { .name = "priority", .key = 130,
859
 
          .arg = "STRING",
860
 
          .doc = "GnuTLS priority string for the TLS handshake",
861
 
          .group = 1 },
 
813
          .arg = "PRIORITY",
 
814
          .doc = "GNUTLS priority", .group = 1 },
862
815
        { .name = NULL }
863
816
      };
 
817
 
864
818
      
865
819
      error_t parse_opt (int key, char *arg,
866
820
                         struct argp_state *state) {
867
821
        /* Get the INPUT argument from `argp_parse', which we know is
868
822
           a pointer to our plugin list pointer. */
869
823
        switch (key) {
870
 
        case 128:               /* --debug */
 
824
        case 128:
871
825
          debug = true;
872
826
          break;
873
 
        case 'c':               /* --connect */
 
827
        case 'c':
874
828
          connect_to = arg;
875
829
          break;
876
 
        case 'i':               /* --interface */
 
830
        case 'i':
877
831
          interface = arg;
878
832
          break;
879
 
        case 's':               /* --seckey */
880
 
          seckey = arg;
881
 
          break;
882
 
        case 'p':               /* --pubkey */
883
 
          pubkey = arg;
884
 
          break;
885
 
        case 129:               /* --dh-bits */
 
833
        case 'd':
 
834
          keydir = arg;
 
835
          break;
 
836
        case 's':
 
837
          seckeyfile = arg;
 
838
          break;
 
839
        case 'p':
 
840
          pubkeyfile = arg;
 
841
          break;
 
842
        case 129:
886
843
          errno = 0;
887
844
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
888
845
          if (errno){
890
847
            exit(EXIT_FAILURE);
891
848
          }
892
849
          break;
893
 
        case 130:               /* --priority */
 
850
        case 130:
894
851
          mc.priority = arg;
895
852
          break;
896
853
        case ARGP_KEY_ARG:
897
854
          argp_usage (state);
898
 
        case ARGP_KEY_END:
899
855
          break;
 
856
          case ARGP_KEY_END:
 
857
            break;
900
858
        default:
901
859
          return ARGP_ERR_UNKNOWN;
902
860
        }
903
861
        return 0;
904
862
      }
905
 
      
 
863
 
906
864
      struct argp argp = { .options = options, .parser = parse_opt,
907
865
                           .args_doc = "",
908
866
                           .doc = "Mandos client -- Get and decrypt"
909
 
                           " passwords from a Mandos server" };
 
867
                           " passwords from mandos server" };
910
868
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
869
      if (ret == ARGP_ERR_UNKNOWN){
912
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
913
 
        exitcode = EXIT_FAILURE;
914
 
        goto end;
915
 
      }
916
 
    }
917
 
    
918
 
    /* If the interface is down, bring it up */
919
 
    {
920
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
921
 
      if(sd < 0) {
922
 
        perror("socket");
923
 
        exitcode = EXIT_FAILURE;
924
 
        goto end;
925
 
      }
926
 
      strcpy(network.ifr_name, interface);
927
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
928
 
      if(ret == -1){
929
 
        perror("ioctl SIOCGIFFLAGS");
930
 
        exitcode = EXIT_FAILURE;
931
 
        goto end;
932
 
      }
933
 
      if((network.ifr_flags & IFF_UP) == 0){
934
 
        network.ifr_flags |= IFF_UP;
935
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
936
 
        if(ret == -1){
937
 
          perror("ioctl SIOCSIFFLAGS");
938
 
          exitcode = EXIT_FAILURE;
939
 
          goto end;
940
 
        }
941
 
      }
942
 
      ret = TEMP_FAILURE_RETRY(close(sd));
943
 
      if(ret == -1){
944
 
        perror("close");
945
 
      }
946
 
    }
947
 
    
 
870
        fprintf(stderr, "Unkown error while parsing arguments\n");
 
871
        exitcode = EXIT_FAILURE;
 
872
        goto end;
 
873
      }
 
874
    }
 
875
      
 
876
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
877
    if (pubkeyfile == NULL){
 
878
      perror("combinepath");
 
879
      exitcode = EXIT_FAILURE;
 
880
      goto end;
 
881
    }
 
882
    
 
883
    seckeyfile = combinepath(keydir, seckeyfile);
 
884
    if (seckeyfile == NULL){
 
885
      perror("combinepath");
 
886
      goto end;
 
887
    }
 
888
 
 
889
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
890
    if (ret == -1){
 
891
      fprintf(stderr, "init_gnutls_global\n");
 
892
      goto end;
 
893
    } else {
 
894
      gnutls_initalized = true;
 
895
    }
 
896
 
948
897
    uid = getuid();
949
898
    gid = getgid();
950
 
    
 
899
 
951
900
    ret = setuid(uid);
952
901
    if (ret == -1){
953
902
      perror("setuid");
958
907
      perror("setgid");
959
908
    }
960
909
    
961
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
962
 
    if (ret == -1){
963
 
      fprintf(stderr, "init_gnutls_global failed\n");
964
 
      exitcode = EXIT_FAILURE;
965
 
      goto end;
966
 
    } else {
967
 
      gnutls_initalized = true;
968
 
    }
969
 
    
970
 
    if(mkdtemp(tempdir) == NULL){
971
 
      perror("mkdtemp");
972
 
      tempdir[0] = '\0';
973
 
      goto end;
974
 
    }
975
 
    
976
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
977
 
      fprintf(stderr, "gpgme_initalized failed\n");
978
 
      exitcode = EXIT_FAILURE;
979
 
      goto end;
980
 
    } else {
981
 
      gpgme_initalized = true;
982
 
    }
983
 
    
984
910
    if_index = (AvahiIfIndex) if_nametoindex(interface);
985
911
    if(if_index == 0){
986
912
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1014
940
      goto end;
1015
941
    }
1016
942
    
 
943
    /* If the interface is down, bring it up */
 
944
    {
 
945
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
946
      if(sd < 0) {
 
947
        perror("socket");
 
948
        exitcode = EXIT_FAILURE;
 
949
        goto end;
 
950
      }
 
951
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
952
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
953
      if(ret == -1){
 
954
        perror("ioctl SIOCGIFFLAGS");
 
955
        exitcode = EXIT_FAILURE;
 
956
        goto end;
 
957
      }
 
958
      if((network.ifr_flags & IFF_UP) == 0){
 
959
        network.ifr_flags |= IFF_UP;
 
960
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
961
        if(ret == -1){
 
962
          perror("ioctl SIOCSIFFLAGS");
 
963
          exitcode = EXIT_FAILURE;
 
964
          goto end;
 
965
        }
 
966
      }
 
967
      close(sd);
 
968
    }
 
969
    
1017
970
    if (not debug){
1018
971
      avahi_set_log_function(empty_log);
1019
972
    }
1029
982
        exitcode = EXIT_FAILURE;
1030
983
        goto end;
1031
984
    }
1032
 
    
 
985
 
1033
986
    {
1034
987
      AvahiServerConfig config;
1035
988
      /* Do not publish any local Zeroconf records */
1038
991
      config.publish_addresses = 0;
1039
992
      config.publish_workstation = 0;
1040
993
      config.publish_domain = 0;
1041
 
      
 
994
 
1042
995
      /* Allocate a new server */
1043
996
      mc.server = avahi_server_new(avahi_simple_poll_get
1044
997
                                   (mc.simple_poll), &config, NULL,
1045
998
                                   NULL, &error);
1046
 
      
 
999
    
1047
1000
      /* Free the Avahi configuration data */
1048
1001
      avahi_server_config_free(&config);
1049
1002
    }
1069
1022
    }
1070
1023
    
1071
1024
    /* Run the main loop */
1072
 
    
 
1025
 
1073
1026
    if (debug){
1074
1027
      fprintf(stderr, "Starting Avahi loop search\n");
1075
1028
    }
1077
1030
    avahi_simple_poll_loop(mc.simple_poll);
1078
1031
    
1079
1032
 end:
1080
 
    
 
1033
 
1081
1034
    if (debug){
1082
1035
      fprintf(stderr, "%s exiting\n", argv[0]);
1083
1036
    }
1088
1041
    
1089
1042
    if (mc.server != NULL)
1090
1043
        avahi_server_free(mc.server);
1091
 
    
 
1044
 
1092
1045
    if (mc.simple_poll != NULL)
1093
1046
        avahi_simple_poll_free(mc.simple_poll);
1094
 
    
 
1047
    free(pubkeyfile);
 
1048
    free(seckeyfile);
 
1049
 
1095
1050
    if (gnutls_initalized){
1096
1051
      gnutls_certificate_free_credentials(mc.cred);
1097
1052
      gnutls_global_deinit ();
1098
 
      gnutls_dh_params_deinit(mc.dh_params);
1099
 
    }
1100
 
    
1101
 
    if(gpgme_initalized){
1102
 
      gpgme_release(mc.ctx);
1103
 
    }
1104
 
    
1105
 
    /* Removes the temp directory used by GPGME */
1106
 
    if(tempdir[0] != '\0'){
1107
 
      DIR *d;
1108
 
      struct dirent *direntry;
1109
 
      d = opendir(tempdir);
1110
 
      if(d == NULL){
1111
 
        perror("opendir");
1112
 
      } else {
1113
 
        while(true){
1114
 
          direntry = readdir(d);
1115
 
          if(direntry == NULL){
1116
 
            break;
1117
 
          }
1118
 
          if (direntry->d_type == DT_REG){
1119
 
            char *fullname = NULL;
1120
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1121
 
                           direntry->d_name);
1122
 
            if(ret < 0){
1123
 
              perror("asprintf");
1124
 
              continue;
1125
 
            }
1126
 
            ret = unlink(fullname);
1127
 
            if(ret == -1){
1128
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1129
 
                      fullname, strerror(errno));
1130
 
            }
1131
 
            free(fullname);
1132
 
          }
1133
 
        }
1134
 
        closedir(d);
1135
 
      }
1136
 
      ret = rmdir(tempdir);
1137
 
      if(ret == -1){
1138
 
        perror("rmdir");
1139
 
      }
1140
 
    }
1141
 
          
 
1053
    }
 
1054
    
1142
1055
    return exitcode;
1143
1056
}