/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
/*
261 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Fix name in header.
3
 * Mandos-client - get and decrypt data from a Mandos server
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
4
 *
5
 * This program is partly derived from an example program for an Avahi
6
 * service browser, downloaded from
7
 * <http://avahi.org/browser/examples/core-browse-services.c>.  This
8
 * includes the following functions: "resolve_callback",
9
 * "browse_callback", and parts of "main".
10
 * 
28 by Teddy Hogeborn
* server.conf: New file.
11
 * Everything else is
466 by Teddy Hogeborn
Update copyright year to "2011" wherever appropriate.
12
 * Copyright © 2008-2011 Teddy Hogeborn
13
 * Copyright © 2008-2011 Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
14
 * 
15
 * This program is free software: you can redistribute it and/or
16
 * modify it under the terms of the GNU General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 * 
20
 * This program is distributed in the hope that it will be useful, but
21
 * WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23
 * General Public License for more details.
24
 * 
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program.  If not, see
27
 * <http://www.gnu.org/licenses/>.
28
 * 
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
29
 * Contact the authors at <mandos@fukt.bsnet.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
30
 */
31
28 by Teddy Hogeborn
* server.conf: New file.
32
/* Needed by GPGME, specifically gpgme_data_seek() */
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
33
#ifndef _LARGEFILE_SOURCE
13 by Björn Påhlsson
Added following support:
34
#define _LARGEFILE_SOURCE
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
35
#endif
36
#ifndef _FILE_OFFSET_BITS
13 by Björn Påhlsson
Added following support:
37
#define _FILE_OFFSET_BITS 64
317 by Teddy Hogeborn
Use "getconf" to get correct LFS compile and link flags.
38
#endif
13 by Björn Påhlsson
Added following support:
39
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
40
#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY(), asprintf() */
24.1.10 by Björn Påhlsson
merge commit
41
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
42
#include <stdio.h>		/* fprintf(), stderr, fwrite(),
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
43
				   stdout, ferror(), remove() */
24.1.26 by Björn Påhlsson
tally count of used symbols
44
#include <stdint.h> 		/* uint16_t, uint32_t */
45
#include <stddef.h>		/* NULL, size_t, ssize_t */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
46
#include <stdlib.h> 		/* free(), EXIT_SUCCESS, srand(),
47
				   strtof(), abort() */
304 by Teddy Hogeborn
Four new interrelated features:
48
#include <stdbool.h>		/* bool, false, true */
24.1.29 by Björn Påhlsson
Added more header file comments
49
#include <string.h>		/* memset(), strcmp(), strlen(),
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
50
				   strerror(), asprintf(), strcpy() */
304 by Teddy Hogeborn
Four new interrelated features:
51
#include <sys/ioctl.h>		/* ioctl */
24.1.26 by Björn Påhlsson
tally count of used symbols
52
#include <sys/types.h>		/* socket(), inet_pton(), sockaddr,
24.1.29 by Björn Påhlsson
Added more header file comments
53
				   sockaddr_in6, PF_INET6,
304 by Teddy Hogeborn
Four new interrelated features:
54
				   SOCK_STREAM, uid_t, gid_t, open(),
55
				   opendir(), DIR */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
56
#include <sys/stat.h>		/* open() */
24.1.26 by Björn Påhlsson
tally count of used symbols
57
#include <sys/socket.h>		/* socket(), struct sockaddr_in6,
304 by Teddy Hogeborn
Four new interrelated features:
58
				   inet_pton(), connect() */
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
59
#include <fcntl.h>		/* open() */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
60
#include <dirent.h>		/* opendir(), struct dirent, readdir()
61
				 */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
62
#include <inttypes.h>		/* PRIu16, PRIdMAX, intmax_t,
63
				   strtoimax() */
24.1.29 by Björn Påhlsson
Added more header file comments
64
#include <assert.h>		/* assert() */
65
#include <errno.h>		/* perror(), errno */
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
66
#include <time.h>		/* nanosleep(), time(), sleep() */
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
67
#include <net/if.h>		/* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
24.1.26 by Björn Påhlsson
tally count of used symbols
68
				   SIOCSIFFLAGS, if_indextoname(),
69
				   if_nametoindex(), IF_NAMESIZE */
304 by Teddy Hogeborn
Four new interrelated features:
70
#include <netinet/in.h>		/* IN6_IS_ADDR_LINKLOCAL,
71
				   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
				*/
24.1.29 by Björn Påhlsson
Added more header file comments
73
#include <unistd.h>		/* close(), SEEK_SET, off_t, write(),
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
74
				   getuid(), getgid(), seteuid(),
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
75
				   setgid(), pause() */
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
76
#include <arpa/inet.h>		/* inet_pton(), htons, inet_ntop() */
304 by Teddy Hogeborn
Four new interrelated features:
77
#include <iso646.h>		/* not, or, and */
24.1.29 by Björn Påhlsson
Added more header file comments
78
#include <argp.h>		/* struct argp_option, error_t, struct
79
				   argp_state, struct argp,
80
				   argp_parse(), ARGP_KEY_ARG,
81
				   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
307 by Teddy Hogeborn
Merge from Björn:
82
#include <signal.h>		/* sigemptyset(), sigaddset(),
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
83
				   sigaction(), SIGTERM, sig_atomic_t,
84
				   raise() */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
85
#include <sysexits.h>		/* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
86
				   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
307 by Teddy Hogeborn
Merge from Björn:
87
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
88
#ifdef __linux__
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
89
#include <sys/klog.h> 		/* klogctl() */
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
90
#endif	/* __linux__ */
24.1.26 by Björn Påhlsson
tally count of used symbols
91
92
/* Avahi */
24.1.29 by Björn Påhlsson
Added more header file comments
93
/* All Avahi types, constants and functions
94
 Avahi*, avahi_*,
95
 AVAHI_* */
96
#include <avahi-core/core.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
97
#include <avahi-core/lookup.h>
24.1.29 by Björn Påhlsson
Added more header file comments
98
#include <avahi-core/log.h>
24.1.26 by Björn Påhlsson
tally count of used symbols
99
#include <avahi-common/simple-watch.h>
100
#include <avahi-common/malloc.h>
101
#include <avahi-common/error.h>
102
103
/* GnuTLS */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
104
#include <gnutls/gnutls.h>	/* All GnuTLS types, constants and
105
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
106
				   gnutls_*
24.1.26 by Björn Påhlsson
tally count of used symbols
107
				   init_gnutls_session(),
24.1.29 by Björn Påhlsson
Added more header file comments
108
				   GNUTLS_* */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
109
#include <gnutls/openpgp.h>
110
			  /* gnutls_certificate_set_openpgp_key_file(),
24.1.29 by Björn Påhlsson
Added more header file comments
111
				   GNUTLS_OPENPGP_FMT_BASE64 */
24.1.26 by Björn Påhlsson
tally count of used symbols
112
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
113
/* GPGME */
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
114
#include <gpgme.h> 		/* All GPGME types, constants and
115
				   functions:
24.1.29 by Björn Påhlsson
Added more header file comments
116
				   gpgme_*
24.1.26 by Björn Påhlsson
tally count of used symbols
117
				   GPGME_PROTOCOL_OpenPGP,
24.1.29 by Björn Påhlsson
Added more header file comments
118
				   GPG_ERR_NO_* */
13 by Björn Påhlsson
Added following support:
119
120
#define BUFFER_SIZE 256
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
121
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
122
#define PATHDIR "/conf/conf.d/mandos"
123
#define SECKEY "seckey.txt"
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
124
#define PUBKEY "pubkey.txt"
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
125
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
126
bool debug = false;
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
127
static const char mandos_protocol_version[] = "1";
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
128
const char *argp_program_version = "mandos-client " VERSION;
24.1.14 by Björn Påhlsson
mandosclient
129
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
130
static const char sys_class_net[] = "/sys/class/net";
131
char *connect_to = NULL;
24.1.10 by Björn Påhlsson
merge commit
132
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
133
/* Used for passing in values through the Avahi callback functions */
13 by Björn Påhlsson
Added following support:
134
typedef struct {
24.1.9 by Björn Påhlsson
not working midwork...
135
  AvahiSimplePoll *simple_poll;
136
  AvahiServer *server;
13 by Björn Påhlsson
Added following support:
137
  gnutls_certificate_credentials_t cred;
24.1.9 by Björn Påhlsson
not working midwork...
138
  unsigned int dh_bits;
24.1.13 by Björn Påhlsson
mandosclient
139
  gnutls_dh_params_t dh_params;
24.1.9 by Björn Påhlsson
not working midwork...
140
  const char *priority;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
141
  gpgme_ctx_t ctx;
24.1.9 by Björn Påhlsson
not working midwork...
142
} mandos_context;
13 by Björn Påhlsson
Added following support:
143
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
144
/* global context so signal handler can reach it*/
24.1.135 by Björn Påhlsson
Earlier signal handling
145
mandos_context mc = { .simple_poll = NULL, .server = NULL,
146
		      .dh_bits = 1024, .priority = "SECURE256"
147
		      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
148
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
149
sig_atomic_t quit_now = 0;
150
int signal_received = 0;
151
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
152
/*
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
153
 * Make additional room in "buffer" for at least BUFFER_SIZE more
154
 * bytes. "buffer_capacity" is how much is currently allocated,
155
 * "buffer_length" is how much is already used.
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
156
 */
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
157
size_t incbuffer(char **buffer, size_t buffer_length,
24.1.10 by Björn Påhlsson
merge commit
158
		  size_t buffer_capacity){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
159
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
24.1.12 by Björn Påhlsson
merge +
160
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
161
    if(buffer == NULL){
24.1.10 by Björn Påhlsson
merge commit
162
      return 0;
163
    }
164
    buffer_capacity += BUFFER_SIZE;
165
  }
166
  return buffer_capacity;
167
}
168
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
169
/* 
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
170
 * Initialize GPGME.
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
171
 */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
172
static bool init_gpgme(const char *seckey,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
173
		       const char *pubkey, const char *tempdir){
13 by Björn Påhlsson
Added following support:
174
  gpgme_error_t rc;
175
  gpgme_engine_info_t engine_info;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
176
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
177
  
178
  /*
288 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Use separate bool variable instead
179
   * Helper function to insert pub and seckey to the engine keyring.
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
180
   */
181
  bool import_key(const char *filename){
361 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
182
    int ret;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
183
    int fd;
184
    gpgme_data_t pgp_data;
185
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
186
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
187
    if(fd == -1){
188
      perror("open");
189
      return false;
190
    }
191
    
192
    rc = gpgme_data_new_from_fd(&pgp_data, fd);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
193
    if(rc != GPG_ERR_NO_ERROR){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
194
      fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
195
	      gpgme_strsource(rc), gpgme_strerror(rc));
196
      return false;
197
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
198
    
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
199
    rc = gpgme_op_import(mc.ctx, pgp_data);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
200
    if(rc != GPG_ERR_NO_ERROR){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
201
      fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
202
	      gpgme_strsource(rc), gpgme_strerror(rc));
203
      return false;
204
    }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
205
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
206
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
207
    if(ret == -1){
208
      perror("close");
209
    }
210
    gpgme_data_release(pgp_data);
211
    return true;
212
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
213
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
214
  if(debug){
307 by Teddy Hogeborn
Merge from Björn:
215
    fprintf(stderr, "Initializing GPGME\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
216
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
217
  
13 by Björn Påhlsson
Added following support:
218
  /* Init GPGME */
219
  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.
220
  rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
221
  if(rc != GPG_ERR_NO_ERROR){
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
222
    fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
223
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
224
    return false;
24.1.4 by Björn Påhlsson
Added optional parameters certdir, certkey and certfile that can be iven at start in the command line.
225
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
226
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
227
    /* Set GPGME home directory for the OpenPGP engine only */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
228
  rc = gpgme_get_engine_info(&engine_info);
229
  if(rc != GPG_ERR_NO_ERROR){
13 by Björn Påhlsson
Added following support:
230
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
231
	    gpgme_strsource(rc), gpgme_strerror(rc));
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
232
    return false;
13 by Björn Påhlsson
Added following support:
233
  }
234
  while(engine_info != NULL){
235
    if(engine_info->protocol == GPGME_PROTOCOL_OpenPGP){
236
      gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
237
			    engine_info->file_name, tempdir);
13 by Björn Påhlsson
Added following support:
238
      break;
239
    }
240
    engine_info = engine_info->next;
241
  }
242
  if(engine_info == NULL){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
243
    fprintf(stderr, "Could not set GPGME home dir to %s\n", tempdir);
244
    return false;
245
  }
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
246
  
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
247
  /* Create new GPGME "context" */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
248
  rc = gpgme_new(&(mc.ctx));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
249
  if(rc != GPG_ERR_NO_ERROR){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
250
    fprintf(stderr, "bad gpgme_new: %s: %s\n",
251
	    gpgme_strsource(rc), gpgme_strerror(rc));
252
    return false;
253
  }
254
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
255
  if(not import_key(pubkey) or not import_key(seckey)){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
256
    return false;
257
  }
258
  
355 by Teddy Hogeborn
* mandos: White-space fixes only.
259
  return true;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
260
}
261
262
/* 
263
 * Decrypt OpenPGP data.
264
 * Returns -1 on error
265
 */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
266
static ssize_t pgp_packet_decrypt(const char *cryptotext,
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
267
				  size_t crypto_size,
268
				  char **plaintext){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
269
  gpgme_data_t dh_crypto, dh_plain;
270
  gpgme_error_t rc;
271
  ssize_t ret;
272
  size_t plaintext_capacity = 0;
273
  ssize_t plaintext_length = 0;
274
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
275
  if(debug){
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
276
    fprintf(stderr, "Trying to decrypt OpenPGP data\n");
13 by Björn Påhlsson
Added following support:
277
  }
278
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
279
  /* Create new GPGME data buffer from memory cryptotext */
280
  rc = gpgme_data_new_from_mem(&dh_crypto, cryptotext, crypto_size,
281
			       0);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
282
  if(rc != GPG_ERR_NO_ERROR){
13 by Björn Påhlsson
Added following support:
283
    fprintf(stderr, "bad gpgme_data_new_from_mem: %s: %s\n",
284
	    gpgme_strsource(rc), gpgme_strerror(rc));
285
    return -1;
286
  }
287
  
288
  /* Create new empty GPGME data buffer for the plaintext */
289
  rc = gpgme_data_new(&dh_plain);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
290
  if(rc != GPG_ERR_NO_ERROR){
13 by Björn Påhlsson
Added following support:
291
    fprintf(stderr, "bad gpgme_data_new: %s: %s\n",
292
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
293
    gpgme_data_release(dh_crypto);
13 by Björn Påhlsson
Added following support:
294
    return -1;
295
  }
296
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
297
  /* Decrypt data from the cryptotext data buffer to the plaintext
298
     data buffer */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
299
  rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
300
  if(rc != GPG_ERR_NO_ERROR){
13 by Björn Påhlsson
Added following support:
301
    fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
302
	    gpgme_strsource(rc), gpgme_strerror(rc));
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
303
    plaintext_length = -1;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
304
    if(debug){
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
305
      gpgme_decrypt_result_t result;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
306
      result = gpgme_op_decrypt_result(mc.ctx);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
307
      if(result == NULL){
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
308
	fprintf(stderr, "gpgme_op_decrypt_result failed\n");
309
      } else {
310
	fprintf(stderr, "Unsupported algorithm: %s\n",
311
		result->unsupported_algorithm);
312
	fprintf(stderr, "Wrong key usage: %u\n",
313
		result->wrong_key_usage);
314
	if(result->file_name != NULL){
315
	  fprintf(stderr, "File name: %s\n", result->file_name);
316
	}
317
	gpgme_recipient_t recipient;
318
	recipient = result->recipients;
349 by Teddy Hogeborn
* plugins.d/mandos-client.c (pgp_packet_decrypt): Remove redundant
319
	while(recipient != NULL){
320
	  fprintf(stderr, "Public key algorithm: %s\n",
321
		  gpgme_pubkey_algo_name(recipient->pubkey_algo));
322
	  fprintf(stderr, "Key ID: %s\n", recipient->keyid);
323
	  fprintf(stderr, "Secret key available: %s\n",
324
		  recipient->status == GPG_ERR_NO_SECKEY
325
		  ? "No" : "Yes");
326
	  recipient = recipient->next;
99 by Teddy Hogeborn
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.
327
	}
328
      }
329
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
330
    goto decrypt_end;
13 by Björn Påhlsson
Added following support:
331
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
332
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
333
  if(debug){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
334
    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
335
  }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
336
  
13 by Björn Påhlsson
Added following support:
337
  /* Seek back to the beginning of the GPGME plaintext data buffer */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
338
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
339
    perror("gpgme_data_seek");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
340
    plaintext_length = -1;
341
    goto decrypt_end;
24.1.5 by Björn Påhlsson
plugbasedclient:
342
  }
343
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
344
  *plaintext = NULL;
13 by Björn Påhlsson
Added following support:
345
  while(true){
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
346
    plaintext_capacity = incbuffer(plaintext,
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
347
				      (size_t)plaintext_length,
24.1.12 by Björn Påhlsson
merge +
348
				      plaintext_capacity);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
349
    if(plaintext_capacity == 0){
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
350
	perror("incbuffer");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
351
	plaintext_length = -1;
352
	goto decrypt_end;
13 by Björn Påhlsson
Added following support:
353
    }
354
    
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
355
    ret = gpgme_data_read(dh_plain, *plaintext + plaintext_length,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
356
			  BUFFER_SIZE);
13 by Björn Påhlsson
Added following support:
357
    /* Print the data, if any */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
358
    if(ret == 0){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
359
      /* EOF */
13 by Björn Påhlsson
Added following support:
360
      break;
361
    }
362
    if(ret < 0){
363
      perror("gpgme_data_read");
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
364
      plaintext_length = -1;
365
      goto decrypt_end;
13 by Björn Påhlsson
Added following support:
366
    }
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
367
    plaintext_length += ret;
13 by Björn Påhlsson
Added following support:
368
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
369
  
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
370
  if(debug){
371
    fprintf(stderr, "Decrypted password is: ");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
372
    for(ssize_t i = 0; i < plaintext_length; i++){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
373
      fprintf(stderr, "%02hhX ", (*plaintext)[i]);
374
    }
375
    fprintf(stderr, "\n");
376
  }
377
  
378
 decrypt_end:
379
  
380
  /* Delete the GPGME cryptotext data buffer */
381
  gpgme_data_release(dh_crypto);
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
382
  
383
  /* Delete the GPGME plaintext data buffer */
13 by Björn Påhlsson
Added following support:
384
  gpgme_data_release(dh_plain);
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
385
  return plaintext_length;
13 by Björn Påhlsson
Added following support:
386
}
387
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
388
static const char * safer_gnutls_strerror(int value){
267 by Teddy Hogeborn
* plugins.d/mandos-client.c: Only comment changes.
389
  const char *ret = gnutls_strerror(value); /* Spurious warning from
390
					       -Wunreachable-code */
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
391
  if(ret == NULL)
13 by Björn Påhlsson
Added following support:
392
    ret = "(unknown)";
393
  return ret;
394
}
395
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
396
/* GnuTLS log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
397
static void debuggnutls(__attribute__((unused)) int level,
398
			const char* string){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
399
  fprintf(stderr, "GnuTLS: %s", string);
13 by Björn Påhlsson
Added following support:
400
}
401
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
402
static int init_gnutls_global(const char *pubkeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
403
			      const char *seckeyfilename){
13 by Björn Påhlsson
Added following support:
404
  int ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
405
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
406
  if(debug){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
407
    fprintf(stderr, "Initializing GnuTLS\n");
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
408
  }
24.1.29 by Björn Påhlsson
Added more header file comments
409
  
410
  ret = gnutls_global_init();
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
411
  if(ret != GNUTLS_E_SUCCESS){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
412
    fprintf(stderr, "GnuTLS global_init: %s\n",
413
	    safer_gnutls_strerror(ret));
13 by Björn Påhlsson
Added following support:
414
    return -1;
415
  }
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
416
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
417
  if(debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
418
    /* "Use a log level over 10 to enable all debugging options."
419
     * - GnuTLS manual
420
     */
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
421
    gnutls_global_set_log_level(11);
422
    gnutls_global_set_log_function(debuggnutls);
423
  }
424
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
425
  /* OpenPGP credentials */
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
426
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
427
  if(ret != GNUTLS_E_SUCCESS){
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
428
    fprintf(stderr, "GnuTLS memory error: %s\n",
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
429
	    safer_gnutls_strerror(ret));
430
    gnutls_global_deinit();
13 by Björn Påhlsson
Added following support:
431
    return -1;
432
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
433
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
434
  if(debug){
147 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Improved wording
435
    fprintf(stderr, "Attempting to use OpenPGP public key %s and"
436
	    " secret key %s as GnuTLS credentials\n", pubkeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
437
	    seckeyfilename);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
438
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
439
  
13 by Björn Påhlsson
Added following support:
440
  ret = gnutls_certificate_set_openpgp_key_file
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
441
    (mc.cred, pubkeyfilename, seckeyfilename,
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
442
     GNUTLS_OPENPGP_FMT_BASE64);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
443
  if(ret != GNUTLS_E_SUCCESS){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
444
    fprintf(stderr,
445
	    "Error[%d] while reading the OpenPGP key pair ('%s',"
76 by Teddy Hogeborn
* plugins.d/password-request.c (init_gnutls_global): Renamed
446
	    " '%s')\n", ret, pubkeyfilename, seckeyfilename);
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
447
    fprintf(stderr, "The GnuTLS error is: %s\n",
13 by Björn Påhlsson
Added following support:
448
	    safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
449
    goto globalfail;
13 by Björn Påhlsson
Added following support:
450
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
451
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
452
  /* GnuTLS server initialization */
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
453
  ret = gnutls_dh_params_init(&mc.dh_params);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
454
  if(ret != GNUTLS_E_SUCCESS){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
455
    fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
456
	    " %s\n", safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
457
    goto globalfail;
13 by Björn Påhlsson
Added following support:
458
  }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
459
  ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
460
  if(ret != GNUTLS_E_SUCCESS){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
461
    fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
462
	    safer_gnutls_strerror(ret));
24.1.20 by Björn Påhlsson
mandosclient
463
    goto globalfail;
13 by Björn Påhlsson
Added following support:
464
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
465
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
466
  gnutls_certificate_set_dh_params(mc.cred, mc.dh_params);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
467
  
24.1.13 by Björn Påhlsson
mandosclient
468
  return 0;
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
469
  
24.1.20 by Björn Påhlsson
mandosclient
470
 globalfail:
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
471
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
472
  gnutls_certificate_free_credentials(mc.cred);
24.1.26 by Björn Påhlsson
tally count of used symbols
473
  gnutls_global_deinit();
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
474
  gnutls_dh_params_deinit(mc.dh_params);
24.1.20 by Björn Påhlsson
mandosclient
475
  return -1;
24.1.13 by Björn Påhlsson
mandosclient
476
}
477
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
478
static int init_gnutls_session(gnutls_session_t *session){
24.1.13 by Björn Påhlsson
mandosclient
479
  int ret;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
480
  /* GnuTLS session creation */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
481
  do {
482
    ret = gnutls_init(session, GNUTLS_SERVER);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
483
    if(quit_now){
484
      return -1;
485
    }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
486
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
487
  if(ret != GNUTLS_E_SUCCESS){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
488
    fprintf(stderr, "Error in GnuTLS session initialization: %s\n",
13 by Björn Påhlsson
Added following support:
489
	    safer_gnutls_strerror(ret));
490
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
491
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
492
  {
493
    const char *err;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
494
    do {
495
      ret = gnutls_priority_set_direct(*session, mc.priority, &err);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
496
      if(quit_now){
497
	gnutls_deinit(*session);
498
	return -1;
499
      }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
500
    } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
501
    if(ret != GNUTLS_E_SUCCESS){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
502
      fprintf(stderr, "Syntax error at: %s\n", err);
503
      fprintf(stderr, "GnuTLS error: %s\n",
504
	      safer_gnutls_strerror(ret));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
505
      gnutls_deinit(*session);
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
506
      return -1;
507
    }
13 by Björn Påhlsson
Added following support:
508
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
509
  
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
510
  do {
511
    ret = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE,
512
				 mc.cred);
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
513
    if(quit_now){
514
      gnutls_deinit(*session);
515
      return -1;
516
    }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
517
  } while(ret == GNUTLS_E_INTERRUPTED or ret == GNUTLS_E_AGAIN);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
518
  if(ret != GNUTLS_E_SUCCESS){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
519
    fprintf(stderr, "Error setting GnuTLS credentials: %s\n",
13 by Björn Påhlsson
Added following support:
520
	    safer_gnutls_strerror(ret));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
521
    gnutls_deinit(*session);
13 by Björn Påhlsson
Added following support:
522
    return -1;
523
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
524
  
13 by Björn Påhlsson
Added following support:
525
  /* ignore client certificate if any. */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
526
  gnutls_certificate_server_set_request(*session, GNUTLS_CERT_IGNORE);
13 by Björn Påhlsson
Added following support:
527
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
528
  gnutls_dh_set_prime_bits(*session, mc.dh_bits);
13 by Björn Påhlsson
Added following support:
529
  
530
  return 0;
531
}
532
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
533
/* Avahi log function callback */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
534
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
535
		      __attribute__((unused)) const char *txt){}
13 by Björn Påhlsson
Added following support:
536
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
537
/* Called when a Mandos server is found */
36 by Teddy Hogeborn
* TODO: Converted to org-mode style
538
static int start_mandos_communication(const char *ip, uint16_t port,
24.1.9 by Björn Påhlsson
not working midwork...
539
				      AvahiIfIndex if_index,
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
540
				      int af){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
541
  int ret, tcp_sd = -1;
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
542
  ssize_t sret;
304 by Teddy Hogeborn
Four new interrelated features:
543
  union {
544
    struct sockaddr_in in;
545
    struct sockaddr_in6 in6;
546
  } to;
13 by Björn Påhlsson
Added following support:
547
  char *buffer = NULL;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
548
  char *decrypted_buffer = NULL;
13 by Björn Påhlsson
Added following support:
549
  size_t buffer_length = 0;
550
  size_t buffer_capacity = 0;
24.1.10 by Björn Påhlsson
merge commit
551
  size_t written;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
552
  int retval = -1;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
553
  gnutls_session_t session;
304 by Teddy Hogeborn
Four new interrelated features:
554
  int pf;			/* Protocol family */
555
  
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
556
  errno = 0;
557
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
558
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
559
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
560
    return -1;
561
  }
562
  
304 by Teddy Hogeborn
Four new interrelated features:
563
  switch(af){
564
  case AF_INET6:
565
    pf = PF_INET6;
566
    break;
567
  case AF_INET:
568
    pf = PF_INET;
569
    break;
570
  default:
571
    fprintf(stderr, "Bad address family: %d\n", af);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
572
    errno = EINVAL;
304 by Teddy Hogeborn
Four new interrelated features:
573
    return -1;
574
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
575
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
576
  ret = init_gnutls_session(&session);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
577
  if(ret != 0){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
578
    return -1;
579
  }
580
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
581
  if(debug){
304 by Teddy Hogeborn
Four new interrelated features:
582
    fprintf(stderr, "Setting up a TCP connection to %s, port %" PRIu16
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
583
	    "\n", ip, port);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
584
  }
13 by Björn Påhlsson
Added following support:
585
  
304 by Teddy Hogeborn
Four new interrelated features:
586
  tcp_sd = socket(pf, SOCK_STREAM, 0);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
587
  if(tcp_sd < 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
588
    int e = errno;
13 by Björn Påhlsson
Added following support:
589
    perror("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
590
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
591
    goto mandos_end;
592
  }
593
  
594
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
595
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
596
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
597
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
598
  
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
599
  memset(&to, 0, sizeof(to));
304 by Teddy Hogeborn
Four new interrelated features:
600
  if(af == AF_INET6){
325 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): "sin6_family"
601
    to.in6.sin6_family = (sa_family_t)af;
304 by Teddy Hogeborn
Four new interrelated features:
602
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
603
  } else {			/* IPv4 */
604
    to.in.sin_family = (sa_family_t)af;
605
    ret = inet_pton(af, ip, &to.in.sin_addr);
606
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
607
  if(ret < 0 ){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
608
    int e = errno;
13 by Björn Påhlsson
Added following support:
609
    perror("inet_pton");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
610
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
611
    goto mandos_end;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
612
  }
13 by Björn Påhlsson
Added following support:
613
  if(ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
614
    int e = errno;
13 by Björn Påhlsson
Added following support:
615
    fprintf(stderr, "Bad address: %s\n", ip);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
616
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
617
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
618
  }
304 by Teddy Hogeborn
Four new interrelated features:
619
  if(af == AF_INET6){
620
    to.in6.sin6_port = htons(port); /* Spurious warnings from
621
				       -Wconversion and
622
				       -Wunreachable-code */
623
    
624
    if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
625
       (&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
626
			      -Wunreachable-code*/
627
      if(if_index == AVAHI_IF_UNSPEC){
628
	fprintf(stderr, "An IPv6 link-local address is incomplete"
629
		" without a network interface\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
630
	errno = EINVAL;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
631
	goto mandos_end;
304 by Teddy Hogeborn
Four new interrelated features:
632
      }
633
      /* Set the network interface number as scope */
634
      to.in6.sin6_scope_id = (uint32_t)if_index;
635
    }
636
  } else {
637
    to.in.sin_port = htons(port); /* Spurious warnings from
267 by Teddy Hogeborn
* plugins.d/mandos-client.c: Only comment changes.
638
				     -Wconversion and
639
				     -Wunreachable-code */
304 by Teddy Hogeborn
Four new interrelated features:
640
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
641
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
642
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
643
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
644
    goto mandos_end;
645
  }
646
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
647
  if(debug){
304 by Teddy Hogeborn
Four new interrelated features:
648
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
649
      char interface[IF_NAMESIZE];
650
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
651
	perror("if_indextoname");
652
      } else {
653
	fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
654
		ip, interface, port);
655
      }
656
    } else {
657
      fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
658
	      port);
659
    }
660
    char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
661
		 INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
662
    const char *pcret;
663
    if(af == AF_INET6){
664
      pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
665
			sizeof(addrstr));
666
    } else {
667
      pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
668
			sizeof(addrstr));
669
    }
670
    if(pcret == NULL){
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
671
      perror("inet_ntop");
672
    } else {
673
      if(strcmp(addrstr, ip) != 0){
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
674
	fprintf(stderr, "Canonical address form: %s\n", addrstr);
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
675
      }
676
    }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
677
  }
13 by Björn Påhlsson
Added following support:
678
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
679
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
680
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
681
    goto mandos_end;
682
  }
683
  
304 by Teddy Hogeborn
Four new interrelated features:
684
  if(af == AF_INET6){
685
    ret = connect(tcp_sd, &to.in6, sizeof(to));
686
  } else {
687
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
688
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
689
  if(ret < 0){
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
690
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
691
      int e = errno;
692
      perror("connect");
693
      errno = e;
694
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
695
    goto mandos_end;
696
  }
697
  
698
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
699
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
700
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
701
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
702
  
24.1.12 by Björn Påhlsson
merge +
703
  const char *out = mandos_protocol_version;
24.1.10 by Björn Påhlsson
merge commit
704
  written = 0;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
705
  while(true){
24.1.10 by Björn Påhlsson
merge commit
706
    size_t out_size = strlen(out);
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
707
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
24.1.10 by Björn Påhlsson
merge commit
708
				   out_size - written));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
709
    if(ret == -1){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
710
      int e = errno;
24.1.10 by Björn Påhlsson
merge commit
711
      perror("write");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
712
      errno = e;
24.1.12 by Björn Påhlsson
merge +
713
      goto mandos_end;
24.1.10 by Björn Påhlsson
merge commit
714
    }
24.1.12 by Björn Påhlsson
merge +
715
    written += (size_t)ret;
24.1.10 by Björn Påhlsson
merge commit
716
    if(written < out_size){
717
      continue;
718
    } else {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
719
      if(out == mandos_protocol_version){
24.1.10 by Björn Påhlsson
merge commit
720
	written = 0;
721
	out = "\r\n";
722
      } else {
723
	break;
724
      }
725
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
726
  
727
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
728
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
729
      goto mandos_end;
730
    }
24.1.10 by Björn Påhlsson
merge commit
731
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
732
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
733
  if(debug){
734
    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
735
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
736
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
737
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
738
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
739
    goto mandos_end;
740
  }
741
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
742
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
743
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
744
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
745
    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
746
    goto mandos_end;
747
  }
748
  
363 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
749
  do {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
750
    ret = gnutls_handshake(session);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
751
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
752
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
753
      goto mandos_end;
754
    }
24.1.29 by Björn Påhlsson
Added more header file comments
755
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
13 by Björn Påhlsson
Added following support:
756
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
757
  if(ret != GNUTLS_E_SUCCESS){
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
758
    if(debug){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
759
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
760
      gnutls_perror(ret);
25 by Teddy Hogeborn
* mandos-clients.conf ([DEFAULT]): New section.
761
    }
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
762
    errno = EPROTO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
763
    goto mandos_end;
13 by Björn Påhlsson
Added following support:
764
  }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
765
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
766
  /* Read OpenPGP packet that contains the wanted password */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
767
  
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
768
  if(debug){
304 by Teddy Hogeborn
Four new interrelated features:
769
    fprintf(stderr, "Retrieving OpenPGP encrypted password from %s\n",
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
770
	    ip);
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
771
  }
143 by Teddy Hogeborn
* Makefile (mandos.8): Add dependency on "overview.xml" and
772
  
13 by Björn Påhlsson
Added following support:
773
  while(true){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
774
    
775
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
776
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
777
      goto mandos_end;
778
    }
779
    
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
780
    buffer_capacity = incbuffer(&buffer, buffer_length,
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
781
				   buffer_capacity);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
782
    if(buffer_capacity == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
783
      int e = errno;
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
784
      perror("incbuffer");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
785
      errno = e;
24.1.12 by Björn Påhlsson
merge +
786
      goto mandos_end;
13 by Björn Påhlsson
Added following support:
787
    }
788
    
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
789
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
790
      errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
791
      goto mandos_end;
792
    }
793
    
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
794
    sret = gnutls_record_recv(session, buffer+buffer_length,
795
			      BUFFER_SIZE);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
796
    if(sret == 0){
13 by Björn Påhlsson
Added following support:
797
      break;
798
    }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
799
    if(sret < 0){
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
800
      switch(sret){
13 by Björn Påhlsson
Added following support:
801
      case GNUTLS_E_INTERRUPTED:
802
      case GNUTLS_E_AGAIN:
803
	break;
804
      case GNUTLS_E_REHANDSHAKE:
363 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
805
	do {
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
806
	  ret = gnutls_handshake(session);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
807
	  
808
	  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
809
	    errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
810
	    goto mandos_end;
811
	  }
24.1.29 by Björn Påhlsson
Added more header file comments
812
	} while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
813
	if(ret < 0){
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
814
	  fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
815
	  gnutls_perror(ret);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
816
	  errno = EPROTO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
817
	  goto mandos_end;
13 by Björn Påhlsson
Added following support:
818
	}
819
	break;
820
      default:
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
821
	fprintf(stderr, "Unknown error while reading data from"
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
822
		" encrypted session with Mandos server\n");
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
823
	gnutls_bye(session, GNUTLS_SHUT_RDWR);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
824
	errno = EIO;
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
825
	goto mandos_end;
13 by Björn Påhlsson
Added following support:
826
      }
827
    } else {
257.1.2 by Mooie
Fixed warnings in the 64 bit build. Added explicit cast to int for
828
      buffer_length += (size_t) sret;
13 by Björn Påhlsson
Added following support:
829
    }
830
  }
15.1.1 by Björn Påhlsson
Added debugg support in form off --debug and --debug=mandosclient
831
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
832
  if(debug){
833
    fprintf(stderr, "Closing TLS session\n");
834
  }
835
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
836
  if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
837
    errno = EINTR;
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
838
    goto mandos_end;
839
  }
840
  
841
  do {
842
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
843
    if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
844
      errno = EINTR;
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
845
      goto mandos_end;
846
    }
847
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
848
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
849
  if(buffer_length > 0){
362 by Teddy Hogeborn
* plugin-runner.c (getplugin, add_environment, main): Handle EINTR
850
    ssize_t decrypted_buffer_size;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
851
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
852
					       buffer_length,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
853
					       &decrypted_buffer);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
854
    if(decrypted_buffer_size >= 0){
361 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
855
      
24.1.10 by Björn Påhlsson
merge commit
856
      written = 0;
28 by Teddy Hogeborn
* server.conf: New file.
857
      while(written < (size_t) decrypted_buffer_size){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
858
	if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
859
	  errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
860
	  goto mandos_end;
861
	}
862
	
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
863
	ret = (int)fwrite(decrypted_buffer + written, 1,
864
			  (size_t)decrypted_buffer_size - written,
865
			  stdout);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
866
	if(ret == 0 and ferror(stdout)){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
867
	  int e = errno;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
868
	  if(debug){
869
	    fprintf(stderr, "Error writing encrypted data: %s\n",
870
		    strerror(errno));
871
	  }
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
872
	  errno = e;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
873
	  goto mandos_end;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
874
	}
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
875
	written += (size_t)ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
876
      }
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
877
      retval = 0;
13 by Björn Påhlsson
Added following support:
878
    }
879
  }
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
880
  
881
  /* Shutdown procedure */
882
  
883
 mandos_end:
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
884
  {
885
    int e = errno;
886
    free(decrypted_buffer);
887
    free(buffer);
888
    if(tcp_sd >= 0){
889
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
890
    }
891
    if(ret == -1){
892
      if(e == 0){
893
	e = errno;
894
      }
895
      perror("close");
896
    }
897
    gnutls_deinit(session);
898
    if(quit_now){
899
      e = EINTR;
900
      retval = -1;
901
    }
902
    errno = e;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
903
  }
13 by Björn Påhlsson
Added following support:
904
  return retval;
905
}
906
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
907
static void resolve_callback(AvahiSServiceResolver *r,
908
			     AvahiIfIndex interface,
304 by Teddy Hogeborn
Four new interrelated features:
909
			     AvahiProtocol proto,
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
910
			     AvahiResolverEvent event,
911
			     const char *name,
912
			     const char *type,
913
			     const char *domain,
914
			     const char *host_name,
915
			     const AvahiAddress *address,
916
			     uint16_t port,
917
			     AVAHI_GCC_UNUSED AvahiStringList *txt,
918
			     AVAHI_GCC_UNUSED AvahiLookupResultFlags
919
			     flags,
307 by Teddy Hogeborn
Merge from Björn:
920
			     AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
921
  assert(r);
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
922
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
923
  /* Called whenever a service has been resolved successfully or
924
     timed out */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
925
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
926
  if(quit_now){
927
    return;
928
  }
929
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
930
  switch(event){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
931
  default:
932
  case AVAHI_RESOLVER_FAILURE:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
933
    fprintf(stderr, "(Avahi Resolver) Failed to resolve service '%s'"
934
	    " of type '%s' in domain '%s': %s\n", name, type, domain,
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
935
	    avahi_strerror(avahi_server_errno(mc.server)));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
936
    break;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
937
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
938
  case AVAHI_RESOLVER_FOUND:
939
    {
940
      char ip[AVAHI_ADDRESS_STR_MAX];
941
      avahi_address_snprint(ip, sizeof(ip), address);
942
      if(debug){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
943
	fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
268 by Teddy Hogeborn
Fixes for sscanf usage:
944
		PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
945
		ip, (intmax_t)interface, port);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
946
      }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
947
      int ret = start_mandos_communication(ip, port, interface,
304 by Teddy Hogeborn
Four new interrelated features:
948
					   avahi_proto_to_af(proto));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
949
      if(ret == 0){
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
950
	avahi_simple_poll_quit(mc.simple_poll);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
951
      }
13 by Björn Påhlsson
Added following support:
952
    }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
953
  }
954
  avahi_s_service_resolver_free(r);
13 by Björn Påhlsson
Added following support:
955
}
956
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
957
static void browse_callback(AvahiSServiceBrowser *b,
958
			    AvahiIfIndex interface,
959
			    AvahiProtocol protocol,
960
			    AvahiBrowserEvent event,
961
			    const char *name,
962
			    const char *type,
963
			    const char *domain,
964
			    AVAHI_GCC_UNUSED AvahiLookupResultFlags
965
			    flags,
307 by Teddy Hogeborn
Merge from Björn:
966
			    AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
967
  assert(b);
24.1.9 by Björn Påhlsson
not working midwork...
968
  
969
  /* Called whenever a new services becomes available on the LAN or
970
     is removed from the LAN */
971
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
972
  if(quit_now){
973
    return;
974
  }
975
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
976
  switch(event){
24.1.9 by Björn Påhlsson
not working midwork...
977
  default:
978
  case AVAHI_BROWSER_FAILURE:
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
979
    
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
980
    fprintf(stderr, "(Avahi browser) %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
981
	    avahi_strerror(avahi_server_errno(mc.server)));
982
    avahi_simple_poll_quit(mc.simple_poll);
24.1.9 by Björn Påhlsson
not working midwork...
983
    return;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
984
    
24.1.9 by Björn Påhlsson
not working midwork...
985
  case AVAHI_BROWSER_NEW:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
986
    /* We ignore the returned Avahi resolver object. In the callback
987
       function we free it. If the Avahi server is terminated before
988
       the callback function is called the Avahi server will free the
989
       resolver for us. */
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
990
    
313 by Teddy Hogeborn
* plugins.d/mandos-client.c (browse_callback, main): Do not require
991
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
992
				    name, type, domain, protocol, 0,
993
				    resolve_callback, NULL) == NULL)
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
994
      fprintf(stderr, "Avahi: Failed to resolve service '%s': %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
995
	      name, avahi_strerror(avahi_server_errno(mc.server)));
24.1.9 by Björn Påhlsson
not working midwork...
996
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
997
    
24.1.9 by Björn Påhlsson
not working midwork...
998
  case AVAHI_BROWSER_REMOVE:
999
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1000
    
24.1.9 by Björn Påhlsson
not working midwork...
1001
  case AVAHI_BROWSER_ALL_FOR_NOW:
1002
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1003
    if(debug){
1004
      fprintf(stderr, "No Mandos server found, still searching...\n");
1005
    }
24.1.9 by Björn Påhlsson
not working midwork...
1006
    break;
1007
  }
13 by Björn Påhlsson
Added following support:
1008
}
1009
24.1.135 by Björn Påhlsson
Earlier signal handling
1010
/* stop main loop after sigterm has been called */
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1011
static void handle_sigterm(int sig){
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1012
  if(quit_now){
1013
    return;
1014
  }
1015
  quit_now = 1;
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1016
  signal_received = sig;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1017
  int old_errno = errno;
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1018
  if(mc.simple_poll != NULL){
1019
    avahi_simple_poll_quit(mc.simple_poll);
1020
  }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1021
  errno = old_errno;
1022
}
1023
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1024
/* 
1025
 * This function determines if a directory entry in /sys/class/net
1026
 * corresponds to an acceptable network device.
1027
 * (This function is passed to scandir(3) as a filter function.)
1028
 */
1029
int good_interface(const struct dirent *if_entry){
1030
  ssize_t ssret;
1031
  char *flagname = NULL;
480 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Check if the interface
1032
  if(if_entry->d_name[0] == '.'){
1033
    return 0;
1034
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1035
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1036
		     if_entry->d_name);
1037
  if(ret < 0){
1038
    perror("asprintf");
1039
    return 0;
1040
  }
1041
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1042
  if(flags_fd == -1){
1043
    perror("open");
480 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Check if the interface
1044
    free(flagname);
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1045
    return 0;
1046
  }
480 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Check if the interface
1047
  free(flagname);
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1048
  typedef short ifreq_flags;	/* ifreq.ifr_flags in netdevice(7) */
1049
  /* read line from flags_fd */
484 by Teddy Hogeborn
* Makefile (plugins.d/mandos-client): Bug fix: Put $^ before all
1050
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1051
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1052
  flagstring[(size_t)to_read] = '\0';
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1053
  if(flagstring == NULL){
1054
    perror("malloc");
1055
    close(flags_fd);
1056
    return 0;
1057
  }
1058
  while(to_read > 0){
1059
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1060
					     (size_t)to_read));
1061
    if(ssret == -1){
1062
      perror("read");
1063
      free(flagstring);
1064
      close(flags_fd);
1065
      return 0;
1066
    }
1067
    to_read -= ssret;
1068
    if(ssret == 0){
1069
      break;
1070
    }
1071
  }
1072
  close(flags_fd);
1073
  intmax_t tmpmax;
1074
  char *tmp;
1075
  errno = 0;
1076
  tmpmax = strtoimax(flagstring, &tmp, 0);
1077
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1078
					 and not (isspace(*tmp)))
1079
     or tmpmax != (ifreq_flags)tmpmax){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1080
    if(debug){
1081
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1082
	      flagstring, if_entry->d_name);
1083
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1084
    free(flagstring);
1085
    return 0;
1086
  }
1087
  free(flagstring);
1088
  ifreq_flags flags = (ifreq_flags)tmpmax;
1089
  /* Reject the loopback device */
1090
  if(flags & IFF_LOOPBACK){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1091
    if(debug){
1092
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1093
	      if_entry->d_name);
1094
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1095
    return 0;
1096
  }
1097
  /* Accept point-to-point devices only if connect_to is specified */
1098
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1099
    if(debug){
1100
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1101
	      if_entry->d_name);
1102
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1103
    return 1;
1104
  }
1105
  /* Otherwise, reject non-broadcast-capable devices */
1106
  if(not (flags & IFF_BROADCAST)){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1107
    if(debug){
1108
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1109
	      if_entry->d_name);
1110
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1111
    return 0;
1112
  }
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1113
  /* Reject non-ARP interfaces (including dummy interfaces) */
1114
  if(flags & IFF_NOARP){
1115
    if(debug){
1116
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1117
	      if_entry->d_name);
1118
    }
1119
    return 0;
1120
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1121
  /* Accept this device */
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1122
  if(debug){
1123
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1124
	    if_entry->d_name);
1125
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1126
  return 1;
1127
}
1128
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1129
int main(int argc, char *argv[]){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1130
  AvahiSServiceBrowser *sb = NULL;
1131
  int error;
1132
  int ret;
1133
  intmax_t tmpmax;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1134
  char *tmp;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1135
  int exitcode = EXIT_SUCCESS;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1136
  const char *interface = "";
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1137
  struct ifreq network;
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1138
  int sd = -1;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1139
  bool take_down_interface = false;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1140
  uid_t uid;
1141
  gid_t gid;
1142
  char tempdir[] = "/tmp/mandosXXXXXX";
1143
  bool tempdir_created = false;
1144
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1145
  const char *seckey = PATHDIR "/" SECKEY;
1146
  const char *pubkey = PATHDIR "/" PUBKEY;
1147
  
1148
  bool gnutls_initialized = false;
1149
  bool gpgme_initialized = false;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1150
  float delay = 2.5f;
1151
  
369 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
1152
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1153
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1154
  
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1155
  uid = getuid();
1156
  gid = getgid();
1157
  
1158
  /* Lower any group privileges we might have, just to be safe */
1159
  errno = 0;
1160
  ret = setgid(gid);
1161
  if(ret == -1){
1162
    perror("setgid");
1163
  }
1164
  
1165
  /* Lower user privileges (temporarily) */
1166
  errno = 0;
1167
  ret = seteuid(uid);
1168
  if(ret == -1){
1169
    perror("seteuid");
1170
  }
1171
  
1172
  if(quit_now){
1173
    goto end;
1174
  }
1175
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1176
  {
1177
    struct argp_option options[] = {
1178
      { .name = "debug", .key = 128,
1179
	.doc = "Debug mode", .group = 3 },
1180
      { .name = "connect", .key = 'c',
1181
	.arg = "ADDRESS:PORT",
1182
	.doc = "Connect directly to a specific Mandos server",
1183
	.group = 1 },
1184
      { .name = "interface", .key = 'i',
1185
	.arg = "NAME",
304 by Teddy Hogeborn
Four new interrelated features:
1186
	.doc = "Network interface that will be used to search for"
1187
	" Mandos servers",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1188
	.group = 1 },
1189
      { .name = "seckey", .key = 's',
1190
	.arg = "FILE",
1191
	.doc = "OpenPGP secret key file base name",
1192
	.group = 1 },
1193
      { .name = "pubkey", .key = 'p',
1194
	.arg = "FILE",
1195
	.doc = "OpenPGP public key file base name",
1196
	.group = 2 },
1197
      { .name = "dh-bits", .key = 129,
1198
	.arg = "BITS",
1199
	.doc = "Bit length of the prime number used in the"
1200
	" Diffie-Hellman key exchange",
1201
	.group = 2 },
1202
      { .name = "priority", .key = 130,
1203
	.arg = "STRING",
1204
	.doc = "GnuTLS priority string for the TLS handshake",
1205
	.group = 1 },
1206
      { .name = "delay", .key = 131,
1207
	.arg = "SECONDS",
1208
	.doc = "Maximum delay to wait for interface startup",
1209
	.group = 2 },
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1210
      /*
1211
       * These reproduce what we would get without ARGP_NO_HELP
1212
       */
1213
      { .name = "help", .key = '?',
1214
	.doc = "Give this help list", .group = -1 },
1215
      { .name = "usage", .key = -3,
1216
	.doc = "Give a short usage message", .group = -1 },
1217
      { .name = "version", .key = 'V',
1218
	.doc = "Print program version", .group = -1 },
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1219
      { .name = NULL }
1220
    };
1221
    
1222
    error_t parse_opt(int key, char *arg,
1223
		      struct argp_state *state){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1224
      errno = 0;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1225
      switch(key){
1226
      case 128:			/* --debug */
1227
	debug = true;
1228
	break;
1229
      case 'c':			/* --connect */
1230
	connect_to = arg;
1231
	break;
1232
      case 'i':			/* --interface */
1233
	interface = arg;
1234
	break;
1235
      case 's':			/* --seckey */
1236
	seckey = arg;
1237
	break;
1238
      case 'p':			/* --pubkey */
1239
	pubkey = arg;
1240
	break;
1241
      case 129:			/* --dh-bits */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1242
	errno = 0;
1243
	tmpmax = strtoimax(arg, &tmp, 10);
1244
	if(errno != 0 or tmp == arg or *tmp != '\0'
1245
	   or tmpmax != (typeof(mc.dh_bits))tmpmax){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1246
	  argp_error(state, "Bad number of DH bits");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1247
	}
1248
	mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1249
	break;
1250
      case 130:			/* --priority */
1251
	mc.priority = arg;
1252
	break;
1253
      case 131:			/* --delay */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1254
	errno = 0;
1255
	delay = strtof(arg, &tmp);
1256
	if(errno != 0 or tmp == arg or *tmp != '\0'){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1257
	  argp_error(state, "Bad delay");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1258
	}
1259
	break;
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1260
	/*
1261
	 * These reproduce what we would get without ARGP_NO_HELP
1262
	 */
1263
      case '?':			/* --help */
1264
	argp_state_help(state, state->out_stream,
1265
			(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1266
			& ~(unsigned int)ARGP_HELP_EXIT_OK);
1267
      case -3:			/* --usage */
1268
	argp_state_help(state, state->out_stream,
1269
			ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1270
      case 'V':			/* --version */
1271
	fprintf(state->out_stream, "%s\n", argp_program_version);
1272
	exit(argp_err_exit_status);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1273
	break;
1274
      default:
1275
	return ARGP_ERR_UNKNOWN;
1276
      }
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1277
      return errno;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1278
    }
1279
    
1280
    struct argp argp = { .options = options, .parser = parse_opt,
1281
			 .args_doc = "",
1282
			 .doc = "Mandos client -- Get and decrypt"
1283
			 " passwords from a Mandos server" };
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1284
    ret = argp_parse(&argp, argc, argv,
1285
		     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1286
    switch(ret){
1287
    case 0:
1288
      break;
1289
    case ENOMEM:
1290
    default:
1291
      errno = ret;
1292
      perror("argp_parse");
1293
      exitcode = EX_OSERR;
1294
      goto end;
1295
    case EINVAL:
1296
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1297
      goto end;
1298
    }
1299
  }
1300
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1301
  if(not debug){
1302
    avahi_set_log_function(empty_log);
1303
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1304
1305
  if(interface[0] == '\0'){
1306
    struct dirent **direntries;
1307
    ret = scandir(sys_class_net, &direntries, good_interface,
1308
		  alphasort);
1309
    if(ret >= 1){
1310
      /* Pick the first good interface */
1311
      interface = strdup(direntries[0]->d_name);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1312
      if(debug){
1313
	fprintf(stderr, "Using interface \"%s\"\n", interface);
1314
      }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1315
      if(interface == NULL){
1316
	perror("malloc");
1317
	free(direntries);
1318
	exitcode = EXIT_FAILURE;
1319
	goto end;
1320
      }
1321
      free(direntries);
1322
    } else {
1323
      free(direntries);
1324
      fprintf(stderr, "Could not find a network interface\n");
1325
      exitcode = EXIT_FAILURE;
1326
      goto end;
1327
    }
1328
  }
309 by Teddy Hogeborn
Merge from Björn:
1329
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1330
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1331
     from the signal handler */
1332
  /* Initialize the pseudo-RNG for Avahi */
1333
  srand((unsigned int) time(NULL));
1334
  mc.simple_poll = avahi_simple_poll_new();
1335
  if(mc.simple_poll == NULL){
1336
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1337
    exitcode = EX_UNAVAILABLE;
24.1.135 by Björn Påhlsson
Earlier signal handling
1338
    goto end;
1339
  }
309 by Teddy Hogeborn
Merge from Björn:
1340
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1341
  sigemptyset(&sigterm_action.sa_mask);
309 by Teddy Hogeborn
Merge from Björn:
1342
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1343
  if(ret == -1){
1344
    perror("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1345
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1346
    goto end;
1347
  }
1348
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1349
  if(ret == -1){
1350
    perror("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1351
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1352
    goto end;
1353
  }
24.1.135 by Björn Påhlsson
Earlier signal handling
1354
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1355
  if(ret == -1){
1356
    perror("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1357
    exitcode = EX_OSERR;
24.1.135 by Björn Påhlsson
Earlier signal handling
1358
    goto end;
1359
  }
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1360
  /* Need to check if the handler is SIG_IGN before handling:
1361
     | [[info:libc:Initial Signal Actions]] |
1362
     | [[info:libc:Basic Signal Handling]]  |
1363
  */
1364
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1365
  if(ret == -1){
1366
    perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1367
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1368
  }
1369
  if(old_sigterm_action.sa_handler != SIG_IGN){
1370
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1371
    if(ret == -1){
1372
      perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1373
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1374
      goto end;
1375
    }
1376
  }
1377
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1378
  if(ret == -1){
1379
    perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1380
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1381
  }
1382
  if(old_sigterm_action.sa_handler != SIG_IGN){
1383
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1384
    if(ret == -1){
1385
      perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1386
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1387
      goto end;
1388
    }
1389
  }
1390
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1391
  if(ret == -1){
1392
    perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1393
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1394
  }
1395
  if(old_sigterm_action.sa_handler != SIG_IGN){
1396
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1397
    if(ret == -1){
1398
      perror("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1399
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1400
      goto end;
1401
    }
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1402
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1403
  
1404
  /* If the interface is down, bring it up */
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1405
  if(strcmp(interface, "none") != 0){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1406
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1407
    if(if_index == 0){
1408
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1409
      exitcode = EX_UNAVAILABLE;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1410
      goto end;
1411
    }
1412
    
1413
    if(quit_now){
1414
      goto end;
1415
    }
1416
    
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1417
    /* Re-raise priviliges */
1418
    errno = 0;
1419
    ret = seteuid(0);
1420
    if(ret == -1){
1421
      perror("seteuid");
1422
    }
1423
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1424
#ifdef __linux__
1425
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1426
       messages about the network interface to mess up the prompt */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1427
    ret = klogctl(8, NULL, 5);
304 by Teddy Hogeborn
Four new interrelated features:
1428
    bool restore_loglevel = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1429
    if(ret == -1){
304 by Teddy Hogeborn
Four new interrelated features:
1430
      restore_loglevel = false;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1431
      perror("klogctl");
1432
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1433
#endif	/* __linux__ */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1434
    
1435
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1436
    if(sd < 0){
1437
      perror("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1438
      exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1439
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1440
      if(restore_loglevel){
1441
	ret = klogctl(7, NULL, 0);
1442
	if(ret == -1){
1443
	  perror("klogctl");
1444
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1445
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1446
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1447
      /* Lower privileges */
1448
      errno = 0;
1449
      ret = seteuid(uid);
1450
      if(ret == -1){
1451
	perror("seteuid");
1452
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1453
      goto end;
1454
    }
1455
    strcpy(network.ifr_name, interface);
1456
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1457
    if(ret == -1){
1458
      perror("ioctl SIOCGIFFLAGS");
1459
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1460
      if(restore_loglevel){
1461
	ret = klogctl(7, NULL, 0);
1462
	if(ret == -1){
1463
	  perror("klogctl");
1464
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1465
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1466
#endif	/* __linux__ */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1467
      exitcode = EX_OSERR;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1468
      /* Lower privileges */
1469
      errno = 0;
1470
      ret = seteuid(uid);
1471
      if(ret == -1){
1472
	perror("seteuid");
1473
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1474
      goto end;
1475
    }
1476
    if((network.ifr_flags & IFF_UP) == 0){
1477
      network.ifr_flags |= IFF_UP;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1478
      take_down_interface = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1479
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1480
      if(ret == -1){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1481
	take_down_interface = false;
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1482
	perror("ioctl SIOCSIFFLAGS +IFF_UP");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1483
	exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1484
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1485
	if(restore_loglevel){
1486
	  ret = klogctl(7, NULL, 0);
1487
	  if(ret == -1){
1488
	    perror("klogctl");
1489
	  }
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
1490
	}
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1491
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1492
	/* Lower privileges */
1493
	errno = 0;
1494
	ret = seteuid(uid);
1495
	if(ret == -1){
1496
	  perror("seteuid");
1497
	}
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1498
	goto end;
1499
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1500
    }
1501
    /* sleep checking until interface is running */
1502
    for(int i=0; i < delay * 4; i++){
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1503
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1504
      if(ret == -1){
1505
	perror("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1506
      } else if(network.ifr_flags & IFF_RUNNING){
1507
	break;
1508
      }
1509
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1510
      ret = nanosleep(&sleeptime, NULL);
1511
      if(ret == -1 and errno != EINTR){
1512
	perror("nanosleep");
1513
      }
1514
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1515
    if(not take_down_interface){
1516
      /* We won't need the socket anymore */
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1517
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1518
      if(ret == -1){
1519
	perror("close");
1520
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1521
    }
1522
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1523
    if(restore_loglevel){
1524
      /* Restores kernel loglevel to default */
1525
      ret = klogctl(7, NULL, 0);
1526
      if(ret == -1){
1527
	perror("klogctl");
1528
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1529
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1530
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1531
    /* Lower privileges */
1532
    errno = 0;
1533
    if(take_down_interface){
1534
      /* Lower privileges */
1535
      ret = seteuid(uid);
1536
      if(ret == -1){
1537
	perror("seteuid");
1538
      }
1539
    } else {
1540
      /* Lower privileges permanently */
1541
      ret = setuid(uid);
1542
      if(ret == -1){
1543
	perror("setuid");
1544
      }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1545
    }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1546
  }
1547
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1548
  if(quit_now){
1549
    goto end;
1550
  }
1551
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1552
  ret = init_gnutls_global(pubkey, seckey);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1553
  if(ret == -1){
1554
    fprintf(stderr, "init_gnutls_global failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1555
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1556
    goto end;
1557
  } else {
1558
    gnutls_initialized = true;
1559
  }
1560
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1561
  if(quit_now){
1562
    goto end;
1563
  }
1564
  
1565
  tempdir_created = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1566
  if(mkdtemp(tempdir) == NULL){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1567
    tempdir_created = false;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1568
    perror("mkdtemp");
1569
    goto end;
1570
  }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1571
  
1572
  if(quit_now){
1573
    goto end;
1574
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1575
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1576
  if(not init_gpgme(pubkey, seckey, tempdir)){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1577
    fprintf(stderr, "init_gpgme failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1578
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1579
    goto end;
1580
  } else {
1581
    gpgme_initialized = true;
1582
  }
1583
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1584
  if(quit_now){
1585
    goto end;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1586
  }
1587
  
1588
  if(connect_to != NULL){
1589
    /* Connect directly, do not use Zeroconf */
1590
    /* (Mainly meant for debugging) */
1591
    char *address = strrchr(connect_to, ':');
1592
    if(address == NULL){
1593
      fprintf(stderr, "No colon in address\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1594
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1595
      goto end;
1596
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1597
    
1598
    if(quit_now){
1599
      goto end;
1600
    }
1601
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1602
    uint16_t port;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1603
    errno = 0;
1604
    tmpmax = strtoimax(address+1, &tmp, 10);
1605
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1606
       or tmpmax != (uint16_t)tmpmax){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1607
      fprintf(stderr, "Bad port number\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1608
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1609
      goto end;
1610
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1611
  
1612
    if(quit_now){
1613
      goto end;
1614
    }
1615
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1616
    port = (uint16_t)tmpmax;
1617
    *address = '\0';
1618
    address = connect_to;
304 by Teddy Hogeborn
Four new interrelated features:
1619
    /* Colon in address indicates IPv6 */
1620
    int af;
1621
    if(strchr(address, ':') != NULL){
1622
      af = AF_INET6;
1623
    } else {
1624
      af = AF_INET;
1625
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1626
    
1627
    if(quit_now){
1628
      goto end;
1629
    }
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
1630
1631
    while(not quit_now){
1632
      ret = start_mandos_communication(address, port, if_index, af);
1633
      if(quit_now or ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1634
	break;
1635
      }
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
1636
      sleep(15);
1637
    };
1638
1639
    if (not quit_now){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1640
      exitcode = EXIT_SUCCESS;
1641
    }
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
1642
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1643
    goto end;
1644
  }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1645
  
1646
  if(quit_now){
1647
    goto end;
1648
  }
1649
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1650
  {
1651
    AvahiServerConfig config;
1652
    /* Do not publish any local Zeroconf records */
1653
    avahi_server_config_init(&config);
1654
    config.publish_hinfo = 0;
1655
    config.publish_addresses = 0;
1656
    config.publish_workstation = 0;
1657
    config.publish_domain = 0;
1658
    
1659
    /* Allocate a new server */
1660
    mc.server = avahi_server_new(avahi_simple_poll_get
1661
				 (mc.simple_poll), &config, NULL,
1662
				 NULL, &error);
1663
    
1664
    /* Free the Avahi configuration data */
1665
    avahi_server_config_free(&config);
1666
  }
1667
  
1668
  /* Check if creating the Avahi server object succeeded */
1669
  if(mc.server == NULL){
1670
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1671
	    avahi_strerror(error));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1672
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1673
    goto end;
1674
  }
1675
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1676
  if(quit_now){
1677
    goto end;
1678
  }
1679
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1680
  /* Create the Avahi service browser */
1681
  sb = avahi_s_service_browser_new(mc.server, if_index,
313 by Teddy Hogeborn
* plugins.d/mandos-client.c (browse_callback, main): Do not require
1682
				   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1683
				   NULL, 0, browse_callback, NULL);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1684
  if(sb == NULL){
1685
    fprintf(stderr, "Failed to create service browser: %s\n",
1686
	    avahi_strerror(avahi_server_errno(mc.server)));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1687
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1688
    goto end;
1689
  }
307 by Teddy Hogeborn
Merge from Björn:
1690
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1691
  if(quit_now){
1692
    goto end;
1693
  }
1694
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1695
  /* Run the main loop */
1696
  
1697
  if(debug){
1698
    fprintf(stderr, "Starting Avahi loop search\n");
1699
  }
1700
  
1701
  avahi_simple_poll_loop(mc.simple_poll);
1702
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1703
 end:
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1704
  
1705
  if(debug){
1706
    fprintf(stderr, "%s exiting\n", argv[0]);
1707
  }
1708
  
1709
  /* Cleanup things */
1710
  if(sb != NULL)
1711
    avahi_s_service_browser_free(sb);
1712
  
1713
  if(mc.server != NULL)
1714
    avahi_server_free(mc.server);
1715
  
1716
  if(mc.simple_poll != NULL)
1717
    avahi_simple_poll_free(mc.simple_poll);
1718
  
1719
  if(gnutls_initialized){
1720
    gnutls_certificate_free_credentials(mc.cred);
1721
    gnutls_global_deinit();
1722
    gnutls_dh_params_deinit(mc.dh_params);
1723
  }
1724
  
1725
  if(gpgme_initialized){
1726
    gpgme_release(mc.ctx);
1727
  }
1728
  
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1729
  /* Take down the network interface */
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1730
  if(take_down_interface){
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1731
    /* Re-raise priviliges */
1732
    errno = 0;
1733
    ret = seteuid(0);
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1734
    if(ret == -1){
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1735
      perror("seteuid");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1736
    }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1737
    if(geteuid() == 0){
1738
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1739
      if(ret == -1){
1740
	perror("ioctl SIOCGIFFLAGS");
1741
      } else if(network.ifr_flags & IFF_UP) {
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1742
	network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1743
	ret = ioctl(sd, SIOCSIFFLAGS, &network);
1744
	if(ret == -1){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1745
	  perror("ioctl SIOCSIFFLAGS -IFF_UP");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1746
	}
1747
      }
1748
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1749
      if(ret == -1){
1750
	perror("close");
1751
      }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1752
      /* Lower privileges permanently */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1753
      errno = 0;
1754
      ret = setuid(uid);
1755
      if(ret == -1){
1756
	perror("setuid");
1757
      }
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1758
    }
1759
  }
1760
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1761
  /* Removes the temp directory used by GPGME */
1762
  if(tempdir_created){
1763
    DIR *d;
1764
    struct dirent *direntry;
1765
    d = opendir(tempdir);
1766
    if(d == NULL){
1767
      if(errno != ENOENT){
1768
	perror("opendir");
1769
      }
1770
    } else {
1771
      while(true){
1772
	direntry = readdir(d);
1773
	if(direntry == NULL){
1774
	  break;
1775
	}
1776
	/* Skip "." and ".." */
1777
	if(direntry->d_name[0] == '.'
1778
	   and (direntry->d_name[1] == '\0'
1779
		or (direntry->d_name[1] == '.'
1780
		    and direntry->d_name[2] == '\0'))){
1781
	  continue;
1782
	}
1783
	char *fullname = NULL;
1784
	ret = asprintf(&fullname, "%s/%s", tempdir,
1785
		       direntry->d_name);
1786
	if(ret < 0){
1787
	  perror("asprintf");
1788
	  continue;
1789
	}
1790
	ret = remove(fullname);
1791
	if(ret == -1){
1792
	  fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1793
		  strerror(errno));
1794
	}
1795
	free(fullname);
1796
      }
1797
      closedir(d);
1798
    }
1799
    ret = rmdir(tempdir);
1800
    if(ret == -1 and errno != ENOENT){
1801
      perror("rmdir");
1802
    }
1803
  }
1804
  
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1805
  if(quit_now){
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1806
    sigemptyset(&old_sigterm_action.sa_mask);
1807
    old_sigterm_action.sa_handler = SIG_DFL;
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
1808
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
1809
					    &old_sigterm_action,
1810
					    NULL));
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1811
    if(ret == -1){
1812
      perror("sigaction");
1813
    }
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
1814
    do {
1815
      ret = raise(signal_received);
1816
    } while(ret != 0 and errno == EINTR);
1817
    if(ret != 0){
1818
      perror("raise");
1819
      abort();
1820
    }
1821
    TEMP_FAILURE_RETRY(pause());
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1822
  }
1823
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1824
  return exitcode;
13 by Björn Påhlsson
Added following support:
1825
}