145916cd2Sjpk /*
245916cd2Sjpk * CDDL HEADER START
345916cd2Sjpk *
445916cd2Sjpk * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
745916cd2Sjpk *
845916cd2Sjpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk * See the License for the specific language governing permissions
1145916cd2Sjpk * and limitations under the License.
1245916cd2Sjpk *
1345916cd2Sjpk * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk *
1945916cd2Sjpk * CDDL HEADER END
2045916cd2Sjpk */
2145916cd2Sjpk /*
22*909c1a33Ston * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2345916cd2Sjpk * Use is subject to license terms.
2445916cd2Sjpk *
2545916cd2Sjpk * From "tsol_getrhent.c 7.6 00/09/22 SMI; TSOL 2.x"
2645916cd2Sjpk */
2745916cd2Sjpk
2845916cd2Sjpk #include <stdio.h>
2945916cd2Sjpk #include <nss_dbdefs.h>
3045916cd2Sjpk #include <libtsnet.h>
3145916cd2Sjpk #include <sys/types.h>
3245916cd2Sjpk #include <sys/socket.h>
3345916cd2Sjpk #include <netinet/in.h>
3445916cd2Sjpk #include <arpa/inet.h>
3545916cd2Sjpk #include <string.h>
3645916cd2Sjpk #include <secdb.h>
3745916cd2Sjpk #include <nss.h>
3845916cd2Sjpk #include <libtsnet.h>
3945916cd2Sjpk #include <libintl.h>
4045916cd2Sjpk
4145916cd2Sjpk extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *); /* from lib.c */
4245916cd2Sjpk
4345916cd2Sjpk static int tsol_rh_stayopen; /* Unsynchronized, but it affects only */
4445916cd2Sjpk /* efficiency, not correctness */
4545916cd2Sjpk static DEFINE_NSS_DB_ROOT(db_root);
4645916cd2Sjpk static DEFINE_NSS_GETENT(context);
4745916cd2Sjpk
4845916cd2Sjpk static void
_nss_initf_tsol_rh(nss_db_params_t * p)4945916cd2Sjpk _nss_initf_tsol_rh(nss_db_params_t *p)
5045916cd2Sjpk {
5145916cd2Sjpk p->name = NSS_DBNAM_TSOL_RH;
5245916cd2Sjpk p->default_config = NSS_DEFCONF_TSOL_RH;
5345916cd2Sjpk }
5445916cd2Sjpk
5545916cd2Sjpk tsol_rhent_t *
tsol_getrhbyaddr(const void * addrp,size_t len,int af)5645916cd2Sjpk tsol_getrhbyaddr(const void *addrp, size_t len, int af)
5745916cd2Sjpk {
5845916cd2Sjpk int err = 0;
5945916cd2Sjpk char *errstr = NULL;
6045916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH];
6145916cd2Sjpk tsol_rhstr_t result;
6245916cd2Sjpk tsol_rhstr_t *rhstrp = NULL;
6345916cd2Sjpk nss_XbyY_args_t arg;
6445916cd2Sjpk
6545916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
6645916cd2Sjpk
6745916cd2Sjpk arg.key.hostaddr.addr = (const char *)addrp;
6845916cd2Sjpk arg.key.hostaddr.len = len;
6945916cd2Sjpk arg.key.hostaddr.type = af;
7045916cd2Sjpk arg.stayopen = tsol_rh_stayopen;
7145916cd2Sjpk arg.h_errno = TSOL_NOT_FOUND;
7245916cd2Sjpk arg.status = nss_search(&db_root, _nss_initf_tsol_rh,
7345916cd2Sjpk NSS_DBOP_TSOL_RH_BYADDR, &arg);
7445916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
7545916cd2Sjpk
7645916cd2Sjpk #ifdef DEBUG
7745916cd2Sjpk (void) fprintf(stdout, "tsol_getrhbyaddr %s: %s\n",
7845916cd2Sjpk (char *)addrp, rhstrp ? rhstrp->template : "NULL");
7945916cd2Sjpk #endif /* DEBUG */
8045916cd2Sjpk
8145916cd2Sjpk if (rhstrp == NULL)
8245916cd2Sjpk return (NULL);
8345916cd2Sjpk
8445916cd2Sjpk return (rhstr_to_ent(rhstrp, &err, &errstr));
8545916cd2Sjpk }
8645916cd2Sjpk
8745916cd2Sjpk void
tsol_setrhent(int stay)8845916cd2Sjpk tsol_setrhent(int stay)
8945916cd2Sjpk {
9045916cd2Sjpk tsol_rh_stayopen |= stay;
9145916cd2Sjpk nss_setent(&db_root, _nss_initf_tsol_rh, &context);
9245916cd2Sjpk }
9345916cd2Sjpk
9445916cd2Sjpk void
tsol_endrhent(void)9545916cd2Sjpk tsol_endrhent(void)
9645916cd2Sjpk {
9745916cd2Sjpk tsol_rh_stayopen = 0;
9845916cd2Sjpk nss_endent(&db_root, _nss_initf_tsol_rh, &context);
9945916cd2Sjpk nss_delete(&db_root);
10045916cd2Sjpk }
10145916cd2Sjpk
10245916cd2Sjpk tsol_rhent_t *
tsol_getrhent(void)10345916cd2Sjpk tsol_getrhent(void)
10445916cd2Sjpk {
10545916cd2Sjpk int err = 0;
10645916cd2Sjpk char *errstr = NULL;
10745916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH];
10845916cd2Sjpk tsol_rhstr_t result;
10945916cd2Sjpk tsol_rhstr_t *rhstrp = NULL;
11045916cd2Sjpk nss_XbyY_args_t arg;
11145916cd2Sjpk
11245916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
11345916cd2Sjpk /* No key, no stayopen */
11445916cd2Sjpk arg.status = nss_getent(&db_root, _nss_initf_tsol_rh, &context, &arg);
11545916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
11645916cd2Sjpk
11745916cd2Sjpk #ifdef DEBUG
11845916cd2Sjpk (void) fprintf(stdout, "tsol_getrhent: %s\n",
11945916cd2Sjpk rhstrp ? rhstrp->template : "NULL");
12045916cd2Sjpk #endif /* DEBUG */
12145916cd2Sjpk
12245916cd2Sjpk if (rhstrp == NULL)
12345916cd2Sjpk return (NULL);
12445916cd2Sjpk
12545916cd2Sjpk return (rhstr_to_ent(rhstrp, &err, &errstr));
12645916cd2Sjpk }
12745916cd2Sjpk
12845916cd2Sjpk tsol_rhent_t *
tsol_fgetrhent(FILE * f,boolean_t * error)129*909c1a33Ston tsol_fgetrhent(FILE *f, boolean_t *error)
13045916cd2Sjpk {
13145916cd2Sjpk int err = 0;
13245916cd2Sjpk char *errstr = NULL;
13345916cd2Sjpk char buf[NSS_BUFLEN_TSOL_RH];
13445916cd2Sjpk tsol_rhstr_t result;
13545916cd2Sjpk tsol_rhstr_t *rhstrp = NULL;
13645916cd2Sjpk tsol_rhent_t *rhentp = NULL;
13745916cd2Sjpk nss_XbyY_args_t arg;
13845916cd2Sjpk
13945916cd2Sjpk NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_rhstr);
14045916cd2Sjpk _nss_XbyY_fgets(f, &arg);
14145916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
14245916cd2Sjpk if (rhstrp == NULL)
14345916cd2Sjpk return (NULL);
14445916cd2Sjpk rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
14545916cd2Sjpk while (rhentp == NULL) {
14645916cd2Sjpk /*
14745916cd2Sjpk * Loop until we find a non-blank, non-comment line, or
14845916cd2Sjpk * until EOF. No need to log blank lines, comments.
14945916cd2Sjpk */
150*909c1a33Ston if (err != LTSNET_EMPTY) {
15145916cd2Sjpk (void) fprintf(stderr, "%s: %.32s%s: %s\n",
15245916cd2Sjpk gettext("Error parsing tnrhdb file"), errstr,
15345916cd2Sjpk (strlen(errstr) > 32)? "...": "",
15445916cd2Sjpk (char *)tsol_strerror(err, errno));
155*909c1a33Ston *error = B_TRUE;
156*909c1a33Ston }
15745916cd2Sjpk _nss_XbyY_fgets(f, &arg);
15845916cd2Sjpk rhstrp = (tsol_rhstr_t *)NSS_XbyY_FINI(&arg);
15945916cd2Sjpk if (rhstrp == NULL) /* EOF */
16045916cd2Sjpk return (NULL);
16145916cd2Sjpk rhentp = rhstr_to_ent(rhstrp, &err, &errstr);
16245916cd2Sjpk }
16345916cd2Sjpk return (rhentp);
16445916cd2Sjpk }
16545916cd2Sjpk
16645916cd2Sjpk /*
16745916cd2Sjpk * This is the callback routine for nss.
16845916cd2Sjpk */
16945916cd2Sjpk int
str_to_rhstr(const char * instr,int lenstr,void * entp,char * buffer,int buflen)17045916cd2Sjpk str_to_rhstr(const char *instr, int lenstr, void *entp, char *buffer,
17145916cd2Sjpk int buflen)
17245916cd2Sjpk {
17345916cd2Sjpk int len;
17445916cd2Sjpk char *str = NULL;
17545916cd2Sjpk char *last = NULL;
17645916cd2Sjpk char *sep = KV_TOKEN_DELIMIT;
17745916cd2Sjpk tsol_rhstr_t *rhstrp = (tsol_rhstr_t *)entp;
17845916cd2Sjpk
17945916cd2Sjpk if ((instr >= buffer && (buffer + buflen) > instr) ||
18045916cd2Sjpk (buffer >= instr && (instr + lenstr) > buffer))
18145916cd2Sjpk return (NSS_STR_PARSE_PARSE);
18245916cd2Sjpk if (lenstr >= buflen)
18345916cd2Sjpk return (NSS_STR_PARSE_ERANGE);
18445916cd2Sjpk (void) strncpy(buffer, instr, buflen);
18545916cd2Sjpk str = _strtok_escape(buffer, sep, &last);
18645916cd2Sjpk rhstrp->address = _do_unescape(str);
18745916cd2Sjpk /*
18845916cd2Sjpk * _do_unesape uses isspace() which removes "\n".
18945916cd2Sjpk * we keep "\n" as we use it in checking for
19045916cd2Sjpk * blank lines.
19145916cd2Sjpk */
19245916cd2Sjpk if (strcmp(instr, "\n") == 0)
19345916cd2Sjpk rhstrp->address = "\n";
19445916cd2Sjpk rhstrp->template = _strtok_escape(NULL, sep, &last);
19545916cd2Sjpk if (rhstrp->template != NULL) {
19645916cd2Sjpk len = strlen(rhstrp->template);
19745916cd2Sjpk if (rhstrp->template[len - 1] == '\n')
19845916cd2Sjpk rhstrp->template[len - 1] = '\0';
19945916cd2Sjpk }
20045916cd2Sjpk if (rhstrp->address == NULL)
20145916cd2Sjpk rhstrp->family = 0;
20245916cd2Sjpk else if (strchr(rhstrp->address, ':') == NULL)
20345916cd2Sjpk rhstrp->family = AF_INET;
20445916cd2Sjpk else
20545916cd2Sjpk rhstrp->family = AF_INET6;
20645916cd2Sjpk
20745916cd2Sjpk #ifdef DEBUG
20845916cd2Sjpk (void) fprintf(stdout,
20945916cd2Sjpk "str_to_rhstr:str - %s\taddress - %s\n\ttemplate - %s\n",
21045916cd2Sjpk instr, rhstrp->address ? rhstrp->address : "NULL",
21145916cd2Sjpk rhstrp->template ? rhstrp->template : "NULL");
21245916cd2Sjpk #endif /* DEBUG */
21345916cd2Sjpk
21445916cd2Sjpk return (NSS_STR_PARSE_SUCCESS);
21545916cd2Sjpk }
21645916cd2Sjpk
21745916cd2Sjpk tsol_host_type_t
tsol_getrhtype(char * rhost)21845916cd2Sjpk tsol_getrhtype(char *rhost) {
21945916cd2Sjpk int herr;
22045916cd2Sjpk struct hostent *hp;
22145916cd2Sjpk in6_addr_t in6;
22245916cd2Sjpk char abuf[INET6_ADDRSTRLEN];
22345916cd2Sjpk tsol_rhent_t rhent;
22445916cd2Sjpk tsol_tpent_t tp;
22545916cd2Sjpk
22645916cd2Sjpk if ((hp = getipnodebyname(rhost, AF_INET6,
22745916cd2Sjpk AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &herr)) == NULL) {
22845916cd2Sjpk return (UNLABELED);
22945916cd2Sjpk }
23045916cd2Sjpk
23145916cd2Sjpk (void) memset(&rhent, 0, sizeof (rhent));
23245916cd2Sjpk (void) memcpy(&in6, hp->h_addr, hp->h_length);
23345916cd2Sjpk
23445916cd2Sjpk if (IN6_IS_ADDR_V4MAPPED(&in6)) {
23545916cd2Sjpk rhent.rh_address.ta_family = AF_INET;
23645916cd2Sjpk IN6_V4MAPPED_TO_INADDR(&in6, &rhent.rh_address.ta_addr_v4);
23745916cd2Sjpk (void) inet_ntop(AF_INET, &rhent.rh_address.ta_addr_v4, abuf,
23845916cd2Sjpk sizeof (abuf));
23945916cd2Sjpk } else {
24045916cd2Sjpk rhent.rh_address.ta_family = AF_INET6;
24145916cd2Sjpk rhent.rh_address.ta_addr_v6 = in6;
24245916cd2Sjpk (void) inet_ntop(AF_INET6, &in6, abuf, sizeof (abuf));
24345916cd2Sjpk }
24445916cd2Sjpk
24545916cd2Sjpk if (tnrh(TNDB_GET, &rhent) != 0)
24645916cd2Sjpk return (UNLABELED);
24745916cd2Sjpk
24845916cd2Sjpk if (rhent.rh_template[0] == '\0')
24945916cd2Sjpk return (UNLABELED);
25045916cd2Sjpk
25145916cd2Sjpk (void) strlcpy(tp.name, rhent.rh_template, sizeof (tp.name));
25245916cd2Sjpk
25345916cd2Sjpk if (tnrhtp(TNDB_GET, &tp) != 0)
25445916cd2Sjpk return (UNLABELED);
25545916cd2Sjpk
25645916cd2Sjpk return (tp.host_type);
25745916cd2Sjpk }
258