17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004-2006, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1996, 1998-2001, 2003  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "port_before.h"
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <syslog.h>
217c478bd9Sstevel@tonic-gate #include <sys/types.h>
227c478bd9Sstevel@tonic-gate #include <sys/socket.h>
237c478bd9Sstevel@tonic-gate #include <sys/un.h>
247c478bd9Sstevel@tonic-gate #include <netinet/in.h>
257c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <errno.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <irs.h>
387c478bd9Sstevel@tonic-gate #include <irp.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "irs_p.h"
417c478bd9Sstevel@tonic-gate #include "irp_p.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include "port_after.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /* Forward. */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static void		irp_close(struct irs_acc *);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define LINEINCR 128
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #if !defined(SUN_LEN)
527c478bd9Sstevel@tonic-gate #define SUN_LEN(su) \
537c478bd9Sstevel@tonic-gate 	(sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /* Public */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* send errors to syslog if true. */
617c478bd9Sstevel@tonic-gate int irp_log_errors = 1;
627c478bd9Sstevel@tonic-gate 
63*9525b14bSRao Shoaib /*%
647c478bd9Sstevel@tonic-gate  * This module handles the irp module connection to irpd.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  * The client expects a synchronous interface to functions like
677c478bd9Sstevel@tonic-gate  * getpwnam(3), so we can't use the ctl_* i/o library on this end of
687c478bd9Sstevel@tonic-gate  * the wire (it's used in the server).
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate 
71*9525b14bSRao Shoaib /*%
727c478bd9Sstevel@tonic-gate  * irs_acc *irs_irp_acc(const char *options);
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *	Initialize the irp module.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate struct irs_acc *
irs_irp_acc(const char * options)777c478bd9Sstevel@tonic-gate irs_irp_acc(const char *options) {
787c478bd9Sstevel@tonic-gate 	struct irs_acc *acc;
797c478bd9Sstevel@tonic-gate 	struct irp_p *irp;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	UNUSED(options);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
847c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
857c478bd9Sstevel@tonic-gate 		return (NULL);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
887c478bd9Sstevel@tonic-gate 	if (!(irp = memget(sizeof *irp))) {
897c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
907c478bd9Sstevel@tonic-gate 		free(acc);
917c478bd9Sstevel@tonic-gate 		return (NULL);
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 	irp->inlast = 0;
947c478bd9Sstevel@tonic-gate 	irp->incurr = 0;
957c478bd9Sstevel@tonic-gate 	irp->fdCxn = -1;
967c478bd9Sstevel@tonic-gate 	acc->private = irp;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_GR
997c478bd9Sstevel@tonic-gate 	acc->gr_map = irs_irp_gr;
1007c478bd9Sstevel@tonic-gate #else
1017c478bd9Sstevel@tonic-gate 	acc->gr_map = NULL;
1027c478bd9Sstevel@tonic-gate #endif
1037c478bd9Sstevel@tonic-gate #ifdef WANT_IRS_PW
1047c478bd9Sstevel@tonic-gate 	acc->pw_map = irs_irp_pw;
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate 	acc->pw_map = NULL;
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate 	acc->sv_map = irs_irp_sv;
1097c478bd9Sstevel@tonic-gate 	acc->pr_map = irs_irp_pr;
1107c478bd9Sstevel@tonic-gate 	acc->ho_map = irs_irp_ho;
1117c478bd9Sstevel@tonic-gate 	acc->nw_map = irs_irp_nw;
1127c478bd9Sstevel@tonic-gate 	acc->ng_map = irs_irp_ng;
1137c478bd9Sstevel@tonic-gate 	acc->close = irp_close;
1147c478bd9Sstevel@tonic-gate 	return (acc);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
irs_irp_connection_setup(struct irp_p * cxndata,int * warned)1197c478bd9Sstevel@tonic-gate irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
1207c478bd9Sstevel@tonic-gate 	if (irs_irp_is_connected(cxndata)) {
1217c478bd9Sstevel@tonic-gate 		return (0);
1227c478bd9Sstevel@tonic-gate 	} else if (irs_irp_connect(cxndata) != 0) {
1237c478bd9Sstevel@tonic-gate 		if (warned != NULL && !*warned) {
1247c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "irpd connection failed: %m\n");
1257c478bd9Sstevel@tonic-gate 			(*warned)++;
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		return (-1);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	return (0);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
134*9525b14bSRao Shoaib /*%
1357c478bd9Sstevel@tonic-gate  * int irs_irp_connect(void);
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  *	Sets up the connection to the remote irpd server.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * Returns:
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate int
irs_irp_connect(struct irp_p * pvt)1457c478bd9Sstevel@tonic-gate irs_irp_connect(struct irp_p *pvt) {
1467c478bd9Sstevel@tonic-gate 	int flags;
1477c478bd9Sstevel@tonic-gate 	struct sockaddr *addr;
1487c478bd9Sstevel@tonic-gate 	struct sockaddr_in iaddr;
1497c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1507c478bd9Sstevel@tonic-gate 	struct sockaddr_un uaddr;
1517c478bd9Sstevel@tonic-gate #endif
1527c478bd9Sstevel@tonic-gate 	long ipaddr;
1537c478bd9Sstevel@tonic-gate 	const char *irphost;
1547c478bd9Sstevel@tonic-gate 	int code;
1557c478bd9Sstevel@tonic-gate 	char text[256];
1567c478bd9Sstevel@tonic-gate 	int socklen = 0;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (pvt->fdCxn != -1) {
1597c478bd9Sstevel@tonic-gate 		perror("fd != 1");
1607c478bd9Sstevel@tonic-gate 		return (-1);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1647c478bd9Sstevel@tonic-gate 	memset(&uaddr, 0, sizeof uaddr);
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate 	memset(&iaddr, 0, sizeof iaddr);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	irphost = getenv(IRPD_HOST_ENV);
1697c478bd9Sstevel@tonic-gate 	if (irphost == NULL) {
1707c478bd9Sstevel@tonic-gate 		irphost = "127.0.0.1";
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1747c478bd9Sstevel@tonic-gate 	if (irphost[0] == '/') {
1757c478bd9Sstevel@tonic-gate 		addr = (struct sockaddr *)&uaddr;
1767c478bd9Sstevel@tonic-gate 		strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
1777c478bd9Sstevel@tonic-gate 		uaddr.sun_family = AF_UNIX;
1787c478bd9Sstevel@tonic-gate 		socklen = SUN_LEN(&uaddr);
1797c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
1807c478bd9Sstevel@tonic-gate 		uaddr.sun_len = socklen;
1817c478bd9Sstevel@tonic-gate #endif
1827c478bd9Sstevel@tonic-gate 	} else
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate 	{
1857c478bd9Sstevel@tonic-gate 		if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
1867c478bd9Sstevel@tonic-gate 			errno = EADDRNOTAVAIL;
1877c478bd9Sstevel@tonic-gate 			perror("inet_pton");
1887c478bd9Sstevel@tonic-gate 			return (-1);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		addr = (struct sockaddr *)&iaddr;
1927c478bd9Sstevel@tonic-gate 		socklen = sizeof iaddr;
1937c478bd9Sstevel@tonic-gate #ifdef HAVE_SA_LEN
1947c478bd9Sstevel@tonic-gate 		iaddr.sin_len = socklen;
1957c478bd9Sstevel@tonic-gate #endif
1967c478bd9Sstevel@tonic-gate 		iaddr.sin_family = AF_INET;
1977c478bd9Sstevel@tonic-gate 		iaddr.sin_port = htons(IRPD_PORT);
1987c478bd9Sstevel@tonic-gate 		iaddr.sin_addr.s_addr = ipaddr;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
2037c478bd9Sstevel@tonic-gate 	if (pvt->fdCxn < 0) {
2047c478bd9Sstevel@tonic-gate 		perror("socket");
2057c478bd9Sstevel@tonic-gate 		return (-1);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (connect(pvt->fdCxn, addr, socklen) != 0) {
2097c478bd9Sstevel@tonic-gate 		perror("connect");
2107c478bd9Sstevel@tonic-gate 		return (-1);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	flags = fcntl(pvt->fdCxn, F_GETFL, 0);
2147c478bd9Sstevel@tonic-gate 	if (flags < 0) {
2157c478bd9Sstevel@tonic-gate 		close(pvt->fdCxn);
2167c478bd9Sstevel@tonic-gate 		perror("close");
2177c478bd9Sstevel@tonic-gate 		return (-1);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #if 0
2217c478bd9Sstevel@tonic-gate 	flags |= O_NONBLOCK;
2227c478bd9Sstevel@tonic-gate 	if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
2237c478bd9Sstevel@tonic-gate 		close(pvt->fdCxn);
2247c478bd9Sstevel@tonic-gate 		perror("fcntl");
2257c478bd9Sstevel@tonic-gate 		return (-1);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	code = irs_irp_read_response(pvt, text, sizeof text);
2307c478bd9Sstevel@tonic-gate 	if (code != IRPD_WELCOME_CODE) {
2317c478bd9Sstevel@tonic-gate 		if (irp_log_errors) {
2327c478bd9Sstevel@tonic-gate 			syslog(LOG_WARNING, "Connection failed: %s", text);
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		irs_irp_disconnect(pvt);
2357c478bd9Sstevel@tonic-gate 		return (-1);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
241*9525b14bSRao Shoaib /*%
2427c478bd9Sstevel@tonic-gate  * int	irs_irp_is_connected(struct irp_p *pvt);
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * Returns:
2457c478bd9Sstevel@tonic-gate  *
2467c478bd9Sstevel@tonic-gate  *	Non-zero if streams are setup to remote.
2477c478bd9Sstevel@tonic-gate  *
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate int
irs_irp_is_connected(struct irp_p * pvt)2517c478bd9Sstevel@tonic-gate irs_irp_is_connected(struct irp_p *pvt) {
2527c478bd9Sstevel@tonic-gate 	return (pvt->fdCxn >= 0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
255*9525b14bSRao Shoaib /*%
2567c478bd9Sstevel@tonic-gate  * void
2577c478bd9Sstevel@tonic-gate  * irs_irp_disconnect(struct irp_p *pvt);
2587c478bd9Sstevel@tonic-gate  *
2597c478bd9Sstevel@tonic-gate  *	Closes streams to remote.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate void
irs_irp_disconnect(struct irp_p * pvt)2637c478bd9Sstevel@tonic-gate irs_irp_disconnect(struct irp_p *pvt) {
2647c478bd9Sstevel@tonic-gate 	if (pvt->fdCxn != -1) {
2657c478bd9Sstevel@tonic-gate 		close(pvt->fdCxn);
2667c478bd9Sstevel@tonic-gate 		pvt->fdCxn = -1;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate int
irs_irp_read_line(struct irp_p * pvt,char * buffer,int len)2737c478bd9Sstevel@tonic-gate irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
2747c478bd9Sstevel@tonic-gate 	char *realstart = &pvt->inbuffer[0];
2757c478bd9Sstevel@tonic-gate 	char *p, *start, *end;
2767c478bd9Sstevel@tonic-gate 	int spare;
2777c478bd9Sstevel@tonic-gate 	int i;
2787c478bd9Sstevel@tonic-gate 	int buffpos = 0;
2797c478bd9Sstevel@tonic-gate 	int left = len - 1;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	while (left > 0) {
2827c478bd9Sstevel@tonic-gate 		start = p = &pvt->inbuffer[pvt->incurr];
2837c478bd9Sstevel@tonic-gate 		end = &pvt->inbuffer[pvt->inlast];
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		while (p != end && *p != '\n')
2867c478bd9Sstevel@tonic-gate 			p++;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		if (p == end) {
2897c478bd9Sstevel@tonic-gate 			/* Found no newline so shift data down if necessary
2907c478bd9Sstevel@tonic-gate 			 * and append new data to buffer
2917c478bd9Sstevel@tonic-gate 			 */
2927c478bd9Sstevel@tonic-gate 			if (start > realstart) {
2937c478bd9Sstevel@tonic-gate 				memmove(realstart, start, end - start);
2947c478bd9Sstevel@tonic-gate 				pvt->inlast = end - start;
2957c478bd9Sstevel@tonic-gate 				start = realstart;
2967c478bd9Sstevel@tonic-gate 				pvt->incurr = 0;
2977c478bd9Sstevel@tonic-gate 				end = &pvt->inbuffer[pvt->inlast];
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 			spare = sizeof (pvt->inbuffer) - pvt->inlast;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 			p = end;
3037c478bd9Sstevel@tonic-gate 			i = read(pvt->fdCxn, end, spare);
3047c478bd9Sstevel@tonic-gate 			if (i < 0) {
3057c478bd9Sstevel@tonic-gate 				close(pvt->fdCxn);
3067c478bd9Sstevel@tonic-gate 				pvt->fdCxn = -1;
3077c478bd9Sstevel@tonic-gate 				return (buffpos > 0 ? buffpos : -1);
3087c478bd9Sstevel@tonic-gate 			} else if (i == 0) {
3097c478bd9Sstevel@tonic-gate 				return (buffpos);
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			end += i;
3137c478bd9Sstevel@tonic-gate 			pvt->inlast += i;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 			while (p != end && *p != '\n')
3167c478bd9Sstevel@tonic-gate 				p++;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		if (p == end) {
3207c478bd9Sstevel@tonic-gate 			/* full buffer and still no newline */
3217c478bd9Sstevel@tonic-gate 			i = sizeof pvt->inbuffer;
3227c478bd9Sstevel@tonic-gate 		} else {
3237c478bd9Sstevel@tonic-gate 			/* include newline */
3247c478bd9Sstevel@tonic-gate 			i = p - start + 1;
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		if (i > left)
3287c478bd9Sstevel@tonic-gate 			i = left;
3297c478bd9Sstevel@tonic-gate 		memcpy(buffer + buffpos, start, i);
3307c478bd9Sstevel@tonic-gate 		pvt->incurr += i;
3317c478bd9Sstevel@tonic-gate 		buffpos += i;
3327c478bd9Sstevel@tonic-gate 		buffer[buffpos] = '\0';
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 		if (p != end) {
3357c478bd9Sstevel@tonic-gate 			left = 0;
3367c478bd9Sstevel@tonic-gate 		} else {
3377c478bd9Sstevel@tonic-gate 			left -= i;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate #if 0
3427c478bd9Sstevel@tonic-gate 	fprintf(stderr, "read line: %s\n", buffer);
3437c478bd9Sstevel@tonic-gate #endif
3447c478bd9Sstevel@tonic-gate 	return (buffpos);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
347*9525b14bSRao Shoaib /*%
3487c478bd9Sstevel@tonic-gate  * int irp_read_response(struct irp_p *pvt);
3497c478bd9Sstevel@tonic-gate  *
3507c478bd9Sstevel@tonic-gate  * Returns:
3517c478bd9Sstevel@tonic-gate  *
3527c478bd9Sstevel@tonic-gate  *	The number found at the beginning of the line read from
3537c478bd9Sstevel@tonic-gate  *	FP. 0 on failure(0 is not a legal response code). The
3547c478bd9Sstevel@tonic-gate  *	rest of the line is discarded.
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate int
irs_irp_read_response(struct irp_p * pvt,char * text,size_t textlen)3597c478bd9Sstevel@tonic-gate irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
3607c478bd9Sstevel@tonic-gate 	char line[1024];
3617c478bd9Sstevel@tonic-gate 	int code;
3627c478bd9Sstevel@tonic-gate 	char *p;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
3657c478bd9Sstevel@tonic-gate 		return (0);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	p = strchr(line, '\n');
3697c478bd9Sstevel@tonic-gate 	if (p == NULL) {
3707c478bd9Sstevel@tonic-gate 		return (0);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (sscanf(line, "%d", &code) != 1) {
3747c478bd9Sstevel@tonic-gate 		code = 0;
375*9525b14bSRao Shoaib 	} else if (text != NULL && textlen > 0U) {
3767c478bd9Sstevel@tonic-gate 		p = line;
3777c478bd9Sstevel@tonic-gate 		while (isspace((unsigned char)*p)) p++;
3787c478bd9Sstevel@tonic-gate 		while (isdigit((unsigned char)*p)) p++;
3797c478bd9Sstevel@tonic-gate 		while (isspace((unsigned char)*p)) p++;
3807c478bd9Sstevel@tonic-gate 		strncpy(text, p, textlen - 1);
3817c478bd9Sstevel@tonic-gate 		p[textlen - 1] = '\0';
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	return (code);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
387*9525b14bSRao Shoaib /*%
3887c478bd9Sstevel@tonic-gate  * char *irp_read_body(struct irp_p *pvt, size_t *size);
3897c478bd9Sstevel@tonic-gate  *
3907c478bd9Sstevel@tonic-gate  *	Read in the body of a response. Terminated by a line with
3917c478bd9Sstevel@tonic-gate  *	just a dot on it. Lines should be terminated with a CR-LF
3927c478bd9Sstevel@tonic-gate  *	sequence, but we're nt piccky if the CR is missing.
3937c478bd9Sstevel@tonic-gate  *	No leading dot escaping is done as the protcol doesn't
3947c478bd9Sstevel@tonic-gate  *	use leading dots anywhere.
3957c478bd9Sstevel@tonic-gate  *
3967c478bd9Sstevel@tonic-gate  * Returns:
3977c478bd9Sstevel@tonic-gate  *
3987c478bd9Sstevel@tonic-gate  *	Pointer to null-terminated buffer allocated by memget.
3997c478bd9Sstevel@tonic-gate  *	*SIZE is set to the length of the buffer.
4007c478bd9Sstevel@tonic-gate  *
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate char *
irs_irp_read_body(struct irp_p * pvt,size_t * size)4047c478bd9Sstevel@tonic-gate irs_irp_read_body(struct irp_p *pvt, size_t *size) {
4057c478bd9Sstevel@tonic-gate 	char line[1024];
4067c478bd9Sstevel@tonic-gate 	u_int linelen;
4077c478bd9Sstevel@tonic-gate 	size_t len = LINEINCR;
4087c478bd9Sstevel@tonic-gate 	char *buffer = memget(len);
4097c478bd9Sstevel@tonic-gate 	int idx = 0;
4107c478bd9Sstevel@tonic-gate 
411*9525b14bSRao Shoaib 	if (buffer == NULL)
412*9525b14bSRao Shoaib 		return (NULL);
413*9525b14bSRao Shoaib 
4147c478bd9Sstevel@tonic-gate 	for (;;) {
4157c478bd9Sstevel@tonic-gate 		if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
4167c478bd9Sstevel@tonic-gate 		    strchr(line, '\n') == NULL)
4177c478bd9Sstevel@tonic-gate 			goto death;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		linelen = strlen(line);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		if (line[linelen - 1] != '\n')
4227c478bd9Sstevel@tonic-gate 			goto death;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		/* We're not strict about missing \r. Should we be??  */
4257c478bd9Sstevel@tonic-gate 		if (linelen > 2 && line[linelen - 2] == '\r') {
4267c478bd9Sstevel@tonic-gate 			line[linelen - 2] = '\n';
4277c478bd9Sstevel@tonic-gate 			line[linelen - 1] = '\0';
4287c478bd9Sstevel@tonic-gate 			linelen--;
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		if (linelen == 2 && line[0] == '.') {
4327c478bd9Sstevel@tonic-gate 			*size = len;
4337c478bd9Sstevel@tonic-gate 			buffer[idx] = '\0';
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 			return (buffer);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		if (linelen > (len - (idx + 1))) {
4397c478bd9Sstevel@tonic-gate 			char *p = memget(len + LINEINCR);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 			if (p == NULL)
4427c478bd9Sstevel@tonic-gate 				goto death;
4437c478bd9Sstevel@tonic-gate 			memcpy(p, buffer, len);
4447c478bd9Sstevel@tonic-gate 			memput(buffer, len);
4457c478bd9Sstevel@tonic-gate 			buffer = p;
4467c478bd9Sstevel@tonic-gate 			len += LINEINCR;
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		memcpy(buffer + idx, line, linelen);
4507c478bd9Sstevel@tonic-gate 		idx += linelen;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate  death:
4537c478bd9Sstevel@tonic-gate 	memput(buffer, len);
4547c478bd9Sstevel@tonic-gate 	return (NULL);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
457*9525b14bSRao Shoaib /*%
4587c478bd9Sstevel@tonic-gate  * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
4597c478bd9Sstevel@tonic-gate  *			char **body, size_t *bodylen);
4607c478bd9Sstevel@tonic-gate  *
4617c478bd9Sstevel@tonic-gate  *	Gets the response to a command. If the response indicates
4627c478bd9Sstevel@tonic-gate  *	there's a body to follow(code % 10 == 1), then the
4637c478bd9Sstevel@tonic-gate  *	body buffer is allcoated with memget and stored in
4647c478bd9Sstevel@tonic-gate  *	*BODY. The length of the allocated body buffer is stored
4657c478bd9Sstevel@tonic-gate  *	in *BODY. The caller must give the body buffer back to
4667c478bd9Sstevel@tonic-gate  *	memput when done. The results code is stored in *CODE.
4677c478bd9Sstevel@tonic-gate  *
4687c478bd9Sstevel@tonic-gate  * Returns:
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  *	0 if a result was read. -1 on some sort of failure.
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate int
irs_irp_get_full_response(struct irp_p * pvt,int * code,char * text,size_t textlen,char ** body,size_t * bodylen)4757c478bd9Sstevel@tonic-gate irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
4767c478bd9Sstevel@tonic-gate 			  size_t textlen, char **body, size_t *bodylen) {
4777c478bd9Sstevel@tonic-gate 	int result = irs_irp_read_response(pvt, text, textlen);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	*body = NULL;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (result == 0) {
4827c478bd9Sstevel@tonic-gate 		return (-1);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	*code = result;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* Code that matches 2xx is a good result code.
4887c478bd9Sstevel@tonic-gate 	 * Code that matches xx1 means there's a response body coming.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	if ((result / 100) == 2 && (result % 10) == 1) {
4917c478bd9Sstevel@tonic-gate 		*body = irs_irp_read_body(pvt, bodylen);
4927c478bd9Sstevel@tonic-gate 		if (*body == NULL) {
4937c478bd9Sstevel@tonic-gate 			return (-1);
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	return (0);
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
500*9525b14bSRao Shoaib /*%
5017c478bd9Sstevel@tonic-gate  * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  *	Sends command to remote connected via the PVT
504*9525b14bSRao Shoaib  *	structure. FMT and args after it are fprintf-like
5057c478bd9Sstevel@tonic-gate  *	arguments for formatting.
5067c478bd9Sstevel@tonic-gate  *
5077c478bd9Sstevel@tonic-gate  * Returns:
5087c478bd9Sstevel@tonic-gate  *
5097c478bd9Sstevel@tonic-gate  *	0 on success, -1 on failure.
5107c478bd9Sstevel@tonic-gate  */
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate int
irs_irp_send_command(struct irp_p * pvt,const char * fmt,...)5137c478bd9Sstevel@tonic-gate irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
5147c478bd9Sstevel@tonic-gate 	va_list ap;
5157c478bd9Sstevel@tonic-gate 	char buffer[1024];
5167c478bd9Sstevel@tonic-gate 	int pos = 0;
5177c478bd9Sstevel@tonic-gate 	int i, todo;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (pvt->fdCxn < 0) {
5217c478bd9Sstevel@tonic-gate 		return (-1);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
525*9525b14bSRao Shoaib 	(void) vsprintf(buffer, fmt, ap);
526*9525b14bSRao Shoaib 	todo = strlen(buffer);
5277c478bd9Sstevel@tonic-gate 	va_end(ap);
5287c478bd9Sstevel@tonic-gate 	if (todo > (int)sizeof(buffer) - 3) {
5297c478bd9Sstevel@tonic-gate 		syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
5307c478bd9Sstevel@tonic-gate 		exit(1);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	strcat(buffer, "\r\n");
5337c478bd9Sstevel@tonic-gate 	todo = strlen(buffer);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	while (todo > 0) {
5367c478bd9Sstevel@tonic-gate 		i = write(pvt->fdCxn, buffer + pos, todo);
5377c478bd9Sstevel@tonic-gate #if 0
5387c478bd9Sstevel@tonic-gate 		/* XXX brister */
5397c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Wrote: \"");
5407c478bd9Sstevel@tonic-gate 		fwrite(buffer + pos, sizeof (char), todo, stderr);
5417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "\"\n");
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate 		if (i < 0) {
5447c478bd9Sstevel@tonic-gate 			close(pvt->fdCxn);
5457c478bd9Sstevel@tonic-gate 			pvt->fdCxn = -1;
5467c478bd9Sstevel@tonic-gate 			return (-1);
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		todo -= i;
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	return (0);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /* Methods */
5567c478bd9Sstevel@tonic-gate 
557*9525b14bSRao Shoaib /*%
5587c478bd9Sstevel@tonic-gate  * void irp_close(struct irs_acc *this)
5597c478bd9Sstevel@tonic-gate  *
5607c478bd9Sstevel@tonic-gate  */
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate static void
irp_close(struct irs_acc * this)5637c478bd9Sstevel@tonic-gate irp_close(struct irs_acc *this) {
5647c478bd9Sstevel@tonic-gate 	struct irp_p *irp = (struct irp_p *)this->private;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (irp != NULL) {
5677c478bd9Sstevel@tonic-gate 		irs_irp_disconnect(irp);
5687c478bd9Sstevel@tonic-gate 		memput(irp, sizeof *irp);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	memput(this, sizeof *this);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 
576*9525b14bSRao Shoaib 
577*9525b14bSRao Shoaib /*! \file */
578