/mandos/release

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