xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/irp.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (C) 2004-2006, 2008  Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM  * Copyright (C) 1996, 1998-2001, 2003  Internet Software Consortium.
40Sstevel@tonic-gate  *
5*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM  * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if !defined(LINT) && !defined(CODECENTER)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irp.c,v 1.12 2008/11/14 02:36:51 marka Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* Imports */
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include "port_before.h"
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <syslog.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/socket.h>
290Sstevel@tonic-gate #include <sys/un.h>
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <arpa/inet.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <syslog.h>
380Sstevel@tonic-gate #include <ctype.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <isc/memcluster.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <irs.h>
440Sstevel@tonic-gate #include <irp.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "irs_p.h"
470Sstevel@tonic-gate #include "irp_p.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include "port_after.h"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /* Forward. */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static void		irp_close(struct irs_acc *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define LINEINCR 128
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #if !defined(SUN_LEN)
580Sstevel@tonic-gate #define SUN_LEN(su) \
590Sstevel@tonic-gate 	(sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /* Public */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* send errors to syslog if true. */
670Sstevel@tonic-gate int irp_log_errors = 1;
680Sstevel@tonic-gate 
69*11038SRao.Shoaib@Sun.COM /*%
700Sstevel@tonic-gate  * This module handles the irp module connection to irpd.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  * The client expects a synchronous interface to functions like
730Sstevel@tonic-gate  * getpwnam(3), so we can't use the ctl_* i/o library on this end of
740Sstevel@tonic-gate  * the wire (it's used in the server).
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
77*11038SRao.Shoaib@Sun.COM /*%
780Sstevel@tonic-gate  * irs_acc *irs_irp_acc(const char *options);
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  *	Initialize the irp module.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate struct irs_acc *
irs_irp_acc(const char * options)830Sstevel@tonic-gate irs_irp_acc(const char *options) {
840Sstevel@tonic-gate 	struct irs_acc *acc;
850Sstevel@tonic-gate 	struct irp_p *irp;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	UNUSED(options);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (!(acc = memget(sizeof *acc))) {
900Sstevel@tonic-gate 		errno = ENOMEM;
910Sstevel@tonic-gate 		return (NULL);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 	memset(acc, 0x5e, sizeof *acc);
940Sstevel@tonic-gate 	if (!(irp = memget(sizeof *irp))) {
950Sstevel@tonic-gate 		errno = ENOMEM;
960Sstevel@tonic-gate 		free(acc);
970Sstevel@tonic-gate 		return (NULL);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	irp->inlast = 0;
1000Sstevel@tonic-gate 	irp->incurr = 0;
1010Sstevel@tonic-gate 	irp->fdCxn = -1;
1020Sstevel@tonic-gate 	acc->private = irp;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #ifdef WANT_IRS_GR
1050Sstevel@tonic-gate 	acc->gr_map = irs_irp_gr;
1060Sstevel@tonic-gate #else
1070Sstevel@tonic-gate 	acc->gr_map = NULL;
1080Sstevel@tonic-gate #endif
1090Sstevel@tonic-gate #ifdef WANT_IRS_PW
1100Sstevel@tonic-gate 	acc->pw_map = irs_irp_pw;
1110Sstevel@tonic-gate #else
1120Sstevel@tonic-gate 	acc->pw_map = NULL;
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate 	acc->sv_map = irs_irp_sv;
1150Sstevel@tonic-gate 	acc->pr_map = irs_irp_pr;
1160Sstevel@tonic-gate 	acc->ho_map = irs_irp_ho;
1170Sstevel@tonic-gate 	acc->nw_map = irs_irp_nw;
1180Sstevel@tonic-gate 	acc->ng_map = irs_irp_ng;
1190Sstevel@tonic-gate 	acc->close = irp_close;
1200Sstevel@tonic-gate 	return (acc);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
irs_irp_connection_setup(struct irp_p * cxndata,int * warned)1250Sstevel@tonic-gate irs_irp_connection_setup(struct irp_p *cxndata, int *warned) {
1260Sstevel@tonic-gate 	if (irs_irp_is_connected(cxndata)) {
1270Sstevel@tonic-gate 		return (0);
1280Sstevel@tonic-gate 	} else if (irs_irp_connect(cxndata) != 0) {
1290Sstevel@tonic-gate 		if (warned != NULL && !*warned) {
1300Sstevel@tonic-gate 			syslog(LOG_ERR, "irpd connection failed: %m\n");
1310Sstevel@tonic-gate 			(*warned)++;
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		return (-1);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
140*11038SRao.Shoaib@Sun.COM /*%
1410Sstevel@tonic-gate  * int irs_irp_connect(void);
1420Sstevel@tonic-gate  *
1430Sstevel@tonic-gate  *	Sets up the connection to the remote irpd server.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * Returns:
1460Sstevel@tonic-gate  *
1470Sstevel@tonic-gate  *	0 on success, -1 on failure.
1480Sstevel@tonic-gate  *
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate int
irs_irp_connect(struct irp_p * pvt)1510Sstevel@tonic-gate irs_irp_connect(struct irp_p *pvt) {
1520Sstevel@tonic-gate 	int flags;
1530Sstevel@tonic-gate 	struct sockaddr *addr;
1540Sstevel@tonic-gate 	struct sockaddr_in iaddr;
1550Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1560Sstevel@tonic-gate 	struct sockaddr_un uaddr;
1570Sstevel@tonic-gate #endif
1580Sstevel@tonic-gate 	long ipaddr;
1590Sstevel@tonic-gate 	const char *irphost;
1600Sstevel@tonic-gate 	int code;
1610Sstevel@tonic-gate 	char text[256];
1620Sstevel@tonic-gate 	int socklen = 0;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (pvt->fdCxn != -1) {
1650Sstevel@tonic-gate 		perror("fd != 1");
1660Sstevel@tonic-gate 		return (-1);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1700Sstevel@tonic-gate 	memset(&uaddr, 0, sizeof uaddr);
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 	memset(&iaddr, 0, sizeof iaddr);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	irphost = getenv(IRPD_HOST_ENV);
1750Sstevel@tonic-gate 	if (irphost == NULL) {
1760Sstevel@tonic-gate 		irphost = "127.0.0.1";
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #ifndef NO_SOCKADDR_UN
1800Sstevel@tonic-gate 	if (irphost[0] == '/') {
1810Sstevel@tonic-gate 		addr = (struct sockaddr *)&uaddr;
1820Sstevel@tonic-gate 		strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path);
1830Sstevel@tonic-gate 		uaddr.sun_family = AF_UNIX;
1840Sstevel@tonic-gate 		socklen = SUN_LEN(&uaddr);
1850Sstevel@tonic-gate #ifdef HAVE_SA_LEN
1860Sstevel@tonic-gate 		uaddr.sun_len = socklen;
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate 	} else
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 	{
1910Sstevel@tonic-gate 		if (inet_pton(AF_INET, irphost, &ipaddr) != 1) {
1920Sstevel@tonic-gate 			errno = EADDRNOTAVAIL;
1930Sstevel@tonic-gate 			perror("inet_pton");
1940Sstevel@tonic-gate 			return (-1);
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		addr = (struct sockaddr *)&iaddr;
1980Sstevel@tonic-gate 		socklen = sizeof iaddr;
1990Sstevel@tonic-gate #ifdef HAVE_SA_LEN
2000Sstevel@tonic-gate 		iaddr.sin_len = socklen;
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate 		iaddr.sin_family = AF_INET;
2030Sstevel@tonic-gate 		iaddr.sin_port = htons(IRPD_PORT);
2040Sstevel@tonic-gate 		iaddr.sin_addr.s_addr = ipaddr;
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC);
2090Sstevel@tonic-gate 	if (pvt->fdCxn < 0) {
2100Sstevel@tonic-gate 		perror("socket");
2110Sstevel@tonic-gate 		return (-1);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	if (connect(pvt->fdCxn, addr, socklen) != 0) {
2150Sstevel@tonic-gate 		perror("connect");
2160Sstevel@tonic-gate 		return (-1);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	flags = fcntl(pvt->fdCxn, F_GETFL, 0);
2200Sstevel@tonic-gate 	if (flags < 0) {
2210Sstevel@tonic-gate 		close(pvt->fdCxn);
2220Sstevel@tonic-gate 		perror("close");
2230Sstevel@tonic-gate 		return (-1);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #if 0
2270Sstevel@tonic-gate 	flags |= O_NONBLOCK;
2280Sstevel@tonic-gate 	if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) {
2290Sstevel@tonic-gate 		close(pvt->fdCxn);
2300Sstevel@tonic-gate 		perror("fcntl");
2310Sstevel@tonic-gate 		return (-1);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate #endif
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	code = irs_irp_read_response(pvt, text, sizeof text);
2360Sstevel@tonic-gate 	if (code != IRPD_WELCOME_CODE) {
2370Sstevel@tonic-gate 		if (irp_log_errors) {
2380Sstevel@tonic-gate 			syslog(LOG_WARNING, "Connection failed: %s", text);
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 		irs_irp_disconnect(pvt);
2410Sstevel@tonic-gate 		return (-1);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
247*11038SRao.Shoaib@Sun.COM /*%
2480Sstevel@tonic-gate  * int	irs_irp_is_connected(struct irp_p *pvt);
2490Sstevel@tonic-gate  *
2500Sstevel@tonic-gate  * Returns:
2510Sstevel@tonic-gate  *
2520Sstevel@tonic-gate  *	Non-zero if streams are setup to remote.
2530Sstevel@tonic-gate  *
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate int
irs_irp_is_connected(struct irp_p * pvt)2570Sstevel@tonic-gate irs_irp_is_connected(struct irp_p *pvt) {
2580Sstevel@tonic-gate 	return (pvt->fdCxn >= 0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
261*11038SRao.Shoaib@Sun.COM /*%
2620Sstevel@tonic-gate  * void
2630Sstevel@tonic-gate  * irs_irp_disconnect(struct irp_p *pvt);
2640Sstevel@tonic-gate  *
2650Sstevel@tonic-gate  *	Closes streams to remote.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate void
irs_irp_disconnect(struct irp_p * pvt)2690Sstevel@tonic-gate irs_irp_disconnect(struct irp_p *pvt) {
2700Sstevel@tonic-gate 	if (pvt->fdCxn != -1) {
2710Sstevel@tonic-gate 		close(pvt->fdCxn);
2720Sstevel@tonic-gate 		pvt->fdCxn = -1;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate int
irs_irp_read_line(struct irp_p * pvt,char * buffer,int len)2790Sstevel@tonic-gate irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) {
2800Sstevel@tonic-gate 	char *realstart = &pvt->inbuffer[0];
2810Sstevel@tonic-gate 	char *p, *start, *end;
2820Sstevel@tonic-gate 	int spare;
2830Sstevel@tonic-gate 	int i;
2840Sstevel@tonic-gate 	int buffpos = 0;
2850Sstevel@tonic-gate 	int left = len - 1;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	while (left > 0) {
2880Sstevel@tonic-gate 		start = p = &pvt->inbuffer[pvt->incurr];
2890Sstevel@tonic-gate 		end = &pvt->inbuffer[pvt->inlast];
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		while (p != end && *p != '\n')
2920Sstevel@tonic-gate 			p++;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		if (p == end) {
2950Sstevel@tonic-gate 			/* Found no newline so shift data down if necessary
2960Sstevel@tonic-gate 			 * and append new data to buffer
2970Sstevel@tonic-gate 			 */
2980Sstevel@tonic-gate 			if (start > realstart) {
2990Sstevel@tonic-gate 				memmove(realstart, start, end - start);
3000Sstevel@tonic-gate 				pvt->inlast = end - start;
3010Sstevel@tonic-gate 				start = realstart;
3020Sstevel@tonic-gate 				pvt->incurr = 0;
3030Sstevel@tonic-gate 				end = &pvt->inbuffer[pvt->inlast];
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 			spare = sizeof (pvt->inbuffer) - pvt->inlast;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 			p = end;
3090Sstevel@tonic-gate 			i = read(pvt->fdCxn, end, spare);
3100Sstevel@tonic-gate 			if (i < 0) {
3110Sstevel@tonic-gate 				close(pvt->fdCxn);
3120Sstevel@tonic-gate 				pvt->fdCxn = -1;
3130Sstevel@tonic-gate 				return (buffpos > 0 ? buffpos : -1);
3140Sstevel@tonic-gate 			} else if (i == 0) {
3150Sstevel@tonic-gate 				return (buffpos);
3160Sstevel@tonic-gate 			}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			end += i;
3190Sstevel@tonic-gate 			pvt->inlast += i;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			while (p != end && *p != '\n')
3220Sstevel@tonic-gate 				p++;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		if (p == end) {
3260Sstevel@tonic-gate 			/* full buffer and still no newline */
3270Sstevel@tonic-gate 			i = sizeof pvt->inbuffer;
3280Sstevel@tonic-gate 		} else {
3290Sstevel@tonic-gate 			/* include newline */
3300Sstevel@tonic-gate 			i = p - start + 1;
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		if (i > left)
3340Sstevel@tonic-gate 			i = left;
3350Sstevel@tonic-gate 		memcpy(buffer + buffpos, start, i);
3360Sstevel@tonic-gate 		pvt->incurr += i;
3370Sstevel@tonic-gate 		buffpos += i;
3380Sstevel@tonic-gate 		buffer[buffpos] = '\0';
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		if (p != end) {
3410Sstevel@tonic-gate 			left = 0;
3420Sstevel@tonic-gate 		} else {
3430Sstevel@tonic-gate 			left -= i;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate #if 0
3480Sstevel@tonic-gate 	fprintf(stderr, "read line: %s\n", buffer);
3490Sstevel@tonic-gate #endif
3500Sstevel@tonic-gate 	return (buffpos);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
353*11038SRao.Shoaib@Sun.COM /*%
3540Sstevel@tonic-gate  * int irp_read_response(struct irp_p *pvt);
3550Sstevel@tonic-gate  *
3560Sstevel@tonic-gate  * Returns:
3570Sstevel@tonic-gate  *
3580Sstevel@tonic-gate  *	The number found at the beginning of the line read from
3590Sstevel@tonic-gate  *	FP. 0 on failure(0 is not a legal response code). The
3600Sstevel@tonic-gate  *	rest of the line is discarded.
3610Sstevel@tonic-gate  *
3620Sstevel@tonic-gate  */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate int
irs_irp_read_response(struct irp_p * pvt,char * text,size_t textlen)3650Sstevel@tonic-gate irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) {
3660Sstevel@tonic-gate 	char line[1024];
3670Sstevel@tonic-gate 	int code;
3680Sstevel@tonic-gate 	char *p;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (irs_irp_read_line(pvt, line, sizeof line) <= 0) {
3710Sstevel@tonic-gate 		return (0);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	p = strchr(line, '\n');
3750Sstevel@tonic-gate 	if (p == NULL) {
3760Sstevel@tonic-gate 		return (0);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (sscanf(line, "%d", &code) != 1) {
3800Sstevel@tonic-gate 		code = 0;
381*11038SRao.Shoaib@Sun.COM 	} else if (text != NULL && textlen > 0U) {
3820Sstevel@tonic-gate 		p = line;
3830Sstevel@tonic-gate 		while (isspace((unsigned char)*p)) p++;
3840Sstevel@tonic-gate 		while (isdigit((unsigned char)*p)) p++;
3850Sstevel@tonic-gate 		while (isspace((unsigned char)*p)) p++;
3860Sstevel@tonic-gate 		strncpy(text, p, textlen - 1);
3870Sstevel@tonic-gate 		p[textlen - 1] = '\0';
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	return (code);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
393*11038SRao.Shoaib@Sun.COM /*%
3940Sstevel@tonic-gate  * char *irp_read_body(struct irp_p *pvt, size_t *size);
3950Sstevel@tonic-gate  *
3960Sstevel@tonic-gate  *	Read in the body of a response. Terminated by a line with
3970Sstevel@tonic-gate  *	just a dot on it. Lines should be terminated with a CR-LF
3980Sstevel@tonic-gate  *	sequence, but we're nt piccky if the CR is missing.
3990Sstevel@tonic-gate  *	No leading dot escaping is done as the protcol doesn't
4000Sstevel@tonic-gate  *	use leading dots anywhere.
4010Sstevel@tonic-gate  *
4020Sstevel@tonic-gate  * Returns:
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  *	Pointer to null-terminated buffer allocated by memget.
4050Sstevel@tonic-gate  *	*SIZE is set to the length of the buffer.
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  */
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate char *
irs_irp_read_body(struct irp_p * pvt,size_t * size)4100Sstevel@tonic-gate irs_irp_read_body(struct irp_p *pvt, size_t *size) {
4110Sstevel@tonic-gate 	char line[1024];
4120Sstevel@tonic-gate 	u_int linelen;
4130Sstevel@tonic-gate 	size_t len = LINEINCR;
4140Sstevel@tonic-gate 	char *buffer = memget(len);
4150Sstevel@tonic-gate 	int idx = 0;
4160Sstevel@tonic-gate 
417*11038SRao.Shoaib@Sun.COM 	if (buffer == NULL)
418*11038SRao.Shoaib@Sun.COM 		return (NULL);
419*11038SRao.Shoaib@Sun.COM 
4200Sstevel@tonic-gate 	for (;;) {
4210Sstevel@tonic-gate 		if (irs_irp_read_line(pvt, line, sizeof line) <= 0 ||
4220Sstevel@tonic-gate 		    strchr(line, '\n') == NULL)
4230Sstevel@tonic-gate 			goto death;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		linelen = strlen(line);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		if (line[linelen - 1] != '\n')
4280Sstevel@tonic-gate 			goto death;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/* We're not strict about missing \r. Should we be??  */
4310Sstevel@tonic-gate 		if (linelen > 2 && line[linelen - 2] == '\r') {
4320Sstevel@tonic-gate 			line[linelen - 2] = '\n';
4330Sstevel@tonic-gate 			line[linelen - 1] = '\0';
4340Sstevel@tonic-gate 			linelen--;
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		if (linelen == 2 && line[0] == '.') {
4380Sstevel@tonic-gate 			*size = len;
4390Sstevel@tonic-gate 			buffer[idx] = '\0';
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 			return (buffer);
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		if (linelen > (len - (idx + 1))) {
4450Sstevel@tonic-gate 			char *p = memget(len + LINEINCR);
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 			if (p == NULL)
4480Sstevel@tonic-gate 				goto death;
4490Sstevel@tonic-gate 			memcpy(p, buffer, len);
4500Sstevel@tonic-gate 			memput(buffer, len);
4510Sstevel@tonic-gate 			buffer = p;
4520Sstevel@tonic-gate 			len += LINEINCR;
4530Sstevel@tonic-gate 		}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		memcpy(buffer + idx, line, linelen);
4560Sstevel@tonic-gate 		idx += linelen;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate  death:
4590Sstevel@tonic-gate 	memput(buffer, len);
4600Sstevel@tonic-gate 	return (NULL);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
463*11038SRao.Shoaib@Sun.COM /*%
4640Sstevel@tonic-gate  * int irs_irp_get_full_response(struct irp_p *pvt, int *code,
4650Sstevel@tonic-gate  *			char **body, size_t *bodylen);
4660Sstevel@tonic-gate  *
4670Sstevel@tonic-gate  *	Gets the response to a command. If the response indicates
4680Sstevel@tonic-gate  *	there's a body to follow(code % 10 == 1), then the
4690Sstevel@tonic-gate  *	body buffer is allcoated with memget and stored in
4700Sstevel@tonic-gate  *	*BODY. The length of the allocated body buffer is stored
4710Sstevel@tonic-gate  *	in *BODY. The caller must give the body buffer back to
4720Sstevel@tonic-gate  *	memput when done. The results code is stored in *CODE.
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * Returns:
4750Sstevel@tonic-gate  *
4760Sstevel@tonic-gate  *	0 if a result was read. -1 on some sort of failure.
4770Sstevel@tonic-gate  *
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate int
irs_irp_get_full_response(struct irp_p * pvt,int * code,char * text,size_t textlen,char ** body,size_t * bodylen)4810Sstevel@tonic-gate irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text,
4820Sstevel@tonic-gate 			  size_t textlen, char **body, size_t *bodylen) {
4830Sstevel@tonic-gate 	int result = irs_irp_read_response(pvt, text, textlen);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	*body = NULL;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (result == 0) {
4880Sstevel@tonic-gate 		return (-1);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	*code = result;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/* Code that matches 2xx is a good result code.
4940Sstevel@tonic-gate 	 * Code that matches xx1 means there's a response body coming.
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	if ((result / 100) == 2 && (result % 10) == 1) {
4970Sstevel@tonic-gate 		*body = irs_irp_read_body(pvt, bodylen);
4980Sstevel@tonic-gate 		if (*body == NULL) {
4990Sstevel@tonic-gate 			return (-1);
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	return (0);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate 
506*11038SRao.Shoaib@Sun.COM /*%
5070Sstevel@tonic-gate  * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...);
5080Sstevel@tonic-gate  *
5090Sstevel@tonic-gate  *	Sends command to remote connected via the PVT
510*11038SRao.Shoaib@Sun.COM  *	structure. FMT and args after it are fprintf-like
5110Sstevel@tonic-gate  *	arguments for formatting.
5120Sstevel@tonic-gate  *
5130Sstevel@tonic-gate  * Returns:
5140Sstevel@tonic-gate  *
5150Sstevel@tonic-gate  *	0 on success, -1 on failure.
5160Sstevel@tonic-gate  */
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate int
irs_irp_send_command(struct irp_p * pvt,const char * fmt,...)5190Sstevel@tonic-gate irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) {
5200Sstevel@tonic-gate 	va_list ap;
5210Sstevel@tonic-gate 	char buffer[1024];
5220Sstevel@tonic-gate 	int pos = 0;
5230Sstevel@tonic-gate 	int i, todo;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (pvt->fdCxn < 0) {
5270Sstevel@tonic-gate 		return (-1);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	va_start(ap, fmt);
531*11038SRao.Shoaib@Sun.COM 	(void) vsprintf(buffer, fmt, ap);
532*11038SRao.Shoaib@Sun.COM 	todo = strlen(buffer);
5330Sstevel@tonic-gate 	va_end(ap);
5340Sstevel@tonic-gate 	if (todo > (int)sizeof(buffer) - 3) {
5350Sstevel@tonic-gate 		syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()");
5360Sstevel@tonic-gate 		exit(1);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	strcat(buffer, "\r\n");
5390Sstevel@tonic-gate 	todo = strlen(buffer);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	while (todo > 0) {
5420Sstevel@tonic-gate 		i = write(pvt->fdCxn, buffer + pos, todo);
5430Sstevel@tonic-gate #if 0
5440Sstevel@tonic-gate 		/* XXX brister */
5450Sstevel@tonic-gate 		fprintf(stderr, "Wrote: \"");
5460Sstevel@tonic-gate 		fwrite(buffer + pos, sizeof (char), todo, stderr);
5470Sstevel@tonic-gate 		fprintf(stderr, "\"\n");
5480Sstevel@tonic-gate #endif
5490Sstevel@tonic-gate 		if (i < 0) {
5500Sstevel@tonic-gate 			close(pvt->fdCxn);
5510Sstevel@tonic-gate 			pvt->fdCxn = -1;
5520Sstevel@tonic-gate 			return (-1);
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 		todo -= i;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	return (0);
5580Sstevel@tonic-gate }
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate /* Methods */
5620Sstevel@tonic-gate 
563*11038SRao.Shoaib@Sun.COM /*%
5640Sstevel@tonic-gate  * void irp_close(struct irs_acc *this)
5650Sstevel@tonic-gate  *
5660Sstevel@tonic-gate  */
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate static void
irp_close(struct irs_acc * this)5690Sstevel@tonic-gate irp_close(struct irs_acc *this) {
5700Sstevel@tonic-gate 	struct irp_p *irp = (struct irp_p *)this->private;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (irp != NULL) {
5730Sstevel@tonic-gate 		irs_irp_disconnect(irp);
5740Sstevel@tonic-gate 		memput(irp, sizeof *irp);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	memput(this, sizeof *this);
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 
582*11038SRao.Shoaib@Sun.COM 
583*11038SRao.Shoaib@Sun.COM /*! \file */
584