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