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