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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * From "tsol_getrhent.c 7.6 00/09/22 SMI; TSOL 2.x"
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <nss_dbdefs.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
43 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t
*); /* from lib.c */
45 static int tsol_rh_stayopen
; /* Unsynchronized, but it affects only */
46 /* efficiency, not correctness */
47 static DEFINE_NSS_DB_ROOT(db_root
);
48 static DEFINE_NSS_GETENT(context
);
51 _nss_initf_tsol_rh(nss_db_params_t
*p
)
53 p
->name
= NSS_DBNAM_TSOL_RH
;
54 p
->default_config
= NSS_DEFCONF_TSOL_RH
;
58 tsol_getrhbyaddr(const void *addrp
, size_t len
, int af
)
62 char buf
[NSS_BUFLEN_TSOL_RH
];
64 tsol_rhstr_t
*rhstrp
= NULL
;
67 NSS_XbyY_INIT(&arg
, &result
, buf
, sizeof (buf
), str_to_rhstr
);
69 arg
.key
.hostaddr
.addr
= (const char *)addrp
;
70 arg
.key
.hostaddr
.len
= len
;
71 arg
.key
.hostaddr
.type
= af
;
72 arg
.stayopen
= tsol_rh_stayopen
;
73 arg
.h_errno
= TSOL_NOT_FOUND
;
74 arg
.status
= nss_search(&db_root
, _nss_initf_tsol_rh
,
75 NSS_DBOP_TSOL_RH_BYADDR
, &arg
);
76 rhstrp
= (tsol_rhstr_t
*)NSS_XbyY_FINI(&arg
);
79 (void) fprintf(stdout
, "tsol_getrhbyaddr %s: %s\n",
80 (char *)addrp
, rhstrp
? rhstrp
->template : "NULL");
86 return (rhstr_to_ent(rhstrp
, &err
, &errstr
));
90 tsol_setrhent(int stay
)
92 tsol_rh_stayopen
|= stay
;
93 nss_setent(&db_root
, _nss_initf_tsol_rh
, &context
);
100 nss_endent(&db_root
, _nss_initf_tsol_rh
, &context
);
101 nss_delete(&db_root
);
109 char buf
[NSS_BUFLEN_TSOL_RH
];
111 tsol_rhstr_t
*rhstrp
= NULL
;
114 NSS_XbyY_INIT(&arg
, &result
, buf
, sizeof (buf
), str_to_rhstr
);
115 /* No key, no stayopen */
116 arg
.status
= nss_getent(&db_root
, _nss_initf_tsol_rh
, &context
, &arg
);
117 rhstrp
= (tsol_rhstr_t
*)NSS_XbyY_FINI(&arg
);
120 (void) fprintf(stdout
, "tsol_getrhent: %s\n",
121 rhstrp
? rhstrp
->template : "NULL");
127 return (rhstr_to_ent(rhstrp
, &err
, &errstr
));
131 tsol_fgetrhent(FILE *f
, boolean_t
*error
)
135 char buf
[NSS_BUFLEN_TSOL_RH
];
137 tsol_rhstr_t
*rhstrp
= NULL
;
138 tsol_rhent_t
*rhentp
= NULL
;
141 NSS_XbyY_INIT(&arg
, &result
, buf
, sizeof (buf
), str_to_rhstr
);
142 _nss_XbyY_fgets(f
, &arg
);
143 rhstrp
= (tsol_rhstr_t
*)NSS_XbyY_FINI(&arg
);
146 rhentp
= rhstr_to_ent(rhstrp
, &err
, &errstr
);
147 while (rhentp
== NULL
) {
149 * Loop until we find a non-blank, non-comment line, or
150 * until EOF. No need to log blank lines, comments.
152 if (err
!= LTSNET_EMPTY
) {
153 (void) fprintf(stderr
, "%s: %.32s%s: %s\n",
154 gettext("Error parsing tnrhdb file"), errstr
,
155 (strlen(errstr
) > 32)? "...": "",
156 (char *)tsol_strerror(err
, errno
));
159 _nss_XbyY_fgets(f
, &arg
);
160 rhstrp
= (tsol_rhstr_t
*)NSS_XbyY_FINI(&arg
);
161 if (rhstrp
== NULL
) /* EOF */
163 rhentp
= rhstr_to_ent(rhstrp
, &err
, &errstr
);
169 * This is the callback routine for nss.
172 str_to_rhstr(const char *instr
, int lenstr
, void *entp
, char *buffer
,
178 char *sep
= KV_TOKEN_DELIMIT
;
179 tsol_rhstr_t
*rhstrp
= (tsol_rhstr_t
*)entp
;
181 if ((instr
>= buffer
&& (buffer
+ buflen
) > instr
) ||
182 (buffer
>= instr
&& (instr
+ lenstr
) > buffer
))
183 return (NSS_STR_PARSE_PARSE
);
184 if (lenstr
>= buflen
)
185 return (NSS_STR_PARSE_ERANGE
);
186 (void) strncpy(buffer
, instr
, buflen
);
187 str
= _strtok_escape(buffer
, sep
, &last
);
188 rhstrp
->address
= _do_unescape(str
);
190 * _do_unesape uses isspace() which removes "\n".
191 * we keep "\n" as we use it in checking for
194 if (strcmp(instr
, "\n") == 0)
195 rhstrp
->address
= "\n";
196 rhstrp
->template = _strtok_escape(NULL
, sep
, &last
);
197 if (rhstrp
->template != NULL
) {
198 len
= strlen(rhstrp
->template);
199 if (rhstrp
->template[len
- 1] == '\n')
200 rhstrp
->template[len
- 1] = '\0';
202 if (rhstrp
->address
== NULL
)
204 else if (strchr(rhstrp
->address
, ':') == NULL
)
205 rhstrp
->family
= AF_INET
;
207 rhstrp
->family
= AF_INET6
;
210 (void) fprintf(stdout
,
211 "str_to_rhstr:str - %s\taddress - %s\n\ttemplate - %s\n",
212 instr
, rhstrp
->address
? rhstrp
->address
: "NULL",
213 rhstrp
->template ? rhstrp
->template : "NULL");
216 return (NSS_STR_PARSE_SUCCESS
);
220 tsol_getrhtype(char *rhost
) {
224 char abuf
[INET6_ADDRSTRLEN
];
228 if ((hp
= getipnodebyname(rhost
, AF_INET6
,
229 AI_ALL
| AI_ADDRCONFIG
| AI_V4MAPPED
, &herr
)) == NULL
) {
233 (void) memset(&rhent
, 0, sizeof (rhent
));
234 (void) memcpy(&in6
, hp
->h_addr
, hp
->h_length
);
236 if (IN6_IS_ADDR_V4MAPPED(&in6
)) {
237 rhent
.rh_address
.ta_family
= AF_INET
;
238 IN6_V4MAPPED_TO_INADDR(&in6
, &rhent
.rh_address
.ta_addr_v4
);
239 (void) inet_ntop(AF_INET
, &rhent
.rh_address
.ta_addr_v4
, abuf
,
242 rhent
.rh_address
.ta_family
= AF_INET6
;
243 rhent
.rh_address
.ta_addr_v6
= in6
;
244 (void) inet_ntop(AF_INET6
, &in6
, abuf
, sizeof (abuf
));
247 if (tnrh(TNDB_GET
, &rhent
) != 0)
250 if (rhent
.rh_template
[0] == '\0')
253 (void) strlcpy(tp
.name
, rhent
.rh_template
, sizeof (tp
.name
));
255 if (tnrhtp(TNDB_GET
, &tp
) != 0)
258 return (tp
.host_type
);