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