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"
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
35 #include <isc/memcluster.h>
43 #include "port_after.h"
47 static void irp_close(struct irs_acc
*);
53 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
60 /* send errors to syslog if true. */
61 int irp_log_errors
= 1;
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).
72 * irs_acc *irs_irp_acc(const char *options);
74 * Initialize the irp module.
77 irs_irp_acc(const char *options
) {
83 if (!(acc
= memget(sizeof *acc
))) {
87 memset(acc
, 0x5e, sizeof *acc
);
88 if (!(irp
= memget(sizeof *irp
))) {
99 acc
->gr_map
= irs_irp_gr
;
104 acc
->pw_map
= irs_irp_pw
;
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
;
119 irs_irp_connection_setup(struct irp_p
*cxndata
, int *warned
) {
120 if (irs_irp_is_connected(cxndata
)) {
122 } else if (irs_irp_connect(cxndata
) != 0) {
123 if (warned
!= NULL
&& !*warned
) {
124 syslog(LOG_ERR
, "irpd connection failed: %m\n");
135 * int irs_irp_connect(void);
137 * Sets up the connection to the remote irpd server.
141 * 0 on success, -1 on failure.
145 irs_irp_connect(struct irp_p
*pvt
) {
147 struct sockaddr
*addr
;
148 struct sockaddr_in iaddr
;
149 #ifndef NO_SOCKADDR_UN
150 struct sockaddr_un uaddr
;
158 if (pvt
->fdCxn
!= -1) {
163 #ifndef NO_SOCKADDR_UN
164 memset(&uaddr
, 0, sizeof uaddr
);
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
);
180 uaddr
.sun_len
= socklen
;
185 if (inet_pton(AF_INET
, irphost
, &ipaddr
) != 1) {
186 errno
= EADDRNOTAVAIL
;
191 addr
= (struct sockaddr
*)&iaddr
;
192 socklen
= sizeof iaddr
;
194 iaddr
.sin_len
= socklen
;
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) {
208 if (connect(pvt
->fdCxn
, addr
, socklen
) != 0) {
213 flags
= fcntl(pvt
->fdCxn
, F_GETFL
, 0);
222 if (fcntl(pvt
->fdCxn
, F_SETFL
, flags
) < 0) {
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
);
242 * int irs_irp_is_connected(struct irp_p *pvt);
246 * Non-zero if streams are setup to remote.
251 irs_irp_is_connected(struct irp_p
*pvt
) {
252 return (pvt
->fdCxn
>= 0);
257 * irs_irp_disconnect(struct irp_p *pvt);
259 * Closes streams to remote.
263 irs_irp_disconnect(struct irp_p
*pvt
) {
264 if (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
;
282 start
= p
= &pvt
->inbuffer
[pvt
->incurr
];
283 end
= &pvt
->inbuffer
[pvt
->inlast
];
285 while (p
!= end
&& *p
!= '\n')
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
;
297 end
= &pvt
->inbuffer
[pvt
->inlast
];
300 spare
= sizeof (pvt
->inbuffer
) - pvt
->inlast
;
303 i
= read(pvt
->fdCxn
, end
, spare
);
307 return (buffpos
> 0 ? buffpos
: -1);
315 while (p
!= end
&& *p
!= '\n')
320 /* full buffer and still no newline */
321 i
= sizeof pvt
->inbuffer
;
323 /* include newline */
329 memcpy(buffer
+ buffpos
, start
, i
);
332 buffer
[buffpos
] = '\0';
342 fprintf(stderr
, "read line: %s\n", buffer
);
348 * int irp_read_response(struct irp_p *pvt);
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
) {
364 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0) {
368 p
= strchr(line
, '\n');
373 if (sscanf(line
, "%d", &code
) != 1) {
375 } else if (text
!= NULL
&& textlen
> 0U) {
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';
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.
398 * Pointer to null-terminated buffer allocated by memget.
399 * *SIZE is set to the length of the buffer.
404 irs_irp_read_body(struct irp_p
*pvt
, size_t *size
) {
407 size_t len
= LINEINCR
;
408 char *buffer
= memget(len
);
415 if (irs_irp_read_line(pvt
, line
, sizeof line
) <= 0 ||
416 strchr(line
, '\n') == NULL
)
419 linelen
= strlen(line
);
421 if (line
[linelen
- 1] != '\n')
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';
431 if (linelen
== 2 && line
[0] == '.') {
438 if (linelen
> (len
- (idx
+ 1))) {
439 char *p
= memget(len
+ LINEINCR
);
443 memcpy(p
, buffer
, len
);
449 memcpy(buffer
+ idx
, line
, linelen
);
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.
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
);
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
);
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.
509 * 0 on success, -1 on failure.
513 irs_irp_send_command(struct irp_p
*pvt
, const char *fmt
, ...) {
520 if (pvt
->fdCxn
< 0) {
525 (void) vsprintf(buffer
, fmt
, ap
);
526 todo
= strlen(buffer
);
528 if (todo
> (int)sizeof(buffer
) - 3) {
529 syslog(LOG_CRIT
, "memory overrun in irs_irp_send_command()");
532 strcat(buffer
, "\r\n");
533 todo
= strlen(buffer
);
536 i
= write(pvt
->fdCxn
, buffer
+ pos
, todo
);
539 fprintf(stderr
, "Wrote: \"");
540 fwrite(buffer
+ pos
, sizeof (char), todo
, stderr
);
541 fprintf(stderr
, "\"\n");
558 * void irp_close(struct irs_acc *this)
563 irp_close(struct irs_acc
*this) {
564 struct irp_p
*irp
= (struct irp_p
*)this->private;
567 irs_irp_disconnect(irp
);
568 memput(irp
, sizeof *irp
);
571 memput(this, sizeof *this);