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