Update.
[platform/upstream/glibc.git] / nis / nss_nisplus / nisplus-spwd.c
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>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
26
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result = NULL;
33 static nis_name tablename_val = NULL;
34 static u_long tablename_len = 0;
35
36 static enum nss_status
37 _nss_create_tablename (void)
38 {
39   if (tablename_val == NULL)
40     {
41       char buf [40 + strlen (nis_local_directory ())];
42       char *p;
43
44       p = __stpcpy (buf, "passwd.org_dir.");
45       p = __stpcpy (p, nis_local_directory ());
46       tablename_val = __strdup (buf);
47       if (tablename_val == NULL)
48         return NSS_STATUS_TRYAGAIN;
49       tablename_len = strlen (tablename_val);
50     }
51   return NSS_STATUS_SUCCESS;
52 }
53
54 enum nss_status
55 _nss_nisplus_setspent (void)
56 {
57   enum nss_status status = NSS_STATUS_SUCCESS;
58
59   __libc_lock_lock (lock);
60
61   if (result)
62     nis_freeresult (result);
63   result = NULL;
64
65   if (tablename_val == NULL)
66     status = _nss_create_tablename ();
67
68   __libc_lock_unlock (lock);
69
70   return NSS_STATUS_SUCCESS;
71 }
72
73 enum nss_status
74 _nss_nisplus_endspent (void)
75 {
76   __libc_lock_lock (lock);
77
78   if (result)
79     nis_freeresult (result);
80   result = NULL;
81
82   __libc_lock_unlock (lock);
83
84   return NSS_STATUS_SUCCESS;
85 }
86
87 static enum nss_status
88 internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen)
89 {
90   int parse_res;
91
92   /* Get the next entry until we found a correct one. */
93   do
94     {
95       nis_result *saved_res;
96
97       if (result == NULL)
98         {
99           saved_res = NULL;
100
101           if (tablename_val == NULL)
102             if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
103               return NSS_STATUS_UNAVAIL;
104
105           result = nis_first_entry (tablename_val);
106           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
107             return niserr2nss (result->status);
108         }
109       else
110         {
111           nis_result *res;
112
113           saved_res = result;
114           res = nis_next_entry (tablename_val, &result->cookie);
115           result = res;
116           if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
117             {
118               nis_freeresult (saved_res);
119               return niserr2nss (result->status);
120             }
121         }
122
123       if ((parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
124                                                  buflen)) == -1)
125         {
126           nis_freeresult (result);
127           result = saved_res;
128           return NSS_STATUS_TRYAGAIN;
129         }
130       else
131         {
132           if (saved_res)
133             nis_freeresult (saved_res);
134         }
135     } while (!parse_res);
136
137   return NSS_STATUS_SUCCESS;
138 }
139
140 enum nss_status
141 _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen)
142 {
143   int status;
144
145   __libc_lock_lock (lock);
146
147   status = internal_nisplus_getspent_r (result, buffer, buflen);
148
149   __libc_lock_unlock (lock);
150
151   return status;
152 }
153
154 enum nss_status
155 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
156                      char *buffer, size_t buflen)
157 {
158   int parse_res;
159
160   if (tablename_val == NULL)
161     if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
162       return NSS_STATUS_UNAVAIL;
163
164   if (name == NULL || strlen (name) > 8)
165     return NSS_STATUS_NOTFOUND;
166   else
167     {
168       nis_result *result;
169       char buf[strlen (name) + 24 + tablename_len];
170
171       sprintf (buf, "[name=%s],%s", name, tablename_val);
172
173       result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
174
175       if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
176         {
177           enum nss_status status = niserr2nss (result->status);
178
179           nis_freeresult (result);
180           return status;
181         }
182
183       parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen);
184       nis_freeresult (result);
185
186       if (parse_res == -1)
187         return NSS_STATUS_TRYAGAIN;
188
189       if (parse_res)
190         return NSS_STATUS_SUCCESS;
191
192       return NSS_STATUS_NOTFOUND;
193     }
194 }