xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/irp_sv.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and 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
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /* extern */
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 
300Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
310Sstevel@tonic-gate #include <db.h>
320Sstevel@tonic-gate #endif
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <string.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <syslog.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <irs.h>
420Sstevel@tonic-gate #include <irp.h>
430Sstevel@tonic-gate #include <isc/irpmarshall.h>
440Sstevel@tonic-gate #include <isc/memcluster.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "irs_p.h"
470Sstevel@tonic-gate #include "lcl_p.h"
480Sstevel@tonic-gate #include "irp_p.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "port_after.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /* Types */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate struct pvt {
550Sstevel@tonic-gate 	struct irp_p	       *girpdata;
560Sstevel@tonic-gate 	int			warned;
570Sstevel@tonic-gate 	struct servent		service;
580Sstevel@tonic-gate };
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Forward */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static void			sv_close(struct irs_sv*);
630Sstevel@tonic-gate static struct servent *		sv_next(struct irs_sv *);
640Sstevel@tonic-gate static struct servent *		sv_byname(struct irs_sv *, const char *,
650Sstevel@tonic-gate 					  const char *);
660Sstevel@tonic-gate static struct servent *		sv_byport(struct irs_sv *, int, const char *);
670Sstevel@tonic-gate static void			sv_rewind(struct irs_sv *);
680Sstevel@tonic-gate static void			sv_minimize(struct irs_sv *);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static void			free_service(struct servent *sv);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* Public */
750Sstevel@tonic-gate 
76*11038SRao.Shoaib@Sun.COM /*%
770Sstevel@tonic-gate  * struct irs_sv * irs_irp_sv(struct irs_acc *this)
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate struct irs_sv *
irs_irp_sv(struct irs_acc * this)820Sstevel@tonic-gate irs_irp_sv(struct irs_acc *this) {
830Sstevel@tonic-gate 	struct irs_sv *sv;
840Sstevel@tonic-gate 	struct pvt *pvt;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if ((sv = memget(sizeof *sv)) == NULL) {
870Sstevel@tonic-gate 		errno = ENOMEM;
880Sstevel@tonic-gate 		return (NULL);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	memset(sv, 0x0, sizeof *sv);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if ((pvt = memget(sizeof *pvt)) == NULL) {
930Sstevel@tonic-gate 		memput(sv, sizeof *sv);
940Sstevel@tonic-gate 		errno = ENOMEM;
950Sstevel@tonic-gate 		return (NULL);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 	memset(pvt, 0, sizeof *pvt);
980Sstevel@tonic-gate 	pvt->girpdata = this->private;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	sv->private = pvt;
1010Sstevel@tonic-gate 	sv->close = sv_close;
1020Sstevel@tonic-gate 	sv->next = sv_next;
1030Sstevel@tonic-gate 	sv->byname = sv_byname;
1040Sstevel@tonic-gate 	sv->byport = sv_byport;
1050Sstevel@tonic-gate 	sv->rewind = sv_rewind;
1060Sstevel@tonic-gate 	sv->minimize = sv_minimize;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (sv);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* Methods */
1120Sstevel@tonic-gate 
113*11038SRao.Shoaib@Sun.COM /*%
1140Sstevel@tonic-gate  * void sv_close(struct irs_sv *this)
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static void
sv_close(struct irs_sv * this)1190Sstevel@tonic-gate sv_close(struct irs_sv *this) {
1200Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	sv_minimize(this);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	free_service(&pvt->service);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	memput(pvt, sizeof *pvt);
1270Sstevel@tonic-gate 	memput(this, sizeof *this);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
130*11038SRao.Shoaib@Sun.COM /*%
1310Sstevel@tonic-gate  *	Fills the cache if necessary and returns the next item from it.
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static struct servent *
sv_next(struct irs_sv * this)1360Sstevel@tonic-gate sv_next(struct irs_sv *this) {
1370Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1380Sstevel@tonic-gate 	struct servent *sv = &pvt->service;
1390Sstevel@tonic-gate 	char *body;
1400Sstevel@tonic-gate 	size_t bodylen;
1410Sstevel@tonic-gate 	int code;
1420Sstevel@tonic-gate 	char text[256];
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1450Sstevel@tonic-gate 		return (NULL);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
1490Sstevel@tonic-gate 		return (NULL);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
1530Sstevel@tonic-gate 				      text, sizeof text,
1540Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
1550Sstevel@tonic-gate 		return (NULL);
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (code == IRPD_GETSERVICE_OK) {
1590Sstevel@tonic-gate 		free_service(sv);
1600Sstevel@tonic-gate 		if (irp_unmarshall_sv(sv, body) != 0) {
1610Sstevel@tonic-gate 			sv = NULL;
1620Sstevel@tonic-gate 		}
1630Sstevel@tonic-gate 	} else {
1640Sstevel@tonic-gate 		sv = NULL;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (body != NULL) {
1680Sstevel@tonic-gate 		memput(body, bodylen);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return (sv);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
174*11038SRao.Shoaib@Sun.COM /*%
1750Sstevel@tonic-gate  * struct servent * sv_byname(struct irs_sv *this, const char *name,
1760Sstevel@tonic-gate  *				const char *proto)
1770Sstevel@tonic-gate  *
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static struct servent *
sv_byname(struct irs_sv * this,const char * name,const char * proto)1810Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
1820Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
1830Sstevel@tonic-gate 	struct servent *sv = &pvt->service;
1840Sstevel@tonic-gate 	char *body;
1850Sstevel@tonic-gate 	char text[256];
1860Sstevel@tonic-gate 	size_t bodylen;
1870Sstevel@tonic-gate 	int code;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (sv->s_name != NULL &&
1900Sstevel@tonic-gate 	    strcmp(name, sv->s_name) == 0 &&
1910Sstevel@tonic-gate 	    strcasecmp(proto, sv->s_proto) == 0) {
1920Sstevel@tonic-gate 		return (sv);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1960Sstevel@tonic-gate 		return (NULL);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
2000Sstevel@tonic-gate 				 name, proto) != 0)
2010Sstevel@tonic-gate 		return (NULL);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
2040Sstevel@tonic-gate 				      text, sizeof text,
2050Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
2060Sstevel@tonic-gate 		return (NULL);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (code == IRPD_GETSERVICE_OK) {
2100Sstevel@tonic-gate 		free_service(sv);
2110Sstevel@tonic-gate 		if (irp_unmarshall_sv(sv, body) != 0) {
2120Sstevel@tonic-gate 			sv = NULL;
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	} else {
2150Sstevel@tonic-gate 		sv = NULL;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (body != NULL) {
2190Sstevel@tonic-gate 		memput(body, bodylen);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (sv);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
225*11038SRao.Shoaib@Sun.COM /*%
2260Sstevel@tonic-gate  * struct servent * sv_byport(struct irs_sv *this, int port,
2270Sstevel@tonic-gate  *				const char *proto)
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static struct servent *
sv_byport(struct irs_sv * this,int port,const char * proto)2320Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
2330Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2340Sstevel@tonic-gate 	struct servent *sv = &pvt->service;
2350Sstevel@tonic-gate 	char *body;
2360Sstevel@tonic-gate 	size_t bodylen;
2370Sstevel@tonic-gate 	char text[256];
2380Sstevel@tonic-gate 	int code;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (sv->s_name != NULL &&
2410Sstevel@tonic-gate 	    port == sv->s_port &&
2420Sstevel@tonic-gate 	    strcasecmp(proto, sv->s_proto) == 0) {
2430Sstevel@tonic-gate 		return (sv);
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2470Sstevel@tonic-gate 		return (NULL);
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
2510Sstevel@tonic-gate 				 ntohs((short)port), proto) != 0) {
2520Sstevel@tonic-gate 		return (NULL);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (irs_irp_get_full_response(pvt->girpdata, &code,
2560Sstevel@tonic-gate 				      text, sizeof text,
2570Sstevel@tonic-gate 				      &body, &bodylen) != 0) {
2580Sstevel@tonic-gate 		return (NULL);
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if (code == IRPD_GETSERVICE_OK) {
2620Sstevel@tonic-gate 		free_service(sv);
2630Sstevel@tonic-gate 		if (irp_unmarshall_sv(sv, body) != 0) {
2640Sstevel@tonic-gate 			sv = NULL;
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 	} else {
2670Sstevel@tonic-gate 		sv = NULL;
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if (body != NULL) {
2710Sstevel@tonic-gate 		memput(body, bodylen);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	return (sv);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
277*11038SRao.Shoaib@Sun.COM /*%
2780Sstevel@tonic-gate  * void sv_rewind(struct irs_sv *this)
2790Sstevel@tonic-gate  *
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate static void
sv_rewind(struct irs_sv * this)2830Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
2840Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
2850Sstevel@tonic-gate 	char text[256];
2860Sstevel@tonic-gate 	int code;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2890Sstevel@tonic-gate 		return;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
2930Sstevel@tonic-gate 		return;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2970Sstevel@tonic-gate 	if (code != IRPD_GETSERVICE_SETOK) {
2980Sstevel@tonic-gate 		if (irp_log_errors) {
2990Sstevel@tonic-gate 			syslog(LOG_WARNING, "setservent failed: %s", text);
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	return;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
306*11038SRao.Shoaib@Sun.COM /*%
3070Sstevel@tonic-gate  * void sv_minimize(struct irs_sv *this)
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate static void
sv_minimize(struct irs_sv * this)3120Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
3130Sstevel@tonic-gate 	struct pvt *pvt = (struct pvt *)this->private;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	irs_irp_disconnect(pvt->girpdata);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate static void
free_service(struct servent * sv)3240Sstevel@tonic-gate free_service(struct servent *sv) {
3250Sstevel@tonic-gate 	char **p;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (sv == NULL) {
3280Sstevel@tonic-gate 		return;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (sv->s_name != NULL) {
3320Sstevel@tonic-gate 		free(sv->s_name);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
3360Sstevel@tonic-gate 		free(*p);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	if (sv->s_proto != NULL) {
3400Sstevel@tonic-gate 		free(sv->s_proto);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 
345*11038SRao.Shoaib@Sun.COM 
346*11038SRao.Shoaib@Sun.COM /*! \file */
347