"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/ldap-pwd.c" (6 Nov 2009, 8870 Bytes) of package /linux/privat/old/nss_ldap-265.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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-pwd.c,v 2.48 2008/10/30 20:49:47 lukeh Exp $";
   23 
   24 #include "config.h"
   25 
   26 #ifdef HAVE_PORT_BEFORE_H
   27 #include <port_before.h>
   28 #endif
   29 
   30 #if defined(HAVE_THREAD_H) && !defined(_AIX)
   31 #include <thread.h>
   32 #elif defined(HAVE_PTHREAD_H)
   33 #include <pthread.h>
   34 #endif
   35 
   36 #include <stdlib.h>
   37 #include <sys/types.h>
   38 #include <sys/param.h>
   39 #include <string.h>
   40 #include <pwd.h>
   41 
   42 #ifdef HAVE_LBER_H
   43 #include <lber.h>
   44 #endif
   45 #ifdef HAVE_LDAP_H
   46 #include <ldap.h>
   47 #endif
   48 
   49 #include "ldap-nss.h"
   50 #include "ldap-pwd.h"
   51 #include "util.h"
   52 
   53 #ifdef HAVE_PORT_AFTER_H
   54 #include <port_after.h>
   55 #endif
   56 
   57 #ifdef HAVE_NSS_H
   58 static ent_context_t *pw_context = NULL;
   59 #endif
   60 
   61 static INLINE NSS_STATUS _nss_ldap_assign_emptystring (char **valptr,
   62                                char **buffer,
   63                                size_t * buflen);
   64 
   65 static INLINE NSS_STATUS
   66 _nss_ldap_assign_emptystring (char **valptr, char **buffer, size_t * buflen)
   67 {
   68   if (*buflen < 2)
   69     return NSS_TRYAGAIN;
   70 
   71   *valptr = *buffer;
   72 
   73   **valptr = '\0';
   74 
   75   (*buffer)++;
   76   (*buflen)--;
   77 
   78   return NSS_SUCCESS;
   79 }
   80 
   81 static NSS_STATUS
   82 _nss_ldap_parse_pw (LDAPMessage * e,
   83             ldap_state_t * pvt,
   84             void *result, char *buffer, size_t buflen)
   85 {
   86   struct passwd *pw = (struct passwd *) result;
   87   char *uid, *gid;
   88   NSS_STATUS stat;
   89   char tmpbuf[sizeof "-4294967295"];
   90   size_t tmplen;
   91   char *tmp;
   92 
   93   if (_nss_ldap_oc_check (e, "shadowAccount") == NSS_SUCCESS)
   94     {
   95       /* don't include password for shadowAccount */
   96       if (buflen < 3)
   97     return NSS_TRYAGAIN;
   98 
   99       pw->pw_passwd = buffer;
  100       strcpy (buffer, "x");
  101       buffer += 2;
  102       buflen -= 2;
  103     }
  104   else
  105     {
  106       stat =
  107     _nss_ldap_assign_userpassword (e, ATM (LM_PASSWD, userPassword),
  108                        &pw->pw_passwd, &buffer, &buflen);
  109       if (stat != NSS_SUCCESS)
  110     return stat;
  111     }
  112 
  113   stat =
  114     _nss_ldap_assign_attrval (e, ATM (LM_PASSWD, uid), &pw->pw_name, &buffer,
  115                   &buflen);
  116   if (stat != NSS_SUCCESS)
  117     return stat;
  118 
  119   tmp = tmpbuf;
  120   tmplen = sizeof (tmpbuf);
  121   stat =
  122     _nss_ldap_assign_attrval (e, AT (uidNumber), &uid, &tmp, &tmplen);
  123   if (stat != NSS_SUCCESS)
  124     return stat;
  125   if (*uid == '\0')
  126     pw->pw_uid = UID_NOBODY;
  127   else
  128     {
  129       stat =
  130         _nss_ldap_parse_uid_t (uid, UID_NOBODY, &pw->pw_uid);
  131       if (stat != NSS_SUCCESS)
  132         return stat;
  133     }
  134 
  135   tmp = tmpbuf;
  136   tmplen = sizeof (tmpbuf);
  137   stat =
  138     _nss_ldap_assign_attrval (e, ATM (LM_PASSWD, gidNumber), &gid, &tmp,
  139                               &tmplen);
  140   if (stat != NSS_SUCCESS)
  141     return stat;
  142   if (*gid == '\0')
  143     pw->pw_gid = GID_NOBODY;
  144   else
  145     {
  146       stat =
  147         _nss_ldap_parse_gid_t (gid, GID_NOBODY, &pw->pw_gid);
  148       if (stat != NSS_SUCCESS)
  149         return stat;
  150     }
  151 
  152   stat =
  153     _nss_ldap_assign_attrval (e, AT (gecos), &pw->pw_gecos, &buffer,
  154                   &buflen);
  155   if (stat != NSS_SUCCESS)
  156     {
  157       pw->pw_gecos = NULL;
  158       stat =
  159     _nss_ldap_assign_attrval (e, ATM (LM_PASSWD, cn), &pw->pw_gecos,
  160                                   &buffer, &buflen);
  161       if (stat != NSS_SUCCESS)
  162     return stat;
  163     }
  164 
  165   stat =
  166     _nss_ldap_assign_attrval (e, AT (homeDirectory), &pw->pw_dir, &buffer,
  167                   &buflen);
  168   if (stat != NSS_SUCCESS)
  169     (void) _nss_ldap_assign_emptystring (&pw->pw_dir, &buffer, &buflen);
  170 
  171   stat =
  172     _nss_ldap_assign_attrval (e, AT (loginShell), &pw->pw_shell, &buffer,
  173                   &buflen);
  174   if (stat != NSS_SUCCESS)
  175     (void) _nss_ldap_assign_emptystring (&pw->pw_shell, &buffer, &buflen);
  176 
  177 #ifdef HAVE_NSSWITCH_H
  178   stat =
  179     _nss_ldap_assign_attrval (e, ATM (LM_PASSWD, description),
  180                               &pw->pw_comment, &buffer, &buflen);
  181   if (stat != NSS_SUCCESS)
  182     {
  183       /* 
  184        * Fix for recall #233
  185        */
  186       pw->pw_comment = pw->pw_gecos;
  187     }
  188   (void) _nss_ldap_assign_emptystring (&pw->pw_age, &buffer, &buflen);
  189 #endif /* HAVE_NSSWITCH_H */
  190 
  191 #ifdef HAVE_PASSWD_PW_CHANGE
  192  tmp = NULL;
  193   stat =
  194     _nss_ldap_assign_attrval (e, AT (shadowMax), &tmp, &buffer, &buflen);
  195   if (stat == NSS_SUCCESS)
  196     _nss_ldap_parse_long (tmp, 0, &pw->pw_change);
  197   else
  198     pw->pw_change = 0;
  199 
  200   if (pw->pw_change > 0)
  201     {
  202       long sp_change;
  203       tmp = NULL;
  204       stat =
  205         _nss_ldap_assign_attrval (e, AT (shadowLastChange), &tmp, &buffer,
  206                           &buflen);
  207       if (stat == NSS_SUCCESS)
  208     {
  209       stat = _nss_ldap_parse_long(tmp, 0, &sp_change);
  210       if (stat == NSS_SUCCESS)
  211         {
  212           pw->pw_change += sp_change;
  213           pw->pw_change *= (24 * 60 * 60);
  214         }
  215       else
  216         pw->pw_change = 0;
  217     }
  218       else
  219     pw->pw_change = 0;
  220     }
  221 #endif /* HAVE_PASSWD_PW_CHANGE */
  222 
  223 #ifdef HAVE_PASSWD_PW_EXPIRE
  224   tmp = NULL;
  225   stat =
  226     _nss_ldap_assign_attrval (e, AT (shadowExpire), &tmp, &buffer, &buflen);
  227   if (stat == NSS_SUCCESS)
  228     {
  229       _nss_ldap_parse_long (tmp, 0, &pw->pw_expire);
  230       pw->pw_expire *= (24 * 60 * 60);
  231     }
  232   else
  233     pw->pw_expire = 0;
  234 #endif /* HAVE_PASSWD_PW_EXPIRE */
  235 
  236   return NSS_SUCCESS;
  237 }
  238 
  239 #ifdef HAVE_NSS_H
  240 NSS_STATUS
  241 _nss_ldap_getpwnam_r (const char *name,
  242               struct passwd * result,
  243               char *buffer, size_t buflen, int *errnop)
  244 {
  245   LOOKUP_NAME (name, result, buffer, buflen, errnop, _nss_ldap_filt_getpwnam,
  246            LM_PASSWD, _nss_ldap_parse_pw, LDAP_NSS_BUFLEN_DEFAULT);
  247 }
  248 #elif defined(HAVE_NSSWITCH_H)
  249 static NSS_STATUS
  250 _nss_ldap_getpwnam_r (nss_backend_t * be, void *args)
  251 {
  252   LOOKUP_NAME (args, _nss_ldap_filt_getpwnam, LM_PASSWD, _nss_ldap_parse_pw,
  253            LDAP_NSS_BUFLEN_DEFAULT);
  254 }
  255 #endif /* HAVE_NSS_H */
  256 
  257 #ifdef HAVE_NSS_H
  258 NSS_STATUS
  259 _nss_ldap_getpwuid_r (uid_t uid,
  260               struct passwd *result,
  261               char *buffer, size_t buflen, int *errnop)
  262 {
  263   LOOKUP_NUMBER (uid, result, buffer, buflen, errnop, _nss_ldap_filt_getpwuid,
  264          LM_PASSWD, _nss_ldap_parse_pw, LDAP_NSS_BUFLEN_DEFAULT);
  265 }
  266 #elif defined(HAVE_NSSWITCH_H)
  267 static NSS_STATUS
  268 _nss_ldap_getpwuid_r (nss_backend_t * be, void *args)
  269 {
  270   LOOKUP_NUMBER (args, key.uid, _nss_ldap_filt_getpwuid, LM_PASSWD,
  271          _nss_ldap_parse_pw, LDAP_NSS_BUFLEN_DEFAULT);
  272 }
  273 #endif
  274 
  275 #if defined(HAVE_NSS_H)
  276 NSS_STATUS
  277 _nss_ldap_setpwent (void)
  278 {
  279   LOOKUP_SETENT (pw_context);
  280 }
  281 #elif defined(HAVE_NSSWITCH_H)
  282 static NSS_STATUS
  283 _nss_ldap_setpwent_r (nss_backend_t * be, void *args)
  284 {
  285   LOOKUP_SETENT (be);
  286 }
  287 #endif
  288 
  289 #if defined(HAVE_NSS_H)
  290 NSS_STATUS
  291 _nss_ldap_endpwent (void)
  292 {
  293   LOOKUP_ENDENT (pw_context);
  294 }
  295 #elif defined(HAVE_NSSWITCH_H)
  296 static NSS_STATUS
  297 _nss_ldap_endpwent_r (nss_backend_t * be, void *args)
  298 {
  299   LOOKUP_ENDENT (be);
  300 }
  301 #endif
  302 
  303 #ifdef HAVE_NSS_H
  304 NSS_STATUS
  305 _nss_ldap_getpwent_r (struct passwd *result,
  306               char *buffer, size_t buflen, int *errnop)
  307 {
  308   LOOKUP_GETENT (pw_context, result, buffer, buflen, errnop,
  309          _nss_ldap_filt_getpwent, LM_PASSWD, _nss_ldap_parse_pw,
  310          LDAP_NSS_BUFLEN_DEFAULT);
  311 }
  312 #elif defined(HAVE_NSSWITCH_H)
  313 static NSS_STATUS
  314 _nss_ldap_getpwent_r (nss_backend_t * be, void *args)
  315 {
  316   LOOKUP_GETENT (args, be, _nss_ldap_filt_getpwent, LM_PASSWD,
  317          _nss_ldap_parse_pw, LDAP_NSS_BUFLEN_DEFAULT);
  318 }
  319 #endif
  320 
  321 #ifdef HAVE_NSSWITCH_H
  322 static NSS_STATUS
  323 _nss_ldap_passwd_destr (nss_backend_t * pw_context, void *args)
  324 {
  325   return _nss_ldap_default_destr (pw_context, args);
  326 }
  327 
  328 static nss_backend_op_t passwd_ops[] = {
  329   _nss_ldap_passwd_destr,
  330   _nss_ldap_endpwent_r,     /* NSS_DBOP_ENDENT */
  331   _nss_ldap_setpwent_r,     /* NSS_DBOP_SETENT */
  332   _nss_ldap_getpwent_r,     /* NSS_DBOP_GETENT */
  333   _nss_ldap_getpwnam_r,     /* NSS_DBOP_PASSWD_BYNAME */
  334   _nss_ldap_getpwuid_r      /* NSS_DBOP_PASSWD_BYUID */
  335 };
  336 
  337 nss_backend_t *
  338 _nss_ldap_passwd_constr (const char *db_name,
  339              const char *src_name, const char *cfg_args)
  340 {
  341   nss_ldap_backend_t *be;
  342 
  343   if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
  344     return NULL;
  345 
  346   be->ops = passwd_ops;
  347   be->n_ops = sizeof (passwd_ops) / sizeof (nss_backend_op_t);
  348 
  349   if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
  350     return NULL;
  351 
  352   return (nss_backend_t *) be;
  353 }
  354 
  355 
  356 #endif /* !HAVE_NSS_H */
  357 
  358 #ifdef HAVE_IRS_H
  359 #include "irs-pwd.c"
  360 #endif /* HAVE_IRS_H */