/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);
1611
	setenv("DEVICE", interface, 1);
1612
	setenv("VERBOSE", debug ? "1" : "0", 1);
1613
	setenv("MODE", "start", 1);
1614
	/* setenv( XXX more here */
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1615
	ret = execl(fullname, direntry->d_name, "start", NULL);
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1616
	perror_plus("execl");
1617
      }
1618
      free(fullname);
1619
      if(quit_now){
1620
	goto end;
1621
      }
1622
    }
1623
    close(devnull);
1624
  }
1625
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1626
  if(not debug){
1627
    avahi_set_log_function(empty_log);
1628
  }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1629
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1630
  if(interface[0] == '\0'){
1631
    struct dirent **direntries;
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1632
    /* First look for interfaces that are up */
1633
    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
1634
		  alphasort);
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1635
    if(ret == 0){
1636
      /* No up interfaces, look for any good interfaces */
1637
      free(direntries);
1638
      ret = scandir(sys_class_net, &direntries, good_interface,
1639
		    alphasort);
1640
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1641
    if(ret >= 1){
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1642
      /* Pick the first interface returned */
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1643
      interface = strdup(direntries[0]->d_name);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1644
      if(debug){
1645
	fprintf(stderr, "Using interface \"%s\"\n", interface);
1646
      }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1647
      if(interface == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1648
	perror_plus("malloc");
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1649
	free(direntries);
1650
	exitcode = EXIT_FAILURE;
1651
	goto end;
1652
      }
1653
      free(direntries);
1654
    } else {
1655
      free(direntries);
1656
      fprintf(stderr, "Could not find a network interface\n");
1657
      exitcode = EXIT_FAILURE;
1658
      goto end;
1659
    }
1660
  }
309 by Teddy Hogeborn
Merge from Björn:
1661
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1662
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1663
     from the signal handler */
1664
  /* Initialize the pseudo-RNG for Avahi */
1665
  srand((unsigned int) time(NULL));
1666
  mc.simple_poll = avahi_simple_poll_new();
1667
  if(mc.simple_poll == NULL){
1668
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1669
    exitcode = EX_UNAVAILABLE;
24.1.135 by Björn Påhlsson
Earlier signal handling
1670
    goto end;
1671
  }
309 by Teddy Hogeborn
Merge from Björn:
1672
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1673
  sigemptyset(&sigterm_action.sa_mask);
309 by Teddy Hogeborn
Merge from Björn:
1674
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1675
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1676
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1677
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1678
    goto end;
1679
  }
1680
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1681
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1682
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1683
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1684
    goto end;
1685
  }
24.1.135 by Björn Påhlsson
Earlier signal handling
1686
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1687
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1688
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1689
    exitcode = EX_OSERR;
24.1.135 by Björn Påhlsson
Earlier signal handling
1690
    goto end;
1691
  }
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1692
  /* Need to check if the handler is SIG_IGN before handling:
1693
     | [[info:libc:Initial Signal Actions]] |
1694
     | [[info:libc:Basic Signal Handling]]  |
1695
  */
1696
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1697
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1698
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1699
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1700
  }
1701
  if(old_sigterm_action.sa_handler != SIG_IGN){
1702
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1703
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1704
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1705
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1706
      goto end;
1707
    }
1708
  }
1709
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1710
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1711
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1712
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1713
  }
1714
  if(old_sigterm_action.sa_handler != SIG_IGN){
1715
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1716
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1717
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1718
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1719
      goto end;
1720
    }
1721
  }
1722
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1723
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1724
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1725
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1726
  }
1727
  if(old_sigterm_action.sa_handler != SIG_IGN){
1728
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1729
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1730
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1731
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1732
      goto end;
1733
    }
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1734
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1735
  
1736
  /* 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
1737
  if(strcmp(interface, "none") != 0){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1738
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1739
    if(if_index == 0){
1740
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1741
      exitcode = EX_UNAVAILABLE;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1742
      goto end;
1743
    }
1744
    
1745
    if(quit_now){
1746
      goto end;
1747
    }
1748
    
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1749
    /* Re-raise priviliges */
1750
    errno = 0;
1751
    ret = seteuid(0);
1752
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1753
      perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1754
    }
1755
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1756
#ifdef __linux__
1757
    /* 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
1758
       messages about the network interface to mess up the prompt */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1759
    ret = klogctl(8, NULL, 5);
304 by Teddy Hogeborn
Four new interrelated features:
1760
    bool restore_loglevel = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1761
    if(ret == -1){
304 by Teddy Hogeborn
Four new interrelated features:
1762
      restore_loglevel = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1763
      perror_plus("klogctl");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1764
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1765
#endif	/* __linux__ */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1766
    
1767
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1768
    if(sd < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1769
      perror_plus("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1770
      exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1771
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1772
      if(restore_loglevel){
1773
	ret = klogctl(7, NULL, 0);
1774
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1775
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1776
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1777
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1778
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1779
      /* Lower privileges */
1780
      errno = 0;
1781
      ret = seteuid(uid);
1782
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1783
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1784
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1785
      goto end;
1786
    }
1787
    strcpy(network.ifr_name, interface);
1788
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1789
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1790
      perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1791
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1792
      if(restore_loglevel){
1793
	ret = klogctl(7, NULL, 0);
1794
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1795
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1796
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1797
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1798
#endif	/* __linux__ */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1799
      exitcode = EX_OSERR;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1800
      /* Lower privileges */
1801
      errno = 0;
1802
      ret = seteuid(uid);
1803
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1804
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1805
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1806
      goto end;
1807
    }
1808
    if((network.ifr_flags & IFF_UP) == 0){
1809
      network.ifr_flags |= IFF_UP;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1810
      take_down_interface = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1811
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1812
      if(ret == -1){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1813
	take_down_interface = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1814
	perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1815
	exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1816
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1817
	if(restore_loglevel){
1818
	  ret = klogctl(7, NULL, 0);
1819
	  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1820
	    perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1821
	  }
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
1822
	}
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1823
#endif	/* __linux__ */
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
	}
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1830
	goto end;
1831
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1832
    }
485 by Teddy Hogeborn
Merge from Björn.
1833
    /* Sleep checking until interface is running.
1834
       Check every 0.25s, up to total time of delay */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1835
    for(int i=0; i < delay * 4; i++){
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1836
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1837
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1838
	perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1839
      } else if(network.ifr_flags & IFF_RUNNING){
1840
	break;
1841
      }
1842
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1843
      ret = nanosleep(&sleeptime, NULL);
1844
      if(ret == -1 and errno != EINTR){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1845
	perror_plus("nanosleep");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1846
      }
1847
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1848
    if(not take_down_interface){
1849
      /* We won't need the socket anymore */
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1850
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1851
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1852
	perror_plus("close");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1853
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1854
    }
1855
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1856
    if(restore_loglevel){
1857
      /* Restores kernel loglevel to default */
1858
      ret = klogctl(7, NULL, 0);
1859
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1860
	perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1861
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1862
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1863
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1864
    /* Lower privileges */
1865
    errno = 0;
1866
    if(take_down_interface){
1867
      /* Lower privileges */
1868
      ret = seteuid(uid);
1869
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1870
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1871
      }
1872
    } else {
1873
      /* Lower privileges permanently */
1874
      ret = setuid(uid);
1875
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1876
	perror_plus("setuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1877
      }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
1878
    }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1879
  }
1880
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1881
  if(quit_now){
1882
    goto end;
1883
  }
1884
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1885
  ret = init_gnutls_global(pubkey, seckey);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1886
  if(ret == -1){
1887
    fprintf(stderr, "init_gnutls_global failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1888
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1889
    goto end;
1890
  } else {
1891
    gnutls_initialized = true;
1892
  }
1893
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1894
  if(quit_now){
1895
    goto end;
1896
  }
1897
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1898
  if(mkdtemp(tempdir) == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1899
    perror_plus("mkdtemp");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1900
    goto end;
1901
  }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
1902
  tempdir_created = true;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1903
  
1904
  if(quit_now){
1905
    goto end;
1906
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1907
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1908
  if(not init_gpgme(pubkey, seckey, tempdir)){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1909
    fprintf(stderr, "init_gpgme failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1910
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1911
    goto end;
1912
  } else {
1913
    gpgme_initialized = true;
1914
  }
1915
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1916
  if(quit_now){
1917
    goto end;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1918
  }
1919
  
1920
  if(connect_to != NULL){
1921
    /* Connect directly, do not use Zeroconf */
1922
    /* (Mainly meant for debugging) */
1923
    char *address = strrchr(connect_to, ':');
1924
    if(address == NULL){
1925
      fprintf(stderr, "No colon in address\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1926
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1927
      goto end;
1928
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1929
    
1930
    if(quit_now){
1931
      goto end;
1932
    }
1933
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1934
    uint16_t port;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1935
    errno = 0;
1936
    tmpmax = strtoimax(address+1, &tmp, 10);
1937
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1938
       or tmpmax != (uint16_t)tmpmax){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1939
      fprintf(stderr, "Bad port number\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1940
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1941
      goto end;
1942
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1943
  
1944
    if(quit_now){
1945
      goto end;
1946
    }
1947
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1948
    port = (uint16_t)tmpmax;
1949
    *address = '\0';
304 by Teddy Hogeborn
Four new interrelated features:
1950
    /* Colon in address indicates IPv6 */
1951
    int af;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1952
    if(strchr(connect_to, ':') != NULL){
304 by Teddy Hogeborn
Four new interrelated features:
1953
      af = AF_INET6;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1954
      /* Accept [] around IPv6 address - see RFC 5952 */
1955
      if(connect_to[0] == '[' and address[-1] == ']')
1956
	{
1957
	  connect_to++;
1958
	  address[-1] = '\0';
1959
	}
304 by Teddy Hogeborn
Four new interrelated features:
1960
    } else {
1961
      af = AF_INET;
1962
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1963
    address = connect_to;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1964
    
1965
    if(quit_now){
1966
      goto end;
1967
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1968
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
1969
    while(not quit_now){
1970
      ret = start_mandos_communication(address, port, if_index, af);
1971
      if(quit_now or ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1972
	break;
1973
      }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1974
      if(debug){
1975
	fprintf(stderr, "Retrying in %d seconds\n",
1976
		(int)retry_interval);
1977
      }
1978
      sleep((int)retry_interval);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1979
    }
1980
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
1981
    if (not quit_now){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1982
      exitcode = EXIT_SUCCESS;
1983
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1984
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1985
    goto end;
1986
  }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1987
  
1988
  if(quit_now){
1989
    goto end;
1990
  }
1991
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1992
  {
1993
    AvahiServerConfig config;
1994
    /* Do not publish any local Zeroconf records */
1995
    avahi_server_config_init(&config);
1996
    config.publish_hinfo = 0;
1997
    config.publish_addresses = 0;
1998
    config.publish_workstation = 0;
1999
    config.publish_domain = 0;
2000
    
2001
    /* Allocate a new server */
2002
    mc.server = avahi_server_new(avahi_simple_poll_get
2003
				 (mc.simple_poll), &config, NULL,
2004
				 NULL, &error);
2005
    
2006
    /* Free the Avahi configuration data */
2007
    avahi_server_config_free(&config);
2008
  }
2009
  
2010
  /* Check if creating the Avahi server object succeeded */
2011
  if(mc.server == NULL){
2012
    fprintf(stderr, "Failed to create Avahi server: %s\n",
2013
	    avahi_strerror(error));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2014
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2015
    goto end;
2016
  }
2017
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2018
  if(quit_now){
2019
    goto end;
2020
  }
2021
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2022
  /* Create the Avahi service browser */
2023
  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
2024
				   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2025
				   NULL, 0, browse_callback, NULL);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2026
  if(sb == NULL){
2027
    fprintf(stderr, "Failed to create service browser: %s\n",
2028
	    avahi_strerror(avahi_server_errno(mc.server)));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2029
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2030
    goto end;
2031
  }
307 by Teddy Hogeborn
Merge from Björn:
2032
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2033
  if(quit_now){
2034
    goto end;
2035
  }
2036
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2037
  /* Run the main loop */
2038
  
2039
  if(debug){
2040
    fprintf(stderr, "Starting Avahi loop search\n");
2041
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2042
485 by Teddy Hogeborn
Merge from Björn.
2043
  ret = avahi_loop_with_timeout(mc.simple_poll,
2044
				(int)(retry_interval * 1000));
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2045
  if(debug){
2046
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
2047
	    (ret == 0) ? "successfully" : "with error");
2048
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2049
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
2050
 end:
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2051
  
2052
  if(debug){
2053
    fprintf(stderr, "%s exiting\n", argv[0]);
2054
  }
2055
  
2056
  /* Cleanup things */
2057
  if(sb != NULL)
2058
    avahi_s_service_browser_free(sb);
2059
  
2060
  if(mc.server != NULL)
2061
    avahi_server_free(mc.server);
2062
  
2063
  if(mc.simple_poll != NULL)
2064
    avahi_simple_poll_free(mc.simple_poll);
2065
  
2066
  if(gnutls_initialized){
2067
    gnutls_certificate_free_credentials(mc.cred);
2068
    gnutls_global_deinit();
2069
    gnutls_dh_params_deinit(mc.dh_params);
2070
  }
2071
  
2072
  if(gpgme_initialized){
2073
    gpgme_release(mc.ctx);
2074
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2075
485 by Teddy Hogeborn
Merge from Björn.
2076
  /* Cleans up the circular linked list of Mandos servers the client
2077
     has seen */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2078
  if(mc.current_server != NULL){
2079
    mc.current_server->prev->next = NULL;
2080
    while(mc.current_server != NULL){
2081
      server *next = mc.current_server->next;
2082
      free(mc.current_server);
2083
      mc.current_server = next;
2084
    }
2085
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2086
  
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
2087
  /* XXX run network hooks "stop" here  */
2088
  
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2089
  /* Take down the network interface */
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2090
  if(take_down_interface){
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2091
    /* Re-raise priviliges */
2092
    errno = 0;
2093
    ret = seteuid(0);
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2094
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2095
      perror_plus("seteuid");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2096
    }
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2097
    if(geteuid() == 0){
2098
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2099
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2100
	perror_plus("ioctl SIOCGIFFLAGS");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2101
      } else if(network.ifr_flags & IFF_UP) {
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
2102
	network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2103
	ret = ioctl(sd, SIOCSIFFLAGS, &network);
2104
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2105
	  perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2106
	}
2107
      }
2108
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2109
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2110
	perror_plus("close");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2111
      }
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2112
      /* Lower privileges permanently */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2113
      errno = 0;
2114
      ret = setuid(uid);
2115
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2116
	perror_plus("setuid");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2117
      }
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2118
    }
2119
  }
2120
  
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2121
  /* Removes the GPGME temp directory and all files inside */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2122
  if(tempdir_created){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2123
    struct dirent **direntries = NULL;
2124
    struct dirent *direntry = NULL;
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2125
    int numentries = scandir(tempdir, &direntries, notdotentries,
2126
			     alphasort);
2127
    if (numentries > 0){
2128
      for(int i = 0; i < numentries; i++){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2129
	direntry = direntries[i];
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2130
	char *fullname = NULL;
2131
	ret = asprintf(&fullname, "%s/%s", tempdir,
2132
		       direntry->d_name);
2133
	if(ret < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2134
	  perror_plus("asprintf");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2135
	  continue;
2136
	}
2137
	ret = remove(fullname);
2138
	if(ret == -1){
2139
	  fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2140
		  strerror(errno));
2141
	}
2142
	free(fullname);
2143
      }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2144
    }
2145
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2146
    /* 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
2147
    free(direntries);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2148
    if (numentries == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2149
      perror_plus("scandir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2150
    }
2151
    ret = rmdir(tempdir);
2152
    if(ret == -1 and errno != ENOENT){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2153
      perror_plus("rmdir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2154
    }
2155
  }
2156
  
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2157
  if(quit_now){
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
2158
    sigemptyset(&old_sigterm_action.sa_mask);
2159
    old_sigterm_action.sa_handler = SIG_DFL;
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2160
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2161
					    &old_sigterm_action,
2162
					    NULL));
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2163
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2164
      perror_plus("sigaction");
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2165
    }
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2166
    do {
2167
      ret = raise(signal_received);
2168
    } while(ret != 0 and errno == EINTR);
2169
    if(ret != 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2170
      perror_plus("raise");
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2171
      abort();
2172
    }
2173
    TEMP_FAILURE_RETRY(pause());
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2174
  }
2175
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2176
  return exitcode;
13 by Björn Påhlsson
Added following support:
2177
}