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