8849 libresolv2: remove rcsid
[unleashed.git] / usr / src / lib / libresolv2 / common / irs / irp.c
blob07659c49af834945f959439e9e5f45cf5e9d607b
1 /*
2 * Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1996, 1998-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 #include "port_before.h"
20 #include <syslog.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <fcntl.h>
31 #include <syslog.h>
32 #include <ctype.h>
33 #include <unistd.h>
35 #include <isc/memcluster.h>
37 #include <irs.h>
38 #include <irp.h>
40 #include "irs_p.h"
41 #include "irp_p.h"
43 #include "port_after.h"
45 /* Forward. */
47 static void irp_close(struct irs_acc *);
49 #define LINEINCR 128
51 #if !defined(SUN_LEN)
52 #define SUN_LEN(su) \
53 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
54 #endif
57 /* Public */
60 /* send errors to syslog if true. */
61 int irp_log_errors = 1;
63 /*%
64 * This module handles the irp module connection to irpd.
66 * The client expects a synchronous interface to functions like
67 * getpwnam(3), so we can't use the ctl_* i/o library on this end of
68 * the wire (it's used in the server).
71 /*%
72 * irs_acc *irs_irp_acc(const char *options);
74 * Initialize the irp module.
76 struct irs_acc *
77 irs_irp_acc(const char *options) {
78 struct irs_acc *acc;
79 struct irp_p *irp;
81 UNUSED(options);
83 if (!(acc = memget(sizeof *acc))) {
84 errno = ENOMEM;
85 return (NULL);
87 memset(acc, 0x5e, sizeof *acc);
88 if (!(irp = memget(sizeof *irp))) {
89 errno = ENOMEM;
90 free(acc);
91 return (NULL);
93 irp->inlast = 0;
94 irp->incurr = 0;
95 irp->fdCxn = -1;
96 acc->private = irp;
98 #ifdef WANT_IRS_GR
99 acc->gr_map = irs_irp_gr;
100 #else
101 acc->gr_map = NULL;
102 #endif
103 #ifdef WANT_IRS_PW
104 acc->pw_map = irs_irp_pw;
105 #else
106 acc->pw_map = NULL;
107 #endif
108 acc->sv_map = irs_irp_sv;
109 acc->pr_map = irs_irp_pr;
110 acc->ho_map = irs_irp_ho;
111 acc->nw_map = irs_irp_nw;
112 acc->ng_map = irs_irp_ng;
113 acc->close = irp_close;
114 return (acc);
119 irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
120 if (irs_irp_is_connected(cxndata)) {
121 return (0);
122 } else if (irs_irp_connect(cxndata) != 0) {
123 if (warned != NULL && !*warned) {
124 syslog(LOG_ERR, "irpd connection failed: %m\n");
125 (*warned)++;
128 return (-1);
131 return (0);
135 * int irs_irp_connect(void);
137 * Sets up the connection to the remote irpd server.
139 * Returns:
141 * 0 on success, -1 on failure.
145 irs_irp_connect(struct irp_p *pvt) {
146 int flags;
147 struct sockaddr *addr;
148 struct sockaddr_in iaddr;
149 #ifndef NO_SOCKADDR_UN
150 struct sockaddr_un uaddr;
151 #endif
152 long ipaddr;
153 const char *irphost;
154 int code;
155 char text[256];
156 int socklen = 0;
158 if (pvt->fdCxn != -1) {
159 perror("fd != 1");
160 return (-1);
163 #ifndef NO_SOCKADDR_UN
164 memset(&uaddr, 0, sizeof uaddr);
165 #endif
166 memset(&iaddr, 0, sizeof iaddr);
168 irphost = getenv(IRPD_HOST_ENV);
169 if (irphost == NULL) {
170 irphost = "127.0.0.1";
173 #ifndef NO_SOCKADDR_UN
174 if (irphost[0] == '/') {
175 addr = (struct sockaddr *)&uaddr;
176 strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
177 uaddr.sun_family = AF_UNIX;
178 socklen = SUN_LEN(&uaddr);
179 #ifdef HAVE_SA_LEN
180 uaddr.sun_len = socklen;
181 #endif
182 } else
183 #endif
185 if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
186 errno = EADDRNOTAVAIL;
187 perror("inet_pton");
188 return (-1);
191 addr = (struct sockaddr *)&iaddr;
192 socklen = sizeof iaddr;
193 #ifdef HAVE_SA_LEN
194 iaddr.sin_len = socklen;
195 #endif
196 iaddr.sin_family = AF_INET;
197 iaddr.sin_port = htons(IRPD_PORT);
198 iaddr.sin_addr.s_addr = ipaddr;
202 pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
203 if (pvt->fdCxn < 0) {
204 perror("socket");
205 return (-1);
208 if (connect(pvt->fdCxn, addr, socklen) != 0) {
209 perror("connect");
210 return (-1);
213 flags = fcntl(pvt->fdCxn, F_GETFL, 0);
214 if (flags < 0) {
215 close(pvt->fdCxn);
216 perror("close");
217 return (-1);
220 #if 0
221 flags |= O_NONBLOCK;
222 if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
223 close(pvt->fdCxn);
224 perror("fcntl");
225 return (-1);
227 #endif
229 code = irs_irp_read_response(pvt, text, sizeof text);
230 if (code != IRPD_WELCOME_CODE) {
231 if (irp_log_errors) {
232 syslog(LOG_WARNING, "Connection failed: %s", text);
234 irs_irp_disconnect(pvt);
235 return (-1);
238 return (0);
242 * int irs_irp_is_connected(struct irp_p *pvt);
244 * Returns:
246 * Non-zero if streams are setup to remote.
251 irs_irp_is_connected(struct irp_p *pvt) {
252 return (pvt->fdCxn >= 0);
256 * void
257 * irs_irp_disconnect(struct irp_p *pvt);
259 * Closes streams to remote.
262 void
263 irs_irp_disconnect(struct irp_p *pvt) {
264 if (pvt->fdCxn != -1) {
265 close(pvt->fdCxn);
266 pvt->fdCxn = -1;
273 irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
274 char *realstart = &pvt->inbuffer[0];
275 char *p, *start, *end;
276 int spare;
277 int i;
278 int buffpos = 0;
279 int left = len - 1;
281 while (left > 0) {
282 start = p = &pvt->inbuffer[pvt->incurr];
283 end = &pvt->inbuffer[pvt->inlast];
285 while (p != end && *p != '\n')
286 p++;
288 if (p == end) {
289 /* Found no newline so shift data down if necessary
290 * and append new data to buffer
292 if (start > realstart) {
293 memmove(realstart, start, end - start);
294 pvt->inlast = end - start;
295 start = realstart;
296 pvt->incurr = 0;
297 end = &pvt->inbuffer[pvt->inlast];
300 spare = sizeof (pvt->inbuffer) - pvt->inlast;
302 p = end;
303 i = read(pvt->fdCxn, end, spare);
304 if (i < 0) {
305 close(pvt->fdCxn);
306 pvt->fdCxn = -1;
307 return (buffpos > 0 ? buffpos : -1);
308 } else if (i == 0) {
309 return (buffpos);
312 end += i;
313 pvt->inlast += i;
315 while (p != end && *p != '\n')
316 p++;
319 if (p == end) {
320 /* full buffer and still no newline */
321 i = sizeof pvt->inbuffer;
322 } else {
323 /* include newline */
324 i = p - start + 1;
327 if (i > left)
328 i = left;
329 memcpy(buffer + buffpos, start, i);
330 pvt->incurr += i;
331 buffpos += i;
332 buffer[buffpos] = '\0';
334 if (p != end) {
335 left = 0;
336 } else {
337 left -= i;
341 #if 0
342 fprintf(stderr, "read line: %s\n", buffer);
343 #endif
344 return (buffpos);
348 * int irp_read_response(struct irp_p *pvt);
350 * Returns:
352 * The number found at the beginning of the line read from
353 * FP. 0 on failure(0 is not a legal response code). The
354 * rest of the line is discarded.
359 irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
360 char line[1024];
361 int code;
362 char *p;
364 if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
365 return (0);
368 p = strchr(line, '\n');
369 if (p == NULL) {
370 return (0);
373 if (sscanf(line, "%d", &code) != 1) {
374 code = 0;
375 } else if (text != NULL && textlen > 0U) {
376 p = line;
377 while (isspace((unsigned char)*p)) p++;
378 while (isdigit((unsigned char)*p)) p++;
379 while (isspace((unsigned char)*p)) p++;
380 strncpy(text, p, textlen - 1);
381 p[textlen - 1] = '\0';
384 return (code);
388 * char *irp_read_body(struct irp_p *pvt, size_t *size);
390 * Read in the body of a response. Terminated by a line with
391 * just a dot on it. Lines should be terminated with a CR-LF
392 * sequence, but we're nt piccky if the CR is missing.
393 * No leading dot escaping is done as the protcol doesn't
394 * use leading dots anywhere.
396 * Returns:
398 * Pointer to null-terminated buffer allocated by memget.
399 * *SIZE is set to the length of the buffer.
403 char *
404 irs_irp_read_body(struct irp_p *pvt, size_t *size) {
405 char line[1024];
406 u_int linelen;
407 size_t len = LINEINCR;
408 char *buffer = memget(len);
409 int idx = 0;
411 if (buffer == NULL)
412 return (NULL);
414 for (;;) {
415 if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
416 strchr(line, '\n') == NULL)
417 goto death;
419 linelen = strlen(line);
421 if (line[linelen - 1] != '\n')
422 goto death;
424 /* We're not strict about missing \r. Should we be?? */
425 if (linelen > 2 && line[linelen - 2] == '\r') {
426 line[linelen - 2] = '\n';
427 line[linelen - 1] = '\0';
428 linelen--;
431 if (linelen == 2 && line[0] == '.') {
432 *size = len;
433 buffer[idx] = '\0';
435 return (buffer);
438 if (linelen > (len - (idx + 1))) {
439 char *p = memget(len + LINEINCR);
441 if (p == NULL)
442 goto death;
443 memcpy(p, buffer, len);
444 memput(buffer, len);
445 buffer = p;
446 len += LINEINCR;
449 memcpy(buffer + idx, line, linelen);
450 idx += linelen;
452 death:
453 memput(buffer, len);
454 return (NULL);
458 * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
459 * char **body, size_t *bodylen);
461 * Gets the response to a command. If the response indicates
462 * there's a body to follow(code % 10 == 1), then the
463 * body buffer is allcoated with memget and stored in
464 * *BODY. The length of the allocated body buffer is stored
465 * in *BODY. The caller must give the body buffer back to
466 * memput when done. The results code is stored in *CODE.
468 * Returns:
470 * 0 if a result was read. -1 on some sort of failure.
475 irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
476 size_t textlen, char **body, size_t *bodylen) {
477 int result = irs_irp_read_response(pvt, text, textlen);
479 *body = NULL;
481 if (result == 0) {
482 return (-1);
485 *code = result;
487 /* Code that matches 2xx is a good result code.
488 * Code that matches xx1 means there's a response body coming.
490 if ((result / 100) == 2 && (result % 10) == 1) {
491 *body = irs_irp_read_body(pvt, bodylen);
492 if (*body == NULL) {
493 return (-1);
497 return (0);
501 * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
503 * Sends command to remote connected via the PVT
504 * structure. FMT and args after it are fprintf-like
505 * arguments for formatting.
507 * Returns:
509 * 0 on success, -1 on failure.
513 irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
514 va_list ap;
515 char buffer[1024];
516 int pos = 0;
517 int i, todo;
520 if (pvt->fdCxn < 0) {
521 return (-1);
524 va_start(ap, fmt);
525 (void) vsprintf(buffer, fmt, ap);
526 todo = strlen(buffer);
527 va_end(ap);
528 if (todo > (int)sizeof(buffer) - 3) {
529 syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
530 exit(1);
532 strcat(buffer, "\r\n");
533 todo = strlen(buffer);
535 while (todo > 0) {
536 i = write(pvt->fdCxn, buffer + pos, todo);
537 #if 0
538 /* XXX brister */
539 fprintf(stderr, "Wrote: \"");
540 fwrite(buffer + pos, sizeof (char), todo, stderr);
541 fprintf(stderr, "\"\n");
542 #endif
543 if (i < 0) {
544 close(pvt->fdCxn);
545 pvt->fdCxn = -1;
546 return (-1);
548 todo -= i;
551 return (0);
555 /* Methods */
558 * void irp_close(struct irs_acc *this)
562 static void
563 irp_close(struct irs_acc *this) {
564 struct irp_p *irp = (struct irp_p *)this->private;
566 if (irp != NULL) {
567 irs_irp_disconnect(irp);
568 memput(irp, sizeof *irp);
571 memput(this, sizeof *this);
577 /*! \file */