/mandos/trunk

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