/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:
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
 
/*
105
 
  #define PATHDIR "/conf/conf.d/mandos"
106
 
*/
107
 
 
108
 
#define PATHDIR "/conf/conf.d/mandos"
109
 
#define SECKEY "seckey.txt"
110
 
#define PUBKEY "pupkey.txt"
111
 
 
112
101
bool debug = false;
 
102
static const char *keydir = "/conf/conf.d/mandos";
113
103
static const char mandos_protocol_version[] = "1";
114
104
const char *argp_program_version = "password-request 1.0";
115
105
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
122
112
  unsigned int dh_bits;
123
113
  gnutls_dh_params_t dh_params;
124
114
  const char *priority;
125
 
  gpgme_ctx_t ctx;
126
115
} mandos_context;
127
116
 
128
117
/*
143
132
}
144
133
 
145
134
/* 
146
 
 * Initialize GPGME.
 
135
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
136
 * Returns -1 on error
147
137
 */
148
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
149
 
                       const char *pubkey, const char *tempdir){
150
 
  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;
151
144
  gpgme_error_t rc;
 
145
  ssize_t ret;
 
146
  size_t plaintext_capacity = 0;
 
147
  ssize_t plaintext_length = 0;
152
148
  gpgme_engine_info_t engine_info;
153
 
 
154
 
  
155
 
  /*
156
 
   * Helper function to insert pub and seckey to the enigne keyring.
157
 
   */
158
 
  bool import_key(const char *filename){
159
 
    int fd;
160
 
    gpgme_data_t pgp_data;
161
 
    
162
 
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
163
 
    if(fd == -1){
164
 
      perror("open");
165
 
      return false;
166
 
    }
167
 
    
168
 
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
169
 
    if (rc != GPG_ERR_NO_ERROR){
170
 
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
171
 
              gpgme_strsource(rc), gpgme_strerror(rc));
172
 
      return false;
173
 
    }
174
 
 
175
 
    rc = gpgme_op_import(mc->ctx, pgp_data);
176
 
    if (rc != GPG_ERR_NO_ERROR){
177
 
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
178
 
              gpgme_strsource(rc), gpgme_strerror(rc));
179
 
      return false;
180
 
    }
181
 
 
182
 
    ret = TEMP_FAILURE_RETRY(close(fd));
183
 
    if(ret == -1){
184
 
      perror("close");
185
 
    }
186
 
    gpgme_data_release(pgp_data);
187
 
    return true;
188
 
  }
189
149
  
190
150
  if (debug){
191
 
    fprintf(stderr, "Initialize gpgme\n");
 
151
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
192
152
  }
193
 
 
 
153
  
194
154
  /* Init GPGME */
195
155
  gpgme_check_version(NULL);
196
156
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
197
157
  if (rc != GPG_ERR_NO_ERROR){
198
158
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
159
            gpgme_strsource(rc), gpgme_strerror(rc));
200
 
    return false;
 
160
    return -1;
201
161
  }
202
 
 
203
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
162
  
 
163
  /* Set GPGME home directory for the OpenPGP engine only */
204
164
  rc = gpgme_get_engine_info (&engine_info);
205
165
  if (rc != GPG_ERR_NO_ERROR){
206
166
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
167
            gpgme_strsource(rc), gpgme_strerror(rc));
208
 
    return false;
 
168
    return -1;
209
169
  }
210
170
  while(engine_info != NULL){
211
171
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
212
172
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
213
 
                            engine_info->file_name, tempdir);
 
173
                            engine_info->file_name, homedir);
214
174
      break;
215
175
    }
216
176
    engine_info = engine_info->next;
217
177
  }
218
178
  if(engine_info == NULL){
219
 
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
220
 
    return false;
221
 
  }
222
 
 
223
 
  /* Create new GPGME "context" */
224
 
  rc = gpgme_new(&(mc->ctx));
225
 
  if (rc != GPG_ERR_NO_ERROR){
226
 
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
227
 
            gpgme_strsource(rc), gpgme_strerror(rc));
228
 
    return false;
229
 
  }
230
 
  
231
 
  if (not import_key(pubkey) or not import_key(seckey)){
232
 
    return false;
233
 
  }
234
 
  
235
 
  return true; 
236
 
}
237
 
 
238
 
/* 
239
 
 * Decrypt OpenPGP data.
240
 
 * Returns -1 on error
241
 
 */
242
 
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
243
 
                                   const char *cryptotext,
244
 
                                   size_t crypto_size,
245
 
                                   char **plaintext){
246
 
  gpgme_data_t dh_crypto, dh_plain;
247
 
  gpgme_error_t rc;
248
 
  ssize_t ret;
249
 
  size_t plaintext_capacity = 0;
250
 
  ssize_t plaintext_length = 0;
251
 
  
252
 
  if (debug){
253
 
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
 
179
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
180
    return -1;
254
181
  }
255
182
  
256
183
  /* Create new GPGME data buffer from memory cryptotext */
271
198
    return -1;
272
199
  }
273
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
  
274
210
  /* Decrypt data from the cryptotext data buffer to the plaintext
275
211
     data buffer */
276
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
212
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
277
213
  if (rc != GPG_ERR_NO_ERROR){
278
214
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
215
            gpgme_strsource(rc), gpgme_strerror(rc));
280
216
    plaintext_length = -1;
281
 
    if (debug){
282
 
      gpgme_decrypt_result_t result;
283
 
      result = gpgme_op_decrypt_result(mc->ctx);
284
 
      if (result == NULL){
285
 
        fprintf(stderr, "gpgme_op_decrypt_result failed\n");
286
 
      } else {
287
 
        fprintf(stderr, "Unsupported algorithm: %s\n",
288
 
                result->unsupported_algorithm);
289
 
        fprintf(stderr, "Wrong key usage: %u\n",
290
 
                result->wrong_key_usage);
291
 
        if(result->file_name != NULL){
292
 
          fprintf(stderr, "File name: %s\n", result->file_name);
293
 
        }
294
 
        gpgme_recipient_t recipient;
295
 
        recipient = result->recipients;
296
 
        if(recipient){
297
 
          while(recipient != NULL){
298
 
            fprintf(stderr, "Public key algorithm: %s\n",
299
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
300
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
301
 
            fprintf(stderr, "Secret key available: %s\n",
302
 
                    recipient->status == GPG_ERR_NO_SECKEY
303
 
                    ? "No" : "Yes");
304
 
            recipient = recipient->next;
305
 
          }
306
 
        }
307
 
      }
308
 
    }
309
217
    goto decrypt_end;
310
218
  }
311
219
  
313
221
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
314
222
  }
315
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
  
316
253
  /* Seek back to the beginning of the GPGME plaintext data buffer */
317
254
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
255
    perror("pgpme_data_seek");
345
282
    }
346
283
    plaintext_length += ret;
347
284
  }
348
 
  
 
285
 
349
286
  if(debug){
350
287
    fprintf(stderr, "Decrypted password is: ");
351
288
    for(ssize_t i = 0; i < plaintext_length; i++){
365
302
}
366
303
 
367
304
static const char * safer_gnutls_strerror (int value) {
368
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
305
  const char *ret = gnutls_strerror (value);
369
306
  if (ret == NULL)
370
307
    ret = "(unknown)";
371
308
  return ret;
378
315
}
379
316
 
380
317
static int init_gnutls_global(mandos_context *mc,
381
 
                              const char *pubkeyfilename,
382
 
                              const char *seckeyfilename){
 
318
                              const char *pubkeyfile,
 
319
                              const char *seckeyfile){
383
320
  int ret;
384
321
  
385
322
  if(debug){
404
341
  /* OpenPGP credentials */
405
342
  gnutls_certificate_allocate_credentials(&mc->cred);
406
343
  if (ret != GNUTLS_E_SUCCESS){
407
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
408
 
                                                     warning */
 
344
    fprintf (stderr, "GnuTLS memory error: %s\n",
409
345
             safer_gnutls_strerror(ret));
410
346
    gnutls_global_deinit ();
411
347
    return -1;
412
348
  }
413
349
  
414
350
  if(debug){
415
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
416
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
417
 
            seckeyfilename);
 
351
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
352
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
353
            seckeyfile);
418
354
  }
419
355
  
420
356
  ret = gnutls_certificate_set_openpgp_key_file
421
 
    (mc->cred, pubkeyfilename, seckeyfilename,
422
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
357
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
423
358
  if (ret != GNUTLS_E_SUCCESS) {
424
359
    fprintf(stderr,
425
360
            "Error[%d] while reading the OpenPGP key pair ('%s',"
426
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
427
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
361
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
362
    fprintf(stdout, "The GnuTLS error is: %s\n",
428
363
            safer_gnutls_strerror(ret));
429
364
    goto globalfail;
430
365
  }
444
379
  }
445
380
  
446
381
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
447
 
  
 
382
 
448
383
  return 0;
449
 
  
 
384
 
450
385
 globalfail:
451
 
  
 
386
 
452
387
  gnutls_certificate_free_credentials(mc->cred);
453
388
  gnutls_global_deinit();
454
389
  return -1;
 
390
 
455
391
}
456
392
 
457
393
static int init_gnutls_session(mandos_context *mc,
529
465
    perror("socket");
530
466
    return -1;
531
467
  }
532
 
  
 
468
 
533
469
  if(debug){
534
470
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
535
471
      perror("if_indextoname");
538
474
    fprintf(stderr, "Binding to interface %s\n", interface);
539
475
  }
540
476
  
541
 
  memset(&to, 0, sizeof(to));
 
477
  memset(&to,0,sizeof(to));     /* Spurious warning */
542
478
  to.in6.sin6_family = AF_INET6;
543
479
  /* It would be nice to have a way to detect if we were passed an
544
480
     IPv4 address here.   Now we assume an IPv6 address. */
551
487
    fprintf(stderr, "Bad address: %s\n", ip);
552
488
    return -1;
553
489
  }
554
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
490
  to.in6.sin6_port = htons(port);       /* Spurious warning */
555
491
  
556
492
  to.in6.sin6_scope_id = (uint32_t)if_index;
557
493
  
574
510
    perror("connect");
575
511
    return -1;
576
512
  }
577
 
  
 
513
 
578
514
  const char *out = mandos_protocol_version;
579
515
  written = 0;
580
516
  while (true){
598
534
      }
599
535
    }
600
536
  }
601
 
  
 
537
 
602
538
  if(debug){
603
539
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
604
540
  }
605
541
  
606
542
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
607
 
  
 
543
 
608
544
  do{
609
545
    ret = gnutls_handshake (session);
610
546
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
624
560
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
625
561
            ip);
626
562
  }
627
 
  
 
563
 
628
564
  while(true){
629
565
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
630
566
                                   buffer_capacity);
674
610
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
611
  
676
612
  if (buffer_length > 0){
677
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
613
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
678
614
                                               buffer_length,
679
 
                                               &decrypted_buffer);
 
615
                                               &decrypted_buffer,
 
616
                                               keydir);
680
617
    if (decrypted_buffer_size >= 0){
681
618
      written = 0;
682
619
      while(written < (size_t) decrypted_buffer_size){
697
634
    } else {
698
635
      retval = -1;
699
636
    }
700
 
  } else {
701
 
    retval = -1;
702
637
  }
703
638
  
704
639
  /* Shutdown procedure */
705
640
  
706
641
 mandos_end:
707
642
  free(buffer);
708
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
709
 
  if(ret == -1){
710
 
    perror("close");
711
 
  }
 
643
  close(tcp_sd);
712
644
  gnutls_deinit (session);
713
645
  return retval;
714
646
}
728
660
                             flags,
729
661
                             void* userdata) {
730
662
  mandos_context *mc = userdata;
731
 
  assert(r);
 
663
  assert(r);                    /* Spurious warning */
732
664
  
733
665
  /* Called whenever a service has been resolved successfully or
734
666
     timed out */
752
684
      }
753
685
      int ret = start_mandos_communication(ip, port, interface, mc);
754
686
      if (ret == 0){
755
 
        avahi_simple_poll_quit(mc->simple_poll);
 
687
        exit(EXIT_SUCCESS);
756
688
      }
757
689
    }
758
690
  }
770
702
                             flags,
771
703
                             void* userdata) {
772
704
  mandos_context *mc = userdata;
773
 
  assert(b);
 
705
  assert(b);                    /* Spurious warning */
774
706
  
775
707
  /* Called whenever a new services becomes available on the LAN or
776
708
     is removed from the LAN */
810
742
  }
811
743
}
812
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
 
813
766
int main(int argc, char *argv[]){
814
767
    AvahiSServiceBrowser *sb = NULL;
815
768
    int error;
821
774
    uid_t uid;
822
775
    gid_t gid;
823
776
    char *connect_to = NULL;
824
 
    char tempdir[] = "/tmp/mandosXXXXXX";
825
777
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
826
 
    const char *seckey = PATHDIR "/" SECKEY;
827
 
    const char *pubkey = PATHDIR "/" PUBKEY;
828
 
    
 
778
    const char *pubkeyfile = "pubkey.txt";
 
779
    const char *seckeyfile = "seckey.txt";
829
780
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
830
 
                          .dh_bits = 1024, .priority = "SECURE256"
831
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
781
                          .dh_bits = 1024, .priority = "SECURE256"};
832
782
    bool gnutls_initalized = false;
833
 
    bool pgpme_initalized = false;
834
783
    
835
784
    {
836
785
      struct argp_option options[] = {
837
786
        { .name = "debug", .key = 128,
838
787
          .doc = "Debug mode", .group = 3 },
839
788
        { .name = "connect", .key = 'c',
840
 
          .arg = "ADDRESS:PORT",
841
 
          .doc = "Connect directly to a specific Mandos server",
 
789
          .arg = "IP",
 
790
          .doc = "Connect directly to a sepcified mandos server",
842
791
          .group = 1 },
843
792
        { .name = "interface", .key = 'i',
844
 
          .arg = "NAME",
845
 
          .doc = "Interface that will be used to search for Mandos"
846
 
          " 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",
847
799
          .group = 1 },
848
800
        { .name = "seckey", .key = 's',
849
 
          .arg = "FILE",
850
 
          .doc = "OpenPGP secret key file base name",
 
801
          .arg = "SECKEY",
 
802
          .doc = "Secret openpgp key for gnutls authentication",
851
803
          .group = 1 },
852
804
        { .name = "pubkey", .key = 'p',
853
 
          .arg = "FILE",
854
 
          .doc = "OpenPGP public key file base name",
 
805
          .arg = "PUBKEY",
 
806
          .doc = "Public openpgp key for gnutls authentication",
855
807
          .group = 2 },
856
808
        { .name = "dh-bits", .key = 129,
857
809
          .arg = "BITS",
858
 
          .doc = "Bit length of the prime number used in the"
859
 
          " Diffie-Hellman key exchange",
 
810
          .doc = "dh-bits to use in gnutls communication",
860
811
          .group = 2 },
861
812
        { .name = "priority", .key = 130,
862
 
          .arg = "STRING",
863
 
          .doc = "GnuTLS priority string for the TLS handshake",
864
 
          .group = 1 },
 
813
          .arg = "PRIORITY",
 
814
          .doc = "GNUTLS priority", .group = 1 },
865
815
        { .name = NULL }
866
816
      };
 
817
 
867
818
      
868
819
      error_t parse_opt (int key, char *arg,
869
820
                         struct argp_state *state) {
870
821
        /* Get the INPUT argument from `argp_parse', which we know is
871
822
           a pointer to our plugin list pointer. */
872
823
        switch (key) {
873
 
        case 128:               /* --debug */
 
824
        case 128:
874
825
          debug = true;
875
826
          break;
876
 
        case 'c':               /* --connect */
 
827
        case 'c':
877
828
          connect_to = arg;
878
829
          break;
879
 
        case 'i':               /* --interface */
 
830
        case 'i':
880
831
          interface = arg;
881
832
          break;
882
 
        case 's':               /* --seckey */
883
 
          seckey = arg;
884
 
          break;
885
 
        case 'p':               /* --pubkey */
886
 
          pubkey = arg;
887
 
          break;
888
 
        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:
889
843
          errno = 0;
890
844
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
891
845
          if (errno){
893
847
            exit(EXIT_FAILURE);
894
848
          }
895
849
          break;
896
 
        case 130:               /* --priority */
 
850
        case 130:
897
851
          mc.priority = arg;
898
852
          break;
899
853
        case ARGP_KEY_ARG:
900
854
          argp_usage (state);
901
 
        case ARGP_KEY_END:
902
855
          break;
 
856
          case ARGP_KEY_END:
 
857
            break;
903
858
        default:
904
859
          return ARGP_ERR_UNKNOWN;
905
860
        }
906
861
        return 0;
907
862
      }
908
 
      
 
863
 
909
864
      struct argp argp = { .options = options, .parser = parse_opt,
910
865
                           .args_doc = "",
911
866
                           .doc = "Mandos client -- Get and decrypt"
912
 
                           " passwords from a Mandos server" };
 
867
                           " passwords from mandos server" };
913
868
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
914
869
      if (ret == ARGP_ERR_UNKNOWN){
915
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
870
        fprintf(stderr, "Unkown error while parsing arguments\n");
916
871
        exitcode = EXIT_FAILURE;
917
872
        goto end;
918
873
      }
919
874
    }
 
875
      
 
876
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
877
    if (pubkeyfile == NULL){
 
878
      perror("combinepath");
 
879
      exitcode = EXIT_FAILURE;
 
880
      goto end;
 
881
    }
920
882
    
921
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
 
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);
922
890
    if (ret == -1){
923
 
      fprintf(stderr, "init_gnutls_global failed\n");
924
 
      exitcode = EXIT_FAILURE;
 
891
      fprintf(stderr, "init_gnutls_global\n");
925
892
      goto end;
926
893
    } else {
927
894
      gnutls_initalized = true;
928
895
    }
929
896
 
930
 
    if(mkdtemp(tempdir) == NULL){
931
 
      perror("mkdtemp");
932
 
      tempdir[0] = '\0';
933
 
      goto end;
934
 
    }
935
 
    
936
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
937
 
      fprintf(stderr, "pgpme_initalized failed\n");
938
 
      exitcode = EXIT_FAILURE;
939
 
      goto end;
940
 
    } else {
941
 
      pgpme_initalized = true;
942
 
    }
943
 
    
944
 
    /* If the interface is down, bring it up */
945
 
    {
946
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
947
 
      if(sd < 0) {
948
 
        perror("socket");
949
 
        exitcode = EXIT_FAILURE;
950
 
        goto end;
951
 
      }
952
 
      strcpy(network.ifr_name, interface);
953
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
954
 
      if(ret == -1){
955
 
        perror("ioctl SIOCGIFFLAGS");
956
 
        exitcode = EXIT_FAILURE;
957
 
        goto end;
958
 
      }
959
 
      if((network.ifr_flags & IFF_UP) == 0){
960
 
        network.ifr_flags |= IFF_UP;
961
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
962
 
        if(ret == -1){
963
 
          perror("ioctl SIOCSIFFLAGS");
964
 
          exitcode = EXIT_FAILURE;
965
 
          goto end;
966
 
        }
967
 
      }
968
 
      ret = TEMP_FAILURE_RETRY(close(sd));
969
 
      if(ret == -1){
970
 
        perror("close");
971
 
      }
972
 
    }
973
 
    
974
897
    uid = getuid();
975
898
    gid = getgid();
976
 
    
 
899
 
977
900
    ret = setuid(uid);
978
901
    if (ret == -1){
979
902
      perror("setuid");
1017
940
      goto end;
1018
941
    }
1019
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
    
1020
970
    if (not debug){
1021
971
      avahi_set_log_function(empty_log);
1022
972
    }
1032
982
        exitcode = EXIT_FAILURE;
1033
983
        goto end;
1034
984
    }
1035
 
    
 
985
 
1036
986
    {
1037
987
      AvahiServerConfig config;
1038
988
      /* Do not publish any local Zeroconf records */
1041
991
      config.publish_addresses = 0;
1042
992
      config.publish_workstation = 0;
1043
993
      config.publish_domain = 0;
1044
 
      
 
994
 
1045
995
      /* Allocate a new server */
1046
996
      mc.server = avahi_server_new(avahi_simple_poll_get
1047
997
                                   (mc.simple_poll), &config, NULL,
1048
998
                                   NULL, &error);
1049
 
      
 
999
    
1050
1000
      /* Free the Avahi configuration data */
1051
1001
      avahi_server_config_free(&config);
1052
1002
    }
1072
1022
    }
1073
1023
    
1074
1024
    /* Run the main loop */
1075
 
    
 
1025
 
1076
1026
    if (debug){
1077
1027
      fprintf(stderr, "Starting Avahi loop search\n");
1078
1028
    }
1080
1030
    avahi_simple_poll_loop(mc.simple_poll);
1081
1031
    
1082
1032
 end:
1083
 
    
 
1033
 
1084
1034
    if (debug){
1085
1035
      fprintf(stderr, "%s exiting\n", argv[0]);
1086
1036
    }
1091
1041
    
1092
1042
    if (mc.server != NULL)
1093
1043
        avahi_server_free(mc.server);
1094
 
    
 
1044
 
1095
1045
    if (mc.simple_poll != NULL)
1096
1046
        avahi_simple_poll_free(mc.simple_poll);
1097
 
    
 
1047
    free(pubkeyfile);
 
1048
    free(seckeyfile);
 
1049
 
1098
1050
    if (gnutls_initalized){
1099
1051
      gnutls_certificate_free_credentials(mc.cred);
1100
1052
      gnutls_global_deinit ();
1101
1053
    }
1102
 
 
1103
 
    if(pgpme_initalized){
1104
 
      gpgme_release(mc.ctx);
1105
 
    }
1106
 
 
1107
 
    /* Removes the temp directory used by GPGME */
1108
 
    if(tempdir[0] != '\0'){
1109
 
      DIR *d;
1110
 
      struct dirent *direntry;
1111
 
      d = opendir(tempdir);
1112
 
      if(d == NULL){
1113
 
        perror("opendir");
1114
 
      } else {
1115
 
        while(true){
1116
 
          direntry = readdir(d);
1117
 
          if(direntry == NULL){
1118
 
            break;
1119
 
          }
1120
 
          if (direntry->d_type == DT_REG){
1121
 
            char *fullname = NULL;
1122
 
            ret = asprintf(&fullname, "%s/%s", tempdir,
1123
 
                           direntry->d_name);
1124
 
            if(ret < 0){
1125
 
              perror("asprintf");
1126
 
              continue;
1127
 
            }
1128
 
            ret = unlink(fullname);
1129
 
            if(ret == -1){
1130
 
              fprintf(stderr, "unlink(\"%s\"): %s",
1131
 
                      fullname, strerror(errno));
1132
 
            }
1133
 
            free(fullname);
1134
 
          }
1135
 
        }
1136
 
      }
1137
 
      ret = rmdir(tempdir);
1138
 
      if(ret == -1){
1139
 
        perror("rmdir");
1140
 
      }
1141
 
    }
1142
 
          
 
1054
    
1143
1055
    return exitcode;
1144
1056
}