xref: /illumos-gate/usr/src/cmd/nscd/getserv.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 /*
27  * Routines to handle getserv* calls in nscd
28  */
29 
30 #include <strings.h>
31 #include "cache.h"
32 
33 
34 #define	name_db	ctx->nsc_db[0]
35 #define	port_db	ctx->nsc_db[1]
36 
37 #define	NSC_NAME_SERVICES_BYNAME	"getservbyname"
38 #define	NSC_NAME_SERVICES_BYPORT	"getservbyport"
39 
40 static void servname_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
41 static int servname_compar(const void *, const void *);
42 static uint_t servname_gethash(nss_XbyY_key_t *, int);
43 
44 static void servport_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
45 static int servport_compar(const void *, const void *);
46 static uint_t servport_gethash(nss_XbyY_key_t *, int);
47 
48 void
serv_init_ctx(nsc_ctx_t * ctx)49 serv_init_ctx(nsc_ctx_t *ctx) {
50 	ctx->dbname = NSS_DBNAM_SERVICES;
51 	ctx->file_name = "/etc/services";
52 	ctx->db_count = 2;
53 	name_db = make_cache(nsc_key_other,
54 			NSS_DBOP_SERVICES_BYNAME,
55 			NSC_NAME_SERVICES_BYNAME,
56 			servname_compar,
57 			servname_getlogstr,
58 			servname_gethash, nsc_ht_default, -1);
59 
60 	port_db = make_cache(nsc_key_other,
61 			NSS_DBOP_SERVICES_BYPORT,
62 			NSC_NAME_SERVICES_BYPORT,
63 			servport_compar,
64 			servport_getlogstr,
65 			servport_gethash, nsc_ht_default, -1);
66 }
67 
68 static int
servname_compar(const void * n1,const void * n2)69 servname_compar(const void *n1, const void *n2) {
70 	nsc_entry_t	*e1, *e2;
71 	int		res, l1, l2;
72 
73 	e1 = (nsc_entry_t *)n1;
74 	e2 = (nsc_entry_t *)n2;
75 
76 	/* compare protocol */
77 	if (e1->key.serv.proto == NULL && e2->key.serv.proto)
78 		return (-1);
79 	if (e1->key.serv.proto && e2->key.serv.proto == NULL)
80 		return (1);
81 	if (e1->key.serv.proto) {
82 		l1 = strlen(e1->key.serv.proto);
83 		l2 = strlen(e2->key.serv.proto);
84 		res = strncmp(e1->key.serv.proto, e2->key.serv.proto,
85 			(l1 > l2)?l1:l2);
86 		if (res > 0)
87 			return (1);
88 		if (res < 0)
89 			return (-1);
90 	}
91 
92 	/* compare service name */
93 	l1 = strlen(e1->key.serv.serv.name);
94 	l2 = strlen(e2->key.serv.serv.name);
95 	res = strncmp(e1->key.serv.serv.name, e2->key.serv.serv.name,
96 			(l1 > l2)?l1:l2);
97 	return (_NSC_INT_KEY_CMP(res, 0));
98 }
99 
100 static uint_t
servname_gethash(nss_XbyY_key_t * key,int htsize)101 servname_gethash(nss_XbyY_key_t *key, int htsize) {
102 	return (ces_gethash(key->serv.serv.name, htsize));
103 }
104 
105 static void
servname_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)106 servname_getlogstr(char *name, char *whoami, size_t len,
107 			nss_XbyY_args_t *argp) {
108 	(void) snprintf(whoami, len, "%s [key=%s, %s]",
109 			name,
110 			argp->key.serv.serv.name,
111 			check_null(argp->key.serv.proto));
112 }
113 
114 static int
servport_compar(const void * n1,const void * n2)115 servport_compar(const void *n1, const void *n2) {
116 	nsc_entry_t	*e1, *e2;
117 	int		res, l1, l2;
118 
119 	e1 = (nsc_entry_t *)n1;
120 	e2 = (nsc_entry_t *)n2;
121 
122 	/* compare protocol */
123 	if (e1->key.serv.proto == NULL && e2->key.serv.proto)
124 		return (-1);
125 	if (e1->key.serv.proto && e2->key.serv.proto == NULL)
126 		return (1);
127 	if (e1->key.serv.proto) {
128 		l1 = strlen(e1->key.serv.proto);
129 		l2 = strlen(e2->key.serv.proto);
130 		res = strncmp(e1->key.serv.proto, e2->key.serv.proto,
131 				(l1 > l2)?l1:l2);
132 		if (res > 0)
133 			return (1);
134 		if (res < 0)
135 			return (-1);
136 	}
137 
138 	/* compare port */
139 	return (_NSC_INT_KEY_CMP(e1->key.serv.serv.port,
140 			e2->key.serv.serv.port));
141 }
142 
143 static uint_t
servport_gethash(nss_XbyY_key_t * key,int htsize)144 servport_gethash(nss_XbyY_key_t *key, int htsize) {
145 	return (db_gethash(&key->serv.serv.port,
146 			sizeof (key->serv.serv.port), htsize));
147 }
148 
149 static void
servport_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)150 servport_getlogstr(char *name, char *whoami, size_t len,
151 			nss_XbyY_args_t *argp) {
152 	(void) snprintf(whoami, len, "%s [key=%d, %s]",
153 			name,
154 			argp->key.serv.serv.port,
155 			check_null(argp->key.serv.proto));
156 }
157