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