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