2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996, 1998 by Internet Software Consortium.
5 * Permission to use, copy, modify, and 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
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if !defined(LINT) && !defined(CODECENTER)
19 static const char rcsid
[] = "$Id: irp.c,v 1.3.2.1.10.4 2006/03/10 00:17:21 marka Exp $";
24 #include "port_before.h"
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
41 #include <isc/memcluster.h>
49 #include "port_after.h"
53 static void irp_close(struct irs_acc
*);
59 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
66 /* send errors to syslog if true. */
67 int irp_log_errors
= 1;
70 * This module handles the irp module connection to irpd.
72 * The client expects a synchronous interface to functions like
73 * getpwnam(3), so we can't use the ctl_* i/o library on this end of
74 * the wire (it's used in the server).
78 * irs_acc *irs_irp_acc(const char *options);
80 * Initialize the irp module.
83 irs_irp_acc(const char *options
) {
89 if (!(acc
= memget(sizeof *acc
))) {
93 memset(acc
, 0x5e, sizeof *acc
);
94 if (!(irp
= memget(sizeof *irp
))) {
105 acc
->gr_map
= irs_irp_gr
;
110 acc
->pw_map
= irs_irp_pw
;
114 acc
->sv_map
= irs_irp_sv
;
115 acc
->pr_map
= irs_irp_pr
;
116 acc
->ho_map
= irs_irp_ho
;
117 acc
->nw_map
= irs_irp_nw
;
118 acc
->ng_map
= irs_irp_ng
;
119 acc
->close
= irp_close
;
125 irs_irp_connection_setup(struct irp_p
*cxndata
, int *warned
) {
126 if (irs_irp_is_connected(cxndata
)) {
128 } else if (irs_irp_connect(cxndata
) != 0) {
129 if (warned
!= NULL
&& !*warned
) {
130 syslog(LOG_ERR
, "irpd connection failed: %m\n");
142 * int irs_irp_connect(void);
144 * Sets up the connection to the remote irpd server.
148 * 0 on success, -1 on failure.
152 irs_irp_connect(struct irp_p
*pvt
) {
154 struct sockaddr
*addr
;
155 struct sockaddr_in iaddr
;
156 #ifndef NO_SOCKADDR_UN
157 struct sockaddr_un uaddr
;
165 if (pvt
->fdCxn
!= -1) {
170 #ifndef NO_SOCKADDR_UN
171 memset(&uaddr
, 0, sizeof uaddr
);
173 memset(&iaddr
, 0, sizeof iaddr
);
175 irphost
= getenv(IRPD_HOST_ENV
);
176 if (irphost
== NULL
) {
177 irphost
= "127.0.0.1";
180 #ifndef NO_SOCKADDR_UN
181 if (irphost
[0] == '/') {
182 addr
= (struct sockaddr
*)&uaddr
;
183 strncpy(uaddr
.sun_path
, irphost
, sizeof uaddr
.sun_path
);
184 uaddr
.sun_family
= AF_UNIX
;
185 socklen
= SUN_LEN(&uaddr
);
187 uaddr
.sun_len
= socklen
;
192 if (inet_pton(AF_INET
, irphost
, &ipaddr
) != 1) {
193 errno
= EADDRNOTAVAIL
;
198 addr
= (struct sockaddr
*)&iaddr
;
199 socklen
= sizeof iaddr
;
201 iaddr
.sin_len
= socklen
;
203 iaddr
.sin_family
= AF_INET
;
204 iaddr
.sin_port
= htons(IRPD_PORT
);
205 iaddr
.sin_addr
.s_addr
= ipaddr
;
209 pvt
->fdCxn
= socket(addr
->sa_family
, SOCK_STREAM
, PF_UNSPEC
);
210 if (pvt
->fdCxn
< 0) {
215 if (connect(pvt
->fdCxn
, addr
, socklen
) != 0) {
220 flags
= fcntl(pvt
->fdCxn
, F_GETFL
, 0);
229 if (fcntl(pvt
->fdCxn
, F_SETFL
, flags
) < 0) {
236 code
= irs_irp_read_response(pvt
, text
, sizeof text
);
237 if (code
!= IRPD_WELCOME_CODE
) {
238 if (irp_log_errors
) {
239 syslog(LOG_WARNING
, "Connection failed: %s", text
);
241 irs_irp_disconnect(pvt
);
251 * int irs_irp_is_connected(struct irp_p *pvt);
255 * Non-zero if streams are setup to remote.
260 irs_irp_is_connected(struct irp_p
*pvt
) {
261 return (pvt
->fdCxn
>= 0);
268 * irs_irp_disconnect(struct irp_p *pvt);
270 * Closes streams to remote.
274 irs_irp_disconnect(struct irp_p
*pvt
) {
275 if (pvt
->fdCxn
!= -1) {
284 irs_irp_read_line(struct irp_p
*pvt
, char *buffer
, int len
) {
285 char *realstart
= &pvt
->inbuffer
[0];
286 char *p
, *start
, *end
;
293 start
= p
= &pvt
->inbuffer
[pvt
->incurr
];
294 end
= &pvt
->inbuffer
[pvt
->inlast
];
296 while (p
!= end
&& *p
!= '\n')
300 /* Found no newline so shift data down if necessary
301 * and append new data to buffer
303 if (start
> realstart
) {
304 memmove(realstart
, start
, end
- start
);
305 pvt
->inlast
= end
- start
;
308 end
= &pvt
->inbuffer
[pvt
->inlast
];
311 spare
= sizeof (pvt
->inbuffer
) - pvt
->inlast
;
314 i
= read(pvt
->fdCxn
, end
, spare
);
318 return (buffpos
> 0 ? buffpos
: -1);
326 while (p
!= end
&& *p
!= '\n')
331 /* full buffer and still no newline */
332 i
= sizeof pvt
->inbuffer
;
334 /* include newline */
340 memcpy(buffer
+ buffpos
, start
, i
);
343 buffer
[buffpos
] = '\0';
353 fprintf(stderr
, "read line: %s\n", buffer
);
363 * int irp_read_response(struct irp_p *pvt);
367 * The number found at the beginning of the line read from
368 * FP. 0 on failure(0 is not a legal response code). The
369 * rest of the line is discarded.
374 irs_irp_read_response(struct irp_p
*pvt
, char *text
, size_t textlen
) {
379 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0) {
383 p
= strchr(line
, '\n');
388 if (sscanf(line
, "%d", &code
) != 1) {
390 } else if (text
!= NULL
&& textlen
> 0U) {
392 while (isspace((unsigned char)*p
)) p
++;
393 while (isdigit((unsigned char)*p
)) p
++;
394 while (isspace((unsigned char)*p
)) p
++;
395 strncpy(text
, p
, textlen
- 1);
396 p
[textlen
- 1] = '\0';
405 * char *irp_read_body(struct irp_p *pvt, size_t *size);
407 * Read in the body of a response. Terminated by a line with
408 * just a dot on it. Lines should be terminated with a CR-LF
409 * sequence, but we're nt piccky if the CR is missing.
410 * No leading dot escaping is done as the protcol doesn't
411 * use leading dots anywhere.
415 * Pointer to null-terminated buffer allocated by memget.
416 * *SIZE is set to the length of the buffer.
421 irs_irp_read_body(struct irp_p
*pvt
, size_t *size
) {
424 size_t len
= LINEINCR
;
425 char *buffer
= memget(len
);
432 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0 ||
433 strchr(line
, '\n') == NULL
)
436 linelen
= strlen(line
);
438 if (line
[linelen
- 1] != '\n')
441 /* We're not strict about missing \r. Should we be?? */
442 if (linelen
> 2 && line
[linelen
- 2] == '\r') {
443 line
[linelen
- 2] = '\n';
444 line
[linelen
- 1] = '\0';
448 if (linelen
== 2 && line
[0] == '.') {
455 if (linelen
> (len
- (idx
+ 1))) {
456 char *p
= memget(len
+ LINEINCR
);
460 memcpy(p
, buffer
, len
);
466 memcpy(buffer
+ idx
, line
, linelen
);
476 * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
477 * char **body, size_t *bodylen);
479 * Gets the response to a command. If the response indicates
480 * there's a body to follow(code % 10 == 1), then the
481 * body buffer is allcoated with memget and stored in
482 * *BODY. The length of the allocated body buffer is stored
483 * in *BODY. The caller must give the body buffer back to
484 * memput when done. The results code is stored in *CODE.
488 * 0 if a result was read. -1 on some sort of failure.
493 irs_irp_get_full_response(struct irp_p
*pvt
, int *code
, char *text
,
494 size_t textlen
, char **body
, size_t *bodylen
) {
495 int result
= irs_irp_read_response(pvt
, text
, textlen
);
505 /* Code that matches 2xx is a good result code.
506 * Code that matches xx1 means there's a response body coming.
508 if ((result
/ 100) == 2 && (result
% 10) == 1) {
509 *body
= irs_irp_read_body(pvt
, bodylen
);
520 * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
522 * Sends command to remote connected via the PVT
523 * structure. FMT and args after it are fprintf-like
524 * arguments for formatting.
528 * 0 on success, -1 on failure.
532 irs_irp_send_command(struct irp_p
*pvt
, const char *fmt
, ...) {
539 if (pvt
->fdCxn
< 0) {
544 todo
= vsprintf(buffer
, fmt
, ap
);
546 if (todo
> (int)sizeof(buffer
) - 3) {
547 syslog(LOG_CRIT
, "memory overrun in irs_irp_send_command()");
550 strcat(buffer
, "\r\n");
551 todo
= strlen(buffer
);
554 i
= write(pvt
->fdCxn
, buffer
+ pos
, todo
);
557 fprintf(stderr
, "Wrote: \"");
558 fwrite(buffer
+ pos
, sizeof (char), todo
, stderr
);
559 fprintf(stderr
, "\"\n");
578 * void irp_close(struct irs_acc *this)
583 irp_close(struct irs_acc
*this) {
584 struct irp_p
*irp
= (struct irp_p
*)this->private;
587 irs_irp_disconnect(irp
);
588 memput(irp
, sizeof *irp
);
591 memput(this, sizeof *this);