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