import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / getuserattr.c
blob33b9d9416dc797d995f7f1c7e3a884326c616b95
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
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include "mt.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #include <nss_dbdefs.h>
35 #include <string.h>
36 #include <user_attr.h>
38 /* externs from libc */
39 extern void _nss_XbyY_fgets(FILE *, nss_XbyY_args_t *);
40 /* externs from parse.c */
41 extern char *_strtok_escape(char *, char *, char **);
44 static int userattr_stayopen;
47 * Unsynchronized, but it affects only
48 * efficiency, not correctness
51 static DEFINE_NSS_DB_ROOT(db_root);
52 static DEFINE_NSS_GETENT(context);
55 void
56 _nss_initf_userattr(nss_db_params_t *p)
58 p->name = NSS_DBNAM_USERATTR;
59 p->config_name = NSS_DBNAM_PASSWD; /* use config for "passwd" */
60 p->default_config = NSS_DEFCONF_USERATTR;
65 * Return values: 0 = success, 1 = parse error, 2 = erange ...
66 * The structure pointer passed in is a structure in the caller's space
67 * wherein the field pointers would be set to areas in the buffer if
68 * need be. instring and buffer should be separate areas.
70 int
71 str2userattr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
73 char *last = NULL;
74 char *sep = KV_TOKEN_DELIMIT;
75 userstr_t *user = (userstr_t *)ent;
77 if (lenstr >= buflen)
78 return (NSS_STR_PARSE_ERANGE);
80 if (instr != buffer)
81 (void) strncpy(buffer, instr, buflen);
84 * Remove newline that nis (yp_match) puts at the
85 * end of the entry it retrieves from the map.
87 if (buffer[lenstr] == '\n') {
88 buffer[lenstr] = '\0';
91 /* quick exit do not entry fill if not needed */
92 if (ent == NULL)
93 return (NSS_STR_PARSE_SUCCESS);
95 user->name = _strtok_escape(buffer, sep, &last);
96 user->qualifier = _strtok_escape(NULL, sep, &last);
97 user->res1 = _strtok_escape(NULL, sep, &last);
98 user->res2 = _strtok_escape(NULL, sep, &last);
99 user->attr = _strtok_escape(NULL, sep, &last);
101 return (0);
105 void
106 _setuserattr(void)
108 userattr_stayopen = 0;
109 nss_setent(&db_root, _nss_initf_userattr, &context);
113 void
114 _enduserattr(void)
116 userattr_stayopen = 0;
117 nss_endent(&db_root, _nss_initf_userattr, &context);
118 nss_delete(&db_root);
122 userstr_t *
123 _getuserattr(userstr_t *result, char *buffer, int buflen, int *h_errnop)
125 nss_XbyY_args_t arg;
126 nss_status_t res;
128 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
129 res = nss_getent(&db_root, _nss_initf_userattr, &context, &arg);
130 arg.status = res;
131 *h_errnop = arg.h_errno;
132 return ((userstr_t *)NSS_XbyY_FINI(&arg));
136 userstr_t *
137 _fgetuserattr(FILE *f, userstr_t *result, char *buffer, int buflen)
139 nss_XbyY_args_t arg;
141 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
142 _nss_XbyY_fgets(f, &arg);
143 return ((userstr_t *)NSS_XbyY_FINI(&arg));
148 userstr_t *
149 _getusernam(const char *name, userstr_t *result, char *buffer, int buflen,
150 int *errnop)
152 nss_XbyY_args_t arg;
153 nss_status_t res;
155 NSS_XbyY_INIT(&arg, result, buffer, buflen, str2userattr);
156 arg.key.name = name;
157 arg.stayopen = userattr_stayopen;
158 res = nss_search(&db_root, _nss_initf_userattr,
159 NSS_DBOP_USERATTR_BYNAME, &arg);
160 arg.status = res;
161 *errnop = arg.h_errno;
162 return ((userstr_t *)NSS_XbyY_FINI(&arg));