summaryrefslogtreecommitdiffstats
path: root/contrib/bind9/lib/bind/irs/irp.c
blob: 649079c31f1c2931f4a657036fc0d702a8962829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1996, 1998 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#if !defined(LINT) && !defined(CODECENTER)
static const char rcsid[] = "$Id: irp.c,v 1.3.2.1.10.4 2006/03/10 00:17:21 marka Exp $";
#endif

/* Imports */

#include "port_before.h"

#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <syslog.h>
#include <ctype.h>
#include <unistd.h>

#include <isc/memcluster.h>

#include <irs.h>
#include <irp.h>

#include "irs_p.h"
#include "irp_p.h"

#include "port_after.h"

/* Forward. */

static void		irp_close(struct irs_acc *);

#define LINEINCR 128

#if !defined(SUN_LEN)
#define SUN_LEN(su) \
	(sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
#endif


/* Public */


/* send errors to syslog if true. */
int irp_log_errors = 1;

/*
 * This module handles the irp module connection to irpd.
 *
 * The client expects a synchronous interface to functions like
 * getpwnam(3), so we can't use the ctl_* i/o library on this end of
 * the wire (it's used in the server).
 */

/*
 * irs_acc *irs_irp_acc(const char *options);
 *
 *	Initialize the irp module.
 */
struct irs_acc *
irs_irp_acc(const char *options) {
	struct irs_acc *acc;
	struct irp_p *irp;

	UNUSED(options);

	if (!(acc = memget(sizeof *acc))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(acc, 0x5e, sizeof *acc);
	if (!(irp = memget(sizeof *irp))) {
		errno = ENOMEM;
		free(acc);
		return (NULL);
	}
	irp->inlast = 0;
	irp->incurr = 0;
	irp->fdCxn = -1;
	acc->private = irp;

#ifdef WANT_IRS_GR
	acc->gr_map = irs_irp_gr;
#else
	acc->gr_map = NULL;
#endif
#ifdef WANT_IRS_PW
	acc->pw_map = irs_irp_pw;
#else
	acc->pw_map = NULL;
#endif
	acc->sv_map = irs_irp_sv;
	acc->pr_map = irs_irp_pr;
	acc->ho_map = irs_irp_ho;
	acc->nw_map = irs_irp_nw;
	acc->ng_map = irs_irp_ng;
	acc->close = irp_close;
	return (acc);
}


int
irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
	if (irs_irp_is_connected(cxndata)) {
		return (0);
	} else if (irs_irp_connect(cxndata) != 0) {
		if (warned != NULL && !*warned) {
			syslog(LOG_ERR, "irpd connection failed: %m\n");
			(*warned)++;
		}

		return (-1);
	}

	return (0);
}


/*
 * int irs_irp_connect(void);
 *
 *	Sets up the connection to the remote irpd server.
 *
 * Returns:
 *
 *	0 on success, -1 on failure.
 *
 */
int
irs_irp_connect(struct irp_p *pvt) {
	int flags;
	struct sockaddr *addr;
	struct sockaddr_in iaddr;
#ifndef NO_SOCKADDR_UN
	struct sockaddr_un uaddr;
#endif
	long ipaddr;
	const char *irphost;
	int code;
	char text[256];
	int socklen = 0;

	if (pvt->fdCxn != -1) {
		perror("fd != 1");
		return (-1);
	}

#ifndef NO_SOCKADDR_UN
	memset(&uaddr, 0, sizeof uaddr);
#endif
	memset(&iaddr, 0, sizeof iaddr);

	irphost = getenv(IRPD_HOST_ENV);
	if (irphost == NULL) {
		irphost = "127.0.0.1";
	}

#ifndef NO_SOCKADDR_UN
	if (irphost[0] == '/') {
		addr = (struct sockaddr *)&uaddr;
		strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
		uaddr.sun_family = AF_UNIX;
		socklen = SUN_LEN(&uaddr);
#ifdef HAVE_SA_LEN
		uaddr.sun_len = socklen;
#endif
	} else
#endif
	{
		if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
			errno = EADDRNOTAVAIL;
			perror("inet_pton");
			return (-1);
		}

		addr = (struct sockaddr *)&iaddr;
		socklen = sizeof iaddr;
#ifdef HAVE_SA_LEN
		iaddr.sin_len = socklen;
#endif
		iaddr.sin_family = AF_INET;
		iaddr.sin_port = htons(IRPD_PORT);
		iaddr.sin_addr.s_addr = ipaddr;
	}


	pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
	if (pvt->fdCxn < 0) {
		perror("socket");
		return (-1);
	}

	if (connect(pvt->fdCxn, addr, socklen) != 0) {
		perror("connect");
		return (-1);
	}

	flags = fcntl(pvt->fdCxn, F_GETFL, 0);
	if (flags < 0) {
		close(pvt->fdCxn);
		perror("close");
		return (-1);
	}

#if 0
	flags |= O_NONBLOCK;
	if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
		close(pvt->fdCxn);
		perror("fcntl");
		return (-1);
	}
#endif

	code = irs_irp_read_response(pvt, text, sizeof text);
	if (code != IRPD_WELCOME_CODE) {
		if (irp_log_errors) {
			syslog(LOG_WARNING, "Connection failed: %s", text);
		}
		irs_irp_disconnect(pvt);
		return (-1);
	}

	return (0);
}



/*
 * int	irs_irp_is_connected(struct irp_p *pvt);
 *
 * Returns:
 *
 *	Non-zero if streams are setup to remote.
 *
 */

int
irs_irp_is_connected(struct irp_p *pvt) {
	return (pvt->fdCxn >= 0);
}



/*
 * void
 * irs_irp_disconnect(struct irp_p *pvt);
 *
 *	Closes streams to remote.
 */

void
irs_irp_disconnect(struct irp_p *pvt) {
	if (pvt->fdCxn != -1) {
		close(pvt->fdCxn);
		pvt->fdCxn = -1;
	}
}



int
irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
	char *realstart = &pvt->inbuffer[0];
	char *p, *start, *end;
	int spare;
	int i;
	int buffpos = 0;
	int left = len - 1;

	while (left > 0) {
		start = p = &pvt->inbuffer[pvt->incurr];
		end = &pvt->inbuffer[pvt->inlast];

		while (p != end && *p != '\n')
			p++;

		if (p == end) {
			/* Found no newline so shift data down if necessary
			 * and append new data to buffer
			 */
			if (start > realstart) {
				memmove(realstart, start, end - start);
				pvt->inlast = end - start;
				start = realstart;
				pvt->incurr = 0;
				end = &pvt->inbuffer[pvt->inlast];
			}

			spare = sizeof (pvt->inbuffer) - pvt->inlast;

			p = end;
			i = read(pvt->fdCxn, end, spare);
			if (i < 0) {
				close(pvt->fdCxn);
				pvt->fdCxn = -1;
				return (buffpos > 0 ? buffpos : -1);
			} else if (i == 0) {
				return (buffpos);
			}

			end += i;
			pvt->inlast += i;

			while (p != end && *p != '\n')
				p++;
		}

		if (p == end) {
			/* full buffer and still no newline */
			i = sizeof pvt->inbuffer;
		} else {
			/* include newline */
			i = p - start + 1;
		}

		if (i > left)
			i = left;
		memcpy(buffer + buffpos, start, i);
		pvt->incurr += i;
		buffpos += i;
		buffer[buffpos] = '\0';

		if (p != end) {
			left = 0;
		} else {
			left -= i;
		}
	}

#if 0
	fprintf(stderr, "read line: %s\n", buffer);
#endif
	return (buffpos);
}





/*
 * int irp_read_response(struct irp_p *pvt);
 *
 * Returns:
 *
 *	The number found at the beginning of the line read from
 *	FP. 0 on failure(0 is not a legal response code). The
 *	rest of the line is discarded.
 *
 */

int
irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
	char line[1024];
	int code;
	char *p;

	if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
		return (0);
	}

	p = strchr(line, '\n');
	if (p == NULL) {
		return (0);
	}

	if (sscanf(line, "%d", &code) != 1) {
		code = 0;
	} else if (text != NULL && textlen > 0U) {
		p = line;
		while (isspace((unsigned char)*p)) p++;
		while (isdigit((unsigned char)*p)) p++;
		while (isspace((unsigned char)*p)) p++;
		strncpy(text, p, textlen - 1);
		p[textlen - 1] = '\0';
	}

	return (code);
}



/*
 * char *irp_read_body(struct irp_p *pvt, size_t *size);
 *
 *	Read in the body of a response. Terminated by a line with
 *	just a dot on it. Lines should be terminated with a CR-LF
 *	sequence, but we're nt piccky if the CR is missing.
 *	No leading dot escaping is done as the protcol doesn't
 *	use leading dots anywhere.
 *
 * Returns:
 *
 *	Pointer to null-terminated buffer allocated by memget.
 *	*SIZE is set to the length of the buffer.
 *
 */

char *
irs_irp_read_body(struct irp_p *pvt, size_t *size) {
	char line[1024];
	u_int linelen;
	size_t len = LINEINCR;
	char *buffer = memget(len);
	int idx = 0;

	if (buffer == NULL)
		return (NULL);

	for (;;) {
		if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
		    strchr(line, '\n') == NULL)
			goto death;

		linelen = strlen(line);

		if (line[linelen - 1] != '\n')
			goto death;

		/* We're not strict about missing \r. Should we be??  */
		if (linelen > 2 && line[linelen - 2] == '\r') {
			line[linelen - 2] = '\n';
			line[linelen - 1] = '\0';
			linelen--;
		}

		if (linelen == 2 && line[0] == '.') {
			*size = len;
			buffer[idx] = '\0';

			return (buffer);
		}

		if (linelen > (len - (idx + 1))) {
			char *p = memget(len + LINEINCR);

			if (p == NULL)
				goto death;
			memcpy(p, buffer, len);
			memput(buffer, len);
			buffer = p;
			len += LINEINCR;
		}

		memcpy(buffer + idx, line, linelen);
		idx += linelen;
	}
 death:
	memput(buffer, len);
	return (NULL);
}


/*
 * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
 *			char **body, size_t *bodylen);
 *
 *	Gets the response to a command. If the response indicates
 *	there's a body to follow(code % 10 == 1), then the
 *	body buffer is allcoated with memget and stored in
 *	*BODY. The length of the allocated body buffer is stored
 *	in *BODY. The caller must give the body buffer back to
 *	memput when done. The results code is stored in *CODE.
 *
 * Returns:
 *
 *	0 if a result was read. -1 on some sort of failure.
 *
 */

int
irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
			  size_t textlen, char **body, size_t *bodylen) {
	int result = irs_irp_read_response(pvt, text, textlen);

	*body = NULL;

	if (result == 0) {
		return (-1);
	}

	*code = result;

	/* Code that matches 2xx is a good result code.
	 * Code that matches xx1 means there's a response body coming.
	 */
	if ((result / 100) == 2 && (result % 10) == 1) {
		*body = irs_irp_read_body(pvt, bodylen);
		if (*body == NULL) {
			return (-1);
		}
	}

	return (0);
}


/*
 * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
 *
 *	Sends command to remote connected via the PVT
 *	structure. FMT and args after it are fprintf-like
 *	arguments for formatting.
 *
 * Returns:
 *
 *	0 on success, -1 on failure.
 */

int
irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
	va_list ap;
	char buffer[1024];
	int pos = 0;
	int i, todo;


	if (pvt->fdCxn < 0) {
		return (-1);
	}

	va_start(ap, fmt);
	todo = vsprintf(buffer, fmt, ap);
	va_end(ap);
	if (todo > (int)sizeof(buffer) - 3) {
		syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
		exit(1);
	}
	strcat(buffer, "\r\n");
	todo = strlen(buffer);

	while (todo > 0) {
		i = write(pvt->fdCxn, buffer + pos, todo);
#if 0
		/* XXX brister */
		fprintf(stderr, "Wrote: \"");
		fwrite(buffer + pos, sizeof (char), todo, stderr);
		fprintf(stderr, "\"\n");
#endif
		if (i < 0) {
			close(pvt->fdCxn);
			pvt->fdCxn = -1;
			return (-1);
		}
		todo -= i;
	}

	return (0);
}


/* Methods */



/*
 * void irp_close(struct irs_acc *this)
 *
 */

static void
irp_close(struct irs_acc *this) {
	struct irp_p *irp = (struct irp_p *)this->private;

	if (irp != NULL) {
		irs_irp_disconnect(irp);
		memput(irp, sizeof *irp);
	}

	memput(this, sizeof *this);
}



OpenPOWER on IntegriCloud