/mandos/trunk

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