17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate * Portions Copyright (c) 1996,1998 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate *
57c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and 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
10*9525b14bSRao Shoaib * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate */
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate /* extern */
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
307c478bd9Sstevel@tonic-gate #ifdef IRS_LCL_SV_DB
317c478bd9Sstevel@tonic-gate #include <db.h>
327c478bd9Sstevel@tonic-gate #endif
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <syslog.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include <irs.h>
427c478bd9Sstevel@tonic-gate #include <irp.h>
437c478bd9Sstevel@tonic-gate #include <isc/irpmarshall.h>
447c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #include "irs_p.h"
477c478bd9Sstevel@tonic-gate #include "lcl_p.h"
487c478bd9Sstevel@tonic-gate #include "irp_p.h"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #include "port_after.h"
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /* Types */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate struct pvt {
557c478bd9Sstevel@tonic-gate struct irp_p *girpdata;
567c478bd9Sstevel@tonic-gate int warned;
577c478bd9Sstevel@tonic-gate struct servent service;
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /* Forward */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate static void sv_close(struct irs_sv*);
637c478bd9Sstevel@tonic-gate static struct servent * sv_next(struct irs_sv *);
647c478bd9Sstevel@tonic-gate static struct servent * sv_byname(struct irs_sv *, const char *,
657c478bd9Sstevel@tonic-gate const char *);
667c478bd9Sstevel@tonic-gate static struct servent * sv_byport(struct irs_sv *, int, const char *);
677c478bd9Sstevel@tonic-gate static void sv_rewind(struct irs_sv *);
687c478bd9Sstevel@tonic-gate static void sv_minimize(struct irs_sv *);
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate static void free_service(struct servent *sv);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate /* Public */
757c478bd9Sstevel@tonic-gate
76*9525b14bSRao Shoaib /*%
777c478bd9Sstevel@tonic-gate * struct irs_sv * irs_irp_sv(struct irs_acc *this)
787c478bd9Sstevel@tonic-gate *
797c478bd9Sstevel@tonic-gate */
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate struct irs_sv *
irs_irp_sv(struct irs_acc * this)827c478bd9Sstevel@tonic-gate irs_irp_sv(struct irs_acc *this) {
837c478bd9Sstevel@tonic-gate struct irs_sv *sv;
847c478bd9Sstevel@tonic-gate struct pvt *pvt;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if ((sv = memget(sizeof *sv)) == NULL) {
877c478bd9Sstevel@tonic-gate errno = ENOMEM;
887c478bd9Sstevel@tonic-gate return (NULL);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate memset(sv, 0x0, sizeof *sv);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate if ((pvt = memget(sizeof *pvt)) == NULL) {
937c478bd9Sstevel@tonic-gate memput(sv, sizeof *sv);
947c478bd9Sstevel@tonic-gate errno = ENOMEM;
957c478bd9Sstevel@tonic-gate return (NULL);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate memset(pvt, 0, sizeof *pvt);
987c478bd9Sstevel@tonic-gate pvt->girpdata = this->private;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate sv->private = pvt;
1017c478bd9Sstevel@tonic-gate sv->close = sv_close;
1027c478bd9Sstevel@tonic-gate sv->next = sv_next;
1037c478bd9Sstevel@tonic-gate sv->byname = sv_byname;
1047c478bd9Sstevel@tonic-gate sv->byport = sv_byport;
1057c478bd9Sstevel@tonic-gate sv->rewind = sv_rewind;
1067c478bd9Sstevel@tonic-gate sv->minimize = sv_minimize;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate return (sv);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /* Methods */
1127c478bd9Sstevel@tonic-gate
113*9525b14bSRao Shoaib /*%
1147c478bd9Sstevel@tonic-gate * void sv_close(struct irs_sv *this)
1157c478bd9Sstevel@tonic-gate *
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate static void
sv_close(struct irs_sv * this)1197c478bd9Sstevel@tonic-gate sv_close(struct irs_sv *this) {
1207c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate sv_minimize(this);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate free_service(&pvt->service);
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate memput(pvt, sizeof *pvt);
1277c478bd9Sstevel@tonic-gate memput(this, sizeof *this);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
130*9525b14bSRao Shoaib /*%
1317c478bd9Sstevel@tonic-gate * Fills the cache if necessary and returns the next item from it.
1327c478bd9Sstevel@tonic-gate *
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate static struct servent *
sv_next(struct irs_sv * this)1367c478bd9Sstevel@tonic-gate sv_next(struct irs_sv *this) {
1377c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1387c478bd9Sstevel@tonic-gate struct servent *sv = &pvt->service;
1397c478bd9Sstevel@tonic-gate char *body;
1407c478bd9Sstevel@tonic-gate size_t bodylen;
1417c478bd9Sstevel@tonic-gate int code;
1427c478bd9Sstevel@tonic-gate char text[256];
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1457c478bd9Sstevel@tonic-gate return (NULL);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) {
1497c478bd9Sstevel@tonic-gate return (NULL);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
1537c478bd9Sstevel@tonic-gate text, sizeof text,
1547c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) {
1557c478bd9Sstevel@tonic-gate return (NULL);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if (code == IRPD_GETSERVICE_OK) {
1597c478bd9Sstevel@tonic-gate free_service(sv);
1607c478bd9Sstevel@tonic-gate if (irp_unmarshall_sv(sv, body) != 0) {
1617c478bd9Sstevel@tonic-gate sv = NULL;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate } else {
1647c478bd9Sstevel@tonic-gate sv = NULL;
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate if (body != NULL) {
1687c478bd9Sstevel@tonic-gate memput(body, bodylen);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate return (sv);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
174*9525b14bSRao Shoaib /*%
1757c478bd9Sstevel@tonic-gate * struct servent * sv_byname(struct irs_sv *this, const char *name,
1767c478bd9Sstevel@tonic-gate * const char *proto)
1777c478bd9Sstevel@tonic-gate *
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate static struct servent *
sv_byname(struct irs_sv * this,const char * name,const char * proto)1817c478bd9Sstevel@tonic-gate sv_byname(struct irs_sv *this, const char *name, const char *proto) {
1827c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
1837c478bd9Sstevel@tonic-gate struct servent *sv = &pvt->service;
1847c478bd9Sstevel@tonic-gate char *body;
1857c478bd9Sstevel@tonic-gate char text[256];
1867c478bd9Sstevel@tonic-gate size_t bodylen;
1877c478bd9Sstevel@tonic-gate int code;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate if (sv->s_name != NULL &&
1907c478bd9Sstevel@tonic-gate strcmp(name, sv->s_name) == 0 &&
1917c478bd9Sstevel@tonic-gate strcasecmp(proto, sv->s_proto) == 0) {
1927c478bd9Sstevel@tonic-gate return (sv);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
1967c478bd9Sstevel@tonic-gate return (NULL);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s",
2007c478bd9Sstevel@tonic-gate name, proto) != 0)
2017c478bd9Sstevel@tonic-gate return (NULL);
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
2047c478bd9Sstevel@tonic-gate text, sizeof text,
2057c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) {
2067c478bd9Sstevel@tonic-gate return (NULL);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (code == IRPD_GETSERVICE_OK) {
2107c478bd9Sstevel@tonic-gate free_service(sv);
2117c478bd9Sstevel@tonic-gate if (irp_unmarshall_sv(sv, body) != 0) {
2127c478bd9Sstevel@tonic-gate sv = NULL;
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate } else {
2157c478bd9Sstevel@tonic-gate sv = NULL;
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate if (body != NULL) {
2197c478bd9Sstevel@tonic-gate memput(body, bodylen);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate return (sv);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
225*9525b14bSRao Shoaib /*%
2267c478bd9Sstevel@tonic-gate * struct servent * sv_byport(struct irs_sv *this, int port,
2277c478bd9Sstevel@tonic-gate * const char *proto)
2287c478bd9Sstevel@tonic-gate *
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate static struct servent *
sv_byport(struct irs_sv * this,int port,const char * proto)2327c478bd9Sstevel@tonic-gate sv_byport(struct irs_sv *this, int port, const char *proto) {
2337c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2347c478bd9Sstevel@tonic-gate struct servent *sv = &pvt->service;
2357c478bd9Sstevel@tonic-gate char *body;
2367c478bd9Sstevel@tonic-gate size_t bodylen;
2377c478bd9Sstevel@tonic-gate char text[256];
2387c478bd9Sstevel@tonic-gate int code;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if (sv->s_name != NULL &&
2417c478bd9Sstevel@tonic-gate port == sv->s_port &&
2427c478bd9Sstevel@tonic-gate strcasecmp(proto, sv->s_proto) == 0) {
2437c478bd9Sstevel@tonic-gate return (sv);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2477c478bd9Sstevel@tonic-gate return (NULL);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s",
2517c478bd9Sstevel@tonic-gate ntohs((short)port), proto) != 0) {
2527c478bd9Sstevel@tonic-gate return (NULL);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate
2557c478bd9Sstevel@tonic-gate if (irs_irp_get_full_response(pvt->girpdata, &code,
2567c478bd9Sstevel@tonic-gate text, sizeof text,
2577c478bd9Sstevel@tonic-gate &body, &bodylen) != 0) {
2587c478bd9Sstevel@tonic-gate return (NULL);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate if (code == IRPD_GETSERVICE_OK) {
2627c478bd9Sstevel@tonic-gate free_service(sv);
2637c478bd9Sstevel@tonic-gate if (irp_unmarshall_sv(sv, body) != 0) {
2647c478bd9Sstevel@tonic-gate sv = NULL;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate } else {
2677c478bd9Sstevel@tonic-gate sv = NULL;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate if (body != NULL) {
2717c478bd9Sstevel@tonic-gate memput(body, bodylen);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate return (sv);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
277*9525b14bSRao Shoaib /*%
2787c478bd9Sstevel@tonic-gate * void sv_rewind(struct irs_sv *this)
2797c478bd9Sstevel@tonic-gate *
2807c478bd9Sstevel@tonic-gate */
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate static void
sv_rewind(struct irs_sv * this)2837c478bd9Sstevel@tonic-gate sv_rewind(struct irs_sv *this) {
2847c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
2857c478bd9Sstevel@tonic-gate char text[256];
2867c478bd9Sstevel@tonic-gate int code;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
2897c478bd9Sstevel@tonic-gate return;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) {
2937c478bd9Sstevel@tonic-gate return;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
2977c478bd9Sstevel@tonic-gate if (code != IRPD_GETSERVICE_SETOK) {
2987c478bd9Sstevel@tonic-gate if (irp_log_errors) {
2997c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "setservent failed: %s", text);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate return;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
306*9525b14bSRao Shoaib /*%
3077c478bd9Sstevel@tonic-gate * void sv_minimize(struct irs_sv *this)
3087c478bd9Sstevel@tonic-gate *
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate static void
sv_minimize(struct irs_sv * this)3127c478bd9Sstevel@tonic-gate sv_minimize(struct irs_sv *this) {
3137c478bd9Sstevel@tonic-gate struct pvt *pvt = (struct pvt *)this->private;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate irs_irp_disconnect(pvt->girpdata);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate static void
free_service(struct servent * sv)3247c478bd9Sstevel@tonic-gate free_service(struct servent *sv) {
3257c478bd9Sstevel@tonic-gate char **p;
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate if (sv == NULL) {
3287c478bd9Sstevel@tonic-gate return;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate if (sv->s_name != NULL) {
3327c478bd9Sstevel@tonic-gate free(sv->s_name);
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) {
3367c478bd9Sstevel@tonic-gate free(*p);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate if (sv->s_proto != NULL) {
3407c478bd9Sstevel@tonic-gate free(sv->s_proto);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate
345*9525b14bSRao Shoaib
346*9525b14bSRao Shoaib /*! \file */
347