/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>
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
113
			 /* gnutls_certificate_set_openpgp_key_file(),
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;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
136
const char *hookdir = HOOKDIR;
24.1.10 by Björn Påhlsson
merge commit
137
485 by Teddy Hogeborn
Merge from Björn.
138
/* 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.
139
typedef struct server{
140
  const char *ip;
141
  uint16_t port;
142
  AvahiIfIndex if_index;
143
  int af;
144
  struct timespec last_seen;
145
  struct server *next;
146
  struct server *prev;
147
} server;
148
42 by Teddy Hogeborn
* plugins.d/mandosclient.c (start_mandos_communication): Change "to"
149
/* Used for passing in values through the Avahi callback functions */
13 by Björn Påhlsson
Added following support:
150
typedef struct {
24.1.9 by Björn Påhlsson
not working midwork...
151
  AvahiSimplePoll *simple_poll;
152
  AvahiServer *server;
13 by Björn Påhlsson
Added following support:
153
  gnutls_certificate_credentials_t cred;
24.1.9 by Björn Påhlsson
not working midwork...
154
  unsigned int dh_bits;
24.1.13 by Björn Påhlsson
mandosclient
155
  gnutls_dh_params_t dh_params;
24.1.9 by Björn Påhlsson
not working midwork...
156
  const char *priority;
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
157
  gpgme_ctx_t ctx;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
158
  server *current_server;
24.1.9 by Björn Påhlsson
not working midwork...
159
} mandos_context;
13 by Björn Påhlsson
Added following support:
160
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
161
/* global context so signal handler can reach it*/
24.1.135 by Björn Påhlsson
Earlier signal handling
162
mandos_context mc = { .simple_poll = NULL, .server = NULL,
163
		      .dh_bits = 1024, .priority = "SECURE256"
485 by Teddy Hogeborn
Merge from Björn.
164
		      ":!CTYPE-X.509:+CTYPE-OPENPGP",
165
		      .current_server = NULL };
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
166
371 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Always fail and
167
sig_atomic_t quit_now = 0;
168
int signal_received = 0;
169
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
170
/* Function to use when printing errors */
171
void perror_plus(const char *print_text){
485 by Teddy Hogeborn
Merge from Björn.
172
  fprintf(stderr, "Mandos plugin %s: ",
173
	  program_invocation_short_name);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
174
  perror(print_text);
175
}
176
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
177
/*
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
178
 * Make additional room in "buffer" for at least BUFFER_SIZE more
179
 * bytes. "buffer_capacity" is how much is currently allocated,
180
 * "buffer_length" is how much is already used.
43 by Teddy Hogeborn
* plugins.d/mandosclient.c: Cosmetic changes.
181
 */
24.1.132 by Björn Påhlsson
Fixed a bug in fallback handling
182
size_t incbuffer(char **buffer, size_t buffer_length,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
183
		 size_t buffer_capacity){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
184
  if(buffer_length + BUFFER_SIZE > buffer_capacity){
24.1.12 by Björn Påhlsson
merge +
185
    *buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
186
    if(buffer == NULL){
24.1.10 by Björn Påhlsson
merge commit
187
      return 0;
188
    }
189
    buffer_capacity += BUFFER_SIZE;
190
  }
191
  return buffer_capacity;
192
}
193
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
194
/* Add server to set of servers to retry periodically */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
195
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
196
	       int af){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
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),
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
204
			  .port = port,
205
			  .if_index = if_index,
206
			  .af = af };
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
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
 */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
234
static bool init_gpgme(const char *seckey, const char *pubkey,
235
		       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,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
428
				   (size_t)plaintext_length,
429
				   plaintext_capacity);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
430
    if(plaintext_capacity == 0){
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
431
      perror_plus("incbuffer");
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
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
719
				-Wunreachable-code*/
304 by Teddy Hogeborn
Four new interrelated features:
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,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
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,
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
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;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
953
    decrypted_buffer_size = pgp_packet_decrypt(buffer, buffer_length,
24.1.81 by Björn Påhlsson
removed keyring pre-requirement for starting password-request.
954
					       &decrypted_buffer);
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
955
    if(decrypted_buffer_size >= 0){
361 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gpgme): Move variable "ret" into the
956
      
24.1.10 by Björn Påhlsson
merge commit
957
      written = 0;
28 by Teddy Hogeborn
* server.conf: New file.
958
      while(written < (size_t) decrypted_buffer_size){
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
959
	if(quit_now){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
960
	  errno = EINTR;
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
961
	  goto mandos_end;
962
	}
963
	
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
964
	ret = (int)fwrite(decrypted_buffer + written, 1,
965
			  (size_t)decrypted_buffer_size - written,
966
			  stdout);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
967
	if(ret == 0 and ferror(stdout)){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
968
	  int e = errno;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
969
	  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
970
	    fprintf(stderr, "Mandos plugin mandos-client: "
971
		    "Error writing encrypted data: %s\n",
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
972
		    strerror(errno));
973
	  }
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
974
	  errno = e;
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
975
	  goto mandos_end;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
976
	}
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
977
	written += (size_t)ret;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
978
      }
372 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
979
      retval = 0;
13 by Björn Påhlsson
Added following support:
980
    }
981
  }
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
982
  
983
  /* Shutdown procedure */
984
  
985
 mandos_end:
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
986
  {
987
    int e = errno;
988
    free(decrypted_buffer);
989
    free(buffer);
990
    if(tcp_sd >= 0){
991
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
992
    }
993
    if(ret == -1){
994
      if(e == 0){
995
	e = errno;
996
      }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
997
      perror_plus("close");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
998
    }
999
    gnutls_deinit(session);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1000
    errno = e;
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1001
    if(quit_now){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1002
      errno = EINTR;
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1003
      retval = -1;
1004
    }
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
1005
  }
13 by Björn Påhlsson
Added following support:
1006
  return retval;
1007
}
1008
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
1009
static void resolve_callback(AvahiSServiceResolver *r,
1010
			     AvahiIfIndex interface,
304 by Teddy Hogeborn
Four new interrelated features:
1011
			     AvahiProtocol proto,
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
1012
			     AvahiResolverEvent event,
1013
			     const char *name,
1014
			     const char *type,
1015
			     const char *domain,
1016
			     const char *host_name,
1017
			     const AvahiAddress *address,
1018
			     uint16_t port,
1019
			     AVAHI_GCC_UNUSED AvahiStringList *txt,
1020
			     AVAHI_GCC_UNUSED AvahiLookupResultFlags
1021
			     flags,
307 by Teddy Hogeborn
Merge from Björn:
1022
			     AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
1023
  assert(r);
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1024
  
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1025
  /* Called whenever a service has been resolved successfully or
1026
     timed out */
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1027
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1028
  if(quit_now){
1029
    return;
1030
  }
1031
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1032
  switch(event){
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1033
  default:
1034
  case AVAHI_RESOLVER_FAILURE:
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1035
    fprintf(stderr, "Mandos plugin mandos-client: "
1036
	    "(Avahi Resolver) Failed to resolve service '%s'"
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1037
	    " 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
1038
	    avahi_strerror(avahi_server_errno(mc.server)));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1039
    break;
22 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Cast "0" argument to
1040
    
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1041
  case AVAHI_RESOLVER_FOUND:
1042
    {
1043
      char ip[AVAHI_ADDRESS_STR_MAX];
1044
      avahi_address_snprint(ip, sizeof(ip), address);
1045
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1046
	fprintf(stderr, "Mandos plugin mandos-client: "
1047
		"Mandos server \"%s\" found on %s (%s, %"
268 by Teddy Hogeborn
Fixes for sscanf usage:
1048
		PRIdMAX ") on port %" PRIu16 "\n", name, host_name,
1049
		ip, (intmax_t)interface, port);
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1050
      }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1051
      int ret = start_mandos_communication(ip, port, interface,
304 by Teddy Hogeborn
Four new interrelated features:
1052
					   avahi_proto_to_af(proto));
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
1053
      if(ret == 0){
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1054
	avahi_simple_poll_quit(mc.simple_poll);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1055
      } else {
1056
	ret = add_server(ip, port, interface,
1057
			 avahi_proto_to_af(proto));
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1058
      }
13 by Björn Påhlsson
Added following support:
1059
    }
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1060
  }
1061
  avahi_s_service_resolver_free(r);
13 by Björn Påhlsson
Added following support:
1062
}
1063
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1064
static void browse_callback(AvahiSServiceBrowser *b,
1065
			    AvahiIfIndex interface,
1066
			    AvahiProtocol protocol,
1067
			    AvahiBrowserEvent event,
1068
			    const char *name,
1069
			    const char *type,
1070
			    const char *domain,
1071
			    AVAHI_GCC_UNUSED AvahiLookupResultFlags
1072
			    flags,
307 by Teddy Hogeborn
Merge from Björn:
1073
			    AVAHI_GCC_UNUSED void* userdata){
84 by Teddy Hogeborn
* Makefile (DOCBOOKTOMAN): Use the local manpages/docbook.xsl file, do
1074
  assert(b);
24.1.9 by Björn Påhlsson
not working midwork...
1075
  
1076
  /* Called whenever a new services becomes available on the LAN or
1077
     is removed from the LAN */
1078
  
358 by Teddy Hogeborn
* plugins.d/mandos-client.c (start_mandos_communication): Check
1079
  if(quit_now){
1080
    return;
1081
  }
1082
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1083
  switch(event){
24.1.9 by Björn Påhlsson
not working midwork...
1084
  default:
1085
  case AVAHI_BROWSER_FAILURE:
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1086
    
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1087
    fprintf(stderr, "Mandos plugin mandos-client: "
1088
	    "(Avahi browser) %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1089
	    avahi_strerror(avahi_server_errno(mc.server)));
1090
    avahi_simple_poll_quit(mc.simple_poll);
24.1.9 by Björn Påhlsson
not working midwork...
1091
    return;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1092
    
24.1.9 by Björn Påhlsson
not working midwork...
1093
  case AVAHI_BROWSER_NEW:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1094
    /* We ignore the returned Avahi resolver object. In the callback
1095
       function we free it. If the Avahi server is terminated before
1096
       the callback function is called the Avahi server will free the
1097
       resolver for us. */
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1098
    
313 by Teddy Hogeborn
* plugins.d/mandos-client.c (browse_callback, main): Do not require
1099
    if(avahi_s_service_resolver_new(mc.server, interface, protocol,
1100
				    name, type, domain, protocol, 0,
1101
				    resolve_callback, NULL) == NULL)
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1102
      fprintf(stderr, "Mandos plugin mandos-client: "
1103
	      "Avahi: Failed to resolve service '%s': %s\n",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1104
	      name, avahi_strerror(avahi_server_errno(mc.server)));
24.1.9 by Björn Påhlsson
not working midwork...
1105
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1106
    
24.1.9 by Björn Påhlsson
not working midwork...
1107
  case AVAHI_BROWSER_REMOVE:
1108
    break;
38 by Teddy Hogeborn
* plugbasedclient.c (main): New "--userid" and "--groupid" options.
1109
    
24.1.9 by Björn Påhlsson
not working midwork...
1110
  case AVAHI_BROWSER_ALL_FOR_NOW:
1111
  case AVAHI_BROWSER_CACHE_EXHAUSTED:
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1112
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1113
      fprintf(stderr, "Mandos plugin mandos-client: "
1114
	      "No Mandos server found, still searching...\n");
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1115
    }
24.1.9 by Björn Påhlsson
not working midwork...
1116
    break;
1117
  }
13 by Björn Påhlsson
Added following support:
1118
}
1119
485 by Teddy Hogeborn
Merge from Björn.
1120
/* Signal handler that stops main loop after SIGTERM */
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1121
static void handle_sigterm(int sig){
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1122
  if(quit_now){
1123
    return;
1124
  }
1125
  quit_now = 1;
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1126
  signal_received = sig;
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1127
  int old_errno = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1128
  /* set main loop to exit */
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1129
  if(mc.simple_poll != NULL){
1130
    avahi_simple_poll_quit(mc.simple_poll);
1131
  }
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
1132
  errno = old_errno;
1133
}
1134
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1135
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
1136
  int ret;
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1137
  
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1138
  int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1139
  if(s < 0){
1140
    perror_plus("socket");
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1141
    return false;
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1142
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1143
  strcpy(ifr->ifr_name, ifname);
1144
  ret = ioctl(s, SIOCGIFFLAGS, ifr);
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1145
  if(ret == -1){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1146
    if(debug){
505.2.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1147
      perror_plus("ioctl SIOCGIFFLAGS");
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1148
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1149
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1150
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1151
  return true;
1152
}
1153
1154
bool good_flags(const char *ifname, const struct ifreq *ifr){
1155
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1156
  /* Reject the loopback device */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1157
  if(ifr->ifr_flags & IFF_LOOPBACK){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1158
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1159
      fprintf(stderr, "Mandos plugin mandos-client: "
1160
	      "Rejecting loopback interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1161
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1162
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1163
  }
1164
  /* 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
1165
  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.
1166
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1167
      fprintf(stderr, "Mandos plugin mandos-client: "
1168
	      "Accepting point-to-point interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1169
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1170
    return true;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1171
  }
1172
  /* Otherwise, reject non-broadcast-capable devices */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1173
  if(not (ifr->ifr_flags & IFF_BROADCAST)){
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1174
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1175
      fprintf(stderr, "Mandos plugin mandos-client: "
1176
	      "Rejecting non-broadcast interface \"%s\"\n", ifname);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1177
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1178
    return false;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1179
  }
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1180
  /* Reject non-ARP interfaces (including dummy interfaces) */
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1181
  if(ifr->ifr_flags & IFF_NOARP){
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1182
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1183
      fprintf(stderr, "Mandos plugin mandos-client: "
1184
	      "Rejecting non-ARP interface \"%s\"\n", ifname);
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1185
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1186
    return false;
481 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Reject non-ARP
1187
  }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1188
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1189
  /* Accept this device */
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1190
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1191
    fprintf(stderr, "Mandos plugin mandos-client: "
1192
	    "Interface \"%s\" is good\n", ifname);
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1193
  }
1194
  return true;
1195
}
1196
1197
/* 
1198
 * This function determines if a directory entry in /sys/class/net
1199
 * corresponds to an acceptable network device.
1200
 * (This function is passed to scandir(3) as a filter function.)
1201
 */
1202
int good_interface(const struct dirent *if_entry){
1203
  if(if_entry->d_name[0] == '.'){
1204
    return 0;
1205
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1206
  
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1207
  struct ifreq ifr;
1208
  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.
1209
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1210
      fprintf(stderr, "Mandos plugin mandos-client: "
1211
	      "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.
1212
	      if_entry->d_name);
1213
    }
505.2.2 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Use SIOCGIFFLAGS instead
1214
    return 0;
1215
  }
1216
  
1217
  if(not good_flags(if_entry->d_name, &ifr)){
1218
    return 0;
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1219
  }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1220
  return 1;
1221
}
1222
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1223
/* 
1224
 * This function determines if a directory entry in /sys/class/net
1225
 * corresponds to an acceptable network device which is up.
1226
 * (This function is passed to scandir(3) as a filter function.)
1227
 */
1228
int up_interface(const struct dirent *if_entry){
1229
  if(if_entry->d_name[0] == '.'){
1230
    return 0;
1231
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1232
  
1233
  struct ifreq ifr;
1234
  if(not get_flags(if_entry->d_name, &ifr)){
1235
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1236
      fprintf(stderr, "Mandos plugin mandos-client: "
1237
	      "Failed to get flags for interface \"%s\"\n",
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1238
	      if_entry->d_name);
1239
    }
1240
    return 0;
1241
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1242
  
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1243
  /* Reject down interfaces */
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1244
  if(not (ifr.ifr_flags & IFF_UP)){
1245
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1246
      fprintf(stderr, "Mandos plugin mandos-client: "
1247
	      "Rejecting down interface \"%s\"\n",
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1248
	      if_entry->d_name);
1249
    }
1250
    return 0;
1251
  }
1252
  
1253
  /* Reject non-running interfaces */
1254
  if(not (ifr.ifr_flags & IFF_RUNNING)){
1255
    if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1256
      fprintf(stderr, "Mandos plugin mandos-client: "
1257
	      "Rejecting non-running interface \"%s\"\n",
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1258
	      if_entry->d_name);
1259
    }
1260
    return 0;
1261
  }
1262
  
1263
  if(not good_flags(if_entry->d_name, &ifr)){
1264
    return 0;
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1265
  }
1266
  return 1;
1267
}
1268
24.1.172 by Björn Påhlsson
using scandir instead of readdir
1269
int notdotentries(const struct dirent *direntry){
1270
  /* Skip "." and ".." */
1271
  if(direntry->d_name[0] == '.'
1272
     and (direntry->d_name[1] == '\0'
1273
	  or (direntry->d_name[1] == '.'
1274
	      and direntry->d_name[2] == '\0'))){
1275
    return 0;
1276
  }
1277
  return 1;
1278
}
1279
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1280
/* Is this directory entry a runnable program? */
1281
int runnable_hook(const struct dirent *direntry){
1282
  int ret;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1283
  size_t sret;
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1284
  struct stat st;
1285
  
1286
  if((direntry->d_name)[0] == '\0'){
1287
    /* Empty name? */
1288
    return 0;
1289
  }
1290
  
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1291
  sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1292
		"abcdefghijklmnopqrstuvwxyz"
1293
		"0123456789"
1294
		"_-");
1295
  if((direntry->d_name)[sret] != '\0'){
1296
    /* Contains non-allowed characters */
1297
    if(debug){
1298
      fprintf(stderr, "Mandos plugin mandos-client: "
1299
	      "Ignoring hook \"%s\" with bad name\n",
1300
	      direntry->d_name);
1301
    }
1302
    return 0;
1303
  }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1304
  
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1305
  char *fullname = NULL;
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1306
  ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1307
  if(ret < 0){
1308
    perror_plus("asprintf");
1309
    return 0;
1310
  }
1311
  
1312
  ret = stat(fullname, &st);
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1313
  if(ret == -1){
1314
    if(debug){
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1315
      perror_plus("Could not stat hook");
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1316
    }
1317
    return 0;
1318
  }
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1319
  if(not (S_ISREG(st.st_mode))){
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1320
    /* Not a regular file */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1321
    if(debug){
1322
      fprintf(stderr, "Mandos plugin mandos-client: "
1323
	      "Ignoring hook \"%s\" - not a file\n",
1324
	      direntry->d_name);
1325
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1326
    return 0;
1327
  }
1328
  if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1329
    /* Not executable */
505.3.6 by teddy at bsnet
* plugins.d/mandos-client.c: Some white space fixes.
1330
    if(debug){
1331
      fprintf(stderr, "Mandos plugin mandos-client: "
1332
	      "Ignoring hook \"%s\" - not executable\n",
1333
	      direntry->d_name);
1334
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1335
    return 0;
1336
  }
1337
  return 1;
1338
}
1339
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1340
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1341
  int ret;
1342
  struct timespec now;
1343
  struct timespec waited_time;
1344
  intmax_t block_time;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1345
  
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1346
  while(true){
1347
    if(mc.current_server == NULL){
1348
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1349
	fprintf(stderr, "Mandos plugin mandos-client: "
485 by Teddy Hogeborn
Merge from Björn.
1350
		"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.
1351
      }
1352
      ret = avahi_simple_poll_iterate(s, -1);
1353
    } else {
1354
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1355
	fprintf(stderr, "Mandos plugin mandos-client: "
1356
		"Check current_server if we should run it,"
485 by Teddy Hogeborn
Merge from Björn.
1357
		" or wait\n");
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1358
      }
1359
      /* the current time */
1360
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1361
      if(ret == -1){
1362
	perror_plus("clock_gettime");
1363
	return -1;
1364
      }
1365
      /* Calculating in ms how long time between now and server
1366
	 who we visted longest time ago. Now - last seen.  */
485 by Teddy Hogeborn
Merge from Björn.
1367
      waited_time.tv_sec = (now.tv_sec
1368
			    - mc.current_server->last_seen.tv_sec);
1369
      waited_time.tv_nsec = (now.tv_nsec
1370
			     - mc.current_server->last_seen.tv_nsec);
1371
      /* total time is 10s/10,000ms.
1372
	 Converting to s from ms by dividing by 1,000,
1373
	 and ns to ms by dividing by 1,000,000. */
1374
      block_time = ((retry_interval
1375
		     - ((intmax_t)waited_time.tv_sec * 1000))
1376
		    - ((intmax_t)waited_time.tv_nsec / 1000000));
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1377
      
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1378
      if (debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1379
	fprintf(stderr, "Mandos plugin mandos-client: "
1380
		"Blocking for %" PRIdMAX " ms\n", block_time);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1381
      }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
1382
      
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1383
      if(block_time <= 0){
1384
	ret = start_mandos_communication(mc.current_server->ip,
485 by Teddy Hogeborn
Merge from Björn.
1385
					 mc.current_server->port,
1386
					 mc.current_server->if_index,
1387
					 mc.current_server->af);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1388
	if(ret == 0){
1389
	  avahi_simple_poll_quit(mc.simple_poll);
1390
	  return 0;
1391
	}
485 by Teddy Hogeborn
Merge from Björn.
1392
	ret = clock_gettime(CLOCK_MONOTONIC,
1393
			    &mc.current_server->last_seen);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1394
	if(ret == -1){
1395
	  perror_plus("clock_gettime");
1396
	  return -1;
1397
	}
1398
	mc.current_server = mc.current_server->next;
485 by Teddy Hogeborn
Merge from Björn.
1399
	block_time = 0; 	/* Call avahi to find new Mandos
1400
				   servers, but don't block */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1401
      }
1402
      
1403
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1404
    }
1405
    if(ret != 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1406
      if (ret > 0 or errno != EINTR){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1407
	return (ret != 1) ? ret : 0;
1408
      }
1409
    }
1410
  }
1411
}
1412
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
1413
bool run_network_hooks(const char *mode, const char *interface,
1414
		       const float delay){
1415
  struct dirent **direntries;
1416
  struct dirent *direntry;
1417
  int ret;
1418
  int numhooks = scandir(hookdir, &direntries, runnable_hook,
1419
			 alphasort);
1420
  if(numhooks == -1){
1421
    perror_plus("scandir");
1422
  } else {
1423
    int devnull = open("/dev/null", O_RDONLY);
1424
    for(int i = 0; i < numhooks; i++){
1425
      direntry = direntries[0];
1426
      char *fullname = NULL;
1427
      ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1428
      if(ret < 0){
1429
	perror_plus("asprintf");
1430
	continue;
1431
      }
1432
      pid_t hook_pid = fork();
1433
      if(hook_pid == 0){
1434
	/* Child */
1435
	dup2(devnull, STDIN_FILENO);
1436
	close(devnull);
1437
	dup2(STDERR_FILENO, STDOUT_FILENO);
1438
	ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1439
	if(ret == -1){
1440
	  perror_plus("setenv");
1441
	  return false;
1442
	}
1443
	ret = setenv("DEVICE", interface, 1);
1444
	if(ret == -1){
1445
	  perror_plus("setenv");
1446
	  return false;
1447
	}
1448
	ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1449
	if(ret == -1){
1450
	  perror_plus("setenv");
1451
	  return false;
1452
	}
1453
	ret = setenv("MODE", mode, 1);
1454
	if(ret == -1){
1455
	  perror_plus("setenv");
1456
	  return false;
1457
	}
1458
	char *delaystring;
1459
	ret = asprintf(&delaystring, "%f", delay);
1460
	if(ret == -1){
1461
	  perror_plus("asprintf");
1462
	  return false;
1463
	}
1464
	ret = setenv("DELAY", delaystring, 1);
1465
	if(ret == -1){
1466
	  free(delaystring);
1467
	  perror_plus("setenv");
1468
	  return false;
1469
	}
1470
	free(delaystring);
1471
	ret = execl(fullname, direntry->d_name, mode, NULL);
1472
	perror_plus("execl");
1473
      } else {
1474
	int status;
1475
	if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1476
	  perror_plus("waitpid");
1477
	  free(fullname);
1478
	  continue;
1479
	}
1480
	if(WIFEXITED(status)){
1481
	  if(WEXITSTATUS(status) != 0){
1482
	    fprintf(stderr, "Mandos plugin mandos-client: "
1483
		    "Warning: network hook \"%s\" exited"
1484
		    " with status %d\n", direntry->d_name,
1485
		    WEXITSTATUS(status));
1486
	    free(fullname);
1487
	    continue;
1488
	  }
1489
	} else if(WIFSIGNALED(status)){
1490
	  fprintf(stderr, "Mandos plugin mandos-client: "
1491
		  "Warning: network hook \"%s\" died by"
1492
		  " signal %d\n", direntry->d_name,
1493
		  WTERMSIG(status));
1494
	  free(fullname);
1495
	  continue;
1496
	} else {
1497
	  fprintf(stderr, "Mandos plugin mandos-client: "
1498
		  "Warning: network hook \"%s\" crashed\n",
1499
		  direntry->d_name);
1500
	  free(fullname);
1501
	  continue;
1502
	}
1503
      }
1504
      free(fullname);
1505
      if(quit_now){
1506
	break;
1507
      }
1508
    }
1509
    close(devnull);
1510
  }
1511
  return true;
1512
}
1513
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
1514
int main(int argc, char *argv[]){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1515
  AvahiSServiceBrowser *sb = NULL;
1516
  int error;
1517
  int ret;
1518
  intmax_t tmpmax;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1519
  char *tmp;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1520
  int exitcode = EXIT_SUCCESS;
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1521
  const char *interface = "";
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1522
  struct ifreq network;
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
1523
  int sd = -1;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1524
  bool take_down_interface = false;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1525
  uid_t uid;
1526
  gid_t gid;
1527
  char tempdir[] = "/tmp/mandosXXXXXX";
1528
  bool tempdir_created = false;
1529
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1530
  const char *seckey = PATHDIR "/" SECKEY;
1531
  const char *pubkey = PATHDIR "/" PUBKEY;
1532
  
1533
  bool gnutls_initialized = false;
1534
  bool gpgme_initialized = false;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1535
  float delay = 2.5f;
485 by Teddy Hogeborn
Merge from Björn.
1536
  double retry_interval = 10; /* 10s between trying a server and
1537
				 retrying the same server again */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1538
  
369 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
1539
  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
1540
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1541
  
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1542
  uid = getuid();
1543
  gid = getgid();
1544
  
1545
  /* Lower any group privileges we might have, just to be safe */
1546
  errno = 0;
1547
  ret = setgid(gid);
1548
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1549
    perror_plus("setgid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1550
  }
1551
  
1552
  /* Lower user privileges (temporarily) */
1553
  errno = 0;
1554
  ret = seteuid(uid);
1555
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1556
    perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1557
  }
1558
  
1559
  if(quit_now){
1560
    goto end;
1561
  }
1562
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1563
  {
1564
    struct argp_option options[] = {
1565
      { .name = "debug", .key = 128,
1566
	.doc = "Debug mode", .group = 3 },
1567
      { .name = "connect", .key = 'c',
1568
	.arg = "ADDRESS:PORT",
1569
	.doc = "Connect directly to a specific Mandos server",
1570
	.group = 1 },
1571
      { .name = "interface", .key = 'i',
1572
	.arg = "NAME",
304 by Teddy Hogeborn
Four new interrelated features:
1573
	.doc = "Network interface that will be used to search for"
1574
	" Mandos servers",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1575
	.group = 1 },
1576
      { .name = "seckey", .key = 's',
1577
	.arg = "FILE",
1578
	.doc = "OpenPGP secret key file base name",
1579
	.group = 1 },
1580
      { .name = "pubkey", .key = 'p',
1581
	.arg = "FILE",
1582
	.doc = "OpenPGP public key file base name",
1583
	.group = 2 },
1584
      { .name = "dh-bits", .key = 129,
1585
	.arg = "BITS",
1586
	.doc = "Bit length of the prime number used in the"
1587
	" Diffie-Hellman key exchange",
1588
	.group = 2 },
1589
      { .name = "priority", .key = 130,
1590
	.arg = "STRING",
1591
	.doc = "GnuTLS priority string for the TLS handshake",
1592
	.group = 1 },
1593
      { .name = "delay", .key = 131,
1594
	.arg = "SECONDS",
1595
	.doc = "Maximum delay to wait for interface startup",
1596
	.group = 2 },
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1597
      { .name = "retry", .key = 132,
1598
	.arg = "SECONDS",
1599
	.doc = "Retry interval used when denied by the mandos server",
1600
	.group = 2 },
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1601
      { .name = "network-hook-dir", .key = 133,
1602
	.arg = "DIR",
1603
	.doc = "Directory where network hooks are located",
1604
	.group = 2 },
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1605
      /*
1606
       * These reproduce what we would get without ARGP_NO_HELP
1607
       */
1608
      { .name = "help", .key = '?',
1609
	.doc = "Give this help list", .group = -1 },
1610
      { .name = "usage", .key = -3,
1611
	.doc = "Give a short usage message", .group = -1 },
1612
      { .name = "version", .key = 'V',
1613
	.doc = "Print program version", .group = -1 },
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1614
      { .name = NULL }
1615
    };
1616
    
1617
    error_t parse_opt(int key, char *arg,
1618
		      struct argp_state *state){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1619
      errno = 0;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1620
      switch(key){
1621
      case 128:			/* --debug */
1622
	debug = true;
1623
	break;
1624
      case 'c':			/* --connect */
1625
	connect_to = arg;
1626
	break;
1627
      case 'i':			/* --interface */
1628
	interface = arg;
1629
	break;
1630
      case 's':			/* --seckey */
1631
	seckey = arg;
1632
	break;
1633
      case 'p':			/* --pubkey */
1634
	pubkey = arg;
1635
	break;
1636
      case 129:			/* --dh-bits */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1637
	errno = 0;
1638
	tmpmax = strtoimax(arg, &tmp, 10);
1639
	if(errno != 0 or tmp == arg or *tmp != '\0'
1640
	   or tmpmax != (typeof(mc.dh_bits))tmpmax){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1641
	  argp_error(state, "Bad number of DH bits");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1642
	}
1643
	mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1644
	break;
1645
      case 130:			/* --priority */
1646
	mc.priority = arg;
1647
	break;
1648
      case 131:			/* --delay */
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
1649
	errno = 0;
1650
	delay = strtof(arg, &tmp);
1651
	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
1652
	  argp_error(state, "Bad delay");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1653
	}
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1654
      case 132:			/* --retry */
1655
	errno = 0;
1656
	retry_interval = strtod(arg, &tmp);
1657
	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.
1658
	   or (retry_interval * 1000) > INT_MAX
1659
	   or retry_interval < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1660
	  argp_error(state, "Bad retry interval");
1661
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1662
	break;
505.3.4 by teddy at bsnet
* plugins.d/mandos-client.c (runnable_hook): Bug fix: stat using the
1663
      case 133:			/* --network-hook-dir */
1664
	hookdir = arg;
1665
	break;
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1666
	/*
1667
	 * These reproduce what we would get without ARGP_NO_HELP
1668
	 */
1669
      case '?':			/* --help */
1670
	argp_state_help(state, state->out_stream,
1671
			(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1672
			& ~(unsigned int)ARGP_HELP_EXIT_OK);
1673
      case -3:			/* --usage */
1674
	argp_state_help(state, state->out_stream,
1675
			ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1676
      case 'V':			/* --version */
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1677
	fprintf(state->out_stream, "Mandos plugin mandos-client: ");
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1678
	fprintf(state->out_stream, "%s\n", argp_program_version);
1679
	exit(argp_err_exit_status);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1680
	break;
1681
      default:
1682
	return ARGP_ERR_UNKNOWN;
1683
      }
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1684
      return errno;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1685
    }
1686
    
1687
    struct argp argp = { .options = options, .parser = parse_opt,
1688
			 .args_doc = "",
1689
			 .doc = "Mandos client -- Get and decrypt"
1690
			 " passwords from a Mandos server" };
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1691
    ret = argp_parse(&argp, argc, argv,
1692
		     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1693
    switch(ret){
1694
    case 0:
1695
      break;
1696
    case ENOMEM:
1697
    default:
1698
      errno = ret;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1699
      perror_plus("argp_parse");
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
1700
      exitcode = EX_OSERR;
1701
      goto end;
1702
    case EINVAL:
1703
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1704
      goto end;
1705
    }
1706
  }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1707
    
1708
  {
1709
    /* Work around Debian bug #633582:
1710
       <http://bugs.debian.org/633582> */
1711
    struct stat st;
1712
    
1713
    /* Re-raise priviliges */
1714
    errno = 0;
1715
    ret = seteuid(0);
1716
    if(ret == -1){
1717
      perror_plus("seteuid");
1718
    }
1719
    
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1720
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1721
      int seckey_fd = open(seckey, O_RDONLY);
1722
      if(seckey_fd == -1){
1723
	perror_plus("open");
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1724
      } else {
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1725
	ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1726
	if(ret == -1){
1727
	  perror_plus("fstat");
1728
	} else {
1729
	  if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1730
	    ret = fchown(seckey_fd, uid, gid);
1731
	    if(ret == -1){
1732
	      perror_plus("fchown");
1733
	    }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1734
	  }
1735
	}
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1736
	TEMP_FAILURE_RETRY(close(seckey_fd));
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1737
      }
1738
    }
1739
    
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1740
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1741
      int pubkey_fd = open(pubkey, O_RDONLY);
1742
      if(pubkey_fd == -1){
1743
	perror_plus("open");
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1744
      } else {
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1745
	ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1746
	if(ret == -1){
1747
	  perror_plus("fstat");
1748
	} else {
1749
	  if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1750
	    ret = fchown(pubkey_fd, uid, gid);
1751
	    if(ret == -1){
1752
	      perror_plus("fchown");
1753
	    }
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1754
	  }
1755
	}
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
1756
	TEMP_FAILURE_RETRY(close(pubkey_fd));
487 by Teddy Hogeborn
* initramfs-tools-script: Abort if plugin-runner is missing. Removed
1757
      }
1758
    }
1759
    
1760
    /* Lower privileges */
1761
    errno = 0;
1762
    ret = seteuid(uid);
1763
    if(ret == -1){
1764
      perror_plus("seteuid");
1765
    }
1766
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1767
  
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
1768
  /* Run network hooks */
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1769
  {
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
1770
    /* Re-raise priviliges */
1771
    errno = 0;
1772
    ret = seteuid(0);
1773
    if(ret == -1){
1774
      perror_plus("seteuid");
1775
    }
1776
    if(not run_network_hooks("start", interface, delay)){
1777
      goto end;
1778
    }
1779
    /* Lower privileges */
1780
    errno = 0;
1781
    ret = seteuid(uid);
1782
    if(ret == -1){
1783
      perror_plus("seteuid");
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1784
    }
1785
  }
1786
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1787
  if(not debug){
1788
    avahi_set_log_function(empty_log);
1789
  }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
1790
  
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1791
  if(interface[0] == '\0'){
1792
    struct dirent **direntries;
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1793
    /* First look for interfaces that are up */
1794
    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
1795
		  alphasort);
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1796
    if(ret == 0){
1797
      /* No up interfaces, look for any good interfaces */
1798
      free(direntries);
1799
      ret = scandir(sys_class_net, &direntries, good_interface,
1800
		    alphasort);
1801
    }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1802
    if(ret >= 1){
505.3.1 by teddy at bsnet
* plugins.d/mandos-client.c (good_interface): Add error message.
1803
      /* Pick the first interface returned */
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1804
      interface = strdup(direntries[0]->d_name);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1805
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1806
	fprintf(stderr, "Mandos plugin mandos-client: "
1807
		"Using interface \"%s\"\n", interface);
237.2.34 by teddy at bsnet
* plugins.d/mandos-client.c: Added debug output.
1808
      }
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1809
      if(interface == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1810
	perror_plus("malloc");
237.2.33 by teddy at bsnet
* plugins.d/mandos-client.c: An empty interface name now means to
1811
	free(direntries);
1812
	exitcode = EXIT_FAILURE;
1813
	goto end;
1814
      }
1815
      free(direntries);
1816
    } else {
1817
      free(direntries);
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1818
      fprintf(stderr, "Mandos plugin mandos-client: "
1819
	      "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
1820
      exitcode = EXIT_FAILURE;
1821
      goto end;
1822
    }
1823
  }
309 by Teddy Hogeborn
Merge from Björn:
1824
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1825
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1826
     from the signal handler */
1827
  /* Initialize the pseudo-RNG for Avahi */
1828
  srand((unsigned int) time(NULL));
1829
  mc.simple_poll = avahi_simple_poll_new();
1830
  if(mc.simple_poll == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1831
    fprintf(stderr, "Mandos plugin mandos-client: "
1832
	    "Avahi: Failed to create simple poll object.\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1833
    exitcode = EX_UNAVAILABLE;
24.1.135 by Björn Påhlsson
Earlier signal handling
1834
    goto end;
1835
  }
309 by Teddy Hogeborn
Merge from Björn:
1836
  
24.1.135 by Björn Påhlsson
Earlier signal handling
1837
  sigemptyset(&sigterm_action.sa_mask);
309 by Teddy Hogeborn
Merge from Björn:
1838
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1839
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1840
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1841
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1842
    goto end;
1843
  }
1844
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1845
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1846
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1847
    exitcode = EX_OSERR;
309 by Teddy Hogeborn
Merge from Björn:
1848
    goto end;
1849
  }
24.1.135 by Björn Påhlsson
Earlier signal handling
1850
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1851
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1852
    perror_plus("sigaddset");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1853
    exitcode = EX_OSERR;
24.1.135 by Björn Påhlsson
Earlier signal handling
1854
    goto end;
1855
  }
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1856
  /* Need to check if the handler is SIG_IGN before handling:
1857
     | [[info:libc:Initial Signal Actions]] |
1858
     | [[info:libc:Basic Signal Handling]]  |
1859
  */
1860
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1861
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1862
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1863
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1864
  }
1865
  if(old_sigterm_action.sa_handler != SIG_IGN){
1866
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1867
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1868
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1869
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1870
      goto end;
1871
    }
1872
  }
1873
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1874
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1875
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1876
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1877
  }
1878
  if(old_sigterm_action.sa_handler != SIG_IGN){
1879
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1880
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1881
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1882
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1883
      goto end;
1884
    }
1885
  }
1886
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1887
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1888
    perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1889
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1890
  }
1891
  if(old_sigterm_action.sa_handler != SIG_IGN){
1892
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1893
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1894
      perror_plus("sigaction");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1895
      exitcode = EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
1896
      goto end;
1897
    }
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
1898
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1899
  
1900
  /* 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
1901
  if(strcmp(interface, "none") != 0){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1902
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1903
    if(if_index == 0){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
1904
      fprintf(stderr, "Mandos plugin mandos-client: "
1905
	      "No such interface: \"%s\"\n", interface);
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1906
      exitcode = EX_UNAVAILABLE;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1907
      goto end;
1908
    }
1909
    
1910
    if(quit_now){
1911
      goto end;
1912
    }
1913
    
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1914
    /* Re-raise priviliges */
1915
    errno = 0;
1916
    ret = seteuid(0);
1917
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1918
      perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1919
    }
1920
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1921
#ifdef __linux__
1922
    /* 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
1923
       messages about the network interface to mess up the prompt */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1924
    ret = klogctl(8, NULL, 5);
304 by Teddy Hogeborn
Four new interrelated features:
1925
    bool restore_loglevel = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1926
    if(ret == -1){
304 by Teddy Hogeborn
Four new interrelated features:
1927
      restore_loglevel = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1928
      perror_plus("klogctl");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1929
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1930
#endif	/* __linux__ */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1931
    
1932
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1933
    if(sd < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1934
      perror_plus("socket");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1935
      exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1936
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1937
      if(restore_loglevel){
1938
	ret = klogctl(7, NULL, 0);
1939
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1940
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1941
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1942
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1943
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1944
      /* Lower privileges */
1945
      errno = 0;
1946
      ret = seteuid(uid);
1947
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1948
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1949
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1950
      goto end;
1951
    }
1952
    strcpy(network.ifr_name, interface);
1953
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1954
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1955
      perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1956
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1957
      if(restore_loglevel){
1958
	ret = klogctl(7, NULL, 0);
1959
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1960
	  perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1961
	}
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1962
      }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1963
#endif	/* __linux__ */
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1964
      exitcode = EX_OSERR;
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1965
      /* Lower privileges */
1966
      errno = 0;
1967
      ret = seteuid(uid);
1968
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1969
	perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1970
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1971
      goto end;
1972
    }
1973
    if((network.ifr_flags & IFF_UP) == 0){
1974
      network.ifr_flags |= IFF_UP;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1975
      take_down_interface = true;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1976
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1977
      if(ret == -1){
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
1978
	take_down_interface = false;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1979
	perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
1980
	exitcode = EX_OSERR;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1981
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
1982
	if(restore_loglevel){
1983
	  ret = klogctl(7, NULL, 0);
1984
	  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1985
	    perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
1986
	  }
24.1.124 by Björn Påhlsson
Added lower kernel loglevel to reduce clutter on system console.
1987
	}
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
1988
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1989
	/* Lower privileges */
1990
	errno = 0;
1991
	ret = seteuid(uid);
1992
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
1993
	  perror_plus("seteuid");
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
1994
	}
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
1995
	goto end;
1996
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
1997
    }
485 by Teddy Hogeborn
Merge from Björn.
1998
    /* Sleep checking until interface is running.
1999
       Check every 0.25s, up to total time of delay */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2000
    for(int i=0; i < delay * 4; i++){
65 by Teddy Hogeborn
* plugins.d/password-request.c (main): Bug fix: Bring up network
2001
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2002
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2003
	perror_plus("ioctl SIOCGIFFLAGS");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2004
      } else if(network.ifr_flags & IFF_RUNNING){
2005
	break;
2006
      }
2007
      struct timespec sleeptime = { .tv_nsec = 250000000 };
2008
      ret = nanosleep(&sleeptime, NULL);
2009
      if(ret == -1 and errno != EINTR){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2010
	perror_plus("nanosleep");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2011
      }
2012
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2013
    if(not take_down_interface){
2014
      /* We won't need the socket anymore */
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2015
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2016
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2017
	perror_plus("close");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2018
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2019
    }
2020
#ifdef __linux__
304 by Teddy Hogeborn
Four new interrelated features:
2021
    if(restore_loglevel){
2022
      /* Restores kernel loglevel to default */
2023
      ret = klogctl(7, NULL, 0);
2024
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2025
	perror_plus("klogctl");
304 by Teddy Hogeborn
Four new interrelated features:
2026
      }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2027
    }
308 by Teddy Hogeborn
* plugin-runner.c: Comment change.
2028
#endif	/* __linux__ */
368 by Teddy Hogeborn
* plugins.d/mandos-client.c (init_gnutls_session): Retry interrupted
2029
    /* Lower privileges */
2030
    errno = 0;
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
2031
    /* Lower privileges */
2032
    ret = seteuid(uid);
2033
    if(ret == -1){
2034
      perror_plus("seteuid");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2035
    }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2036
  }
2037
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2038
  if(quit_now){
2039
    goto end;
2040
  }
2041
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2042
  ret = init_gnutls_global(pubkey, seckey);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2043
  if(ret == -1){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2044
    fprintf(stderr, "Mandos plugin mandos-client: "
2045
	    "init_gnutls_global failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2046
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2047
    goto end;
2048
  } else {
2049
    gnutls_initialized = true;
2050
  }
2051
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2052
  if(quit_now){
2053
    goto end;
2054
  }
2055
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2056
  if(mkdtemp(tempdir) == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2057
    perror_plus("mkdtemp");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2058
    goto end;
2059
  }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2060
  tempdir_created = true;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2061
  
2062
  if(quit_now){
2063
    goto end;
2064
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2065
  
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2066
  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
2067
    fprintf(stderr, "Mandos plugin mandos-client: "
2068
	    "init_gpgme failed\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2069
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2070
    goto end;
2071
  } else {
2072
    gpgme_initialized = true;
2073
  }
2074
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2075
  if(quit_now){
2076
    goto end;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2077
  }
2078
  
2079
  if(connect_to != NULL){
2080
    /* Connect directly, do not use Zeroconf */
2081
    /* (Mainly meant for debugging) */
2082
    char *address = strrchr(connect_to, ':');
2083
    if(address == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2084
      fprintf(stderr, "Mandos plugin mandos-client: "
2085
	      "No colon in address\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2086
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2087
      goto end;
2088
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2089
    
2090
    if(quit_now){
2091
      goto end;
2092
    }
2093
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2094
    uint16_t port;
311 by Teddy Hogeborn
Overflows are not detected by sscanf(), so stop using it:
2095
    errno = 0;
2096
    tmpmax = strtoimax(address+1, &tmp, 10);
2097
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
2098
       or tmpmax != (uint16_t)tmpmax){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2099
      fprintf(stderr, "Mandos plugin mandos-client: "
2100
	      "Bad port number\n");
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2101
      exitcode = EX_USAGE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2102
      goto end;
2103
    }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2104
  
2105
    if(quit_now){
2106
      goto end;
2107
    }
2108
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2109
    port = (uint16_t)tmpmax;
2110
    *address = '\0';
304 by Teddy Hogeborn
Four new interrelated features:
2111
    /* Colon in address indicates IPv6 */
2112
    int af;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2113
    if(strchr(connect_to, ':') != NULL){
304 by Teddy Hogeborn
Four new interrelated features:
2114
      af = AF_INET6;
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2115
      /* Accept [] around IPv6 address - see RFC 5952 */
2116
      if(connect_to[0] == '[' and address[-1] == ']')
2117
	{
2118
	  connect_to++;
2119
	  address[-1] = '\0';
2120
	}
304 by Teddy Hogeborn
Four new interrelated features:
2121
    } else {
2122
      af = AF_INET;
2123
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2124
    address = connect_to;
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2125
    
2126
    if(quit_now){
2127
      goto end;
2128
    }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2129
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
2130
    while(not quit_now){
2131
      ret = start_mandos_communication(address, port, if_index, af);
2132
      if(quit_now or ret == 0){
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2133
	break;
2134
      }
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2135
      if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2136
	fprintf(stderr, "Mandos plugin mandos-client: "
2137
		"Retrying in %d seconds\n", (int)retry_interval);
491 by teddy at bsnet
* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
2138
      }
2139
      sleep((int)retry_interval);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2140
    }
2141
    
24.1.163 by Björn Påhlsson
mandos-client: Added never ending loop for --connect
2142
    if (not quit_now){
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2143
      exitcode = EXIT_SUCCESS;
2144
    }
505.2.3 by Teddy Hogeborn
Intermediate commit - this does *not* work yet.
2145
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2146
    goto end;
2147
  }
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2148
  
2149
  if(quit_now){
2150
    goto end;
2151
  }
2152
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2153
  {
2154
    AvahiServerConfig config;
2155
    /* Do not publish any local Zeroconf records */
2156
    avahi_server_config_init(&config);
2157
    config.publish_hinfo = 0;
2158
    config.publish_addresses = 0;
2159
    config.publish_workstation = 0;
2160
    config.publish_domain = 0;
2161
    
2162
    /* Allocate a new server */
2163
    mc.server = avahi_server_new(avahi_simple_poll_get
2164
				 (mc.simple_poll), &config, NULL,
2165
				 NULL, &error);
2166
    
2167
    /* Free the Avahi configuration data */
2168
    avahi_server_config_free(&config);
2169
  }
2170
  
2171
  /* Check if creating the Avahi server object succeeded */
2172
  if(mc.server == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2173
    fprintf(stderr, "Mandos plugin mandos-client: "
2174
	    "Failed to create Avahi server: %s\n",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2175
	    avahi_strerror(error));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2176
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2177
    goto end;
2178
  }
2179
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2180
  if(quit_now){
2181
    goto end;
2182
  }
2183
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2184
  /* Create the Avahi service browser */
2185
  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
2186
				   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
24.1.134 by Björn Påhlsson
plugin-runner: Added support for empty string arguments
2187
				   NULL, 0, browse_callback, NULL);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2188
  if(sb == NULL){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2189
    fprintf(stderr, "Mandos plugin mandos-client: "
2190
	    "Failed to create service browser: %s\n",
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2191
	    avahi_strerror(avahi_server_errno(mc.server)));
399 by Teddy Hogeborn
* Makefile (BROKEN_PIE): Add comment.
2192
    exitcode = EX_UNAVAILABLE;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2193
    goto end;
2194
  }
307 by Teddy Hogeborn
Merge from Björn:
2195
  
353 by Teddy Hogeborn
* plugins.d/mandos-client.c (quit_now): Move up declaration before
2196
  if(quit_now){
2197
    goto end;
2198
  }
2199
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2200
  /* Run the main loop */
2201
  
2202
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2203
    fprintf(stderr, "Mandos plugin mandos-client: "
2204
	    "Starting Avahi loop search\n");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2205
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2206
485 by Teddy Hogeborn
Merge from Björn.
2207
  ret = avahi_loop_with_timeout(mc.simple_poll,
2208
				(int)(retry_interval * 1000));
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2209
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2210
    fprintf(stderr, "Mandos plugin mandos-client: "
2211
	    "avahi_loop_with_timeout exited %s\n",
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2212
	    (ret == 0) ? "successfully" : "with error");
2213
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2214
  
40 by Teddy Hogeborn
* plugins.d/mandosclient.c (initgnutls): Moved "err" variable into its
2215
 end:
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2216
  
2217
  if(debug){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2218
    fprintf(stderr, "Mandos plugin mandos-client: "
2219
	    "%s exiting\n", argv[0]);
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2220
  }
2221
  
2222
  /* Cleanup things */
2223
  if(sb != NULL)
2224
    avahi_s_service_browser_free(sb);
2225
  
2226
  if(mc.server != NULL)
2227
    avahi_server_free(mc.server);
2228
  
2229
  if(mc.simple_poll != NULL)
2230
    avahi_simple_poll_free(mc.simple_poll);
2231
  
2232
  if(gnutls_initialized){
2233
    gnutls_certificate_free_credentials(mc.cred);
2234
    gnutls_global_deinit();
2235
    gnutls_dh_params_deinit(mc.dh_params);
2236
  }
2237
  
2238
  if(gpgme_initialized){
2239
    gpgme_release(mc.ctx);
2240
  }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2241
485 by Teddy Hogeborn
Merge from Björn.
2242
  /* Cleans up the circular linked list of Mandos servers the client
2243
     has seen */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2244
  if(mc.current_server != NULL){
2245
    mc.current_server->prev->next = NULL;
2246
    while(mc.current_server != NULL){
2247
      server *next = mc.current_server->next;
2248
      free(mc.current_server);
2249
      mc.current_server = next;
2250
    }
2251
  }
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2252
  
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
2253
  /* Re-raise priviliges */
2254
  {
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2255
    errno = 0;
2256
    ret = seteuid(0);
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2257
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2258
      perror_plus("seteuid");
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2259
    }
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
2260
    /* Run network hooks */
2261
    if(not run_network_hooks("stop", interface, delay)){
2262
      goto end;
2263
    }
2264
    
2265
    /* Take down the network interface */
2266
    if(take_down_interface and geteuid() == 0){
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2267
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
2268
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2269
	perror_plus("ioctl SIOCGIFFLAGS");
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2270
      } else if(network.ifr_flags & IFF_UP){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
2271
	network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2272
	ret = ioctl(sd, SIOCSIFFLAGS, &network);
2273
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2274
	  perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2275
	}
2276
      }
2277
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
2278
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2279
	perror_plus("close");
365 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
2280
      }
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2281
    }
2282
  }
505.3.8 by Teddy Hogeborn
* plugins.d/mandos-client.c (run_network_hooks): New.
2283
  /* Lower privileges permanently */
2284
  errno = 0;
2285
  ret = setuid(uid);
2286
  if(ret == -1){
2287
    perror_plus("setuid");
2288
  }
350 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Take down network interface on
2289
  
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2290
  /* Removes the GPGME temp directory and all files inside */
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2291
  if(tempdir_created){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2292
    struct dirent **direntries = NULL;
2293
    struct dirent *direntry = NULL;
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2294
    int numentries = scandir(tempdir, &direntries, notdotentries,
2295
			     alphasort);
2296
    if (numentries > 0){
2297
      for(int i = 0; i < numentries; i++){
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2298
	direntry = direntries[i];
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2299
	char *fullname = NULL;
2300
	ret = asprintf(&fullname, "%s/%s", tempdir,
2301
		       direntry->d_name);
2302
	if(ret < 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2303
	  perror_plus("asprintf");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2304
	  continue;
2305
	}
2306
	ret = remove(fullname);
2307
	if(ret == -1){
505.3.3 by teddy at bsnet
* plugins.d/mandos-client.c: Prefix all printouts with "Mandos plugin
2308
	  fprintf(stderr, "Mandos plugin mandos-client: "
2309
		  "remove(\"%s\"): %s\n", fullname, strerror(errno));
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2310
	}
2311
	free(fullname);
2312
      }
24.1.172 by Björn Påhlsson
using scandir instead of readdir
2313
    }
2314
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2315
    /* 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
2316
    free(direntries);
492 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not even try to work around
2317
    if (numentries == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2318
      perror_plus("scandir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2319
    }
2320
    ret = rmdir(tempdir);
2321
    if(ret == -1 and errno != ENOENT){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2322
      perror_plus("rmdir");
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2323
    }
2324
  }
2325
  
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2326
  if(quit_now){
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
2327
    sigemptyset(&old_sigterm_action.sa_mask);
2328
    old_sigterm_action.sa_handler = SIG_DFL;
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2329
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
2330
					    &old_sigterm_action,
2331
					    NULL));
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2332
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2333
      perror_plus("sigaction");
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2334
    }
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2335
    do {
2336
      ret = raise(signal_received);
2337
    } while(ret != 0 and errno == EINTR);
2338
    if(ret != 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
2339
      perror_plus("raise");
374 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Try harder to raise signal on
2340
      abort();
2341
    }
2342
    TEMP_FAILURE_RETRY(pause());
354 by Teddy Hogeborn
* plugins.d/mandos-client.c (signal_received): New.
2343
  }
2344
  
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
2345
  return exitcode;
13 by Björn Påhlsson
Added following support:
2346
}