/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1
/*  -*- coding: utf-8 -*- */
2
/*
3
 * Mandos client - get and decrypt data from a Mandos server
4
 *
5
 * This program is partly derived from an example program for an Avahi
6
 * service browser, downloaded from
7
 * <http://avahi.org/browser/examples/core-browse-services.c>.  This
8
 * includes the following functions: "resolve_callback",
9
 * "browse_callback", and parts of "main".
10
 * 
28 by Teddy Hogeborn
* server.conf: New file.
11
 * Everything else is
24.1.102 by Björn Påhlsson
changed 2007-2008 copyright notice to 2008
12
 * Copyright © 2008 Teddy Hogeborn & Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
13
 * 
14
 * This program is free software: you can redistribute it and/or
15
 * modify it under the terms of the GNU General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 * 
19
 * This program is distributed in the hope that it will be useful, but
20
 * WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22
 * General Public License for more details.
23
 * 
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program.  If not, see
26
 * <http://www.gnu.org/licenses/>.
27
 * 
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
28
 * Contact the authors at <mandos@fukt.bsnet.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
29
 */
30
28 by Teddy Hogeborn
* server.conf: New file.
31
/* Needed by GPGME, specifically gpgme_data_seek() */
13 by Björn Påhlsson
Added following support:
32
#define _LARGEFILE_SOURCE
33
#define _FILE_OFFSET_BITS 64
34
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
35
#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY(), asprintf() */
24.1.10 by Björn Påhlsson
merge commit
36
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
37
#include <stdio.h>		/* fprintf(), stderr, fwrite(),
38
				   stdout, ferror() */
24.1.26 by Björn Påhlsson
tally count of used symbols
39
#include <stdint.h> 		/* uint16_t, uint32_t */
40
#include <stddef.h>		/* NULL, size_t, ssize_t */
24.1.29 by Björn Påhlsson
Added more header file comments
41
#include <stdlib.h> 		/* free(), EXIT_SUCCESS, EXIT_FAILURE,
42
				   srand() */
24.1.26 by Björn Påhlsson
tally count of used symbols
43
#include <stdbool.h>		/* bool, true */
24.1.29 by Björn Påhlsson
Added more header file comments
44
#include <string.h>		/* memset(), strcmp(), strlen(),
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
45
				   strerror(), asprintf(), strcpy() */
24.1.29 by Björn Påhlsson
Added more header file comments
46
#include <sys/ioctl.h>          /* ioctl */
24.1.26 by Björn Påhlsson
tally count of used symbols
47
#include <sys/types.h>		/* socket(), inet_pton(), sockaddr,
24.1.29 by Björn Påhlsson
Added more header file comments
48
				   sockaddr_in6, PF_INET6,
49
				   SOCK_STREAM, INET6_ADDRSTRLEN,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
50
				   uid_t, gid_t, open(), opendir(), DIR */
51
#include <sys/stat.h>		/* open() */
24.1.26 by Björn Påhlsson
tally count of used symbols
52
#include <sys/socket.h>		/* socket(), struct sockaddr_in6,
53
				   struct in6_addr, inet_pton(),
54
				   connect() */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
55
#include <fcntl.h>		/* open() */
56
#include <dirent.h>		/* opendir(), struct dirent, readdir() */
57
#include <inttypes.h>		/* PRIu16 */
24.1.29 by Björn Påhlsson
Added more header file comments
58
#include <assert.h>		/* assert() */
59
#include <errno.h>		/* perror(), errno */
60
#include <time.h>		/* time() */
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
61
#include <net/if.h>		/* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
24.1.26 by Björn Påhlsson
tally count of used symbols
62
				   SIOCSIFFLAGS, if_indextoname(),
63
				   if_nametoindex(), IF_NAMESIZE */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
64
#include <netinet/in.h>
24.1.29 by Björn Påhlsson
Added more header file comments
65
#include <unistd.h>		/* close(), SEEK_SET, off_t, write(),
66
				   getuid(), getgid(), setuid(),
67
				   setgid() */
24.1.26 by Björn Påhlsson
tally count of used symbols
68
#include <arpa/inet.h>		/* inet_pton(), htons */
24.1.29 by Björn Påhlsson
Added more header file comments
69
#include <iso646.h>		/* not, and */
70
#include <argp.h>		/* struct argp_option, error_t, struct
71
				   argp_state, struct argp,
72
				   argp_parse(), ARGP_KEY_ARG,
73
				   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
24.1.26 by Björn Påhlsson
tally count of used symbols
74
75
/* Avahi */
24.1.29 by Björn Påhlsson
Added more header file comments
76
/* All Avahi types, constants and functions
77
 Avahi*, avahi_*,
78
 AVAHI_* */
79
#include <avahi-core/core.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
80
#include <avahi-core/lookup.h>
24.1.29 by Björn Påhlsson
Added more header file comments
81
#include <avahi-core/log.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
82
#include <avahi-common/simple-watch.h>
83
#include <avahi-common/malloc.h>
84
#include <avahi-common/error.h>
85
86
/* GnuTLS */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
87
#include <gnutls/gnutls.h>	/* All GnuTLS types, constants and
88
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
89
				   gnutls_*
24.1.26 by Björn Påhlsson
tally count of used symbols
90
				   init_gnutls_session(),
24.1.29 by Björn Påhlsson
Added more header file comments
91
				   GNUTLS_* */
92
#include <gnutls/openpgp.h>     /* gnutls_certificate_set_openpgp_key_file(),
93
				   GNUTLS_OPENPGP_FMT_BASE64 */
24.1.26 by Björn Påhlsson
tally count of used symbols
94
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
95
/* GPGME */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
96
#include <gpgme.h> 		/* All GPGME types, constants and
97
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
98
				   gpgme_*
24.1.26 by Björn Påhlsson
tally count of used symbols
99
				   GPGME_PROTOCOL_OpenPGP,
24.1.29 by Björn Påhlsson
Added more header file comments
100
				   GPG_ERR_NO_* */
13 by Björn Påhlsson
Added following support:
101
102
#define BUFFER_SIZE 256
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
103
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
104
#define PATHDIR "/conf/conf.d/mandos"
105
#define SECKEY "seckey.txt"
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
106
#define PUBKEY "pubkey.txt"
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
107
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
108
bool debug = false;
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
109
static const char mandos_protocol_version[] = "1";
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
110
const char *argp_program_version = "mandos-client " VERSION;
24.1.14 by Björn Påhlsson
mandosclient
111
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
24.1.10 by Björn Påhlsson
merge commit
112
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
113
/* Used for passing in values through the Avahi callback functions */
13 by Björn Påhlsson
Added following support:
114
typedef struct {
24.1.9 by Björn Påhlsson
not working midwork...
115
  AvahiSimplePoll *simple_poll;
116
  AvahiServer *server;
13 by Björn Påhlsson
Added following support:
117
  gnutls_certificate_credentials_t cred;
24.1.9 by Björn Påhlsson
not working midwork...
118
  unsigned int dh_bits;
24.1.13 by Björn Påhlsson
mandosclient
119
  gnutls_dh_params_t dh_params;
24.1.9 by Björn Påhlsson
not working midwork...
120
  const char *priority;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
121
  gpgme_ctx_t ctx;
24.1.9 by Björn Påhlsson
not working midwork...
122
} mandos_context;
13 by Björn Påhlsson
Added following support:
123
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
124
/*
125
 * Make room in "buffer" for at least BUFFER_SIZE additional bytes.
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
126
 * "buffer_capacity" is how much is currently allocated,
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
127
 * "buffer_length" is how much is already used.
128
 */
24.1.12 by Björn Påhlsson
merge +
129
size_t adjustbuffer(char **buffer, size_t buffer_length,
24.1.10 by Björn Påhlsson
merge commit
130
		  size_t buffer_capacity){
131
  if (buffer_length + BUFFER_SIZE > buffer_capacity){
24.1.12 by Björn Påhlsson
merge +
132
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
24.1.10 by Björn Påhlsson
merge commit
133
    if (buffer == NULL){
134
      return 0;
135
    }
136
    buffer_capacity += BUFFER_SIZE;
137
  }
138
  return buffer_capacity;
139
}
140
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
141
/* 
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
142
 * Initialize GPGME.
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
143
 */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
144
static bool init_gpgme(mandos_context *mc, const char *seckey,
145
		       const char *pubkey, const char *tempdir){
146
  int ret;
13 by Björn Påhlsson
Added following support:
147
  gpgme_error_t rc;
148
  gpgme_engine_info_t engine_info;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
149
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
150
  
151
  /*
152
   * Helper function to insert pub and seckey to the enigne keyring.
153
   */
154
  bool import_key(const char *filename){
155
    int fd;
156
    gpgme_data_t pgp_data;
157
    
158
    fd = TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
159
    if(fd == -1){
160
      perror("open");
161
      return false;
162
    }
163
    
164
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
165
    if (rc != GPG_ERR_NO_ERROR){
166
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
167
	      gpgme_strsource(rc), gpgme_strerror(rc));
168
      return false;
169
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
170
    
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
171
    rc = gpgme_op_import(mc->ctx, pgp_data);
172
    if (rc != GPG_ERR_NO_ERROR){
173
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
174
	      gpgme_strsource(rc), gpgme_strerror(rc));
175
      return false;
176
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
177
    
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
178
    ret = TEMP_FAILURE_RETRY(close(fd));
179
    if(ret == -1){
180
      perror("close");
181
    }
182
    gpgme_data_release(pgp_data);
183
    return true;
184
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
185
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
186
  if (debug){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
187
    fprintf(stderr, "Initialize gpgme\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
188
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
189
  
13 by Björn Påhlsson
Added following support:
190
  /* Init GPGME */
191
  gpgme_check_version(NULL);
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
192
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
193
  if (rc != GPG_ERR_NO_ERROR){
194
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
195
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
196
    return false;
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
197
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
198
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
199
    /* Set GPGME home directory for the OpenPGP engine only */
13 by Björn Påhlsson
Added following support:
200
  rc = gpgme_get_engine_info (&engine_info);
201
  if (rc != GPG_ERR_NO_ERROR){
202
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
203
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
204
    return false;
13 by Björn Påhlsson
Added following support:
205
  }
206
  while(engine_info != NULL){
207
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
208
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
209
			    engine_info->file_name, tempdir);
13 by Björn Påhlsson
Added following support:
210
      break;
211
    }
212
    engine_info = engine_info->next;
213
  }
214
  if(engine_info == NULL){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
215
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
216
    return false;
217
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
218
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
219
  /* Create new GPGME "context" */
220
  rc = gpgme_new(&(mc->ctx));
221
  if (rc != GPG_ERR_NO_ERROR){
222
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
223
	    gpgme_strsource(rc), gpgme_strerror(rc));
224
    return false;
225
  }
226
  
227
  if (not import_key(pubkey) or not import_key(seckey)){
228
    return false;
229
  }
230
  
231
  return true; 
232
}
233
234
/* 
235
 * Decrypt OpenPGP data.
236
 * Returns -1 on error
237
 */
238
static ssize_t pgp_packet_decrypt (const mandos_context *mc,
239
				   const char *cryptotext,
240
				   size_t crypto_size,
241
				   char **plaintext){
242
  gpgme_data_t dh_crypto, dh_plain;
243
  gpgme_error_t rc;
244
  ssize_t ret;
245
  size_t plaintext_capacity = 0;
246
  ssize_t plaintext_length = 0;
247
  
248
  if (debug){
249
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
13 by Björn Påhlsson
Added following support:
250
  }
251
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
252
  /* Create new GPGME data buffer from memory cryptotext */
253
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
254
			       0);
13 by Björn Påhlsson
Added following support:
255
  if (rc != GPG_ERR_NO_ERROR){
256
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
257
	    gpgme_strsource(rc), gpgme_strerror(rc));
258
    return -1;
259
  }
260
  
261
  /* Create new empty GPGME data buffer for the plaintext */
262
  rc = gpgme_data_new(&dh_plain);
263
  if (rc != GPG_ERR_NO_ERROR){
264
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
265
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
266
    gpgme_data_release(dh_crypto);
13 by Björn Påhlsson
Added following support:
267
    return -1;
268
  }
269
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
270
  /* Decrypt data from the cryptotext data buffer to the plaintext
271
     data buffer */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
272
  rc = gpgme_op_decrypt(mc->ctx, dh_crypto, dh_plain);
13 by Björn Påhlsson
Added following support:
273
  if (rc != GPG_ERR_NO_ERROR){
274
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
275
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
276
    plaintext_length = -1;
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
277
    if (debug){
278
      gpgme_decrypt_result_t result;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
279
      result = gpgme_op_decrypt_result(mc->ctx);
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
280
      if (result == NULL){
281
	fprintf(stderr, "gpgme_op_decrypt_result failed\n");
282
      } else {
283
	fprintf(stderr, "Unsupported algorithm: %s\n",
284
		result->unsupported_algorithm);
285
	fprintf(stderr, "Wrong key usage: %u\n",
286
		result->wrong_key_usage);
287
	if(result->file_name != NULL){
288
	  fprintf(stderr, "File name: %s\n", result->file_name);
289
	}
290
	gpgme_recipient_t recipient;
291
	recipient = result->recipients;
292
	if(recipient){
293
	  while(recipient != NULL){
294
	    fprintf(stderr, "Public key algorithm: %s\n",
295
		    gpgme_pubkey_algo_name(recipient->pubkey_algo));
296
	    fprintf(stderr, "Key ID: %s\n", recipient->keyid);
297
	    fprintf(stderr, "Secret key available: %s\n",
298
		    recipient->status == GPG_ERR_NO_SECKEY
299
		    ? "No" : "Yes");
300
	    recipient = recipient->next;
301
	  }
302
	}
303
      }
304
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
305
    goto decrypt_end;
13 by Björn Påhlsson
Added following support:
306
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
307
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
308
  if(debug){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
309
    fprintf(stderr, "Decryption of OpenPGP data succeeded\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
310
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
311
  
13 by Björn Påhlsson
Added following support:
312
  /* Seek back to the beginning of the GPGME plaintext data buffer */
24.1.5 by Björn Påhlsson
plugbasedclient:
313
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
314
    perror("gpgme_data_seek");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
315
    plaintext_length = -1;
316
    goto decrypt_end;
24.1.5 by Björn Påhlsson
plugbasedclient:
317
  }
318
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
319
  *plaintext = NULL;
13 by Björn Påhlsson
Added following support:
320
  while(true){
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
321
    plaintext_capacity = adjustbuffer(plaintext,
322
				      (size_t)plaintext_length,
24.1.12 by Björn Påhlsson
merge +
323
				      plaintext_capacity);
324
    if (plaintext_capacity == 0){
24.1.10 by Björn Påhlsson
merge commit
325
	perror("adjustbuffer");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
326
	plaintext_length = -1;
327
	goto decrypt_end;
13 by Björn Påhlsson
Added following support:
328
    }
329
    
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
330
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
331
			  BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
332
    /* Print the data, if any */
333
    if (ret == 0){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
334
      /* EOF */
13 by Björn Påhlsson
Added following support:
335
      break;
336
    }
337
    if(ret < 0){
338
      perror("gpgme_data_read");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
339
      plaintext_length = -1;
340
      goto decrypt_end;
13 by Björn Påhlsson
Added following support:
341
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
342
    plaintext_length += ret;
13 by Björn Påhlsson
Added following support:
343
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
344
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
345
  if(debug){
346
    fprintf(stderr, "Decrypted password is: ");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
347
    for(ssize_t i = 0; i < plaintext_length; i++){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
348
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
349
    }
350
    fprintf(stderr, "\n");
351
  }
352
  
353
 decrypt_end:
354
  
355
  /* Delete the GPGME cryptotext data buffer */
356
  gpgme_data_release(dh_crypto);
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
357
  
358
  /* Delete the GPGME plaintext data buffer */
13 by Björn Påhlsson
Added following support:
359
  gpgme_data_release(dh_plain);
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
360
  return plaintext_length;
13 by Björn Påhlsson
Added following support:
361
}
362
363
static const char * safer_gnutls_strerror (int value) {
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
364
  const char *ret = gnutls_strerror (value); /* Spurious warning */
13 by Björn Påhlsson
Added following support:
365
  if (ret == NULL)
366
    ret = "(unknown)";
367
  return ret;
368
}
369
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
370
/* GnuTLS log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
371
static void debuggnutls(__attribute__((unused)) int level,
372
			const char* string){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
373
  fprintf(stderr, "GnuTLS: %s", string);
13 by Björn Påhlsson
Added following support:
374
}
375
24.1.14 by Björn Påhlsson
mandosclient
376
static int init_gnutls_global(mandos_context *mc,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
377
			      const char *pubkeyfilename,
378
			      const char *seckeyfilename){
13 by Björn Påhlsson
Added following support:
379
  int ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
380
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
381
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
382
    fprintf(stderr, "Initializing GnuTLS\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
383
  }
24.1.29 by Björn Påhlsson
Added more header file comments
384
  
385
  ret = gnutls_global_init();
386
  if (ret != GNUTLS_E_SUCCESS) {
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
387
    fprintf (stderr, "GnuTLS global_init: %s\n",
388
	     safer_gnutls_strerror(ret));
13 by Björn Påhlsson
Added following support:
389
    return -1;
390
  }
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
391
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
392
  if (debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
393
    /* "Use a log level over 10 to enable all debugging options."
394
     * - GnuTLS manual
395
     */
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
396
    gnutls_global_set_log_level(11);
397
    gnutls_global_set_log_function(debuggnutls);
398
  }
399
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
400
  /* OpenPGP credentials */
24.1.29 by Björn Påhlsson
Added more header file comments
401
  gnutls_certificate_allocate_credentials(&mc->cred);
402
  if (ret != GNUTLS_E_SUCCESS){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
403
    fprintf (stderr, "GnuTLS memory error: %s\n", /* Spurious
404
						     warning */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
405
	     safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
406
    gnutls_global_deinit ();
13 by Björn Påhlsson
Added following support:
407
    return -1;
408
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
409
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
410
  if(debug){
147 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Improved wording
411
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
412
	    " secret key %s as GnuTLS credentials\n", pubkeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
413
	    seckeyfilename);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
414
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
415
  
13 by Björn Påhlsson
Added following support:
416
  ret = gnutls_certificate_set_openpgp_key_file
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
417
    (mc->cred, pubkeyfilename, seckeyfilename,
418
     GNUTLS_OPENPGP_FMT_BASE64);
13 by Björn Påhlsson
Added following support:
419
  if (ret != GNUTLS_E_SUCCESS) {
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
420
    fprintf(stderr,
421
	    "Error[%d] while reading the OpenPGP key pair ('%s',"
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
422
	    " '%s')\n", ret, pubkeyfilename, seckeyfilename);
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
423
    fprintf(stderr, "The GnuTLS error is: %s\n",
13 by Björn Påhlsson
Added following support:
424
	    safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
425
    goto globalfail;
13 by Björn Påhlsson
Added following support:
426
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
427
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
428
  /* GnuTLS server initialization */
24.1.13 by Björn Påhlsson
mandosclient
429
  ret = gnutls_dh_params_init(&mc->dh_params);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
430
  if (ret != GNUTLS_E_SUCCESS) {
431
    fprintf (stderr, "Error in GnuTLS DH parameter initialization:"
432
	     " %s\n", safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
433
    goto globalfail;
13 by Björn Påhlsson
Added following support:
434
  }
24.1.13 by Björn Påhlsson
mandosclient
435
  ret = gnutls_dh_params_generate2(mc->dh_params, mc->dh_bits);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
436
  if (ret != GNUTLS_E_SUCCESS) {
437
    fprintf (stderr, "Error in GnuTLS prime generation: %s\n",
13 by Björn Påhlsson
Added following support:
438
	     safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
439
    goto globalfail;
13 by Björn Påhlsson
Added following support:
440
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
441
  
24.1.13 by Björn Påhlsson
mandosclient
442
  gnutls_certificate_set_dh_params(mc->cred, mc->dh_params);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
443
  
24.1.13 by Björn Påhlsson
mandosclient
444
  return 0;
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
445
  
24.1.20 by Björn Påhlsson
mandosclient
446
 globalfail:
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
447
  
24.1.26 by Björn Påhlsson
tally count of used symbols
448
  gnutls_certificate_free_credentials(mc->cred);
449
  gnutls_global_deinit();
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
450
  gnutls_dh_params_deinit(mc->dh_params);
24.1.20 by Björn Påhlsson
mandosclient
451
  return -1;
24.1.13 by Björn Påhlsson
mandosclient
452
}
453
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
454
static int init_gnutls_session(mandos_context *mc,
455
			       gnutls_session_t *session){
24.1.13 by Björn Påhlsson
mandosclient
456
  int ret;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
457
  /* GnuTLS session creation */
458
  ret = gnutls_init(session, GNUTLS_SERVER);
459
  if (ret != GNUTLS_E_SUCCESS){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
460
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
13 by Björn Påhlsson
Added following support:
461
	    safer_gnutls_strerror(ret));
462
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
463
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
464
  {
465
    const char *err;
466
    ret = gnutls_priority_set_direct(*session, mc->priority, &err);
467
    if (ret != GNUTLS_E_SUCCESS) {
468
      fprintf(stderr, "Syntax error at: %s\n", err);
469
      fprintf(stderr, "GnuTLS error: %s\n",
470
	      safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
471
      gnutls_deinit (*session);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
472
      return -1;
473
    }
13 by Björn Påhlsson
Added following support:
474
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
475
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
476
  ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
477
			       mc->cred);
478
  if (ret != GNUTLS_E_SUCCESS) {
479
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
13 by Björn Påhlsson
Added following support:
480
	    safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
481
    gnutls_deinit (*session);
13 by Björn Påhlsson
Added following support:
482
    return -1;
483
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
484
  
13 by Björn Påhlsson
Added following support:
485
  /* ignore client certificate if any. */
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
486
  gnutls_certificate_server_set_request (*session,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
487
					 GNUTLS_CERT_IGNORE);
13 by Björn Påhlsson
Added following support:
488
  
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
489
  gnutls_dh_set_prime_bits (*session, mc->dh_bits);
13 by Björn Påhlsson
Added following support:
490
  
491
  return 0;
492
}
493
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
494
/* Avahi log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
495
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
496
		      __attribute__((unused)) const char *txt){}
13 by Björn Påhlsson
Added following support:
497
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
498
/* Called when a Mandos server is found */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
499
static int start_mandos_communication(const char *ip, uint16_t port,
24.1.9 by Björn Påhlsson
not working midwork...
500
				      AvahiIfIndex if_index,
501
				      mandos_context *mc){
13 by Björn Påhlsson
Added following support:
502
  int ret, tcp_sd;
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
503
  union { struct sockaddr in; struct sockaddr_in6 in6; } to;
13 by Björn Påhlsson
Added following support:
504
  char *buffer = NULL;
505
  char *decrypted_buffer;
506
  size_t buffer_length = 0;
507
  size_t buffer_capacity = 0;
508
  ssize_t decrypted_buffer_size;
24.1.10 by Björn Påhlsson
merge commit
509
  size_t written;
13 by Björn Påhlsson
Added following support:
510
  int retval = 0;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
511
  char interface[IF_NAMESIZE];
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
512
  gnutls_session_t session;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
513
  
24.1.13 by Björn Påhlsson
mandosclient
514
  ret = init_gnutls_session (mc, &session);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
515
  if (ret != 0){
516
    return -1;
517
  }
518
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
519
  if(debug){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
520
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
521
	    "\n", ip, port);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
522
  }
13 by Björn Påhlsson
Added following support:
523
  
524
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
525
  if(tcp_sd < 0) {
526
    perror("socket");
527
    return -1;
528
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
529
  
24.1.6 by Björn Påhlsson
plugbasedclient
530
  if(debug){
24.1.7 by Björn Påhlsson
merge
531
    if(if_indextoname((unsigned int)if_index, interface) == NULL){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
532
      perror("if_indextoname");
24.1.6 by Björn Påhlsson
plugbasedclient
533
      return -1;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
534
    }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
535
    fprintf(stderr, "Binding to interface %s\n", interface);
536
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
537
  
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
538
  memset(&to, 0, sizeof(to));
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
539
  to.in6.sin6_family = AF_INET6;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
540
  /* It would be nice to have a way to detect if we were passed an
541
     IPv4 address here.   Now we assume an IPv6 address. */
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
542
  ret = inet_pton(AF_INET6, ip, &to.in6.sin6_addr);
13 by Björn Påhlsson
Added following support:
543
  if (ret < 0 ){
544
    perror("inet_pton");
545
    return -1;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
546
  }
13 by Björn Påhlsson
Added following support:
547
  if(ret == 0){
548
    fprintf(stderr, "Bad address: %s\n", ip);
549
    return -1;
550
  }
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
551
  to.in6.sin6_port = htons(port); /* Spurious warning */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
552
  
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
553
  to.in6.sin6_scope_id = (uint32_t)if_index;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
554
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
555
  if(debug){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
556
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
557
	    port);
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
558
    char addrstr[INET6_ADDRSTRLEN] = "";
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
559
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
560
		 sizeof(addrstr)) == NULL){
561
      perror("inet_ntop");
562
    } else {
563
      if(strcmp(addrstr, ip) != 0){
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
564
	fprintf(stderr, "Canonical address form: %s\n", addrstr);
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
565
      }
566
    }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
567
  }
13 by Björn Påhlsson
Added following support:
568
  
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
569
  ret = connect(tcp_sd, &to.in, sizeof(to));
13 by Björn Påhlsson
Added following support:
570
  if (ret < 0){
571
    perror("connect");
572
    return -1;
573
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
574
  
24.1.12 by Björn Påhlsson
merge +
575
  const char *out = mandos_protocol_version;
24.1.10 by Björn Påhlsson
merge commit
576
  written = 0;
577
  while (true){
578
    size_t out_size = strlen(out);
579
    ret = TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
580
				   out_size - written));
581
    if (ret == -1){
582
      perror("write");
583
      retval = -1;
24.1.12 by Björn Påhlsson
merge +
584
      goto mandos_end;
24.1.10 by Björn Påhlsson
merge commit
585
    }
24.1.12 by Björn Påhlsson
merge +
586
    written += (size_t)ret;
24.1.10 by Björn Påhlsson
merge commit
587
    if(written < out_size){
588
      continue;
589
    } else {
590
      if (out == mandos_protocol_version){
591
	written = 0;
592
	out = "\r\n";
593
      } else {
594
	break;
595
      }
596
    }
597
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
598
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
599
  if(debug){
600
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
601
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
602
  
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
603
  gnutls_transport_set_ptr (session, (gnutls_transport_ptr_t) tcp_sd);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
604
  
24.1.29 by Björn Påhlsson
Added more header file comments
605
  do{
606
    ret = gnutls_handshake (session);
607
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
13 by Björn Påhlsson
Added following support:
608
  
609
  if (ret != GNUTLS_E_SUCCESS){
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
610
    if(debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
611
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
612
      gnutls_perror (ret);
613
    }
13 by Björn Påhlsson
Added following support:
614
    retval = -1;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
615
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
616
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
617
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
618
  /* Read OpenPGP packet that contains the wanted password */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
619
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
620
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
621
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
622
	    ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
623
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
624
  
13 by Björn Påhlsson
Added following support:
625
  while(true){
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
626
    buffer_capacity = adjustbuffer(&buffer, buffer_length,
627
				   buffer_capacity);
24.1.10 by Björn Påhlsson
merge commit
628
    if (buffer_capacity == 0){
629
      perror("adjustbuffer");
630
      retval = -1;
24.1.12 by Björn Påhlsson
merge +
631
      goto mandos_end;
13 by Björn Påhlsson
Added following support:
632
    }
633
    
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
634
    ret = gnutls_record_recv(session, buffer+buffer_length,
635
			     BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
636
    if (ret == 0){
637
      break;
638
    }
639
    if (ret < 0){
640
      switch(ret){
641
      case GNUTLS_E_INTERRUPTED:
642
      case GNUTLS_E_AGAIN:
643
	break;
644
      case GNUTLS_E_REHANDSHAKE:
24.1.29 by Björn Påhlsson
Added more header file comments
645
	do{
646
	  ret = gnutls_handshake (session);
647
	} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
13 by Björn Påhlsson
Added following support:
648
	if (ret < 0){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
649
	  fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
13 by Björn Påhlsson
Added following support:
650
	  gnutls_perror (ret);
651
	  retval = -1;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
652
	  goto mandos_end;
13 by Björn Påhlsson
Added following support:
653
	}
654
	break;
655
      default:
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
656
	fprintf(stderr, "Unknown error while reading data from"
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
657
		" encrypted session with Mandos server\n");
13 by Björn Påhlsson
Added following support:
658
	retval = -1;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
659
	gnutls_bye (session, GNUTLS_SHUT_RDWR);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
660
	goto mandos_end;
13 by Björn Påhlsson
Added following support:
661
      }
662
    } else {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
663
      buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
664
    }
665
  }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
666
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
667
  if(debug){
668
    fprintf(stderr, "Closing TLS session\n");
669
  }
670
  
671
  gnutls_bye (session, GNUTLS_SHUT_RDWR);
672
  
13 by Björn Påhlsson
Added following support:
673
  if (buffer_length > 0){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
674
    decrypted_buffer_size = pgp_packet_decrypt(mc, buffer,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
675
					       buffer_length,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
676
					       &decrypted_buffer);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
677
    if (decrypted_buffer_size >= 0){
24.1.10 by Björn Påhlsson
merge commit
678
      written = 0;
28 by Teddy Hogeborn
* server.conf: New file.
679
      while(written < (size_t) decrypted_buffer_size){
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
680
	ret = (int)fwrite (decrypted_buffer + written, 1,
681
			   (size_t)decrypted_buffer_size - written,
682
			   stdout);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
683
	if(ret == 0 and ferror(stdout)){
684
	  if(debug){
685
	    fprintf(stderr, "Error writing encrypted data: %s\n",
686
		    strerror(errno));
687
	  }
688
	  retval = -1;
689
	  break;
690
	}
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
691
	written += (size_t)ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
692
      }
13 by Björn Påhlsson
Added following support:
693
      free(decrypted_buffer);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
694
    } else {
695
      retval = -1;
13 by Björn Påhlsson
Added following support:
696
    }
24.1.63 by Björn Påhlsson
merge + fallback bugg
697
  } else {
698
    retval = -1;
13 by Björn Påhlsson
Added following support:
699
  }
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
700
  
701
  /* Shutdown procedure */
702
  
703
 mandos_end:
13 by Björn Påhlsson
Added following support:
704
  free(buffer);
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
705
  ret = TEMP_FAILURE_RETRY(close(tcp_sd));
706
  if(ret == -1){
707
    perror("close");
708
  }
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
709
  gnutls_deinit (session);
13 by Björn Påhlsson
Added following support:
710
  return retval;
711
}
712
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
713
static void resolve_callback(AvahiSServiceResolver *r,
714
			     AvahiIfIndex interface,
715
			     AVAHI_GCC_UNUSED AvahiProtocol protocol,
716
			     AvahiResolverEvent event,
717
			     const char *name,
718
			     const char *type,
719
			     const char *domain,
720
			     const char *host_name,
721
			     const AvahiAddress *address,
722
			     uint16_t port,
723
			     AVAHI_GCC_UNUSED AvahiStringList *txt,
724
			     AVAHI_GCC_UNUSED AvahiLookupResultFlags
725
			     flags,
726
			     void* userdata) {
24.1.9 by Björn Påhlsson
not working midwork...
727
  mandos_context *mc = userdata;
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
728
  assert(r);
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
729
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
730
  /* Called whenever a service has been resolved successfully or
731
     timed out */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
732
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
733
  switch (event) {
734
  default:
735
  case AVAHI_RESOLVER_FAILURE:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
736
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
737
	    " of type '%s' in domain '%s': %s\n", name, type, domain,
24.1.9 by Björn Påhlsson
not working midwork...
738
	    avahi_strerror(avahi_server_errno(mc->server)));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
739
    break;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
740
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
741
  case AVAHI_RESOLVER_FOUND:
742
    {
743
      char ip[AVAHI_ADDRESS_STR_MAX];
744
      avahi_address_snprint(ip, sizeof(ip), address);
745
      if(debug){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
746
	fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
747
		PRIu16 ") on port %d\n", name, host_name, ip,
748
		interface, port);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
749
      }
24.1.9 by Björn Påhlsson
not working midwork...
750
      int ret = start_mandos_communication(ip, port, interface, mc);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
751
      if (ret == 0){
24.1.46 by Björn Påhlsson
mandos-client
752
	avahi_simple_poll_quit(mc->simple_poll);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
753
      }
13 by Björn Påhlsson
Added following support:
754
    }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
755
  }
756
  avahi_s_service_resolver_free(r);
13 by Björn Påhlsson
Added following support:
757
}
758
24.1.9 by Björn Påhlsson
not working midwork...
759
static void browse_callback( AvahiSServiceBrowser *b,
760
			     AvahiIfIndex interface,
761
			     AvahiProtocol protocol,
762
			     AvahiBrowserEvent event,
763
			     const char *name,
764
			     const char *type,
765
			     const char *domain,
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
766
			     AVAHI_GCC_UNUSED AvahiLookupResultFlags
767
			     flags,
24.1.9 by Björn Påhlsson
not working midwork...
768
			     void* userdata) {
769
  mandos_context *mc = userdata;
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
770
  assert(b);
24.1.9 by Björn Påhlsson
not working midwork...
771
  
772
  /* Called whenever a new services becomes available on the LAN or
773
     is removed from the LAN */
774
  
775
  switch (event) {
776
  default:
777
  case AVAHI_BROWSER_FAILURE:
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
778
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
779
    fprintf(stderr, "(Avahi browser) %s\n",
24.1.9 by Björn Påhlsson
not working midwork...
780
	    avahi_strerror(avahi_server_errno(mc->server)));
781
    avahi_simple_poll_quit(mc->simple_poll);
782
    return;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
783
    
24.1.9 by Björn Påhlsson
not working midwork...
784
  case AVAHI_BROWSER_NEW:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
785
    /* We ignore the returned Avahi resolver object. In the callback
786
       function we free it. If the Avahi server is terminated before
787
       the callback function is called the Avahi server will free the
788
       resolver for us. */
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
789
    
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
790
    if (!(avahi_s_service_resolver_new(mc->server, interface,
791
				       protocol, name, type, domain,
24.1.9 by Björn Påhlsson
not working midwork...
792
				       AVAHI_PROTO_INET6, 0,
793
				       resolve_callback, mc)))
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
794
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
795
	      name, avahi_strerror(avahi_server_errno(mc->server)));
24.1.9 by Björn Påhlsson
not working midwork...
796
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
797
    
24.1.9 by Björn Påhlsson
not working midwork...
798
  case AVAHI_BROWSER_REMOVE:
799
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
800
    
24.1.9 by Björn Påhlsson
not working midwork...
801
  case AVAHI_BROWSER_ALL_FOR_NOW:
802
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
803
    if(debug){
804
      fprintf(stderr, "No Mandos server found, still searching...\n");
805
    }
24.1.9 by Björn Påhlsson
not working midwork...
806
    break;
807
  }
13 by Björn Påhlsson
Added following support:
808
}
809
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
810
int main(int argc, char *argv[]){
13 by Björn Påhlsson
Added following support:
811
    AvahiSServiceBrowser *sb = NULL;
812
    int error;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
813
    int ret;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
814
    int exitcode = EXIT_SUCCESS;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
815
    const char *interface = "eth0";
24.1.6 by Björn Påhlsson
plugbasedclient
816
    struct ifreq network;
817
    int sd;
24.1.13 by Björn Påhlsson
mandosclient
818
    uid_t uid;
819
    gid_t gid;
24.1.7 by Björn Påhlsson
merge
820
    char *connect_to = NULL;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
821
    char tempdir[] = "/tmp/mandosXXXXXX";
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
822
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
823
    const char *seckey = PATHDIR "/" SECKEY;
824
    const char *pubkey = PATHDIR "/" PUBKEY;
825
    
24.1.9 by Björn Påhlsson
not working midwork...
826
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
827
			  .dh_bits = 1024, .priority = "SECURE256"
828
			  ":!CTYPE-X.509:+CTYPE-OPENPGP" };
24.1.20 by Björn Påhlsson
mandosclient
829
    bool gnutls_initalized = false;
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
830
    bool gpgme_initalized = false;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
831
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
832
    {
24.1.14 by Björn Påhlsson
mandosclient
833
      struct argp_option options[] = {
834
	{ .name = "debug", .key = 128,
835
	  .doc = "Debug mode", .group = 3 },
836
	{ .name = "connect", .key = 'c',
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
837
	  .arg = "ADDRESS:PORT",
838
	  .doc = "Connect directly to a specific Mandos server",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
839
	  .group = 1 },
24.1.14 by Björn Påhlsson
mandosclient
840
	{ .name = "interface", .key = 'i',
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
841
	  .arg = "NAME",
842
	  .doc = "Interface that will be used to search for Mandos"
843
	  " servers",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
844
	  .group = 1 },
24.1.14 by Björn Påhlsson
mandosclient
845
	{ .name = "seckey", .key = 's',
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
846
	  .arg = "FILE",
847
	  .doc = "OpenPGP secret key file base name",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
848
	  .group = 1 },
24.1.14 by Björn Påhlsson
mandosclient
849
	{ .name = "pubkey", .key = 'p',
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
850
	  .arg = "FILE",
851
	  .doc = "OpenPGP public key file base name",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
852
	  .group = 2 },
24.1.14 by Björn Påhlsson
mandosclient
853
	{ .name = "dh-bits", .key = 129,
854
	  .arg = "BITS",
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
855
	  .doc = "Bit length of the prime number used in the"
856
	  " Diffie-Hellman key exchange",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
857
	  .group = 2 },
24.1.14 by Björn Påhlsson
mandosclient
858
	{ .name = "priority", .key = 130,
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
859
	  .arg = "STRING",
860
	  .doc = "GnuTLS priority string for the TLS handshake",
861
	  .group = 1 },
24.1.14 by Björn Påhlsson
mandosclient
862
	{ .name = NULL }
863
      };
864
      
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
865
      error_t parse_opt (int key, char *arg,
866
			 struct argp_state *state) {
867
	/* Get the INPUT argument from `argp_parse', which we know is
868
	   a pointer to our plugin list pointer. */
24.1.14 by Björn Påhlsson
mandosclient
869
	switch (key) {
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
870
	case 128:		/* --debug */
24.1.14 by Björn Påhlsson
mandosclient
871
	  debug = true;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
872
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
873
	case 'c':		/* --connect */
24.1.14 by Björn Påhlsson
mandosclient
874
	  connect_to = arg;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
875
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
876
	case 'i':		/* --interface */
24.1.14 by Björn Påhlsson
mandosclient
877
	  interface = arg;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
878
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
879
	case 's':		/* --seckey */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
880
	  seckey = arg;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
881
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
882
	case 'p':		/* --pubkey */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
883
	  pubkey = arg;
24.1.14 by Björn Påhlsson
mandosclient
884
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
885
	case 129:		/* --dh-bits */
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
886
	  errno = 0;
24.1.14 by Björn Påhlsson
mandosclient
887
	  mc.dh_bits = (unsigned int) strtol(arg, NULL, 10);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
888
	  if (errno){
889
	    perror("strtol");
890
	    exit(EXIT_FAILURE);
891
	  }
892
	  break;
142 by Teddy Hogeborn
* plugins.d/password-request.c (main): Change default GnuTLS priority
893
	case 130:		/* --priority */
24.1.14 by Björn Påhlsson
mandosclient
894
	  mc.priority = arg;
895
	  break;
896
	case ARGP_KEY_ARG:
897
	  argp_usage (state);
24.1.46 by Björn Påhlsson
mandos-client
898
	case ARGP_KEY_END:
24.1.14 by Björn Påhlsson
mandosclient
899
	  break;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
900
	default:
24.1.14 by Björn Påhlsson
mandosclient
901
	  return ARGP_ERR_UNKNOWN;
24.1.9 by Björn Påhlsson
not working midwork...
902
	}
24.1.14 by Björn Påhlsson
mandosclient
903
	return 0;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
904
      }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
905
      
24.1.14 by Björn Påhlsson
mandosclient
906
      struct argp argp = { .options = options, .parser = parse_opt,
907
			   .args_doc = "",
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
908
			   .doc = "Mandos client -- Get and decrypt"
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
909
			   " passwords from a Mandos server" };
24.1.29 by Björn Påhlsson
Added more header file comments
910
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
911
      if (ret == ARGP_ERR_UNKNOWN){
24.1.43 by Björn Påhlsson
merge
912
	fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.29 by Björn Påhlsson
Added more header file comments
913
	exitcode = EXIT_FAILURE;
914
	goto end;
915
      }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
916
    }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
917
    
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
918
    /* If the interface is down, bring it up */
919
    {
920
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
921
      if(sd < 0) {
922
	perror("socket");
923
	exitcode = EXIT_FAILURE;
924
	goto end;
925
      }
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
926
      strcpy(network.ifr_name, interface);
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
927
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
928
      if(ret == -1){
929
	perror("ioctl SIOCGIFFLAGS");
930
	exitcode = EXIT_FAILURE;
931
	goto end;
932
      }
933
      if((network.ifr_flags & IFF_UP) == 0){
934
	network.ifr_flags |= IFF_UP;
935
	ret = ioctl(sd, SIOCSIFFLAGS, &network);
936
	if(ret == -1){
937
	  perror("ioctl SIOCSIFFLAGS");
938
	  exitcode = EXIT_FAILURE;
939
	  goto end;
940
	}
941
      }
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
942
      ret = TEMP_FAILURE_RETRY(close(sd));
943
      if(ret == -1){
944
	perror("close");
945
      }
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
946
    }
947
    
24.1.13 by Björn Påhlsson
mandosclient
948
    uid = getuid();
949
    gid = getgid();
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
950
    
24.1.13 by Björn Påhlsson
mandosclient
951
    ret = setuid(uid);
952
    if (ret == -1){
953
      perror("setuid");
954
    }
955
    
956
    setgid(gid);
957
    if (ret == -1){
958
      perror("setgid");
959
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
960
    
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
961
    ret = init_gnutls_global(&mc, pubkey, seckey);
962
    if (ret == -1){
963
      fprintf(stderr, "init_gnutls_global failed\n");
964
      exitcode = EXIT_FAILURE;
965
      goto end;
966
    } else {
967
      gnutls_initalized = true;
968
    }
969
    
970
    if(mkdtemp(tempdir) == NULL){
971
      perror("mkdtemp");
972
      tempdir[0] = '\0';
973
      goto end;
974
    }
975
    
976
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
176 by Teddy Hogeborn
Merge & resolve.
977
      fprintf(stderr, "gpgme_initalized failed\n");
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
978
      exitcode = EXIT_FAILURE;
979
      goto end;
980
    } else {
176 by Teddy Hogeborn
Merge & resolve.
981
      gpgme_initalized = true;
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
982
    }
983
    
24.1.7 by Björn Påhlsson
merge
984
    if_index = (AvahiIfIndex) if_nametoindex(interface);
985
    if(if_index == 0){
986
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
987
      exit(EXIT_FAILURE);
28 by Teddy Hogeborn
* server.conf: New file.
988
    }
989
    
990
    if(connect_to != NULL){
991
      /* Connect directly, do not use Zeroconf */
992
      /* (Mainly meant for debugging) */
993
      char *address = strrchr(connect_to, ':');
994
      if(address == NULL){
995
        fprintf(stderr, "No colon in address\n");
24.1.13 by Björn Påhlsson
mandosclient
996
	exitcode = EXIT_FAILURE;
997
	goto end;
28 by Teddy Hogeborn
* server.conf: New file.
998
      }
999
      errno = 0;
1000
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
1001
      if(errno){
1002
	perror("Bad port number");
24.1.13 by Björn Påhlsson
mandosclient
1003
	exitcode = EXIT_FAILURE;
1004
	goto end;
28 by Teddy Hogeborn
* server.conf: New file.
1005
      }
1006
      *address = '\0';
1007
      address = connect_to;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1008
      ret = start_mandos_communication(address, port, if_index, &mc);
28 by Teddy Hogeborn
* server.conf: New file.
1009
      if(ret < 0){
24.1.13 by Björn Påhlsson
mandosclient
1010
	exitcode = EXIT_FAILURE;
28 by Teddy Hogeborn
* server.conf: New file.
1011
      } else {
24.1.13 by Björn Påhlsson
mandosclient
1012
	exitcode = EXIT_SUCCESS;
28 by Teddy Hogeborn
* server.conf: New file.
1013
      }
24.1.13 by Björn Påhlsson
mandosclient
1014
      goto end;
28 by Teddy Hogeborn
* server.conf: New file.
1015
    }
1016
    
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
1017
    if (not debug){
1018
      avahi_set_log_function(empty_log);
1019
    }
13 by Björn Påhlsson
Added following support:
1020
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1021
    /* Initialize the pseudo-RNG for Avahi */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1022
    srand((unsigned int) time(NULL));
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1023
    
1024
    /* Allocate main Avahi loop object */
1025
    mc.simple_poll = avahi_simple_poll_new();
1026
    if (mc.simple_poll == NULL) {
1027
        fprintf(stderr, "Avahi: Failed to create simple poll"
1028
		" object.\n");
1029
	exitcode = EXIT_FAILURE;
1030
        goto end;
1031
    }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1032
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1033
    {
1034
      AvahiServerConfig config;
1035
      /* Do not publish any local Zeroconf records */
1036
      avahi_server_config_init(&config);
1037
      config.publish_hinfo = 0;
1038
      config.publish_addresses = 0;
1039
      config.publish_workstation = 0;
1040
      config.publish_domain = 0;
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1041
      
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1042
      /* Allocate a new server */
1043
      mc.server = avahi_server_new(avahi_simple_poll_get
1044
				   (mc.simple_poll), &config, NULL,
1045
				   NULL, &error);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1046
      
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1047
      /* Free the Avahi configuration data */
1048
      avahi_server_config_free(&config);
1049
    }
1050
    
1051
    /* Check if creating the Avahi server object succeeded */
1052
    if (mc.server == NULL) {
1053
        fprintf(stderr, "Failed to create Avahi server: %s\n",
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1054
		avahi_strerror(error));
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1055
	exitcode = EXIT_FAILURE;
1056
        goto end;
13 by Björn Påhlsson
Added following support:
1057
    }
1058
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1059
    /* Create the Avahi service browser */
24.1.9 by Björn Påhlsson
not working midwork...
1060
    sb = avahi_s_service_browser_new(mc.server, if_index,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1061
				     AVAHI_PROTO_INET6,
1062
				     "_mandos._tcp", NULL, 0,
24.1.9 by Björn Påhlsson
not working midwork...
1063
				     browse_callback, &mc);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1064
    if (sb == NULL) {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1065
        fprintf(stderr, "Failed to create service browser: %s\n",
24.1.9 by Björn Påhlsson
not working midwork...
1066
		avahi_strerror(avahi_server_errno(mc.server)));
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1067
	exitcode = EXIT_FAILURE;
1068
        goto end;
13 by Björn Påhlsson
Added following support:
1069
    }
1070
    
1071
    /* Run the main loop */
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1072
    
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
1073
    if (debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1074
      fprintf(stderr, "Starting Avahi loop search\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
1075
    }
1076
    
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1077
    avahi_simple_poll_loop(mc.simple_poll);
13 by Björn Påhlsson
Added following support:
1078
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1079
 end:
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1080
    
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
1081
    if (debug){
1082
      fprintf(stderr, "%s exiting\n", argv[0]);
1083
    }
13 by Björn Påhlsson
Added following support:
1084
    
1085
    /* Cleanup things */
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1086
    if (sb != NULL)
13 by Björn Påhlsson
Added following support:
1087
        avahi_s_service_browser_free(sb);
1088
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1089
    if (mc.server != NULL)
24.1.9 by Björn Påhlsson
not working midwork...
1090
        avahi_server_free(mc.server);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1091
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1092
    if (mc.simple_poll != NULL)
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1093
        avahi_simple_poll_free(mc.simple_poll);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
1094
    
24.1.20 by Björn Påhlsson
mandosclient
1095
    if (gnutls_initalized){
24.1.29 by Björn Påhlsson
Added more header file comments
1096
      gnutls_certificate_free_credentials(mc.cred);
24.1.20 by Björn Påhlsson
mandosclient
1097
      gnutls_global_deinit ();
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
1098
      gnutls_dh_params_deinit(mc.dh_params);
24.1.20 by Björn Påhlsson
mandosclient
1099
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
1100
    
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
1101
    if(gpgme_initalized){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
1102
      gpgme_release(mc.ctx);
1103
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
1104
    
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
1105
    /* Removes the temp directory used by GPGME */
1106
    if(tempdir[0] != '\0'){
1107
      DIR *d;
1108
      struct dirent *direntry;
1109
      d = opendir(tempdir);
1110
      if(d == NULL){
1111
	perror("opendir");
1112
      } else {
1113
	while(true){
1114
	  direntry = readdir(d);
1115
	  if(direntry == NULL){
1116
	    break;
1117
	  }
1118
	  if (direntry->d_type == DT_REG){
1119
	    char *fullname = NULL;
1120
	    ret = asprintf(&fullname, "%s/%s", tempdir,
1121
			   direntry->d_name);
1122
	    if(ret < 0){
1123
	      perror("asprintf");
1124
	      continue;
1125
	    }
1126
	    ret = unlink(fullname);
1127
	    if(ret == -1){
1128
	      fprintf(stderr, "unlink(\"%s\"): %s",
1129
		      fullname, strerror(errno));
1130
	    }
1131
	    free(fullname);
1132
	  }
1133
	}
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
1134
	closedir(d);
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
1135
      }
1136
      ret = rmdir(tempdir);
1137
      if(ret == -1){
1138
	perror("rmdir");
1139
      }
1140
    }
1141
	  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1142
    return exitcode;
13 by Björn Påhlsson
Added following support:
1143
}