/mandos/trunk

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