/mandos/release

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