/mandos/release

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