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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-10 03:32:42 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080810033242-ea0qikoxqik0nibr
* mandos-client.c (main): Cast pid_t to unsigned int before printing.

* plugins.d/password-request.c (pgp_packet_decrypt): Use "%u" to print
                                                     wrong_key_usage.
  (start_mandos_communication): Use PRIu16 to print uint16_t.

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