1 /* Copyright (C) 1997-2005 Luke Howard.
2    This file is part of the nss_ldap library.
3    Contributed by Luke Howard, <lukeh@padl.com>, 1997.
4 
5    The nss_ldap 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 nss_ldap 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 nss_ldap 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 
21 static char rcsId[] =
22   "$Id: ldap-spwd.c,v 2.35 2008/10/30 20:49:47 lukeh Exp $";
23 
24 #include "config.h"
25 
26 #ifdef HAVE_SHADOW_H
27 
28 #ifdef HAVE_PORT_BEFORE_H
29 #include <port_before.h>
30 #endif
31 
32 #if defined(HAVE_THREAD_H) && !defined(_AIX)
33 #include <thread.h>
34 #elif defined(HAVE_PTHREAD_H)
35 #include <pthread.h>
36 #endif
37 
38 #include <stdlib.h>
39 #include <string.h>
40 #ifdef HAVE_PROT_H
41 #define _PROT_INCLUDED
42 #endif
43 #include <shadow.h>
44 
45 #ifdef HAVE_LBER_H
46 #include <lber.h>
47 #endif
48 #ifdef HAVE_LDAP_H
49 #include <ldap.h>
50 #endif
51 
52 #include "ldap-nss.h"
53 #include "ldap-spwd.h"
54 #include "util.h"
55 
56 #ifdef HAVE_PORT_AFTER_H
57 #include <port_after.h>
58 #endif
59 
60 #if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
61 
62 #ifdef HAVE_NSS_H
63 static ent_context_t *sp_context = NULL;
64 #endif
65 
66 static NSS_STATUS
_nss_ldap_parse_sp(LDAPMessage * e,ldap_state_t * pvt,void * result,char * buffer,size_t buflen)67 _nss_ldap_parse_sp (LDAPMessage * e,
68 		    ldap_state_t * pvt,
69 		    void *result, char *buffer, size_t buflen)
70 {
71   struct spwd *sp = (struct spwd *) result;
72   NSS_STATUS stat;
73   char *tmp = NULL;
74 
75   stat =
76     _nss_ldap_assign_userpassword (e, ATM (LM_SHADOW, userPassword),
77                                    &sp->sp_pwdp, &buffer, &buflen);
78   if (stat != NSS_SUCCESS)
79     return stat;
80 
81   stat =
82     _nss_ldap_assign_attrval (e, ATM (LM_SHADOW, uid), &sp->sp_namp, &buffer,
83 			      &buflen);
84   if (stat != NSS_SUCCESS)
85     return stat;
86 
87   stat =
88     _nss_ldap_assign_attrval (e, AT (shadowLastChange), &tmp, &buffer,
89 			      &buflen);
90   if (stat == NSS_SUCCESS)
91     _nss_ldap_shadow_date (tmp, -1, &sp->sp_lstchg);
92   else
93     sp->sp_lstchg = -1;
94 
95   stat =
96     _nss_ldap_assign_attrval (e, AT (shadowMax), &tmp, &buffer, &buflen);
97   if (stat == NSS_SUCCESS)
98     _nss_ldap_parse_long (tmp, -1, &sp->sp_max);
99   else
100     sp->sp_max = -1;
101 
102   stat =
103     _nss_ldap_assign_attrval (e, AT (shadowMin), &tmp, &buffer, &buflen);
104   if (stat == NSS_SUCCESS)
105     _nss_ldap_parse_long (tmp, -1, &sp->sp_min);
106   else
107     sp->sp_min = -1;
108 
109   stat =
110     _nss_ldap_assign_attrval (e, AT (shadowWarning), &tmp, &buffer,
111 			      &buflen);
112   if (stat == NSS_SUCCESS)
113     _nss_ldap_parse_long (tmp, -1, &sp->sp_warn);
114   else
115     sp->sp_warn = -1;
116 
117   stat =
118     _nss_ldap_assign_attrval (e, AT (shadowInactive), &tmp, &buffer,
119 			      &buflen);
120   if (stat == NSS_SUCCESS)
121     _nss_ldap_parse_long (tmp, -1, &sp->sp_inact);
122   else
123     sp->sp_inact = -1;
124 
125   stat =
126     _nss_ldap_assign_attrval (e, AT (shadowExpire), &tmp, &buffer,
127 			      &buflen);
128   if (stat == NSS_SUCCESS)
129     _nss_ldap_shadow_date (tmp, -1, &sp->sp_expire);
130   else
131     sp->sp_expire = -1;
132 
133   stat =
134     _nss_ldap_assign_attrval (e, AT (shadowFlag), &tmp, &buffer, &buflen);
135   if (stat == NSS_SUCCESS)
136     _nss_ldap_parse_ulong (tmp, -1, &sp->sp_flag);
137   else
138     sp->sp_flag = -1;
139 
140   _nss_ldap_shadow_handle_flag(sp);
141 
142   return NSS_SUCCESS;
143 }
144 
145 #ifdef HAVE_NSS_H
146 NSS_STATUS
_nss_ldap_getspnam_r(const char * name,struct spwd * result,char * buffer,size_t buflen,int * errnop)147 _nss_ldap_getspnam_r (const char *name,
148 		      struct spwd * result,
149 		      char *buffer, size_t buflen, int *errnop)
150 {
151   LOOKUP_NAME (name, result, buffer, buflen, errnop, _nss_ldap_filt_getspnam,
152 	       LM_SHADOW, _nss_ldap_parse_sp, LDAP_NSS_BUFLEN_DEFAULT);
153 }
154 #elif defined(HAVE_NSSWITCH_H)
155 static NSS_STATUS
_nss_ldap_getspnam_r(nss_backend_t * be,void * args)156 _nss_ldap_getspnam_r (nss_backend_t * be, void *args)
157 {
158   LOOKUP_NAME (args, _nss_ldap_filt_getspnam, LM_SHADOW, _nss_ldap_parse_sp,
159 	       LDAP_NSS_BUFLEN_DEFAULT);
160 }
161 #endif /* HAVE_NSS_H */
162 
163 #if defined(HAVE_NSS_H)
_nss_ldap_setspent(void)164 NSS_STATUS _nss_ldap_setspent (void)
165 #else
166 static NSS_STATUS
167 _nss_ldap_setspent_r (nss_backend_t * sp_context, void *args)
168 #endif
169 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
170 {
171   LOOKUP_SETENT (sp_context);
172 }
173 #endif
174 
175 #if defined(HAVE_NSS_H)
_nss_ldap_endspent(void)176 NSS_STATUS _nss_ldap_endspent (void)
177 #else
178 static NSS_STATUS
179 _nss_ldap_endspent_r (nss_backend_t * sp_context, void *args)
180 #endif
181 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
182 {
183   LOOKUP_ENDENT (sp_context);
184 }
185 #endif
186 
187 #ifdef HAVE_NSS_H
188 NSS_STATUS
_nss_ldap_getspent_r(struct spwd * result,char * buffer,size_t buflen,int * errnop)189 _nss_ldap_getspent_r (struct spwd *result,
190 		      char *buffer, size_t buflen, int *errnop)
191 {
192   LOOKUP_GETENT (sp_context, result, buffer, buflen, errnop,
193 		 _nss_ldap_filt_getspent, LM_SHADOW, _nss_ldap_parse_sp,
194 		 LDAP_NSS_BUFLEN_DEFAULT);
195 }
196 #elif defined(HAVE_NSSWITCH_H)
197 static NSS_STATUS
_nss_ldap_getspent_r(nss_backend_t * sp_context,void * args)198 _nss_ldap_getspent_r (nss_backend_t * sp_context, void *args)
199 {
200   LOOKUP_GETENT (args, sp_context, _nss_ldap_filt_getspent, LM_SHADOW,
201 		 _nss_ldap_parse_sp, LDAP_NSS_BUFLEN_DEFAULT);
202 }
203 #endif
204 
205 #ifdef HAVE_NSSWITCH_H
206 static NSS_STATUS
_nss_ldap_shadow_destr(nss_backend_t * sp_context,void * args)207 _nss_ldap_shadow_destr (nss_backend_t * sp_context, void *args)
208 {
209   return _nss_ldap_default_destr (sp_context, args);
210 }
211 
212 static nss_backend_op_t shadow_ops[] = {
213   _nss_ldap_shadow_destr,
214   _nss_ldap_endspent_r,		/* NSS_DBOP_ENDENT */
215   _nss_ldap_setspent_r,		/* NSS_DBOP_SETENT */
216   _nss_ldap_getspent_r,		/* NSS_DBOP_GETENT */
217   _nss_ldap_getspnam_r		/* NSS_DBOP_SHADOW_BYNAME */
218 };
219 
220 
221 nss_backend_t *
_nss_ldap_shadow_constr(const char * db_name,const char * src_name,const char * cfg_args)222 _nss_ldap_shadow_constr (const char *db_name,
223 			 const char *src_name, const char *cfg_args)
224 {
225   nss_ldap_backend_t *be;
226 
227   if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
228     return NULL;
229 
230   be->ops = shadow_ops;
231   be->n_ops = sizeof (shadow_ops) / sizeof (nss_backend_op_t);
232 
233   if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
234     return NULL;
235 
236   return (nss_backend_t *) be;
237 }
238 
239 #endif /* !HAVE_NSS_H */
240 #endif
241 
242 #endif /* HAVE_SHADOW_H */
243