xref: /illumos-gate/usr/src/cmd/nscd/getserv.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Routines to handle getserv* calls in nscd
30  */
31 
32 #include <strings.h>
33 #include "cache.h"
34 
35 
36 #define	name_db	ctx->nsc_db[0]
37 #define	port_db	ctx->nsc_db[1]
38 
39 #define	NSC_NAME_SERVICES_BYNAME	"getservbyname"
40 #define	NSC_NAME_SERVICES_BYPORT	"getservbyport"
41 
42 static void servname_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
43 static int servname_compar(const void *, const void *);
44 static uint_t servname_gethash(nss_XbyY_key_t *, int);
45 
46 static void servport_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
47 static int servport_compar(const void *, const void *);
48 static uint_t servport_gethash(nss_XbyY_key_t *, int);
49 
50 void
51 serv_init_ctx(nsc_ctx_t *ctx) {
52 	ctx->dbname = NSS_DBNAM_SERVICES;
53 	ctx->file_name = "/etc/services";
54 	ctx->db_count = 2;
55 	name_db = make_cache(nsc_key_other,
56 			NSS_DBOP_SERVICES_BYNAME,
57 			NSC_NAME_SERVICES_BYNAME,
58 			servname_compar,
59 			servname_getlogstr,
60 			servname_gethash, nsc_ht_default, -1);
61 
62 	port_db = make_cache(nsc_key_other,
63 			NSS_DBOP_SERVICES_BYPORT,
64 			NSC_NAME_SERVICES_BYPORT,
65 			servport_compar,
66 			servport_getlogstr,
67 			servport_gethash, nsc_ht_default, -1);
68 }
69 
70 static int
71 servname_compar(const void *n1, const void *n2) {
72 	nsc_entry_t	*e1, *e2;
73 	int		res, l1, l2;
74 
75 	e1 = (nsc_entry_t *)n1;
76 	e2 = (nsc_entry_t *)n2;
77 
78 	/* compare protocol */
79 	if (e1->key.serv.proto == NULL && e2->key.serv.proto)
80 		return (-1);
81 	if (e1->key.serv.proto && e2->key.serv.proto == NULL)
82 		return (1);
83 	if (e1->key.serv.proto) {
84 		l1 = strlen(e1->key.serv.proto);
85 		l2 = strlen(e2->key.serv.proto);
86 		res = strncmp(e1->key.serv.proto, e2->key.serv.proto,
87 			(l1 > l2)?l1:l2);
88 		if (res > 0)
89 			return (1);
90 		if (res < 0)
91 			return (-1);
92 	}
93 
94 	/* compare service name */
95 	l1 = strlen(e1->key.serv.serv.name);
96 	l2 = strlen(e2->key.serv.serv.name);
97 	res = strncmp(e1->key.serv.serv.name, e2->key.serv.serv.name,
98 			(l1 > l2)?l1:l2);
99 	return (_NSC_INT_KEY_CMP(res, 0));
100 }
101 
102 static uint_t
103 servname_gethash(nss_XbyY_key_t *key, int htsize) {
104 	return (ces_gethash(key->serv.serv.name, htsize));
105 }
106 
107 static void
108 servname_getlogstr(char *name, char *whoami, size_t len,
109 			nss_XbyY_args_t *argp) {
110 	(void) snprintf(whoami, len, "%s [key=%s, %s]",
111 			name,
112 			argp->key.serv.serv.name,
113 			check_null(argp->key.serv.proto));
114 }
115 
116 static int
117 servport_compar(const void *n1, const void *n2) {
118 	nsc_entry_t	*e1, *e2;
119 	int		res, l1, l2;
120 
121 	e1 = (nsc_entry_t *)n1;
122 	e2 = (nsc_entry_t *)n2;
123 
124 	/* compare protocol */
125 	if (e1->key.serv.proto == NULL && e2->key.serv.proto)
126 		return (-1);
127 	if (e1->key.serv.proto && e2->key.serv.proto == NULL)
128 		return (1);
129 	if (e1->key.serv.proto) {
130 		l1 = strlen(e1->key.serv.proto);
131 		l2 = strlen(e2->key.serv.proto);
132 		res = strncmp(e1->key.serv.proto, e2->key.serv.proto,
133 				(l1 > l2)?l1:l2);
134 		if (res > 0)
135 			return (1);
136 		if (res < 0)
137 			return (-1);
138 	}
139 
140 	/* compare port */
141 	return (_NSC_INT_KEY_CMP(e1->key.serv.serv.port,
142 			e2->key.serv.serv.port));
143 }
144 
145 static uint_t
146 servport_gethash(nss_XbyY_key_t *key, int htsize) {
147 	return (db_gethash(&key->serv.serv.port,
148 			sizeof (key->serv.serv.port), htsize));
149 }
150 
151 static void
152 servport_getlogstr(char *name, char *whoami, size_t len,
153 			nss_XbyY_args_t *argp) {
154 	(void) snprintf(whoami, len, "%s [key=%d, %s]",
155 			name,
156 			argp->key.serv.serv.port,
157 			check_null(argp->key.serv.proto));
158 }
159