Merge branch 'less_closed'
[unleashed.git] / usr / src / lib / libtsnet / common / tsol_gettpent.c
blob467bd8fccbcd38c811c9ab67ef497b732487c7ee
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * From "tsol_gettpent.c 7.13 00/10/13 SMI; TSOL 2.x"
28 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <stdio.h>
31 #include <string.h>
32 #include <nss_dbdefs.h>
33 #include <libtsnet.h>
34 #include <secdb.h>
35 #include <nss.h>
36 #include <libintl.h>
38 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *); /* from lib.c */
40 static int tsol_tp_stayopen; /* Unsynchronized, but it affects only */
41 /* efficiency, not correctness */
42 static DEFINE_NSS_DB_ROOT(db_root);
43 static DEFINE_NSS_GETENT(context);
46 static void
47 _nss_initf_tsol_tp(nss_db_params_t *p)
49 p->name = NSS_DBNAM_TSOL_TP;
50 p->default_config = NSS_DEFCONF_TSOL_TP;
53 tsol_tpent_t *
54 tsol_gettpbyname(const char *name)
56 int err = 0;
57 char *errstr = NULL;
58 char buf[NSS_BUFLEN_TSOL_TP];
59 tsol_tpstr_t result;
60 tsol_tpstr_t *tpstrp = NULL;
61 nss_XbyY_args_t arg;
63 NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
65 arg.key.name = name;
66 arg.stayopen = tsol_tp_stayopen;
67 arg.h_errno = TSOL_NOT_FOUND;
68 arg.status = nss_search(&db_root, _nss_initf_tsol_tp,
69 NSS_DBOP_TSOL_TP_BYNAME, &arg);
70 tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
72 #ifdef DEBUG
73 (void) fprintf(stdout, "tsol_gettpbyname %s: %s\n",
74 name, tpstrp ? tpstrp->template : "NULL");
75 #endif /* DEBUG */
77 if (tpstrp == NULL)
78 return (NULL);
80 return (tpstr_to_ent(tpstrp, &err, &errstr));
83 void
84 tsol_settpent(int stay)
86 tsol_tp_stayopen |= stay;
87 nss_setent(&db_root, _nss_initf_tsol_tp, &context);
90 void
91 tsol_endtpent(void)
93 tsol_tp_stayopen = 0;
94 nss_endent(&db_root, _nss_initf_tsol_tp, &context);
95 nss_delete(&db_root);
98 tsol_tpent_t *
99 tsol_gettpent(void)
101 int err = 0;
102 char *errstr = NULL;
103 char buf[NSS_BUFLEN_TSOL_TP];
104 tsol_tpstr_t result;
105 tsol_tpstr_t *tpstrp = NULL;
106 nss_XbyY_args_t arg;
108 NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
109 /* No key, no stayopen */
110 arg.status = nss_getent(&db_root, _nss_initf_tsol_tp, &context, &arg);
111 tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
113 #ifdef DEBUG
114 (void) fprintf(stdout, "tsol_gettpent: %s\n",
115 tpstrp ? tpstrp->template : "NULL");
116 #endif /* DEBUG */
118 if (tpstrp == NULL)
119 return (NULL);
121 return (tpstr_to_ent(tpstrp, &err, &errstr));
124 tsol_tpent_t *
125 tsol_fgettpent(FILE *f, boolean_t *error)
127 int err = 0;
128 char *errstr = NULL;
129 char buf[NSS_BUFLEN_TSOL_TP];
130 tsol_tpstr_t result;
131 tsol_tpstr_t *tpstrp = NULL;
132 tsol_tpent_t *tpentp = NULL;
133 nss_XbyY_args_t arg;
135 NSS_XbyY_INIT(&arg, &result, buf, sizeof (buf), str_to_tpstr);
136 _nss_XbyY_fgets(f, &arg);
137 tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
138 if (tpstrp == NULL)
139 return (NULL);
140 tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
141 while (tpentp == NULL) {
143 * Loop until we find a non-blank, non-comment line, or
144 * until EOF. No need to log blank lines, comments.
146 if (err != LTSNET_EMPTY) {
147 (void) fprintf(stderr, "%s: %.32s%s: %s\n",
148 gettext("Error parsing tnrhtp file"), errstr,
149 (strlen(errstr) > 32)? "...": "",
150 (char *)tsol_strerror(err, errno));
151 *error = B_TRUE;
153 _nss_XbyY_fgets(f, &arg);
154 tpstrp = (tsol_tpstr_t *)NSS_XbyY_FINI(&arg);
155 if (tpstrp == NULL) /* EOF */
156 return (NULL);
157 tpentp = tpstr_to_ent(tpstrp, &err, &errstr);
159 return (tpentp);
163 * This is the callback routine for nss. It just wraps the tsol_sgettpent
164 * parser.
167 str_to_tpstr(const char *instr, int lenstr, void *entp, char *buffer,
168 int buflen)
170 int len;
171 char *last = NULL;
172 char *sep = KV_TOKEN_DELIMIT;
173 tsol_tpstr_t *tpstrp = (tsol_tpstr_t *)entp;
175 if ((instr >= buffer && (buffer + buflen) > instr) ||
176 (buffer >= instr && (instr + lenstr) > buffer))
177 return (NSS_STR_PARSE_PARSE);
178 if (lenstr >= buflen)
179 return (NSS_STR_PARSE_ERANGE);
180 (void) strncpy(buffer, instr, buflen);
181 tpstrp->template = _strtok_escape(buffer, sep, &last);
182 tpstrp->attrs = _strtok_escape(NULL, sep, &last);
183 if (tpstrp->attrs != NULL) {
184 len = strlen(tpstrp->attrs);
185 if (tpstrp->attrs[len - 1] == '\n')
186 tpstrp->attrs[len - 1] = '\0';
189 #ifdef DEBUG
190 (void) fprintf(stdout,
191 "str_to_tpstr:\nstr - %s\n\ttemplate - %s\n\tattrs - %s\n",
192 instr, tpstrp->template ? tpstrp->template : "NULL",
193 tpstrp->attrs ? tpstrp->attrs : "NULL");
194 #endif /* DEBUG */
196 return (NSS_STR_PARSE_SUCCESS);