/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

Added more header file comments
fixed a small perror bug

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 */
52
53
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
54
                                   struct in6_addr, inet_pton(),
54
55
                                   connect() */
55
 
#include <fcntl.h>              /* open() */
56
 
#include <dirent.h>             /* opendir(), struct dirent, readdir() */
57
 
#include <inttypes.h>           /* PRIu16 */
58
56
#include <assert.h>             /* assert() */
59
57
#include <errno.h>              /* perror(), errno */
60
58
#include <time.h>               /* time() */
61
59
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
62
60
                                   SIOCSIFFLAGS, if_indextoname(),
63
61
                                   if_nametoindex(), IF_NAMESIZE */
64
 
#include <netinet/in.h>
65
62
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
66
63
                                   getuid(), getgid(), setuid(),
67
64
                                   setgid() */
 
65
#include <netinet/in.h>
68
66
#include <arpa/inet.h>          /* inet_pton(), htons */
69
67
#include <iso646.h>             /* not, and */
70
68
#include <argp.h>               /* struct argp_option, error_t, struct
84
82
#include <avahi-common/error.h>
85
83
 
86
84
/* GnuTLS */
87
 
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and
88
 
                                   functions:
 
85
#include <gnutls/gnutls.h>      /* All GnuTLS types, constants and functions
89
86
                                   gnutls_*
90
87
                                   init_gnutls_session(),
91
88
                                   GNUTLS_* */
93
90
                                   GNUTLS_OPENPGP_FMT_BASE64 */
94
91
 
95
92
/* GPGME */
96
 
#include <gpgme.h>              /* All GPGME types, constants and
97
 
                                   functions:
 
93
#include <gpgme.h>              /* All GPGME types, constants and functions
98
94
                                   gpgme_*
99
95
                                   GPGME_PROTOCOL_OpenPGP,
100
96
                                   GPG_ERR_NO_* */
101
97
 
102
98
#define BUFFER_SIZE 256
103
99
 
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 "pubkey.txt"
111
 
 
112
100
bool debug = false;
 
101
static const char *keydir = "/conf/conf.d/mandos";
113
102
static const char mandos_protocol_version[] = "1";
114
 
const char *argp_program_version = "mandos-client 1.0";
 
103
const char *argp_program_version = "mandosclient 0.9";
115
104
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
116
105
 
117
106
/* Used for passing in values through the Avahi callback functions */
122
111
  unsigned int dh_bits;
123
112
  gnutls_dh_params_t dh_params;
124
113
  const char *priority;
125
 
  gpgme_ctx_t ctx;
126
114
} mandos_context;
127
115
 
128
116
/*
143
131
}
144
132
 
145
133
/* 
146
 
 * Initialize GPGME.
 
134
 * Decrypt OpenPGP data using keyrings in HOMEDIR.
 
135
 * Returns -1 on error
147
136
 */
148
 
static bool init_gpgme(mandos_context *mc, const char *seckey,
149
 
                       const char *pubkey, const char *tempdir){
150
 
  int ret;
 
137
static ssize_t pgp_packet_decrypt (const char *cryptotext,
 
138
                                   size_t crypto_size,
 
139
                                   char **plaintext,
 
140
                                   const char *homedir){
 
141
  gpgme_data_t dh_crypto, dh_plain;
 
142
  gpgme_ctx_t ctx;
151
143
  gpgme_error_t rc;
 
144
  ssize_t ret;
 
145
  size_t plaintext_capacity = 0;
 
146
  ssize_t plaintext_length = 0;
152
147
  gpgme_engine_info_t engine_info;
153
148
  
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
 
  
190
149
  if (debug){
191
 
    fprintf(stderr, "Initialize gpgme\n");
 
150
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
192
151
  }
193
152
  
194
153
  /* Init GPGME */
197
156
  if (rc != GPG_ERR_NO_ERROR){
198
157
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
199
158
            gpgme_strsource(rc), gpgme_strerror(rc));
200
 
    return false;
 
159
    return -1;
201
160
  }
202
161
  
203
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
162
  /* Set GPGME home directory for the OpenPGP engine only */
204
163
  rc = gpgme_get_engine_info (&engine_info);
205
164
  if (rc != GPG_ERR_NO_ERROR){
206
165
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
207
166
            gpgme_strsource(rc), gpgme_strerror(rc));
208
 
    return false;
 
167
    return -1;
209
168
  }
210
169
  while(engine_info != NULL){
211
170
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
212
171
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
213
 
                            engine_info->file_name, tempdir);
 
172
                            engine_info->file_name, homedir);
214
173
      break;
215
174
    }
216
175
    engine_info = engine_info->next;
217
176
  }
218
177
  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");
 
178
    fprintf(stderr, "Could not set GPGME home dir to %s\n", homedir);
 
179
    return -1;
254
180
  }
255
181
  
256
182
  /* Create new GPGME data buffer from memory cryptotext */
271
197
    return -1;
272
198
  }
273
199
  
 
200
  /* Create new GPGME "context" */
 
201
  rc = gpgme_new(&ctx);
 
202
  if (rc != GPG_ERR_NO_ERROR){
 
203
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
 
204
            gpgme_strsource(rc), gpgme_strerror(rc));
 
205
    plaintext_length = -1;
 
206
    goto decrypt_end;
 
207
  }
 
208
  
274
209
  /* Decrypt data from the cryptotext data buffer to the plaintext
275
210
     data buffer */
276
 
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
 
211
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
277
212
  if (rc != GPG_ERR_NO_ERROR){
278
213
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
279
214
            gpgme_strsource(rc), gpgme_strerror(rc));
280
215
    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
216
    goto decrypt_end;
310
217
  }
311
218
  
313
220
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
314
221
  }
315
222
  
 
223
  if (debug){
 
224
    gpgme_decrypt_result_t result;
 
225
    result = gpgme_op_decrypt_result(ctx);
 
226
    if (result == NULL){
 
227
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
 
228
    } else {
 
229
      fprintf(stderr, "Unsupported algorithm: %s\n",
 
230
              result->unsupported_algorithm);
 
231
      fprintf(stderr, "Wrong key usage: %d\n",
 
232
              result->wrong_key_usage);
 
233
      if(result->file_name != NULL){
 
234
        fprintf(stderr, "File name: %s\n", result->file_name);
 
235
      }
 
236
      gpgme_recipient_t recipient;
 
237
      recipient = result->recipients;
 
238
      if(recipient){
 
239
        while(recipient != NULL){
 
240
          fprintf(stderr, "Public key algorithm: %s\n",
 
241
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
242
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
243
          fprintf(stderr, "Secret key available: %s\n",
 
244
                  recipient->status == GPG_ERR_NO_SECKEY
 
245
                  ? "No" : "Yes");
 
246
          recipient = recipient->next;
 
247
        }
 
248
      }
 
249
    }
 
250
  }
 
251
  
316
252
  /* Seek back to the beginning of the GPGME plaintext data buffer */
317
253
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
318
254
    perror("pgpme_data_seek");
345
281
    }
346
282
    plaintext_length += ret;
347
283
  }
348
 
  
 
284
 
349
285
  if(debug){
350
286
    fprintf(stderr, "Decrypted password is: ");
351
287
    for(ssize_t i = 0; i < plaintext_length; i++){
365
301
}
366
302
 
367
303
static const char * safer_gnutls_strerror (int value) {
368
 
  const char *ret = gnutls_strerror (value); /* Spurious warning */
 
304
  const char *ret = gnutls_strerror (value);
369
305
  if (ret == NULL)
370
306
    ret = "(unknown)";
371
307
  return ret;
378
314
}
379
315
 
380
316
static int init_gnutls_global(mandos_context *mc,
381
 
                              const char *pubkeyfilename,
382
 
                              const char *seckeyfilename){
 
317
                              const char *pubkeyfile,
 
318
                              const char *seckeyfile){
383
319
  int ret;
384
320
  
385
321
  if(debug){
404
340
  /* OpenPGP credentials */
405
341
  gnutls_certificate_allocate_credentials(&mc->cred);
406
342
  if (ret != GNUTLS_E_SUCCESS){
407
 
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
408
 
                                                     warning */
 
343
    fprintf (stderr, "GnuTLS memory error: %s\n",
409
344
             safer_gnutls_strerror(ret));
410
345
    gnutls_global_deinit ();
411
346
    return -1;
412
347
  }
413
348
  
414
349
  if(debug){
415
 
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
416
 
            " secret key %s as GnuTLS credentials\n", pubkeyfilename,
417
 
            seckeyfilename);
 
350
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
 
351
            " and keyfile %s as GnuTLS credentials\n", pubkeyfile,
 
352
            seckeyfile);
418
353
  }
419
354
  
420
355
  ret = gnutls_certificate_set_openpgp_key_file
421
 
    (mc->cred, pubkeyfilename, seckeyfilename,
422
 
     GNUTLS_OPENPGP_FMT_BASE64);
 
356
    (mc->cred, pubkeyfile, seckeyfile, GNUTLS_OPENPGP_FMT_BASE64);
423
357
  if (ret != GNUTLS_E_SUCCESS) {
424
358
    fprintf(stderr,
425
359
            "Error[%d] while reading the OpenPGP key pair ('%s',"
426
 
            " '%s')\n", ret, pubkeyfilename, seckeyfilename);
427
 
    fprintf(stderr, "The GnuTLS error is: %s\n",
 
360
            " '%s')\n", ret, pubkeyfile, seckeyfile);
 
361
    fprintf(stdout, "The GnuTLS error is: %s\n",
428
362
            safer_gnutls_strerror(ret));
429
363
    goto globalfail;
430
364
  }
444
378
  }
445
379
  
446
380
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
447
 
  
 
381
 
448
382
  return 0;
449
 
  
 
383
 
450
384
 globalfail:
451
 
  
 
385
 
452
386
  gnutls_certificate_free_credentials(mc->cred);
453
387
  gnutls_global_deinit();
454
388
  return -1;
 
389
 
455
390
}
456
391
 
457
392
static int init_gnutls_session(mandos_context *mc,
520
455
  }
521
456
  
522
457
  if(debug){
523
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
524
 
            "\n", ip, port);
 
458
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
459
            ip, port);
525
460
  }
526
461
  
527
462
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
529
464
    perror("socket");
530
465
    return -1;
531
466
  }
532
 
  
 
467
 
533
468
  if(debug){
534
469
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
535
470
      perror("if_indextoname");
538
473
    fprintf(stderr, "Binding to interface %s\n", interface);
539
474
  }
540
475
  
541
 
  memset(&to, 0, sizeof(to));
 
476
  memset(&to,0,sizeof(to));     /* Spurious warning */
542
477
  to.in6.sin6_family = AF_INET6;
543
478
  /* It would be nice to have a way to detect if we were passed an
544
479
     IPv4 address here.   Now we assume an IPv6 address. */
551
486
    fprintf(stderr, "Bad address: %s\n", ip);
552
487
    return -1;
553
488
  }
554
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
489
  to.in6.sin6_port = htons(port);       /* Spurious warning */
555
490
  
556
491
  to.in6.sin6_scope_id = (uint32_t)if_index;
557
492
  
558
493
  if(debug){
559
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
560
 
            port);
 
494
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
561
495
    char addrstr[INET6_ADDRSTRLEN] = "";
562
496
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
563
497
                 sizeof(addrstr)) == NULL){
574
508
    perror("connect");
575
509
    return -1;
576
510
  }
577
 
  
 
511
 
578
512
  const char *out = mandos_protocol_version;
579
513
  written = 0;
580
514
  while (true){
598
532
      }
599
533
    }
600
534
  }
601
 
  
 
535
 
602
536
  if(debug){
603
537
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
604
538
  }
605
539
  
606
540
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
607
 
  
 
541
 
608
542
  do{
609
543
    ret = gnutls_handshake (session);
610
544
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
624
558
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
625
559
            ip);
626
560
  }
627
 
  
 
561
 
628
562
  while(true){
629
563
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
630
564
                                   buffer_capacity);
674
608
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
675
609
  
676
610
  if (buffer_length > 0){
677
 
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
 
611
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
678
612
                                               buffer_length,
679
 
                                               &decrypted_buffer);
 
613
                                               &decrypted_buffer,
 
614
                                               keydir);
680
615
    if (decrypted_buffer_size >= 0){
681
616
      written = 0;
682
617
      while(written < (size_t) decrypted_buffer_size){
697
632
    } else {
698
633
      retval = -1;
699
634
    }
700
 
  } else {
701
 
    retval = -1;
702
635
  }
703
636
  
704
637
  /* Shutdown procedure */
705
638
  
706
639
 mandos_end:
707
640
  free(buffer);
708
 
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
709
 
  if(ret == -1){
710
 
    perror("close");
711
 
  }
 
641
  close(tcp_sd);
712
642
  gnutls_deinit (session);
713
643
  return retval;
714
644
}
728
658
                             flags,
729
659
                             void* userdata) {
730
660
  mandos_context *mc = userdata;
731
 
  assert(r);
 
661
  assert(r);                    /* Spurious warning */
732
662
  
733
663
  /* Called whenever a service has been resolved successfully or
734
664
     timed out */
746
676
      char ip[AVAHI_ADDRESS_STR_MAX];
747
677
      avahi_address_snprint(ip, sizeof(ip), address);
748
678
      if(debug){
749
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
750
 
                PRIu16 ") on port %d\n", name, host_name, ip,
751
 
                interface, port);
 
679
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
680
                " port %d\n", name, host_name, ip, interface, port);
752
681
      }
753
682
      int ret = start_mandos_communication(ip, port, interface, mc);
754
683
      if (ret == 0){
755
 
        avahi_simple_poll_quit(mc->simple_poll);
 
684
        exit(EXIT_SUCCESS);
756
685
      }
757
686
    }
758
687
  }
770
699
                             flags,
771
700
                             void* userdata) {
772
701
  mandos_context *mc = userdata;
773
 
  assert(b);
 
702
  assert(b);                    /* Spurious warning */
774
703
  
775
704
  /* Called whenever a new services becomes available on the LAN or
776
705
     is removed from the LAN */
810
739
  }
811
740
}
812
741
 
 
742
/* Combines file name and path and returns the malloced new
 
743
   string. some sane checks could/should be added */
 
744
static const char *combinepath(const char *first, const char *second){
 
745
  size_t f_len = strlen(first);
 
746
  size_t s_len = strlen(second);
 
747
  char *tmp = malloc(f_len + s_len + 2);
 
748
  if (tmp == NULL){
 
749
    return NULL;
 
750
  }
 
751
  if(f_len > 0){
 
752
    memcpy(tmp, first, f_len);  /* Spurious warning */
 
753
  }
 
754
  tmp[f_len] = '/';
 
755
  if(s_len > 0){
 
756
    memcpy(tmp + f_len + 1, second, s_len); /* Spurious warning */
 
757
  }
 
758
  tmp[f_len + 1 + s_len] = '\0';
 
759
  return tmp;
 
760
}
 
761
 
 
762
 
813
763
int main(int argc, char *argv[]){
814
764
    AvahiSServiceBrowser *sb = NULL;
815
765
    int error;
821
771
    uid_t uid;
822
772
    gid_t gid;
823
773
    char *connect_to = NULL;
824
 
    char tempdir[] = "/tmp/mandosXXXXXX";
825
774
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
826
 
    const char *seckey = PATHDIR "/" SECKEY;
827
 
    const char *pubkey = PATHDIR "/" PUBKEY;
828
 
    
 
775
    const char *pubkeyfile = "pubkey.txt";
 
776
    const char *seckeyfile = "seckey.txt";
829
777
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
830
 
                          .dh_bits = 1024, .priority = "SECURE256"
831
 
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
 
778
                          .dh_bits = 1024, .priority = "SECURE256"};
832
779
    bool gnutls_initalized = false;
833
 
    bool pgpme_initalized = false;
834
780
    
835
781
    {
836
782
      struct argp_option options[] = {
837
783
        { .name = "debug", .key = 128,
838
784
          .doc = "Debug mode", .group = 3 },
839
785
        { .name = "connect", .key = 'c',
840
 
          .arg = "ADDRESS:PORT",
841
 
          .doc = "Connect directly to a specific Mandos server",
 
786
          .arg = "IP",
 
787
          .doc = "Connect directly to a sepcified mandos server",
842
788
          .group = 1 },
843
789
        { .name = "interface", .key = 'i',
844
 
          .arg = "NAME",
845
 
          .doc = "Interface that will be used to search for Mandos"
846
 
          " servers",
 
790
          .arg = "INTERFACE",
 
791
          .doc = "Interface that Avahi will conntect through",
 
792
          .group = 1 },
 
793
        { .name = "keydir", .key = 'd',
 
794
          .arg = "KEYDIR",
 
795
          .doc = "Directory where the openpgp keyring is",
847
796
          .group = 1 },
848
797
        { .name = "seckey", .key = 's',
849
 
          .arg = "FILE",
850
 
          .doc = "OpenPGP secret key file base name",
 
798
          .arg = "SECKEY",
 
799
          .doc = "Secret openpgp key for gnutls authentication",
851
800
          .group = 1 },
852
801
        { .name = "pubkey", .key = 'p',
853
 
          .arg = "FILE",
854
 
          .doc = "OpenPGP public key file base name",
 
802
          .arg = "PUBKEY",
 
803
          .doc = "Public openpgp key for gnutls authentication",
855
804
          .group = 2 },
856
805
        { .name = "dh-bits", .key = 129,
857
806
          .arg = "BITS",
858
 
          .doc = "Bit length of the prime number used in the"
859
 
          " Diffie-Hellman key exchange",
 
807
          .doc = "dh-bits to use in gnutls communication",
860
808
          .group = 2 },
861
809
        { .name = "priority", .key = 130,
862
 
          .arg = "STRING",
863
 
          .doc = "GnuTLS priority string for the TLS handshake",
864
 
          .group = 1 },
 
810
          .arg = "PRIORITY",
 
811
          .doc = "GNUTLS priority", .group = 1 },
865
812
        { .name = NULL }
866
813
      };
 
814
 
867
815
      
868
816
      error_t parse_opt (int key, char *arg,
869
817
                         struct argp_state *state) {
870
818
        /* Get the INPUT argument from `argp_parse', which we know is
871
819
           a pointer to our plugin list pointer. */
872
820
        switch (key) {
873
 
        case 128:               /* --debug */
 
821
        case 128:
874
822
          debug = true;
875
823
          break;
876
 
        case 'c':               /* --connect */
 
824
        case 'c':
877
825
          connect_to = arg;
878
826
          break;
879
 
        case 'i':               /* --interface */
 
827
        case 'i':
880
828
          interface = arg;
881
829
          break;
882
 
        case 's':               /* --seckey */
883
 
          seckey = arg;
884
 
          break;
885
 
        case 'p':               /* --pubkey */
886
 
          pubkey = arg;
887
 
          break;
888
 
        case 129:               /* --dh-bits */
 
830
        case 'd':
 
831
          keydir = arg;
 
832
          break;
 
833
        case 's':
 
834
          seckeyfile = arg;
 
835
          break;
 
836
        case 'p':
 
837
          pubkeyfile = arg;
 
838
          break;
 
839
        case 129:
889
840
          errno = 0;
890
841
          mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
891
842
          if (errno){
893
844
            exit(EXIT_FAILURE);
894
845
          }
895
846
          break;
896
 
        case 130:               /* --priority */
 
847
        case 130:
897
848
          mc.priority = arg;
898
849
          break;
899
850
        case ARGP_KEY_ARG:
900
851
          argp_usage (state);
901
 
        case ARGP_KEY_END:
902
852
          break;
 
853
          case ARGP_KEY_END:
 
854
            break;
903
855
        default:
904
856
          return ARGP_ERR_UNKNOWN;
905
857
        }
906
858
        return 0;
907
859
      }
908
 
      
 
860
 
909
861
      struct argp argp = { .options = options, .parser = parse_opt,
910
862
                           .args_doc = "",
911
863
                           .doc = "Mandos client -- Get and decrypt"
912
 
                           " passwords from a Mandos server" };
 
864
                           " passwords from mandos server" };
913
865
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
914
866
      if (ret == ARGP_ERR_UNKNOWN){
915
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
916
 
        exitcode = EXIT_FAILURE;
917
 
        goto end;
918
 
      }
919
 
    }
920
 
    
921
 
    /* If the interface is down, bring it up */
922
 
    {
923
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
924
 
      if(sd < 0) {
925
 
        perror("socket");
926
 
        exitcode = EXIT_FAILURE;
927
 
        goto end;
928
 
      }
929
 
      strcpy(network.ifr_name, interface);
930
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
931
 
      if(ret == -1){
932
 
        perror("ioctl SIOCGIFFLAGS");
933
 
        exitcode = EXIT_FAILURE;
934
 
        goto end;
935
 
      }
936
 
      if((network.ifr_flags & IFF_UP) == 0){
937
 
        network.ifr_flags |= IFF_UP;
938
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
939
 
        if(ret == -1){
940
 
          perror("ioctl SIOCSIFFLAGS");
941
 
          exitcode = EXIT_FAILURE;
942
 
          goto end;
943
 
        }
944
 
      }
945
 
      ret = TEMP_FAILURE_RETRY(close(sd));
946
 
      if(ret == -1){
947
 
        perror("close");
948
 
      }
949
 
    }
950
 
    
 
867
        fprintf(stderr, "Unkown error while parsing arguments\n");
 
868
        exitcode = EXIT_FAILURE;
 
869
        goto end;
 
870
      }
 
871
    }
 
872
      
 
873
    pubkeyfile = combinepath(keydir, pubkeyfile);
 
874
    if (pubkeyfile == NULL){
 
875
      perror("combinepath");
 
876
      exitcode = EXIT_FAILURE;
 
877
      goto end;
 
878
    }
 
879
    
 
880
    seckeyfile = combinepath(keydir, seckeyfile);
 
881
    if (seckeyfile == NULL){
 
882
      perror("combinepath");
 
883
      goto end;
 
884
    }
 
885
 
 
886
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
 
887
    if (ret == -1){
 
888
      fprintf(stderr, "init_gnutls_global\n");
 
889
      goto end;
 
890
    } else {
 
891
      gnutls_initalized = true;
 
892
    }
 
893
 
951
894
    uid = getuid();
952
895
    gid = getgid();
953
 
    
 
896
 
954
897
    ret = setuid(uid);
955
898
    if (ret == -1){
956
899
      perror("setuid");
961
904
      perror("setgid");
962
905
    }
963
906
    
964
 
    ret = init_gnutls_global(&mc, pubkey, seckey);
965
 
    if (ret == -1){
966
 
      fprintf(stderr, "init_gnutls_global failed\n");
967
 
      exitcode = EXIT_FAILURE;
968
 
      goto end;
969
 
    } else {
970
 
      gnutls_initalized = true;
971
 
    }
972
 
    
973
 
    if(mkdtemp(tempdir) == NULL){
974
 
      perror("mkdtemp");
975
 
      tempdir[0] = '\0';
976
 
      goto end;
977
 
    }
978
 
    
979
 
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
980
 
      fprintf(stderr, "pgpme_initalized failed\n");
981
 
      exitcode = EXIT_FAILURE;
982
 
      goto end;
983
 
    } else {
984
 
      pgpme_initalized = true;
985
 
    }
986
 
    
987
907
    if_index = (AvahiIfIndex) if_nametoindex(interface);
988
908
    if(if_index == 0){
989
909
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1017
937
      goto end;
1018
938
    }
1019
939
    
 
940
    /* If the interface is down, bring it up */
 
941
    {
 
942
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
943
      if(sd < 0) {
 
944
        perror("socket");
 
945
        exitcode = EXIT_FAILURE;
 
946
        goto end;
 
947
      }
 
948
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
949
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
950
      if(ret == -1){
 
951
        perror("ioctl SIOCGIFFLAGS");
 
952
        exitcode = EXIT_FAILURE;
 
953
        goto end;
 
954
      }
 
955
      if((network.ifr_flags & IFF_UP) == 0){
 
956
        network.ifr_flags |= IFF_UP;
 
957
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
958
        if(ret == -1){
 
959
          perror("ioctl SIOCSIFFLAGS");
 
960
          exitcode = EXIT_FAILURE;
 
961
          goto end;
 
962
        }
 
963
      }
 
964
      close(sd);
 
965
    }
 
966
    
1020
967
    if (not debug){
1021
968
      avahi_set_log_function(empty_log);
1022
969
    }
1032
979
        exitcode = EXIT_FAILURE;
1033
980
        goto end;
1034
981
    }
1035
 
    
 
982
 
1036
983
    {
1037
984
      AvahiServerConfig config;
1038
985
      /* Do not publish any local Zeroconf records */
1041
988
      config.publish_addresses = 0;
1042
989
      config.publish_workstation = 0;
1043
990
      config.publish_domain = 0;
1044
 
      
 
991
 
1045
992
      /* Allocate a new server */
1046
993
      mc.server = avahi_server_new(avahi_simple_poll_get
1047
994
                                   (mc.simple_poll), &config, NULL,
1048
995
                                   NULL, &error);
1049
 
      
 
996
    
1050
997
      /* Free the Avahi configuration data */
1051
998
      avahi_server_config_free(&config);
1052
999
    }
1072
1019
    }
1073
1020
    
1074
1021
    /* Run the main loop */
1075
 
    
 
1022
 
1076
1023
    if (debug){
1077
1024
      fprintf(stderr, "Starting Avahi loop search\n");
1078
1025
    }
1080
1027
    avahi_simple_poll_loop(mc.simple_poll);
1081
1028
    
1082
1029
 end:
1083
 
    
 
1030
 
1084
1031
    if (debug){
1085
1032
      fprintf(stderr, "%s exiting\n", argv[0]);
1086
1033
    }
1091
1038
    
1092
1039
    if (mc.server != NULL)
1093
1040
        avahi_server_free(mc.server);
1094
 
    
 
1041
 
1095
1042
    if (mc.simple_poll != NULL)
1096
1043
        avahi_simple_poll_free(mc.simple_poll);
1097
 
    
 
1044
    free(pubkeyfile);
 
1045
    free(seckeyfile);
 
1046
 
1098
1047
    if (gnutls_initalized){
1099
1048
      gnutls_certificate_free_credentials(mc.cred);
1100
1049
      gnutls_global_deinit ();
1101
1050
    }
1102
1051
    
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
 
          
1143
1052
    return exitcode;
1144
1053
}