4 * Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1996, 1998-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid
[] = "Id: irp.c,v 1.12 2008/11/14 02:36:51 marka Exp";
26 #include "port_before.h"
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
43 #include <isc/memcluster.h>
51 #include "port_after.h"
55 static void irp_close(struct irs_acc
*);
61 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
68 /* send errors to syslog if true. */
69 int irp_log_errors
= 1;
72 * This module handles the irp module connection to irpd.
74 * The client expects a synchronous interface to functions like
75 * getpwnam(3), so we can't use the ctl_* i/o library on this end of
76 * the wire (it's used in the server).
80 * irs_acc *irs_irp_acc(const char *options);
82 * Initialize the irp module.
85 irs_irp_acc(const char *options
) {
91 if (!(acc
= memget(sizeof *acc
))) {
95 memset(acc
, 0x5e, sizeof *acc
);
96 if (!(irp
= memget(sizeof *irp
))) {
107 acc
->gr_map
= irs_irp_gr
;
112 acc
->pw_map
= irs_irp_pw
;
116 acc
->sv_map
= irs_irp_sv
;
117 acc
->pr_map
= irs_irp_pr
;
118 acc
->ho_map
= irs_irp_ho
;
119 acc
->nw_map
= irs_irp_nw
;
120 acc
->ng_map
= irs_irp_ng
;
121 acc
->close
= irp_close
;
127 irs_irp_connection_setup(struct irp_p
*cxndata
, int *warned
) {
128 if (irs_irp_is_connected(cxndata
)) {
130 } else if (irs_irp_connect(cxndata
) != 0) {
131 if (warned
!= NULL
&& !*warned
) {
132 syslog(LOG_ERR
, "irpd connection failed: %m\n");
143 * int irs_irp_connect(void);
145 * Sets up the connection to the remote irpd server.
149 * 0 on success, -1 on failure.
153 irs_irp_connect(struct irp_p
*pvt
) {
155 struct sockaddr
*addr
;
156 struct sockaddr_in iaddr
;
157 #ifndef NO_SOCKADDR_UN
158 struct sockaddr_un uaddr
;
166 if (pvt
->fdCxn
!= -1) {
171 #ifndef NO_SOCKADDR_UN
172 memset(&uaddr
, 0, sizeof uaddr
);
174 memset(&iaddr
, 0, sizeof iaddr
);
176 irphost
= getenv(IRPD_HOST_ENV
);
177 if (irphost
== NULL
) {
178 irphost
= "127.0.0.1";
181 #ifndef NO_SOCKADDR_UN
182 if (irphost
[0] == '/') {
183 addr
= (struct sockaddr
*)&uaddr
;
184 strncpy(uaddr
.sun_path
, irphost
, sizeof uaddr
.sun_path
);
185 uaddr
.sun_family
= AF_UNIX
;
186 socklen
= SUN_LEN(&uaddr
);
188 uaddr
.sun_len
= socklen
;
193 if (inet_pton(AF_INET
, irphost
, &ipaddr
) != 1) {
194 errno
= EADDRNOTAVAIL
;
199 addr
= (struct sockaddr
*)&iaddr
;
200 socklen
= sizeof iaddr
;
202 iaddr
.sin_len
= socklen
;
204 iaddr
.sin_family
= AF_INET
;
205 iaddr
.sin_port
= htons(IRPD_PORT
);
206 iaddr
.sin_addr
.s_addr
= ipaddr
;
210 pvt
->fdCxn
= socket(addr
->sa_family
, SOCK_STREAM
, PF_UNSPEC
);
211 if (pvt
->fdCxn
< 0) {
216 if (connect(pvt
->fdCxn
, addr
, socklen
) != 0) {
221 flags
= fcntl(pvt
->fdCxn
, F_GETFL
, 0);
230 if (fcntl(pvt
->fdCxn
, F_SETFL
, flags
) < 0) {
237 code
= irs_irp_read_response(pvt
, text
, sizeof text
);
238 if (code
!= IRPD_WELCOME_CODE
) {
239 if (irp_log_errors
) {
240 syslog(LOG_WARNING
, "Connection failed: %s", text
);
242 irs_irp_disconnect(pvt
);
250 * int irs_irp_is_connected(struct irp_p *pvt);
254 * Non-zero if streams are setup to remote.
259 irs_irp_is_connected(struct irp_p
*pvt
) {
260 return (pvt
->fdCxn
>= 0);
265 * irs_irp_disconnect(struct irp_p *pvt);
267 * Closes streams to remote.
271 irs_irp_disconnect(struct irp_p
*pvt
) {
272 if (pvt
->fdCxn
!= -1) {
281 irs_irp_read_line(struct irp_p
*pvt
, char *buffer
, int len
) {
282 char *realstart
= &pvt
->inbuffer
[0];
283 char *p
, *start
, *end
;
290 start
= p
= &pvt
->inbuffer
[pvt
->incurr
];
291 end
= &pvt
->inbuffer
[pvt
->inlast
];
293 while (p
!= end
&& *p
!= '\n')
297 /* Found no newline so shift data down if necessary
298 * and append new data to buffer
300 if (start
> realstart
) {
301 memmove(realstart
, start
, end
- start
);
302 pvt
->inlast
= end
- start
;
305 end
= &pvt
->inbuffer
[pvt
->inlast
];
308 spare
= sizeof (pvt
->inbuffer
) - pvt
->inlast
;
311 i
= read(pvt
->fdCxn
, end
, spare
);
315 return (buffpos
> 0 ? buffpos
: -1);
323 while (p
!= end
&& *p
!= '\n')
328 /* full buffer and still no newline */
329 i
= sizeof pvt
->inbuffer
;
331 /* include newline */
337 memcpy(buffer
+ buffpos
, start
, i
);
340 buffer
[buffpos
] = '\0';
350 fprintf(stderr
, "read line: %s\n", buffer
);
356 * int irp_read_response(struct irp_p *pvt);
360 * The number found at the beginning of the line read from
361 * FP. 0 on failure(0 is not a legal response code). The
362 * rest of the line is discarded.
367 irs_irp_read_response(struct irp_p
*pvt
, char *text
, size_t textlen
) {
372 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0) {
376 p
= strchr(line
, '\n');
381 if (sscanf(line
, "%d", &code
) != 1) {
383 } else if (text
!= NULL
&& textlen
> 0U) {
385 while (isspace((unsigned char)*p
)) p
++;
386 while (isdigit((unsigned char)*p
)) p
++;
387 while (isspace((unsigned char)*p
)) p
++;
388 strncpy(text
, p
, textlen
- 1);
389 p
[textlen
- 1] = '\0';
396 * char *irp_read_body(struct irp_p *pvt, size_t *size);
398 * Read in the body of a response. Terminated by a line with
399 * just a dot on it. Lines should be terminated with a CR-LF
400 * sequence, but we're nt piccky if the CR is missing.
401 * No leading dot escaping is done as the protcol doesn't
402 * use leading dots anywhere.
406 * Pointer to null-terminated buffer allocated by memget.
407 * *SIZE is set to the length of the buffer.
412 irs_irp_read_body(struct irp_p
*pvt
, size_t *size
) {
415 size_t len
= LINEINCR
;
416 char *buffer
= memget(len
);
423 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0 ||
424 strchr(line
, '\n') == NULL
)
427 linelen
= strlen(line
);
429 if (line
[linelen
- 1] != '\n')
432 /* We're not strict about missing \r. Should we be?? */
433 if (linelen
> 2 && line
[linelen
- 2] == '\r') {
434 line
[linelen
- 2] = '\n';
435 line
[linelen
- 1] = '\0';
439 if (linelen
== 2 && line
[0] == '.') {
446 if (linelen
> (len
- (idx
+ 1))) {
447 char *p
= memget(len
+ LINEINCR
);
451 memcpy(p
, buffer
, len
);
457 memcpy(buffer
+ idx
, line
, linelen
);
466 * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
467 * char **body, size_t *bodylen);
469 * Gets the response to a command. If the response indicates
470 * there's a body to follow(code % 10 == 1), then the
471 * body buffer is allcoated with memget and stored in
472 * *BODY. The length of the allocated body buffer is stored
473 * in *BODY. The caller must give the body buffer back to
474 * memput when done. The results code is stored in *CODE.
478 * 0 if a result was read. -1 on some sort of failure.
483 irs_irp_get_full_response(struct irp_p
*pvt
, int *code
, char *text
,
484 size_t textlen
, char **body
, size_t *bodylen
) {
485 int result
= irs_irp_read_response(pvt
, text
, textlen
);
495 /* Code that matches 2xx is a good result code.
496 * Code that matches xx1 means there's a response body coming.
498 if ((result
/ 100) == 2 && (result
% 10) == 1) {
499 *body
= irs_irp_read_body(pvt
, bodylen
);
509 * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
511 * Sends command to remote connected via the PVT
512 * structure. FMT and args after it are fprintf-like
513 * arguments for formatting.
517 * 0 on success, -1 on failure.
521 irs_irp_send_command(struct irp_p
*pvt
, const char *fmt
, ...) {
528 if (pvt
->fdCxn
< 0) {
533 (void) vsprintf(buffer
, fmt
, ap
);
534 todo
= strlen(buffer
);
536 if (todo
> (int)sizeof(buffer
) - 3) {
537 syslog(LOG_CRIT
, "memory overrun in irs_irp_send_command()");
540 strcat(buffer
, "\r\n");
541 todo
= strlen(buffer
);
544 i
= write(pvt
->fdCxn
, buffer
+ pos
, todo
);
547 fprintf(stderr
, "Wrote: \"");
548 fwrite(buffer
+ pos
, sizeof (char), todo
, stderr
);
549 fprintf(stderr
, "\"\n");
566 * void irp_close(struct irs_acc *this)
571 irp_close(struct irs_acc
*this) {
572 struct irp_p
*irp
= (struct irp_p
*)this->private;
575 irs_irp_disconnect(irp
);
576 memput(irp
, sizeof *irp
);
579 memput(this, sizeof *this);