/mandos/release

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