xref: /netbsd-src/external/bsd/libbind/dist/irs/irp.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1*5bbd2a12Schristos /*	$NetBSD: irp.c,v 1.1.1.2 2012/09/09 16:07:50 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (C) 2004-2006, 2008  Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (C) 1996, 1998-2001, 2003  Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12b5677b36Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13b5677b36Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14b5677b36Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15b5677b36Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16b5677b36Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17b5677b36Schristos  * PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if !defined(LINT) && !defined(CODECENTER)
21b5677b36Schristos static const char rcsid[] = "Id: irp.c,v 1.12 2008/11/14 02:36:51 marka Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos /* Imports */
25b5677b36Schristos 
26b5677b36Schristos #include "port_before.h"
27b5677b36Schristos 
28b5677b36Schristos #include <syslog.h>
29b5677b36Schristos #include <sys/types.h>
30b5677b36Schristos #include <sys/socket.h>
31b5677b36Schristos #include <sys/un.h>
32b5677b36Schristos #include <netinet/in.h>
33b5677b36Schristos #include <arpa/inet.h>
34b5677b36Schristos #include <stdlib.h>
35b5677b36Schristos #include <errno.h>
36b5677b36Schristos #include <string.h>
37b5677b36Schristos #include <stdarg.h>
38b5677b36Schristos #include <fcntl.h>
39b5677b36Schristos #include <syslog.h>
40b5677b36Schristos #include <ctype.h>
41b5677b36Schristos #include <unistd.h>
42b5677b36Schristos 
43b5677b36Schristos #include <isc/memcluster.h>
44b5677b36Schristos 
45b5677b36Schristos #include <irs.h>
46b5677b36Schristos #include <irp.h>
47b5677b36Schristos 
48b5677b36Schristos #include "irs_p.h"
49b5677b36Schristos #include "irp_p.h"
50b5677b36Schristos 
51b5677b36Schristos #include "port_after.h"
52b5677b36Schristos 
53b5677b36Schristos /* Forward. */
54b5677b36Schristos 
55b5677b36Schristos static void		irp_close(struct irs_acc *);
56b5677b36Schristos 
57b5677b36Schristos #define LINEINCR 128
58b5677b36Schristos 
59b5677b36Schristos #if !defined(SUN_LEN)
60b5677b36Schristos #define SUN_LEN(su) \
61b5677b36Schristos 	(sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
62b5677b36Schristos #endif
63b5677b36Schristos 
64b5677b36Schristos 
65b5677b36Schristos /* Public */
66b5677b36Schristos 
67b5677b36Schristos 
68b5677b36Schristos /* send errors to syslog if true. */
69b5677b36Schristos int irp_log_errors = 1;
70b5677b36Schristos 
71b5677b36Schristos /*%
72b5677b36Schristos  * This module handles the irp module connection to irpd.
73b5677b36Schristos  *
74b5677b36Schristos  * The client expects a synchronous interface to functions like
75b5677b36Schristos  * getpwnam(3), so we can't use the ctl_* i/o library on this end of
76b5677b36Schristos  * the wire (it's used in the server).
77b5677b36Schristos  */
78b5677b36Schristos 
79b5677b36Schristos /*%
80b5677b36Schristos  * irs_acc *irs_irp_acc(const char *options);
81b5677b36Schristos  *
82b5677b36Schristos  *	Initialize the irp module.
83b5677b36Schristos  */
84b5677b36Schristos struct irs_acc *
irs_irp_acc(const char * options)85b5677b36Schristos irs_irp_acc(const char *options) {
86b5677b36Schristos 	struct irs_acc *acc;
87b5677b36Schristos 	struct irp_p *irp;
88b5677b36Schristos 
89b5677b36Schristos 	UNUSED(options);
90b5677b36Schristos 
91b5677b36Schristos 	if (!(acc = memget(sizeof *acc))) {
92b5677b36Schristos 		errno = ENOMEM;
93b5677b36Schristos 		return (NULL);
94b5677b36Schristos 	}
95b5677b36Schristos 	memset(acc, 0x5e, sizeof *acc);
96b5677b36Schristos 	if (!(irp = memget(sizeof *irp))) {
97b5677b36Schristos 		errno = ENOMEM;
98b5677b36Schristos 		free(acc);
99b5677b36Schristos 		return (NULL);
100b5677b36Schristos 	}
101b5677b36Schristos 	irp->inlast = 0;
102b5677b36Schristos 	irp->incurr = 0;
103b5677b36Schristos 	irp->fdCxn = -1;
104b5677b36Schristos 	acc->private = irp;
105b5677b36Schristos 
106b5677b36Schristos #ifdef WANT_IRS_GR
107b5677b36Schristos 	acc->gr_map = irs_irp_gr;
108b5677b36Schristos #else
109b5677b36Schristos 	acc->gr_map = NULL;
110b5677b36Schristos #endif
111b5677b36Schristos #ifdef WANT_IRS_PW
112b5677b36Schristos 	acc->pw_map = irs_irp_pw;
113b5677b36Schristos #else
114b5677b36Schristos 	acc->pw_map = NULL;
115b5677b36Schristos #endif
116b5677b36Schristos 	acc->sv_map = irs_irp_sv;
117b5677b36Schristos 	acc->pr_map = irs_irp_pr;
118b5677b36Schristos 	acc->ho_map = irs_irp_ho;
119b5677b36Schristos 	acc->nw_map = irs_irp_nw;
120b5677b36Schristos 	acc->ng_map = irs_irp_ng;
121b5677b36Schristos 	acc->close = irp_close;
122b5677b36Schristos 	return (acc);
123b5677b36Schristos }
124b5677b36Schristos 
125b5677b36Schristos 
126b5677b36Schristos int
irs_irp_connection_setup(struct irp_p * cxndata,int * warned)127b5677b36Schristos irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
128b5677b36Schristos 	if (irs_irp_is_connected(cxndata)) {
129b5677b36Schristos 		return (0);
130b5677b36Schristos 	} else if (irs_irp_connect(cxndata) != 0) {
131b5677b36Schristos 		if (warned != NULL && !*warned) {
132b5677b36Schristos 			syslog(LOG_ERR, "irpd connection failed: %m\n");
133b5677b36Schristos 			(*warned)++;
134b5677b36Schristos 		}
135b5677b36Schristos 
136b5677b36Schristos 		return (-1);
137b5677b36Schristos 	}
138b5677b36Schristos 
139b5677b36Schristos 	return (0);
140b5677b36Schristos }
141b5677b36Schristos 
142b5677b36Schristos /*%
143b5677b36Schristos  * int irs_irp_connect(void);
144b5677b36Schristos  *
145b5677b36Schristos  *	Sets up the connection to the remote irpd server.
146b5677b36Schristos  *
147b5677b36Schristos  * Returns:
148b5677b36Schristos  *
149b5677b36Schristos  *	0 on success, -1 on failure.
150b5677b36Schristos  *
151b5677b36Schristos  */
152b5677b36Schristos int
irs_irp_connect(struct irp_p * pvt)153b5677b36Schristos irs_irp_connect(struct irp_p *pvt) {
154b5677b36Schristos 	int flags;
155b5677b36Schristos 	struct sockaddr *addr;
156b5677b36Schristos 	struct sockaddr_in iaddr;
157b5677b36Schristos #ifndef NO_SOCKADDR_UN
158b5677b36Schristos 	struct sockaddr_un uaddr;
159b5677b36Schristos #endif
160b5677b36Schristos 	long ipaddr;
161b5677b36Schristos 	const char *irphost;
162b5677b36Schristos 	int code;
163b5677b36Schristos 	char text[256];
164b5677b36Schristos 	int socklen = 0;
165b5677b36Schristos 
166b5677b36Schristos 	if (pvt->fdCxn != -1) {
167b5677b36Schristos 		perror("fd != 1");
168b5677b36Schristos 		return (-1);
169b5677b36Schristos 	}
170b5677b36Schristos 
171b5677b36Schristos #ifndef NO_SOCKADDR_UN
172b5677b36Schristos 	memset(&uaddr, 0, sizeof uaddr);
173b5677b36Schristos #endif
174b5677b36Schristos 	memset(&iaddr, 0, sizeof iaddr);
175b5677b36Schristos 
176b5677b36Schristos 	irphost = getenv(IRPD_HOST_ENV);
177b5677b36Schristos 	if (irphost == NULL) {
178b5677b36Schristos 		irphost = "127.0.0.1";
179b5677b36Schristos 	}
180b5677b36Schristos 
181b5677b36Schristos #ifndef NO_SOCKADDR_UN
182b5677b36Schristos 	if (irphost[0] == '/') {
183b5677b36Schristos 		addr = (struct sockaddr *)&uaddr;
184b5677b36Schristos 		strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
185b5677b36Schristos 		uaddr.sun_family = AF_UNIX;
186b5677b36Schristos 		socklen = SUN_LEN(&uaddr);
187b5677b36Schristos #ifdef HAVE_SA_LEN
188b5677b36Schristos 		uaddr.sun_len = socklen;
189b5677b36Schristos #endif
190b5677b36Schristos 	} else
191b5677b36Schristos #endif
192b5677b36Schristos 	{
193b5677b36Schristos 		if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
194b5677b36Schristos 			errno = EADDRNOTAVAIL;
195b5677b36Schristos 			perror("inet_pton");
196b5677b36Schristos 			return (-1);
197b5677b36Schristos 		}
198b5677b36Schristos 
199b5677b36Schristos 		addr = (struct sockaddr *)&iaddr;
200b5677b36Schristos 		socklen = sizeof iaddr;
201b5677b36Schristos #ifdef HAVE_SA_LEN
202b5677b36Schristos 		iaddr.sin_len = socklen;
203b5677b36Schristos #endif
204b5677b36Schristos 		iaddr.sin_family = AF_INET;
205b5677b36Schristos 		iaddr.sin_port = htons(IRPD_PORT);
206b5677b36Schristos 		iaddr.sin_addr.s_addr = ipaddr;
207b5677b36Schristos 	}
208b5677b36Schristos 
209b5677b36Schristos 
210b5677b36Schristos 	pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
211b5677b36Schristos 	if (pvt->fdCxn < 0) {
212b5677b36Schristos 		perror("socket");
213b5677b36Schristos 		return (-1);
214b5677b36Schristos 	}
215b5677b36Schristos 
216b5677b36Schristos 	if (connect(pvt->fdCxn, addr, socklen) != 0) {
217b5677b36Schristos 		perror("connect");
218b5677b36Schristos 		return (-1);
219b5677b36Schristos 	}
220b5677b36Schristos 
221b5677b36Schristos 	flags = fcntl(pvt->fdCxn, F_GETFL, 0);
222b5677b36Schristos 	if (flags < 0) {
223b5677b36Schristos 		close(pvt->fdCxn);
224b5677b36Schristos 		perror("close");
225b5677b36Schristos 		return (-1);
226b5677b36Schristos 	}
227b5677b36Schristos 
228b5677b36Schristos #if 0
229b5677b36Schristos 	flags |= O_NONBLOCK;
230b5677b36Schristos 	if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
231b5677b36Schristos 		close(pvt->fdCxn);
232b5677b36Schristos 		perror("fcntl");
233b5677b36Schristos 		return (-1);
234b5677b36Schristos 	}
235b5677b36Schristos #endif
236b5677b36Schristos 
237b5677b36Schristos 	code = irs_irp_read_response(pvt, text, sizeof text);
238b5677b36Schristos 	if (code != IRPD_WELCOME_CODE) {
239b5677b36Schristos 		if (irp_log_errors) {
240b5677b36Schristos 			syslog(LOG_WARNING, "Connection failed: %s", text);
241b5677b36Schristos 		}
242b5677b36Schristos 		irs_irp_disconnect(pvt);
243b5677b36Schristos 		return (-1);
244b5677b36Schristos 	}
245b5677b36Schristos 
246b5677b36Schristos 	return (0);
247b5677b36Schristos }
248b5677b36Schristos 
249b5677b36Schristos /*%
250b5677b36Schristos  * int	irs_irp_is_connected(struct irp_p *pvt);
251b5677b36Schristos  *
252b5677b36Schristos  * Returns:
253b5677b36Schristos  *
254b5677b36Schristos  *	Non-zero if streams are setup to remote.
255b5677b36Schristos  *
256b5677b36Schristos  */
257b5677b36Schristos 
258b5677b36Schristos int
irs_irp_is_connected(struct irp_p * pvt)259b5677b36Schristos irs_irp_is_connected(struct irp_p *pvt) {
260b5677b36Schristos 	return (pvt->fdCxn >= 0);
261b5677b36Schristos }
262b5677b36Schristos 
263b5677b36Schristos /*%
264b5677b36Schristos  * void
265b5677b36Schristos  * irs_irp_disconnect(struct irp_p *pvt);
266b5677b36Schristos  *
267b5677b36Schristos  *	Closes streams to remote.
268b5677b36Schristos  */
269b5677b36Schristos 
270b5677b36Schristos void
irs_irp_disconnect(struct irp_p * pvt)271b5677b36Schristos irs_irp_disconnect(struct irp_p *pvt) {
272b5677b36Schristos 	if (pvt->fdCxn != -1) {
273b5677b36Schristos 		close(pvt->fdCxn);
274b5677b36Schristos 		pvt->fdCxn = -1;
275b5677b36Schristos 	}
276b5677b36Schristos }
277b5677b36Schristos 
278b5677b36Schristos 
279b5677b36Schristos 
280b5677b36Schristos int
irs_irp_read_line(struct irp_p * pvt,char * buffer,int len)281b5677b36Schristos irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
282b5677b36Schristos 	char *realstart = &pvt->inbuffer[0];
283b5677b36Schristos 	char *p, *start, *end;
284b5677b36Schristos 	int spare;
285b5677b36Schristos 	int i;
286b5677b36Schristos 	int buffpos = 0;
287b5677b36Schristos 	int left = len - 1;
288b5677b36Schristos 
289b5677b36Schristos 	while (left > 0) {
290b5677b36Schristos 		start = p = &pvt->inbuffer[pvt->incurr];
291b5677b36Schristos 		end = &pvt->inbuffer[pvt->inlast];
292b5677b36Schristos 
293b5677b36Schristos 		while (p != end && *p != '\n')
294b5677b36Schristos 			p++;
295b5677b36Schristos 
296b5677b36Schristos 		if (p == end) {
297b5677b36Schristos 			/* Found no newline so shift data down if necessary
298b5677b36Schristos 			 * and append new data to buffer
299b5677b36Schristos 			 */
300b5677b36Schristos 			if (start > realstart) {
301b5677b36Schristos 				memmove(realstart, start, end - start);
302b5677b36Schristos 				pvt->inlast = end - start;
303b5677b36Schristos 				start = realstart;
304b5677b36Schristos 				pvt->incurr = 0;
305b5677b36Schristos 				end = &pvt->inbuffer[pvt->inlast];
306b5677b36Schristos 			}
307b5677b36Schristos 
308b5677b36Schristos 			spare = sizeof (pvt->inbuffer) - pvt->inlast;
309b5677b36Schristos 
310b5677b36Schristos 			p = end;
311b5677b36Schristos 			i = read(pvt->fdCxn, end, spare);
312b5677b36Schristos 			if (i < 0) {
313b5677b36Schristos 				close(pvt->fdCxn);
314b5677b36Schristos 				pvt->fdCxn = -1;
315b5677b36Schristos 				return (buffpos > 0 ? buffpos : -1);
316b5677b36Schristos 			} else if (i == 0) {
317b5677b36Schristos 				return (buffpos);
318b5677b36Schristos 			}
319b5677b36Schristos 
320b5677b36Schristos 			end += i;
321b5677b36Schristos 			pvt->inlast += i;
322b5677b36Schristos 
323b5677b36Schristos 			while (p != end && *p != '\n')
324b5677b36Schristos 				p++;
325b5677b36Schristos 		}
326b5677b36Schristos 
327b5677b36Schristos 		if (p == end) {
328b5677b36Schristos 			/* full buffer and still no newline */
329b5677b36Schristos 			i = sizeof pvt->inbuffer;
330b5677b36Schristos 		} else {
331b5677b36Schristos 			/* include newline */
332b5677b36Schristos 			i = p - start + 1;
333b5677b36Schristos 		}
334b5677b36Schristos 
335b5677b36Schristos 		if (i > left)
336b5677b36Schristos 			i = left;
337b5677b36Schristos 		memcpy(buffer + buffpos, start, i);
338b5677b36Schristos 		pvt->incurr += i;
339b5677b36Schristos 		buffpos += i;
340b5677b36Schristos 		buffer[buffpos] = '\0';
341b5677b36Schristos 
342b5677b36Schristos 		if (p != end) {
343b5677b36Schristos 			left = 0;
344b5677b36Schristos 		} else {
345b5677b36Schristos 			left -= i;
346b5677b36Schristos 		}
347b5677b36Schristos 	}
348b5677b36Schristos 
349b5677b36Schristos #if 0
350b5677b36Schristos 	fprintf(stderr, "read line: %s\n", buffer);
351b5677b36Schristos #endif
352b5677b36Schristos 	return (buffpos);
353b5677b36Schristos }
354b5677b36Schristos 
355b5677b36Schristos /*%
356b5677b36Schristos  * int irp_read_response(struct irp_p *pvt);
357b5677b36Schristos  *
358b5677b36Schristos  * Returns:
359b5677b36Schristos  *
360b5677b36Schristos  *	The number found at the beginning of the line read from
361b5677b36Schristos  *	FP. 0 on failure(0 is not a legal response code). The
362b5677b36Schristos  *	rest of the line is discarded.
363b5677b36Schristos  *
364b5677b36Schristos  */
365b5677b36Schristos 
366b5677b36Schristos int
irs_irp_read_response(struct irp_p * pvt,char * text,size_t textlen)367b5677b36Schristos irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
368b5677b36Schristos 	char line[1024];
369b5677b36Schristos 	int code;
370b5677b36Schristos 	char *p;
371b5677b36Schristos 
372b5677b36Schristos 	if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
373b5677b36Schristos 		return (0);
374b5677b36Schristos 	}
375b5677b36Schristos 
376b5677b36Schristos 	p = strchr(line, '\n');
377b5677b36Schristos 	if (p == NULL) {
378b5677b36Schristos 		return (0);
379b5677b36Schristos 	}
380b5677b36Schristos 
381b5677b36Schristos 	if (sscanf(line, "%d", &code) != 1) {
382b5677b36Schristos 		code = 0;
383b5677b36Schristos 	} else if (text != NULL && textlen > 0U) {
384b5677b36Schristos 		p = line;
385b5677b36Schristos 		while (isspace((unsigned char)*p)) p++;
386b5677b36Schristos 		while (isdigit((unsigned char)*p)) p++;
387b5677b36Schristos 		while (isspace((unsigned char)*p)) p++;
388b5677b36Schristos 		strncpy(text, p, textlen - 1);
389b5677b36Schristos 		p[textlen - 1] = '\0';
390b5677b36Schristos 	}
391b5677b36Schristos 
392b5677b36Schristos 	return (code);
393b5677b36Schristos }
394b5677b36Schristos 
395b5677b36Schristos /*%
396b5677b36Schristos  * char *irp_read_body(struct irp_p *pvt, size_t *size);
397b5677b36Schristos  *
398b5677b36Schristos  *	Read in the body of a response. Terminated by a line with
399b5677b36Schristos  *	just a dot on it. Lines should be terminated with a CR-LF
400b5677b36Schristos  *	sequence, but we're nt piccky if the CR is missing.
401b5677b36Schristos  *	No leading dot escaping is done as the protcol doesn't
402b5677b36Schristos  *	use leading dots anywhere.
403b5677b36Schristos  *
404b5677b36Schristos  * Returns:
405b5677b36Schristos  *
406b5677b36Schristos  *	Pointer to null-terminated buffer allocated by memget.
407b5677b36Schristos  *	*SIZE is set to the length of the buffer.
408b5677b36Schristos  *
409b5677b36Schristos  */
410b5677b36Schristos 
411b5677b36Schristos char *
irs_irp_read_body(struct irp_p * pvt,size_t * size)412b5677b36Schristos irs_irp_read_body(struct irp_p *pvt, size_t *size) {
413b5677b36Schristos 	char line[1024];
414b5677b36Schristos 	u_int linelen;
415b5677b36Schristos 	size_t len = LINEINCR;
416b5677b36Schristos 	char *buffer = memget(len);
417b5677b36Schristos 	int idx = 0;
418b5677b36Schristos 
419b5677b36Schristos 	if (buffer == NULL)
420b5677b36Schristos 		return (NULL);
421b5677b36Schristos 
422b5677b36Schristos 	for (;;) {
423b5677b36Schristos 		if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
424b5677b36Schristos 		    strchr(line, '\n') == NULL)
425b5677b36Schristos 			goto death;
426b5677b36Schristos 
427b5677b36Schristos 		linelen = strlen(line);
428b5677b36Schristos 
429b5677b36Schristos 		if (line[linelen - 1] != '\n')
430b5677b36Schristos 			goto death;
431b5677b36Schristos 
432b5677b36Schristos 		/* We're not strict about missing \r. Should we be??  */
433b5677b36Schristos 		if (linelen > 2 && line[linelen - 2] == '\r') {
434b5677b36Schristos 			line[linelen - 2] = '\n';
435b5677b36Schristos 			line[linelen - 1] = '\0';
436b5677b36Schristos 			linelen--;
437b5677b36Schristos 		}
438b5677b36Schristos 
439b5677b36Schristos 		if (linelen == 2 && line[0] == '.') {
440b5677b36Schristos 			*size = len;
441b5677b36Schristos 			buffer[idx] = '\0';
442b5677b36Schristos 
443b5677b36Schristos 			return (buffer);
444b5677b36Schristos 		}
445b5677b36Schristos 
446b5677b36Schristos 		if (linelen > (len - (idx + 1))) {
447b5677b36Schristos 			char *p = memget(len + LINEINCR);
448b5677b36Schristos 
449b5677b36Schristos 			if (p == NULL)
450b5677b36Schristos 				goto death;
451b5677b36Schristos 			memcpy(p, buffer, len);
452b5677b36Schristos 			memput(buffer, len);
453b5677b36Schristos 			buffer = p;
454b5677b36Schristos 			len += LINEINCR;
455b5677b36Schristos 		}
456b5677b36Schristos 
457b5677b36Schristos 		memcpy(buffer + idx, line, linelen);
458b5677b36Schristos 		idx += linelen;
459b5677b36Schristos 	}
460b5677b36Schristos  death:
461b5677b36Schristos 	memput(buffer, len);
462b5677b36Schristos 	return (NULL);
463b5677b36Schristos }
464b5677b36Schristos 
465b5677b36Schristos /*%
466b5677b36Schristos  * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
467b5677b36Schristos  *			char **body, size_t *bodylen);
468b5677b36Schristos  *
469b5677b36Schristos  *	Gets the response to a command. If the response indicates
470b5677b36Schristos  *	there's a body to follow(code % 10 == 1), then the
471b5677b36Schristos  *	body buffer is allcoated with memget and stored in
472b5677b36Schristos  *	*BODY. The length of the allocated body buffer is stored
473b5677b36Schristos  *	in *BODY. The caller must give the body buffer back to
474b5677b36Schristos  *	memput when done. The results code is stored in *CODE.
475b5677b36Schristos  *
476b5677b36Schristos  * Returns:
477b5677b36Schristos  *
478b5677b36Schristos  *	0 if a result was read. -1 on some sort of failure.
479b5677b36Schristos  *
480b5677b36Schristos  */
481b5677b36Schristos 
482b5677b36Schristos int
irs_irp_get_full_response(struct irp_p * pvt,int * code,char * text,size_t textlen,char ** body,size_t * bodylen)483b5677b36Schristos irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
484b5677b36Schristos 			  size_t textlen, char **body, size_t *bodylen) {
485b5677b36Schristos 	int result = irs_irp_read_response(pvt, text, textlen);
486b5677b36Schristos 
487b5677b36Schristos 	*body = NULL;
488b5677b36Schristos 
489b5677b36Schristos 	if (result == 0) {
490b5677b36Schristos 		return (-1);
491b5677b36Schristos 	}
492b5677b36Schristos 
493b5677b36Schristos 	*code = result;
494b5677b36Schristos 
495b5677b36Schristos 	/* Code that matches 2xx is a good result code.
496b5677b36Schristos 	 * Code that matches xx1 means there's a response body coming.
497b5677b36Schristos 	 */
498b5677b36Schristos 	if ((result / 100) == 2 && (result % 10) == 1) {
499b5677b36Schristos 		*body = irs_irp_read_body(pvt, bodylen);
500b5677b36Schristos 		if (*body == NULL) {
501b5677b36Schristos 			return (-1);
502b5677b36Schristos 		}
503b5677b36Schristos 	}
504b5677b36Schristos 
505b5677b36Schristos 	return (0);
506b5677b36Schristos }
507b5677b36Schristos 
508b5677b36Schristos /*%
509b5677b36Schristos  * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
510b5677b36Schristos  *
511b5677b36Schristos  *	Sends command to remote connected via the PVT
512b5677b36Schristos  *	structure. FMT and args after it are fprintf-like
513b5677b36Schristos  *	arguments for formatting.
514b5677b36Schristos  *
515b5677b36Schristos  * Returns:
516b5677b36Schristos  *
517b5677b36Schristos  *	0 on success, -1 on failure.
518b5677b36Schristos  */
519b5677b36Schristos 
520b5677b36Schristos int
irs_irp_send_command(struct irp_p * pvt,const char * fmt,...)521b5677b36Schristos irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
522b5677b36Schristos 	va_list ap;
523b5677b36Schristos 	char buffer[1024];
524b5677b36Schristos 	int pos = 0;
525b5677b36Schristos 	int i, todo;
526b5677b36Schristos 
527b5677b36Schristos 
528b5677b36Schristos 	if (pvt->fdCxn < 0) {
529b5677b36Schristos 		return (-1);
530b5677b36Schristos 	}
531b5677b36Schristos 
532b5677b36Schristos 	va_start(ap, fmt);
533b5677b36Schristos 	(void) vsprintf(buffer, fmt, ap);
534b5677b36Schristos 	todo = strlen(buffer);
535b5677b36Schristos 	va_end(ap);
536b5677b36Schristos 	if (todo > (int)sizeof(buffer) - 3) {
537b5677b36Schristos 		syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
538b5677b36Schristos 		exit(1);
539b5677b36Schristos 	}
540b5677b36Schristos 	strcat(buffer, "\r\n");
541b5677b36Schristos 	todo = strlen(buffer);
542b5677b36Schristos 
543b5677b36Schristos 	while (todo > 0) {
544b5677b36Schristos 		i = write(pvt->fdCxn, buffer + pos, todo);
545b5677b36Schristos #if 0
546b5677b36Schristos 		/* XXX brister */
547b5677b36Schristos 		fprintf(stderr, "Wrote: \"");
548b5677b36Schristos 		fwrite(buffer + pos, sizeof (char), todo, stderr);
549b5677b36Schristos 		fprintf(stderr, "\"\n");
550b5677b36Schristos #endif
551b5677b36Schristos 		if (i < 0) {
552b5677b36Schristos 			close(pvt->fdCxn);
553b5677b36Schristos 			pvt->fdCxn = -1;
554b5677b36Schristos 			return (-1);
555b5677b36Schristos 		}
556b5677b36Schristos 		todo -= i;
557b5677b36Schristos 	}
558b5677b36Schristos 
559b5677b36Schristos 	return (0);
560b5677b36Schristos }
561b5677b36Schristos 
562b5677b36Schristos 
563b5677b36Schristos /* Methods */
564b5677b36Schristos 
565b5677b36Schristos /*%
566b5677b36Schristos  * void irp_close(struct irs_acc *this)
567b5677b36Schristos  *
568b5677b36Schristos  */
569b5677b36Schristos 
570b5677b36Schristos static void
irp_close(struct irs_acc * this)571b5677b36Schristos irp_close(struct irs_acc *this) {
572b5677b36Schristos 	struct irp_p *irp = (struct irp_p *)this->private;
573b5677b36Schristos 
574b5677b36Schristos 	if (irp != NULL) {
575b5677b36Schristos 		irs_irp_disconnect(irp);
576b5677b36Schristos 		memput(irp, sizeof *irp);
577b5677b36Schristos 	}
578b5677b36Schristos 
579b5677b36Schristos 	memput(this, sizeof *this);
580b5677b36Schristos }
581b5677b36Schristos 
582b5677b36Schristos 
583b5677b36Schristos 
584b5677b36Schristos 
585b5677b36Schristos /*! \file */
586