/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
35
#include <stdio.h>
36
#include <assert.h>
37
#include <stdlib.h>
38
#include <time.h>
39
#include <net/if.h>		/* if_nametoindex */
40
41
#include <avahi-core/core.h>
42
#include <avahi-core/lookup.h>
43
#include <avahi-core/log.h>
44
#include <avahi-common/simple-watch.h>
45
#include <avahi-common/malloc.h>
46
#include <avahi-common/error.h>
47
48
//mandos client part
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
49
#include <sys/types.h>		/* socket(), inet_pton() */
50
#include <sys/socket.h>		/* socket(), struct sockaddr_in6,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
51
				   struct in6_addr, inet_pton() */
52
#include <gnutls/gnutls.h>	/* All GnuTLS stuff */
53
#include <gnutls/openpgp.h>	/* GnuTLS with openpgp stuff */
13 by Björn Påhlsson
Added following support:
54
55
#include <unistd.h>		/* close() */
56
#include <netinet/in.h>
57
#include <stdbool.h>		/* true */
58
#include <string.h>		/* memset */
59
#include <arpa/inet.h>		/* inet_pton() */
60
#include <iso646.h>		/* not */
61
62
// gpgme
63
#include <errno.h>		/* perror() */
64
#include <gpgme.h>
65
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
66
// getopt long
67
#include <getopt.h>
13 by Björn Påhlsson
Added following support:
68
69
#define BUFFER_SIZE 256
70
#define DH_BITS 1024
71
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.
72
const char *certdir = "/conf/conf.d/cryptkeyreq/";
73
const char *certfile = "openpgp-client.txt";
74
const char *certkey = "openpgp-client-key.txt";
75
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
76
bool debug = false;
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
77
13 by Björn Påhlsson
Added following support:
78
typedef struct {
79
  gnutls_session_t session;
80
  gnutls_certificate_credentials_t cred;
81
  gnutls_dh_params_t dh_params;
82
} encrypted_session;
83
84
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
85
ssize_t pgp_packet_decrypt (char *packet, size_t packet_size,
86
			    char **new_packet, const char *homedir){
13 by Björn Påhlsson
Added following support:
87
  gpgme_data_t dh_crypto, dh_plain;
88
  gpgme_ctx_t ctx;
89
  gpgme_error_t rc;
90
  ssize_t ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
91
  ssize_t new_packet_capacity = 0;
92
  ssize_t new_packet_length = 0;
13 by Björn Påhlsson
Added following support:
93
  gpgme_engine_info_t engine_info;
94
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
95
  if (debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
96
    fprintf(stderr, "Trying to decrypt OpenPGP packet\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
97
  }
98
  
13 by Björn Påhlsson
Added following support:
99
  /* Init GPGME */
100
  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.
101
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
102
  if (rc != GPG_ERR_NO_ERROR){
103
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
104
	    gpgme_strsource(rc), gpgme_strerror(rc));
105
    return -1;
106
  }
13 by Björn Påhlsson
Added following support:
107
  
108
  /* Set GPGME home directory */
109
  rc = gpgme_get_engine_info (&engine_info);
110
  if (rc != GPG_ERR_NO_ERROR){
111
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
112
	    gpgme_strsource(rc), gpgme_strerror(rc));
113
    return -1;
114
  }
115
  while(engine_info != NULL){
116
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
117
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
118
			    engine_info->file_name, homedir);
119
      break;
120
    }
121
    engine_info = engine_info->next;
122
  }
123
  if(engine_info == NULL){
124
    fprintf(stderr, "Could not set home dir to %s\n", homedir);
125
    return -1;
126
  }
127
  
128
  /* Create new GPGME data buffer from packet buffer */
129
  rc = gpgme_data_new_from_mem(&dh_crypto, packet, packet_size, 0);
130
  if (rc != GPG_ERR_NO_ERROR){
131
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
132
	    gpgme_strsource(rc), gpgme_strerror(rc));
133
    return -1;
134
  }
135
  
136
  /* Create new empty GPGME data buffer for the plaintext */
137
  rc = gpgme_data_new(&dh_plain);
138
  if (rc != GPG_ERR_NO_ERROR){
139
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
140
	    gpgme_strsource(rc), gpgme_strerror(rc));
141
    return -1;
142
  }
143
  
144
  /* Create new GPGME "context" */
145
  rc = gpgme_new(&ctx);
146
  if (rc != GPG_ERR_NO_ERROR){
147
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
148
	    gpgme_strsource(rc), gpgme_strerror(rc));
149
    return -1;
150
  }
151
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
152
  /* Decrypt data from the FILE pointer to the plaintext data
153
     buffer */
13 by Björn Påhlsson
Added following support:
154
  rc = gpgme_op_decrypt(ctx, dh_crypto, dh_plain);
155
  if (rc != GPG_ERR_NO_ERROR){
156
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
157
	    gpgme_strsource(rc), gpgme_strerror(rc));
158
    return -1;
159
  }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
160
161
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
162
    fprintf(stderr, "Decryption of OpenPGP packet succeeded\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
163
  }
164
165
  if (debug){
166
    gpgme_decrypt_result_t result;
167
    result = gpgme_op_decrypt_result(ctx);
168
    if (result == NULL){
169
      fprintf(stderr, "gpgme_op_decrypt_result failed\n");
170
    } else {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
171
      fprintf(stderr, "Unsupported algorithm: %s\n",
172
	      result->unsupported_algorithm);
173
      fprintf(stderr, "Wrong key usage: %d\n",
174
	      result->wrong_key_usage);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
175
      if(result->file_name != NULL){
176
	fprintf(stderr, "File name: %s\n", result->file_name);
177
      }
178
      gpgme_recipient_t recipient;
179
      recipient = result->recipients;
180
      if(recipient){
181
	while(recipient != NULL){
182
	  fprintf(stderr, "Public key algorithm: %s\n",
183
		  gpgme_pubkey_algo_name(recipient->pubkey_algo));
184
	  fprintf(stderr, "Key ID: %s\n", recipient->keyid);
185
	  fprintf(stderr, "Secret key available: %s\n",
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
186
		  recipient->status == GPG_ERR_NO_SECKEY
187
		  ? "No" : "Yes");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
188
	  recipient = recipient->next;
189
	}
190
      }
191
    }
192
  }
13 by Björn Påhlsson
Added following support:
193
  
194
  /* Delete the GPGME FILE pointer cryptotext data buffer */
195
  gpgme_data_release(dh_crypto);
196
  
197
  /* Seek back to the beginning of the GPGME plaintext data buffer */
24.1.5 by Björn Påhlsson
plugbasedclient:
198
  if (gpgme_data_seek(dh_plain, (off_t) 0, SEEK_SET) == -1){
199
    perror("pgpme_data_seek");
200
  }
201
  
13 by Björn Påhlsson
Added following support:
202
  *new_packet = 0;
203
  while(true){
204
    if (new_packet_length + BUFFER_SIZE > new_packet_capacity){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
205
      *new_packet = realloc(*new_packet,
206
			    (unsigned int)new_packet_capacity
207
			    + BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
208
      if (*new_packet == NULL){
209
	perror("realloc");
210
	return -1;
211
      }
212
      new_packet_capacity += BUFFER_SIZE;
213
    }
214
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
215
    ret = gpgme_data_read(dh_plain, *new_packet + new_packet_length,
216
			  BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
217
    /* Print the data, if any */
218
    if (ret == 0){
219
      break;
220
    }
221
    if(ret < 0){
222
      perror("gpgme_data_read");
223
      return -1;
224
    }
225
    new_packet_length += ret;
226
  }
227
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
228
  /* FIXME: check characters before printing to screen so to not print
229
     terminal control characters */
230
  /*   if(debug){ */
231
  /*     fprintf(stderr, "decrypted password is: "); */
232
  /*     fwrite(*new_packet, 1, new_packet_length, stderr); */
233
  /*     fprintf(stderr, "\n"); */
234
  /*   } */
235
  
236
  /* Delete the GPGME plaintext data buffer */
13 by Björn Påhlsson
Added following support:
237
  gpgme_data_release(dh_plain);
238
  return new_packet_length;
239
}
240
241
static const char * safer_gnutls_strerror (int value) {
242
  const char *ret = gnutls_strerror (value);
243
  if (ret == NULL)
244
    ret = "(unknown)";
245
  return ret;
246
}
247
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
248
void debuggnutls(__attribute__((unused)) int level,
249
		 const char* string){
13 by Björn Påhlsson
Added following support:
250
  fprintf(stderr, "%s", string);
251
}
252
253
int initgnutls(encrypted_session *es){
254
  const char *err;
255
  int ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
256
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
257
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
258
    fprintf(stderr, "Initializing GnuTLS\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
259
  }
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.
260
13 by Björn Påhlsson
Added following support:
261
  if ((ret = gnutls_global_init ())
262
      != GNUTLS_E_SUCCESS) {
263
    fprintf (stderr, "global_init: %s\n", safer_gnutls_strerror(ret));
264
    return -1;
265
  }
266
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
267
  if (debug){
268
    gnutls_global_set_log_level(11);
269
    gnutls_global_set_log_function(debuggnutls);
270
  }
271
  
13 by Björn Påhlsson
Added following support:
272
  /* openpgp credentials */
273
  if ((ret = gnutls_certificate_allocate_credentials (&es->cred))
274
      != GNUTLS_E_SUCCESS) {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
275
    fprintf (stderr, "memory error: %s\n",
276
	     safer_gnutls_strerror(ret));
13 by Björn Påhlsson
Added following support:
277
    return -1;
278
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
279
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
280
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
281
    fprintf(stderr, "Attempting to use OpenPGP certificate %s"
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.
282
	    " and keyfile %s as GnuTLS credentials\n", certfile,
283
	    certkey);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
284
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
285
  
13 by Björn Påhlsson
Added following support:
286
  ret = gnutls_certificate_set_openpgp_key_file
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.
287
    (es->cred, certfile, certkey, GNUTLS_OPENPGP_FMT_BASE64);
13 by Björn Påhlsson
Added following support:
288
  if (ret != GNUTLS_E_SUCCESS) {
289
    fprintf
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
290
      (stderr, "Error[%d] while reading the OpenPGP key pair ('%s',"
291
       " '%s')\n",
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.
292
       ret, certfile, certkey);
13 by Björn Påhlsson
Added following support:
293
    fprintf(stdout, "The Error is: %s\n",
294
	    safer_gnutls_strerror(ret));
295
    return -1;
296
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
297
  
298
  //GnuTLS server initialization
13 by Björn Påhlsson
Added following support:
299
  if ((ret = gnutls_dh_params_init (&es->dh_params))
300
      != GNUTLS_E_SUCCESS) {
301
    fprintf (stderr, "Error in dh parameter initialization: %s\n",
302
	     safer_gnutls_strerror(ret));
303
    return -1;
304
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
305
  
13 by Björn Påhlsson
Added following support:
306
  if ((ret = gnutls_dh_params_generate2 (es->dh_params, DH_BITS))
307
      != GNUTLS_E_SUCCESS) {
308
    fprintf (stderr, "Error in prime generation: %s\n",
309
	     safer_gnutls_strerror(ret));
310
    return -1;
311
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
312
  
13 by Björn Påhlsson
Added following support:
313
  gnutls_certificate_set_dh_params (es->cred, es->dh_params);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
314
  
315
  // GnuTLS session creation
13 by Björn Påhlsson
Added following support:
316
  if ((ret = gnutls_init (&es->session, GNUTLS_SERVER))
317
      != GNUTLS_E_SUCCESS){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
318
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
13 by Björn Påhlsson
Added following support:
319
	    safer_gnutls_strerror(ret));
320
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
321
  
13 by Björn Påhlsson
Added following support:
322
  if ((ret = gnutls_priority_set_direct (es->session, "NORMAL", &err))
323
      != GNUTLS_E_SUCCESS) {
324
    fprintf(stderr, "Syntax error at: %s\n", err);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
325
    fprintf(stderr, "GnuTLS error: %s\n",
13 by Björn Påhlsson
Added following support:
326
	    safer_gnutls_strerror(ret));
327
    return -1;
328
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
329
  
13 by Björn Påhlsson
Added following support:
330
  if ((ret = gnutls_credentials_set
331
       (es->session, GNUTLS_CRD_CERTIFICATE, es->cred))
332
      != GNUTLS_E_SUCCESS) {
333
    fprintf(stderr, "Error setting a credentials set: %s\n",
334
	    safer_gnutls_strerror(ret));
335
    return -1;
336
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
337
  
13 by Björn Påhlsson
Added following support:
338
  /* ignore client certificate if any. */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
339
  gnutls_certificate_server_set_request (es->session,
340
					 GNUTLS_CERT_IGNORE);
13 by Björn Påhlsson
Added following support:
341
  
342
  gnutls_dh_set_prime_bits (es->session, DH_BITS);
343
  
344
  return 0;
345
}
346
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
347
void empty_log(__attribute__((unused)) AvahiLogLevel level,
348
	       __attribute__((unused)) const char *txt){}
13 by Björn Påhlsson
Added following support:
349
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
350
int start_mandos_communication(const char *ip, uint16_t port,
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
351
			       AvahiIfIndex if_index){
13 by Björn Påhlsson
Added following support:
352
  int ret, tcp_sd;
353
  struct sockaddr_in6 to;
354
  encrypted_session es;
355
  char *buffer = NULL;
356
  char *decrypted_buffer;
357
  size_t buffer_length = 0;
358
  size_t buffer_capacity = 0;
359
  ssize_t decrypted_buffer_size;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
360
  size_t written = 0;
13 by Björn Påhlsson
Added following support:
361
  int retval = 0;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
362
  char interface[IF_NAMESIZE];
363
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
364
  if(debug){
28 by Teddy Hogeborn
* server.conf: New file.
365
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
366
	    ip, port);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
367
  }
13 by Björn Påhlsson
Added following support:
368
  
369
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
370
  if(tcp_sd < 0) {
371
    perror("socket");
372
    return -1;
373
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
374
  
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
375
  if(if_indextoname((unsigned int)if_index, interface) == NULL){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
376
    if(debug){
377
      perror("if_indextoname");
378
    }
379
    return -1;
380
  }
381
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
382
  if(debug){
383
    fprintf(stderr, "Binding to interface %s\n", interface);
384
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
385
  
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
386
  memset(&to,0,sizeof(to));	/* Spurious warning */
13 by Björn Påhlsson
Added following support:
387
  to.sin6_family = AF_INET6;
18 by Teddy Hogeborn
* plugins.d/Makefile: Removed
388
  ret = inet_pton(AF_INET6, ip, &to.sin6_addr);
13 by Björn Påhlsson
Added following support:
389
  if (ret < 0 ){
390
    perror("inet_pton");
391
    return -1;
392
  }  
393
  if(ret == 0){
394
    fprintf(stderr, "Bad address: %s\n", ip);
395
    return -1;
396
  }
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
397
  to.sin6_port = htons(port);	/* Spurious warning */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
398
  
399
  to.sin6_scope_id = (uint32_t)if_index;
400
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
401
  if(debug){
28 by Teddy Hogeborn
* server.conf: New file.
402
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
403
/*     char addrstr[INET6_ADDRSTRLEN]; */
404
/*     if(inet_ntop(to.sin6_family, &(to.sin6_addr), addrstr, */
405
/* 		 sizeof(addrstr)) == NULL){ */
406
/*       perror("inet_ntop"); */
407
/*     } else { */
408
/*       fprintf(stderr, "Really connecting to: %s, port %d\n", */
409
/* 	      addrstr, ntohs(to.sin6_port)); */
410
/*     } */
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
411
  }
13 by Björn Påhlsson
Added following support:
412
  
413
  ret = connect(tcp_sd, (struct sockaddr *) &to, sizeof(to));
414
  if (ret < 0){
415
    perror("connect");
416
    return -1;
417
  }
418
  
419
  ret = initgnutls (&es);
420
  if (ret != 0){
421
    retval = -1;
422
    return -1;
423
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
424
  
425
  gnutls_transport_set_ptr (es.session,
426
			    (gnutls_transport_ptr_t) tcp_sd);
427
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
428
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
429
    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
430
  }
431
  
13 by Björn Påhlsson
Added following support:
432
  ret = gnutls_handshake (es.session);
433
  
434
  if (ret != GNUTLS_E_SUCCESS){
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
435
    if(debug){
436
      fprintf(stderr, "\n*** Handshake failed ***\n");
437
      gnutls_perror (ret);
438
    }
13 by Björn Påhlsson
Added following support:
439
    retval = -1;
440
    goto exit;
441
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
442
  
443
  //Retrieve OpenPGP packet that contains the wanted password
444
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
445
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
446
    fprintf(stderr, "Retrieving pgp encrypted password from %s\n",
447
	    ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
448
  }
449
13 by Björn Påhlsson
Added following support:
450
  while(true){
451
    if (buffer_length + BUFFER_SIZE > buffer_capacity){
452
      buffer = realloc(buffer, buffer_capacity + BUFFER_SIZE);
453
      if (buffer == NULL){
454
	perror("realloc");
455
	goto exit;
456
      }
457
      buffer_capacity += BUFFER_SIZE;
458
    }
459
    
460
    ret = gnutls_record_recv
461
      (es.session, buffer+buffer_length, BUFFER_SIZE);
462
    if (ret == 0){
463
      break;
464
    }
465
    if (ret < 0){
466
      switch(ret){
467
      case GNUTLS_E_INTERRUPTED:
468
      case GNUTLS_E_AGAIN:
469
	break;
470
      case GNUTLS_E_REHANDSHAKE:
471
	ret = gnutls_handshake (es.session);
472
	if (ret < 0){
473
	  fprintf(stderr, "\n*** Handshake failed ***\n");
474
	  gnutls_perror (ret);
475
	  retval = -1;
476
	  goto exit;
477
	}
478
	break;
479
      default:
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
480
	fprintf(stderr, "Unknown error while reading data from"
481
		" encrypted session with mandos server\n");
13 by Björn Påhlsson
Added following support:
482
	retval = -1;
483
	gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
484
	goto exit;
485
      }
486
    } else {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
487
      buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
488
    }
489
  }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
490
  
13 by Björn Påhlsson
Added following support:
491
  if (buffer_length > 0){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
492
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
493
					       buffer_length,
494
					       &decrypted_buffer,
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.
495
					       certdir);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
496
    if (decrypted_buffer_size >= 0){
28 by Teddy Hogeborn
* server.conf: New file.
497
      while(written < (size_t) decrypted_buffer_size){
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
498
	ret = (int)fwrite (decrypted_buffer + written, 1,
499
			   (size_t)decrypted_buffer_size - written,
500
			   stdout);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
501
	if(ret == 0 and ferror(stdout)){
502
	  if(debug){
503
	    fprintf(stderr, "Error writing encrypted data: %s\n",
504
		    strerror(errno));
505
	  }
506
	  retval = -1;
507
	  break;
508
	}
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
509
	written += (size_t)ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
510
      }
13 by Björn Påhlsson
Added following support:
511
      free(decrypted_buffer);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
512
    } else {
513
      retval = -1;
13 by Björn Påhlsson
Added following support:
514
    }
515
  }
516
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
517
  //shutdown procedure
518
519
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
520
    fprintf(stderr, "Closing TLS session\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
521
  }
522
13 by Björn Påhlsson
Added following support:
523
  free(buffer);
524
  gnutls_bye (es.session, GNUTLS_SHUT_RDWR);
525
 exit:
526
  close(tcp_sd);
527
  gnutls_deinit (es.session);
528
  gnutls_certificate_free_credentials (es.cred);
529
  gnutls_global_deinit ();
530
  return retval;
531
}
532
533
static AvahiSimplePoll *simple_poll = NULL;
534
static AvahiServer *server = NULL;
535
536
static void resolve_callback(
537
    AvahiSServiceResolver *r,
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
538
    AvahiIfIndex interface,
13 by Björn Påhlsson
Added following support:
539
    AVAHI_GCC_UNUSED AvahiProtocol protocol,
540
    AvahiResolverEvent event,
541
    const char *name,
542
    const char *type,
543
    const char *domain,
544
    const char *host_name,
545
    const AvahiAddress *address,
546
    uint16_t port,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
547
    AVAHI_GCC_UNUSED AvahiStringList *txt,
548
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
13 by Björn Påhlsson
Added following support:
549
    AVAHI_GCC_UNUSED void* userdata) {
550
    
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
551
  assert(r);			/* Spurious warning */
552
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
553
  /* Called whenever a service has been resolved successfully or
554
     timed out */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
555
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
556
  switch (event) {
557
  default:
558
  case AVAHI_RESOLVER_FAILURE:
559
    fprintf(stderr, "(Resolver) Failed to resolve service '%s' of"
560
	    " type '%s' in domain '%s': %s\n", name, type, domain,
561
	    avahi_strerror(avahi_server_errno(server)));
562
    break;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
563
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
564
  case AVAHI_RESOLVER_FOUND:
565
    {
566
      char ip[AVAHI_ADDRESS_STR_MAX];
567
      avahi_address_snprint(ip, sizeof(ip), address);
568
      if(debug){
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
569
	fprintf(stderr, "Mandos server \"%s\" found on %s (%s) on"
570
		" port %d\n", name, host_name, ip, port);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
571
      }
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
572
      int ret = start_mandos_communication(ip, port, interface);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
573
      if (ret == 0){
574
	exit(EXIT_SUCCESS);
575
      }
13 by Björn Påhlsson
Added following support:
576
    }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
577
  }
578
  avahi_s_service_resolver_free(r);
13 by Björn Påhlsson
Added following support:
579
}
580
581
static void browse_callback(
582
    AvahiSServiceBrowser *b,
583
    AvahiIfIndex interface,
584
    AvahiProtocol protocol,
585
    AvahiBrowserEvent event,
586
    const char *name,
587
    const char *type,
588
    const char *domain,
589
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
590
    void* userdata) {
591
    
592
    AvahiServer *s = userdata;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
593
    assert(b);			/* Spurious warning */
594
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
595
    /* Called whenever a new services becomes available on the LAN or
596
       is removed from the LAN */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
597
    
13 by Björn Påhlsson
Added following support:
598
    switch (event) {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
599
    default:
600
    case AVAHI_BROWSER_FAILURE:
601
      
602
      fprintf(stderr, "(Browser) %s\n",
603
	      avahi_strerror(avahi_server_errno(server)));
604
      avahi_simple_poll_quit(simple_poll);
605
      return;
606
      
607
    case AVAHI_BROWSER_NEW:
608
      /* We ignore the returned resolver object. In the callback
609
	 function we free it. If the server is terminated before
610
	 the callback function is called the server will free
611
	 the resolver for us. */
612
      
613
      if (!(avahi_s_service_resolver_new(s, interface, protocol, name,
614
					 type, domain,
615
					 AVAHI_PROTO_INET6, 0,
616
					 resolve_callback, s)))
617
	fprintf(stderr, "Failed to resolve service '%s': %s\n", name,
618
		avahi_strerror(avahi_server_errno(s)));
619
      break;
620
      
621
    case AVAHI_BROWSER_REMOVE:
622
      break;
623
      
624
    case AVAHI_BROWSER_ALL_FOR_NOW:
625
    case AVAHI_BROWSER_CACHE_EXHAUSTED:
626
      break;
13 by Björn Påhlsson
Added following support:
627
    }
628
}
629
24.1.5 by Björn Påhlsson
plugbasedclient:
630
/* combinds file name and path and returns the malloced new string. som sane checks could/should be added */
631
const char *combinepath(const char *first, const char *second){
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.
632
  char *tmp;
24.1.5 by Björn Påhlsson
plugbasedclient:
633
  tmp = malloc(strlen(first) + strlen(second) + 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.
634
  if (tmp == NULL){
635
    perror("malloc");
636
    return NULL;
637
  }
638
  strcpy(tmp, first);
24.1.5 by Björn Påhlsson
plugbasedclient:
639
  if (first[0] != '\0' and first[strlen(first) - 1] != '/'){
640
    strcat(tmp, "/");
641
  }
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.
642
  strcat(tmp, second);
643
  return tmp;
644
}
645
646
13 by Björn Påhlsson
Added following support:
647
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
648
    AvahiServerConfig config;
649
    AvahiSServiceBrowser *sb = NULL;
650
    int error;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
651
    int ret;
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
652
    int returncode = EXIT_SUCCESS;
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
653
    const char *interface = NULL;
654
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
28 by Teddy Hogeborn
* server.conf: New file.
655
    char *connect_to = NULL;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
656
    
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
657
    while (true){
658
      static struct option long_options[] = {
659
	{"debug", no_argument, (int *)&debug, 1},
30 by Teddy Hogeborn
Merge.
660
	{"connect", required_argument, 0, 'C'},
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
661
	{"interface", required_argument, 0, 'i'},
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.
662
	{"certdir", required_argument, 0, 'd'},
663
	{"certkey", required_argument, 0, 'c'},
664
	{"certfile", required_argument, 0, 'k'},
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
665
	{0, 0, 0, 0} };
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
666
      
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
667
      int option_index = 0;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
668
      ret = getopt_long (argc, argv, "i:", long_options,
669
			 &option_index);
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
670
      
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
671
      if (ret == -1){
672
	break;
673
      }
674
      
675
      switch(ret){
676
      case 0:
677
	break;
678
      case 'i':
679
	interface = optarg;
680
	break;
30 by Teddy Hogeborn
Merge.
681
      case 'C':
682
	connect_to = optarg;
683
	break;
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.
684
      case 'd':
685
	certdir = optarg;
686
	break;
687
      case 'c':
688
	certfile = optarg;
689
	break;
690
      case 'k':
691
	certkey = optarg;
692
	break;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
693
      default:
694
	exit(EXIT_FAILURE);
695
      }
696
    }
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.
697
24.1.5 by Björn Påhlsson
plugbasedclient:
698
    certfile = combinepath(certdir, certfile);
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.
699
    if (certfile == NULL){
700
      goto exit;
701
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
702
    
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
703
    if(interface != NULL){
704
      if_index = (AvahiIfIndex) if_nametoindex(interface);
705
      if(if_index == 0){
706
	fprintf(stderr, "No such interface: \"%s\"\n", interface);
707
	exit(EXIT_FAILURE);
708
      }
28 by Teddy Hogeborn
* server.conf: New file.
709
    }
710
    
711
    if(connect_to != NULL){
712
      /* Connect directly, do not use Zeroconf */
713
      /* (Mainly meant for debugging) */
714
      char *address = strrchr(connect_to, ':');
715
      if(address == NULL){
716
        fprintf(stderr, "No colon in address\n");
717
	exit(EXIT_FAILURE);
718
      }
719
      errno = 0;
720
      uint16_t port = (uint16_t) strtol(address+1, NULL, 10);
721
      if(errno){
722
	perror("Bad port number");
723
	exit(EXIT_FAILURE);
724
      }
725
      *address = '\0';
726
      address = connect_to;
727
      ret = start_mandos_communication(address, port, if_index);
728
      if(ret < 0){
729
	exit(EXIT_FAILURE);
730
      } else {
731
	exit(EXIT_SUCCESS);
732
      }
733
    }
734
    
24.1.5 by Björn Påhlsson
plugbasedclient:
735
    certkey = combinepath(certdir, certkey);
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.
736
    if (certkey == NULL){
737
      goto exit;
738
    }
30 by Teddy Hogeborn
Merge.
739
    
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
740
    if (not debug){
741
      avahi_set_log_function(empty_log);
742
    }
13 by Björn Påhlsson
Added following support:
743
    
744
    /* Initialize the psuedo-RNG */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
745
    srand((unsigned int) time(NULL));
13 by Björn Påhlsson
Added following support:
746
747
    /* Allocate main loop object */
748
    if (!(simple_poll = avahi_simple_poll_new())) {
749
        fprintf(stderr, "Failed to create simple poll object.\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
750
	
751
        goto exit;
13 by Björn Påhlsson
Added following support:
752
    }
753
754
    /* Do not publish any local records */
755
    avahi_server_config_init(&config);
756
    config.publish_hinfo = 0;
757
    config.publish_addresses = 0;
758
    config.publish_workstation = 0;
759
    config.publish_domain = 0;
760
761
    /* Allocate a new server */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
762
    server = avahi_server_new(avahi_simple_poll_get(simple_poll),
763
			      &config, NULL, NULL, &error);
13 by Björn Påhlsson
Added following support:
764
765
    /* Free the configuration data */
766
    avahi_server_config_free(&config);
767
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
768
    /* Check if creating the server object succeeded */
13 by Björn Påhlsson
Added following support:
769
    if (!server) {
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
770
        fprintf(stderr, "Failed to create server: %s\n",
771
		avahi_strerror(error));
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
772
	returncode = EXIT_FAILURE;
773
        goto exit;
13 by Björn Påhlsson
Added following support:
774
    }
775
    
776
    /* Create the service browser */
29 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Changed
777
    sb = avahi_s_service_browser_new(server, if_index,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
778
				     AVAHI_PROTO_INET6,
779
				     "_mandos._tcp", NULL, 0,
780
				     browse_callback, server);
781
    if (!sb) {
782
        fprintf(stderr, "Failed to create service browser: %s\n",
783
		avahi_strerror(avahi_server_errno(server)));
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
784
	returncode = EXIT_FAILURE;
785
        goto exit;
13 by Björn Påhlsson
Added following support:
786
    }
787
    
788
    /* Run the main loop */
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
789
790
    if (debug){
791
      fprintf(stderr, "Starting avahi loop search\n");
792
    }
793
    
13 by Björn Påhlsson
Added following support:
794
    avahi_simple_poll_loop(simple_poll);
795
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
796
 exit:
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
797
798
    if (debug){
799
      fprintf(stderr, "%s exiting\n", argv[0]);
800
    }
13 by Björn Påhlsson
Added following support:
801
    
802
    /* Cleanup things */
803
    if (sb)
804
        avahi_s_service_browser_free(sb);
805
    
806
    if (server)
807
        avahi_server_free(server);
808
809
    if (simple_poll)
810
        avahi_simple_poll_free(simple_poll);
24.1.5 by Björn Påhlsson
plugbasedclient:
811
    free(certfile);
812
    free(certkey);
813
    
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
814
    return returncode;
13 by Björn Påhlsson
Added following support:
815
}