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