]>
Commit | Line | Data |
---|---|---|
e61abf83 UD |
1 | /* Copyright (C) 1997 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. | |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public License as | |
7 | published by the Free Software Foundation; either version 2 of the | |
8 | License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
20 | #include <nss.h> | |
21 | #include <errno.h> | |
22 | #include <shadow.h> | |
23 | #include <string.h> | |
5107cf1d | 24 | #include <bits/libc-lock.h> |
e61abf83 UD |
25 | #include <rpcsvc/nis.h> |
26 | #include <rpcsvc/nislib.h> | |
27 | ||
28 | #include "nss-nisplus.h" | |
2d7da676 | 29 | #include "nisplus-parser.h" |
e61abf83 UD |
30 | |
31 | __libc_lock_define_initialized (static, lock) | |
32 | ||
33 | static nis_result *result = NULL; | |
2d7da676 UD |
34 | static nis_name tablename_val = NULL; |
35 | static u_long tablename_len = 0; | |
e61abf83 | 36 | |
2d7da676 UD |
37 | static enum nss_status |
38 | _nss_create_tablename (void) | |
e61abf83 | 39 | { |
2d7da676 | 40 | if (tablename_val == NULL) |
714a562f | 41 | { |
2d7da676 UD |
42 | char buf [40 + strlen (nis_local_directory ())]; |
43 | char *p; | |
44 | ||
45 | p = stpcpy (buf, "passwd.org_dir."); | |
46 | p = stpcpy (p, nis_local_directory ()); | |
47 | tablename_val = strdup (buf); | |
48 | if (tablename_val == NULL) | |
49 | return NSS_STATUS_TRYAGAIN; | |
50 | tablename_len = strlen (tablename_val); | |
714a562f | 51 | } |
2d7da676 | 52 | return NSS_STATUS_SUCCESS; |
e61abf83 UD |
53 | } |
54 | ||
55 | enum nss_status | |
56 | _nss_nisplus_setspent (void) | |
57 | { | |
2d7da676 UD |
58 | enum nss_status status = NSS_STATUS_SUCCESS; |
59 | ||
e61abf83 UD |
60 | __libc_lock_lock (lock); |
61 | ||
62 | if (result) | |
63 | nis_freeresult (result); | |
64 | result = NULL; | |
2d7da676 UD |
65 | |
66 | if (tablename_val == NULL) | |
67 | status = _nss_create_tablename (); | |
e61abf83 UD |
68 | |
69 | __libc_lock_unlock (lock); | |
70 | ||
71 | return NSS_STATUS_SUCCESS; | |
72 | } | |
73 | ||
74 | enum nss_status | |
75 | _nss_nisplus_endspent (void) | |
76 | { | |
77 | __libc_lock_lock (lock); | |
78 | ||
79 | if (result) | |
80 | nis_freeresult (result); | |
81 | result = NULL; | |
e61abf83 UD |
82 | |
83 | __libc_lock_unlock (lock); | |
84 | ||
85 | return NSS_STATUS_SUCCESS; | |
86 | } | |
87 | ||
88 | static enum nss_status | |
89 | internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen) | |
90 | { | |
91 | int parse_res; | |
92 | ||
93 | /* Get the next entry until we found a correct one. */ | |
94 | do | |
95 | { | |
96 | if (result == NULL) | |
97 | { | |
2d7da676 UD |
98 | if (tablename_val == NULL) |
99 | if (_nss_create_tablename () != NSS_STATUS_SUCCESS) | |
100 | return NSS_STATUS_UNAVAIL; | |
e61abf83 | 101 | |
2d7da676 | 102 | result = nis_first_entry (tablename_val); |
e61abf83 UD |
103 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) |
104 | return niserr2nss (result->status); | |
105 | } | |
106 | else | |
107 | { | |
108 | nis_result *res; | |
109 | ||
2d7da676 | 110 | res = nis_next_entry (tablename_val, &result->cookie); |
e61abf83 UD |
111 | nis_freeresult (result); |
112 | result = res; | |
113 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
114 | return niserr2nss (result->status); | |
115 | } | |
116 | ||
117 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); | |
118 | } while (!parse_res); | |
119 | ||
120 | return NSS_STATUS_SUCCESS; | |
121 | } | |
122 | ||
123 | enum nss_status | |
124 | _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen) | |
125 | { | |
126 | int status; | |
127 | ||
128 | __libc_lock_lock (lock); | |
129 | ||
130 | status = internal_nisplus_getspent_r (result, buffer, buflen); | |
131 | ||
132 | __libc_lock_unlock (lock); | |
133 | ||
134 | return status; | |
135 | } | |
136 | ||
137 | enum nss_status | |
138 | _nss_nisplus_getspnam_r (const char *name, struct spwd *sp, | |
139 | char *buffer, size_t buflen) | |
140 | { | |
141 | int parse_res; | |
142 | ||
2d7da676 UD |
143 | if (tablename_val == NULL) |
144 | if (_nss_create_tablename () != NSS_STATUS_SUCCESS) | |
145 | return NSS_STATUS_UNAVAIL; | |
146 | ||
e61abf83 UD |
147 | if (name == NULL || strlen (name) > 8) |
148 | return NSS_STATUS_NOTFOUND; | |
149 | else | |
150 | { | |
151 | nis_result *result; | |
2d7da676 | 152 | char buf[strlen (name) + 24 + tablename_len]; |
e61abf83 | 153 | |
2d7da676 | 154 | sprintf (buf, "[name=%s],%s", name, tablename_val); |
e61abf83 | 155 | |
2d7da676 | 156 | result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); |
e61abf83 UD |
157 | |
158 | if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) | |
159 | { | |
160 | enum nss_status status = niserr2nss (result->status); | |
161 | ||
162 | nis_freeresult (result); | |
163 | return status; | |
164 | } | |
165 | ||
166 | parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen); | |
167 | ||
168 | nis_freeresult (result); | |
169 | ||
170 | if (parse_res) | |
171 | return NSS_STATUS_SUCCESS; | |
172 | ||
173 | if (!parse_res && errno == ERANGE) | |
174 | return NSS_STATUS_TRYAGAIN; | |
175 | else | |
176 | return NSS_STATUS_NOTFOUND; | |
177 | } | |
178 | } |