merge from 2.2
[Samba/gbeck.git] / source / passdb / ldap.c
blobb520f521bdddbb36287ed6cc319d94c3424f75e7
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 LDAP protocol helper functions for SAMBA
5 Copyright (C) Jean François Micouleau 1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #ifdef WITH_LDAP
25 #include "includes.h"
27 #include <lber.h>
28 #include <ldap.h>
30 #define ADD_USER 1
31 #define MODIFY_USER 2
33 extern int DEBUGLEVEL;
35 /*******************************************************************
36 open a connection to the ldap serve.
37 ******************************************************************/
38 static BOOL ldap_open_connection(LDAP **ldap_struct)
40 if ( (*ldap_struct = ldap_open(lp_ldap_server(),lp_ldap_port()) ) == NULL)
42 DEBUG( 0, ( "The LDAP server is not responding !\n" ) );
43 return( False );
45 DEBUG(2,("ldap_open_connection: connection opened\n"));
46 return (True);
50 /*******************************************************************
51 connect anonymously to the ldap server.
52 FIXME: later (jfm)
53 ******************************************************************/
54 static BOOL ldap_connect_anonymous(LDAP *ldap_struct)
56 if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
58 DEBUG( 0, ( "Couldn't bind to the LDAP server !\n" ) );
59 return(False);
61 return (True);
65 /*******************************************************************
66 connect to the ldap server under system privileg.
67 ******************************************************************/
68 static BOOL ldap_connect_system(LDAP *ldap_struct)
70 if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
72 DEBUG( 0, ( "Couldn't bind to the LDAP server!\n" ) );
73 return(False);
75 DEBUG(2,("ldap_connect_system: succesful connection to the LDAP server\n"));
76 return (True);
79 /*******************************************************************
80 connect to the ldap server under a particular user.
81 ******************************************************************/
82 static BOOL ldap_connect_user(LDAP *ldap_struct, char *user, char *password)
84 if ( ldap_simple_bind_s(ldap_struct,lp_ldap_root(),lp_ldap_rootpasswd()) ! = LDAP_SUCCESS)
86 DEBUG( 0, ( "Couldn't bind to the LDAP server !\n" ) );
87 return(False);
89 DEBUG(2,("ldap_connect_user: succesful connection to the LDAP server\n"));
90 return (True);
93 /*******************************************************************
94 run the search by name.
95 ******************************************************************/
96 static BOOL ldap_search_one_user(LDAP *ldap_struct, char *filter, LDAPMessage **result)
98 int scope = LDAP_SCOPE_ONELEVEL;
99 int rc;
101 DEBUG(2,("ldap_search_one_user: searching for:[%s]\n", filter));
103 rc = ldap_search_s(ldap_struct, lp_ldap_suffix(), scope, filter, NULL, 0, result);
105 if (rc ! = LDAP_SUCCESS )
107 DEBUG( 0, ( "Problem during the LDAP search\n" ) );
108 return(False);
110 return (True);
113 /*******************************************************************
114 run the search by name.
115 ******************************************************************/
116 static BOOL ldap_search_one_user_by_name(LDAP *ldap_struct, char *user, LDAPMessage **result)
118 pstring filter;
120 in the filter expression, replace %u with the real name
121 so in ldap filter, %u MUST exist :-)
123 pstrcpy(filter,lp_ldap_filter());
124 pstring_sub(filter,"%u",user);
126 if ( !ldap_search_one_user(ldap_struct, filter, result) )
128 return(False);
130 return (True);
133 /*******************************************************************
134 run the search by uid.
135 ******************************************************************/
136 static BOOL ldap_search_one_user_by_uid(LDAP *ldap_struct, int uid, LDAPMessage **result)
138 pstring filter;
140 slprintf(filter, sizeof(pstring)-1, "uidAccount = %d", uid);
142 if ( !ldap_search_one_user(ldap_struct, filter, result) )
144 return(False);
146 return (True);
149 /*******************************************************************
150 search an attribute and return the first value found.
151 ******************************************************************/
152 static void get_single_attribute(LDAP *ldap_struct, LDAPMessage *entry, char *attribute, char *value)
154 char **valeurs;
156 if ( (valeurs = ldap_get_values(ldap_struct, entry, attribute)) ! = NULL)
158 pstrcpy(value, valeurs[0]);
159 ldap_value_free(valeurs);
160 DEBUG(3,("get_single_attribute: [%s] = [%s]\n", attribute, value));
162 else
164 value = NULL;
168 /*******************************************************************
169 check if the returned entry is a sambaAccount objectclass.
170 ******************************************************************/
171 static BOOL ldap_check_user(LDAP *ldap_struct, LDAPMessage *entry)
173 BOOL sambaAccount = False;
174 char **valeur;
175 int i;
177 DEBUG(2,("ldap_check_user: "));
178 valeur = ldap_get_values(ldap_struct, entry, "objectclass");
179 if (valeur! = NULL)
181 for (i = 0;valeur[i]! = NULL;i++)
183 if (!strcmp(valeur[i],"sambaAccount")) sambaAccount = True;
186 DEBUG(2,("%s\n",sambaAccount?"yes":"no"));
187 ldap_value_free(valeur);
188 return (sambaAccount);
191 /*******************************************************************
192 check if the returned entry is a sambaTrust objectclass.
193 ******************************************************************/
194 static BOOL ldap_check_trust(LDAP *ldap_struct, LDAPMessage *entry)
196 BOOL sambaTrust = False;
197 char **valeur;
198 int i;
200 DEBUG(2,("ldap_check_trust: "));
201 valeur = ldap_get_values(ldap_struct, entry, "objectclass");
202 if (valeur! = NULL)
204 for (i = 0;valeur[i]! = NULL;i++)
206 if (!strcmp(valeur[i],"sambaTrust")) sambaTrust = True;
209 DEBUG(2,("%s\n",sambaTrust?"yes":"no"));
210 ldap_value_free(valeur);
211 return (sambaTrust);
214 /*******************************************************************
215 retrieve the user's info and contruct a smb_passwd structure.
216 ******************************************************************/
217 static void ldap_get_smb_passwd(LDAP *ldap_struct,LDAPMessage *entry,
218 struct smb_passwd *user)
220 static pstring user_name;
221 static pstring user_pass;
222 static pstring temp;
223 static unsigned char smblmpwd[16];
224 static unsigned char smbntpwd[16];
226 pdb_init_smb(user);
228 memset((char *)smblmpwd, '\0', sizeof(smblmpwd));
229 memset((char *)smbntpwd, '\0', sizeof(smbntpwd));
231 get_single_attribute(ldap_struct, entry, "cn", user_name);
232 DEBUG(2,("ldap_get_smb_passwd: user: %s\n",user_name));
234 #ifdef LDAP_PLAINTEXT_PASSWORD
235 get_single_attribute(ldap_struct, entry, "userPassword", temp);
236 nt_lm_owf_gen(temp, user->smb_nt_passwd, user->smb_passwd);
237 memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
238 #else
239 get_single_attribute(ldap_struct, entry, "unicodePwd", temp);
240 pdb_gethexpwd(temp, smbntpwd);
241 memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
243 get_single_attribute(ldap_struct, entry, "dBCSPwd", temp);
244 pdb_gethexpwd(temp, smblmpwd);
245 memset((char *)temp, '\0', sizeof(temp)); /* destroy local copy of the password */
246 #endif
248 get_single_attribute(ldap_struct, entry, "userAccountControl", temp);
249 user->acct_ctrl = pdb_decode_acct_ctrl(temp);
251 get_single_attribute(ldap_struct, entry, "pwdLastSet", temp);
252 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
254 get_single_attribute(ldap_struct, entry, "rid", temp);
256 /* the smb (unix) ids are not stored: they are created */
257 user->smb_userid = pdb_user_rid_to_uid (atoi(temp));
259 if (user->acct_ctrl & (ACB_DOMTRUST|ACB_WSTRUST|ACB_SVRTRUST) )
261 DEBUG(0,("Inconsistency in the LDAP database\n"));
263 if (user->acct_ctrl & ACB_NORMAL)
265 user->smb_name = user_name;
266 user->smb_passwd = smblmpwd;
267 user->smb_nt_passwd = smbntpwd;
271 /*******************************************************************
272 retrieve the user's info and contruct a sam_passwd structure.
274 calls ldap_get_smb_passwd function first, though, to save code duplication.
276 ******************************************************************/
277 static void ldap_get_sam_passwd(LDAP *ldap_struct, LDAPMessage *entry,
278 struct sam_passwd *user)
280 static pstring user_name;
281 static pstring fullname;
282 static pstring home_dir;
283 static pstring dir_drive;
284 static pstring logon_script;
285 static pstring profile_path;
286 static pstring acct_desc;
287 static pstring workstations;
288 static pstring temp;
289 static struct smb_passwd pw_buf;
291 pdb_init_sam(user);
293 ldap_get_smb_passwd(ldap_struct, entry, &pw_buf);
295 user->pass_last_set_time = pw_buf.pass_last_set_time;
297 get_single_attribute(ldap_struct, entry, "logonTime", temp);
298 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
300 get_single_attribute(ldap_struct, entry, "logoffTime", temp);
301 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
303 get_single_attribute(ldap_struct, entry, "kickoffTime", temp);
304 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
306 get_single_attribute(ldap_struct, entry, "pwdLastSet", temp);
307 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
309 get_single_attribute(ldap_struct, entry, "pwdCanChange", temp);
310 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
312 get_single_attribute(ldap_struct, entry, "pwdMustChange", temp);
313 user->pass_last_set_time = (time_t)strtol(temp, NULL, 16);
315 user->smb_name = pw_buf.smb_name;
317 DEBUG(2,("ldap_get_sam_passwd: user: %s\n", user_name));
319 get_single_attribute(ldap_struct, entry, "userFullName", fullname);
320 user->full_name = fullname;
322 get_single_attribute(ldap_struct, entry, "homeDirectory", home_dir);
323 user->home_dir = home_dir;
325 get_single_attribute(ldap_struct, entry, "homeDrive", dir_drive);
326 user->dir_drive = dir_drive;
328 get_single_attribute(ldap_struct, entry, "scriptPath", logon_script);
329 user->logon_script = logon_script;
331 get_single_attribute(ldap_struct, entry, "profilePath", profile_path);
332 user->profile_path = profile_path;
334 get_single_attribute(ldap_struct, entry, "comment", acct_desc);
335 user->acct_desc = acct_desc;
337 get_single_attribute(ldap_struct, entry, "userWorkstations", workstations);
338 user->workstations = workstations;
340 user->unknown_str = NULL; /* don't know, yet! */
341 user->munged_dial = NULL; /* "munged" dial-back telephone number */
343 get_single_attribute(ldap_struct, entry, "rid", temp);
344 user->user_rid = atoi(temp);
346 get_single_attribute(ldap_struct, entry, "primaryGroupID", temp);
347 user->group_rid = atoi(temp);
349 /* the smb (unix) ids are not stored: they are created */
350 user->smb_userid = pw_buf.smb_userid;
351 user->smb_grpid = group_rid_to_uid(user->group_rid);
353 user->acct_ctrl = pw_buf.acct_ctrl;
355 user->unknown_3 = 0xffffff; /* don't know */
356 user->logon_divs = 168; /* hours per week */
357 user->hours_len = 21; /* 21 times 8 bits = 168 */
358 memset(user->hours, 0xff, user->hours_len); /* available at all hours */
359 user->unknown_5 = 0x00000000; /* don't know */
360 user->unknown_6 = 0x000004ec; /* don't know */
362 if (user->acct_ctrl & (ACB_DOMTRUST|ACB_WSTRUST|ACB_SVRTRUST) )
364 DEBUG(0,("Inconsistency in the LDAP database\n"));
367 if (!(user->acct_ctrl & ACB_NORMAL))
369 DEBUG(0,("User's acct_ctrl bits not set to ACT_NORMAL in LDAP database\n"));
370 return;
374 /************************************************************************
375 Routine to manage the LDAPMod structure array
376 manage memory used by the array, by each struct, and values
378 ************************************************************************/
379 static void make_a_mod(LDAPMod ***modlist,int modop, char *attribute, char *value)
381 LDAPMod **mods;
382 int i;
383 int j;
385 mods = *modlist;
387 if (mods == NULL)
389 mods = (LDAPMod **)malloc( sizeof(LDAPMod *) );
390 if (mods == NULL)
392 DEBUG(0,("make_a_mod: out of memory!\n"));
393 return;
395 mods[0] = NULL;
398 for ( i = 0; mods[ i ] ! = NULL; ++i )
400 if ( mods[ i ]->mod_op == modop &&
401 !strcasecmp( mods[ i ]->mod_type, attribute ) )
403 break;
407 if (mods[i] == NULL)
409 mods = (LDAPMod **)realloc( mods, (i+2) * sizeof( LDAPMod * ) );
410 if (mods == NULL)
412 DEBUG(0,("make_a_mod: out of memory!\n"));
413 return;
415 mods[i] = (LDAPMod *)malloc( sizeof( LDAPMod ) );
416 if (mods[i] == NULL)
418 DEBUG(0,("make_a_mod: out of memory!\n"));
419 return;
421 mods[i]->mod_op = modop;
422 mods[i]->mod_values = NULL;
423 mods[i]->mod_type = strdup( attribute );
424 mods[i+1] = NULL;
427 if (value ! = NULL )
429 j = 0;
430 if ( mods[ i ]->mod_values ! = NULL )
432 for ( ; mods[ i ]->mod_values[ j ] ! = NULL; j++ );
434 mods[ i ]->mod_values = (char **)realloc(mods[ i ]->mod_values,
435 (j+2) * sizeof( char * ));
436 if ( mods[ i ]->mod_values == NULL)
438 DEBUG(0, "make_a_mod: Memory allocation failure!\n");
439 return;
441 mods[ i ]->mod_values[ j ] = strdup(value);
442 mods[ i ]->mod_values[ j + 1 ] = NULL;
444 *modlist = mods;
447 /************************************************************************
448 Add or modify an entry. Only the smb struct values
450 *************************************************************************/
451 static BOOL modadd_ldappwd_entry(struct smb_passwd *newpwd, int flag)
454 /* assume the struct is correct and filled
455 that's the job of passdb.c to check */
456 int scope = LDAP_SCOPE_ONELEVEL;
457 int rc;
458 char *smb_name;
459 int trust = False;
460 int ldap_state;
461 pstring filter;
462 pstring dn;
463 pstring lmhash;
464 pstring nthash;
465 pstring rid;
466 pstring lst;
467 pstring temp;
469 LDAP *ldap_struct;
470 LDAPMessage *result;
471 LDAPMod **mods;
473 smb_name = newpwd->smb_name;
475 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
477 return False;
480 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
482 ldap_unbind(ldap_struct);
483 return False;
486 if (smb_name[strlen(smb_name)-1] == '$' )
488 smb_name[strlen(smb_name)-1] = '\0';
489 trust = True;
492 slprintf(filter, sizeof(filter)-1,
493 "(&(cn = %s)(|(objectclass = sambaTrust)(objectclass = sambaAccount)))",
494 smb_name);
496 rc = ldap_search_s(ldap_struct, lp_ldap_suffix(), scope, filter, NULL, 0, &result);
498 switch (flag)
500 case ADD_USER:
502 if (ldap_count_entries(ldap_struct, result) ! = 0)
504 DEBUG(0,("User already in the base, with samba properties\n"));
505 ldap_unbind(ldap_struct);
506 return False;
508 ldap_state = LDAP_MOD_ADD;
509 break;
511 case MODIFY_USER:
513 if (ldap_count_entries(ldap_struct, result) ! = 1)
515 DEBUG(0,("No user to modify !\n"));
516 ldap_unbind(ldap_struct);
517 return False;
519 ldap_state = LDAP_MOD_REPLACE;
520 break;
522 default:
524 DEBUG(0,("How did you come here? \n"));
525 ldap_unbind(ldap_struct);
526 return False;
527 break;
530 slprintf(dn, sizeof(dn)-1, "cn = %s, %s",smb_name, lp_ldap_suffix() );
532 if (newpwd->smb_passwd ! = NULL)
534 int i;
535 for( i = 0; i < 16; i++)
537 slprintf(&temp[2*i], sizeof(temp) - 1, "%02X", newpwd->smb_passwd[i]);
541 else
543 if (newpwd->acct_ctrl & ACB_PWNOTREQ)
545 slprintf(temp, sizeof(temp) - 1, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX");
547 else
549 slprintf(temp, sizeof(temp) - 1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
552 slprintf(lmhash, sizeof(lmhash)-1, "%s", temp);
554 if (newpwd->smb_nt_passwd ! = NULL)
556 int i;
557 for( i = 0; i < 16; i++)
559 slprintf(&temp[2*i], sizeof(temp) - 1, "%02X", newpwd->smb_nt_passwd[i]);
563 else
565 if (newpwd->acct_ctrl & ACB_PWNOTREQ)
567 slprintf(temp, sizeof(temp) - 1, "NO PASSWORDXXXXXXXXXXXXXXXXXXXXX");
569 else
571 slprintf(temp, sizeof(temp) - 1, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
574 slprintf(nthash, sizeof(nthash)-1, "%s", temp);
576 slprintf(rid, sizeof(rid)-1, "%d", uid_to_user_rid(newpwd->smb_userid) );
577 slprintf(lst, sizeof(lst)-1, "%08X", newpwd->pass_last_set_time);
579 mods = NULL;
581 if (trust)
583 make_a_mod(&mods, ldap_state, "objectclass", "sambaTrust");
584 make_a_mod(&mods, ldap_state, "netbiosTrustName", smb_name);
585 make_a_mod(&mods, ldap_state, "trustPassword", nthash);
587 else
589 make_a_mod(&mods, ldap_state, "objectclass", "sambaAccount");
590 make_a_mod(&mods, ldap_state, "dBCSPwd", lmhash);
591 make_a_mod(&mods, ldap_state, "uid", smb_name);
592 make_a_mod(&mods, ldap_state, "unicodePwd", nthash);
595 make_a_mod(&mods, ldap_state, "cn", smb_name);
597 make_a_mod(&mods, ldap_state, "rid", rid);
598 make_a_mod(&mods, ldap_state, "pwdLastSet", lst);
599 make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl, NEW_PW_FORMAT_SPACE_PADDED_LEN));
601 switch(flag)
603 case ADD_USER:
605 ldap_add_s(ldap_struct, dn, mods);
606 DEBUG(2,("modadd_ldappwd_entry: added: cn = %s in the LDAP database\n",smb_name));
607 break;
609 case MODIFY_USER:
611 ldap_modify_s(ldap_struct, dn, mods);
612 DEBUG(2,("modadd_ldappwd_entry: changed: cn = %s in the LDAP database_n",smb_name));
613 break;
615 default:
617 DEBUG(2,("modadd_ldappwd_entry: How did you come here? \n"));
618 ldap_unbind(ldap_struct);
619 return False;
620 break;
624 ldap_mods_free(mods, 1);
626 ldap_unbind(ldap_struct);
628 return True;
631 /************************************************************************
632 Add or modify an entry. everything except the smb struct
634 *************************************************************************/
635 static BOOL modadd_ldap21pwd_entry(struct sam_passwd *newpwd, int flag)
638 /* assume the struct is correct and filled
639 that's the job of passdb.c to check */
640 int scope = LDAP_SCOPE_ONELEVEL;
641 int rc;
642 char *smb_name;
643 int trust = False;
644 int ldap_state;
645 pstring filter;
646 pstring dn;
647 pstring lmhash;
648 pstring nthash;
649 pstring rid;
650 pstring lst;
651 pstring temp;
653 LDAP *ldap_struct;
654 LDAPMessage *result;
655 LDAPMod **mods;
657 smb_name = newpwd->smb_name;
659 if (!ldap_open_connection(&ldap_struct)) /* open a connection to the server */
661 return False;
664 if (!ldap_connect_system(ldap_struct)) /* connect as system account */
666 ldap_unbind(ldap_struct);
667 return False;
670 if (smb_name[strlen(smb_name)-1] == '$' )
672 smb_name[strlen(smb_name)-1] = '\0';
673 trust = True;
676 slprintf(filter, sizeof(filter)-1,
677 "(&(cn = %s)(|(objectclass = sambaTrust)(objectclass = sambaAccount)))",
678 smb_name);
680 rc = ldap_search_s(ldap_struct, lp_ldap_suffix(), scope, filter, NULL, 0, &result);
682 switch (flag)
684 case ADD_USER:
686 if (ldap_count_entries(ldap_struct, result) ! = 1)
688 DEBUG(2,("User already in the base, with samba properties\n"));
689 ldap_unbind(ldap_struct);
690 return False;
692 ldap_state = LDAP_MOD_ADD;
693 break;
696 case MODIFY_USER:
698 if (ldap_count_entries(ldap_struct, result) ! = 1)
700 DEBUG(2,("No user to modify !\n"));
701 ldap_unbind(ldap_struct);
702 return False;
704 ldap_state = LDAP_MOD_REPLACE;
705 break;
708 default:
710 DEBUG(2,("How did you come here? \n"));
711 ldap_unbind(ldap_struct);
712 return False;
713 break;
716 slprintf(dn, sizeof(dn)-1, "cn = %s, %s",smb_name, lp_ldap_suffix() );
718 mods = NULL;
720 if (trust)
723 else
727 make_a_mod(&mods, ldap_state, "cn", smb_name);
729 make_a_mod(&mods, ldap_state, "rid", rid);
730 make_a_mod(&mods, ldap_state, "pwdLastSet", lst);
731 make_a_mod(&mods, ldap_state, "userAccountControl", pdb_encode_acct_ctrl(newpwd->acct_ctrl,NEW_PW_FORMAT_SPACE_PADDED_LEN));
733 ldap_modify_s(ldap_struct, dn, mods);
735 ldap_mods_free(mods, 1);
737 ldap_unbind(ldap_struct);
739 return True;
742 /************************************************************************
743 Routine to add an entry to the ldap passwd file.
745 do not call this function directly. use passdb.c instead.
747 *************************************************************************/
748 static BOOL add_ldappwd_entry(struct smb_passwd *newpwd)
750 return (modadd_ldappwd_entry(newpwd, ADD_USER) );
753 /************************************************************************
754 Routine to search the ldap passwd file for an entry matching the username.
755 and then modify its password entry. We can't use the startldappwent()/
756 getldappwent()/endldappwent() interfaces here as we depend on looking
757 in the actual file to decide how much room we have to write data.
758 override = False, normal
759 override = True, override XXXXXXXX'd out password or NO PASS
761 do not call this function directly. use passdb.c instead.
763 ************************************************************************/
764 static BOOL mod_ldappwd_entry(struct smb_passwd *pwd, BOOL override)
766 return (modadd_ldappwd_entry(pwd, MODIFY_USER) );
769 /************************************************************************
770 Routine to add an entry to the ldap passwd file.
772 do not call this function directly. use passdb.c instead.
774 *************************************************************************/
775 static BOOL add_ldap21pwd_entry(struct sam_passwd *newpwd)
777 return( modadd_ldappwd_entry(newpwd, ADD_USER)?
778 modadd_ldap21pwd_entry(newpwd, ADD_USER):False);
781 /************************************************************************
782 Routine to search the ldap passwd file for an entry matching the username.
783 and then modify its password entry. We can't use the startldappwent()/
784 getldappwent()/endldappwent() interfaces here as we depend on looking
785 in the actual file to decide how much room we have to write data.
786 override = False, normal
787 override = True, override XXXXXXXX'd out password or NO PASS
789 do not call this function directly. use passdb.c instead.
791 ************************************************************************/
792 static BOOL mod_ldap21pwd_entry(struct sam_passwd *pwd, BOOL override)
794 return( modadd_ldappwd_entry(pwd, MODIFY_USER)?
795 modadd_ldap21pwd_entry(pwd, MODIFY_USER):False);
798 struct ldap_enum_info
800 LDAP *ldap_struct;
801 LDAPMessage *result;
802 LDAPMessage *entry;
805 static struct ldap_enum_info ldap_ent;
807 /***************************************************************
808 Start to enumerate the ldap passwd list. Returns a void pointer
809 to ensure no modification outside this module.
811 do not call this function directly. use passdb.c instead.
813 ****************************************************************/
814 static void *startldappwent(BOOL update)
816 int scope = LDAP_SCOPE_ONELEVEL;
817 int rc;
819 pstring filter;
821 if (!ldap_open_connection(&ldap_ent.ldap_struct)) /* open a connection to the server */
823 return NULL;
826 if (!ldap_connect_system(ldap_ent.ldap_struct)) /* connect as system account */
828 return NULL;
831 /* when the class is known the search is much faster */
832 switch (0)
834 case 1:
836 pstrcpy(filter, "objectclass = sambaAccount");
837 break;
839 case 2:
841 pstrcpy(filter, "objectclass = sambaTrust");
842 break;
844 default:
846 pstrcpy(filter, "(|(objectclass = sambaTrust)(objectclass = sambaAccount))");
847 break;
851 rc = ldap_search_s(ldap_ent.ldap_struct, lp_ldap_suffix(), scope, filter, NULL, 0, &ldap_ent.result);
853 DEBUG(2,("%d entries in the base!\n", ldap_count_entries(ldap_ent.ldap_struct, ldap_ent.result) ));
855 ldap_ent.entry = ldap_first_entry(ldap_ent.ldap_struct, ldap_ent.result);
857 return &ldap_ent;
860 /*************************************************************************
861 Routine to return the next entry in the ldap passwd list.
863 do not call this function directly. use passdb.c instead.
865 *************************************************************************/
866 static struct smb_passwd *getldappwent(void *vp)
868 static struct smb_passwd user;
869 struct ldap_enum_info *ldap_vp = (struct ldap_enum_info *)vp;
871 ldap_vp->entry = ldap_next_entry(ldap_vp->ldap_struct, ldap_vp->entry);
873 if (ldap_vp->entry ! = NULL)
875 ldap_get_smb_passwd(ldap_vp->ldap_struct, ldap_vp->entry, &user);
876 return &user;
878 return NULL;
881 /*************************************************************************
882 Routine to return the next entry in the ldap passwd list.
884 do not call this function directly. use passdb.c instead.
886 *************************************************************************/
887 static struct sam_passwd *getldap21pwent(void *vp)
889 static struct sam_passwd user;
890 struct ldap_enum_info *ldap_vp = (struct ldap_enum_info *)vp;
892 ldap_vp->entry = ldap_next_entry(ldap_vp->ldap_struct, ldap_vp->entry);
894 if (ldap_vp->entry ! = NULL)
896 ldap_get_sam_passwd(ldap_vp->ldap_struct, ldap_vp->entry, &user);
897 return &user;
899 return NULL;
902 /***************************************************************
903 End enumeration of the ldap passwd list.
905 do not call this function directly. use passdb.c instead.
907 ****************************************************************/
908 static void endldappwent(void *vp)
910 struct ldap_enum_info *ldap_vp = (struct ldap_enum_info *)vp;
911 ldap_msgfree(ldap_vp->result);
912 ldap_unbind(ldap_vp->ldap_struct);
915 /*************************************************************************
916 Return the current position in the ldap passwd list as an SMB_BIG_UINT.
917 This must be treated as an opaque token.
919 do not call this function directly. use passdb.c instead.
921 *************************************************************************/
922 static SMB_BIG_UINT getldappwpos(void *vp)
924 return (SMB_BIG_UINT)0;
927 /*************************************************************************
928 Set the current position in the ldap passwd list from SMB_BIG_UINT.
929 This must be treated as an opaque token.
931 do not call this function directly. use passdb.c instead.
933 *************************************************************************/
934 static BOOL setldappwpos(void *vp, SMB_BIG_UINT tok)
936 return False;
940 * Ldap derived functions.
943 static struct smb_passwd *getldappwnam(char *name)
945 return pdb_sam_to_smb(iterate_getsam21pwnam(name));
948 static struct smb_passwd *getldappwuid(uid_t smb_userid)
950 return pdb_sam_to_smb(iterate_getsam21pwuid(smb_userid));
953 static struct smb_passwd *getldappwrid(uint32 user_rid)
955 return pdb_sam_to_smb(iterate_getsam21pwuid(pdb_user_rid_to_uid(user_rid)));
958 static struct smb_passwd *getldappwent(void *vp)
960 return pdb_sam_to_smb(getldap21pwent(vp));
963 static BOOL add_ldappwd_entry(struct smb_passwd *newpwd)
965 return add_ldap21pwd_entry(pdb_smb_to_sam(newpwd));
968 static BOOL mod_ldappwd_entry(struct smb_passwd* pwd, BOOL override)
970 return mod_ldap21pwd_entry(pdb_smb_to_sam(pwd), override);
973 static BOOL del_ldappwd_entry(const char *name)
975 return False; /* Dummy... */
978 static struct sam_disp_info *getldapdispnam(char *name)
980 return pdb_sam_to_dispinfo(getldap21pwnam(name));
983 static struct sam_disp_info *getldapdisprid(uint32 rid)
985 return pdb_sam_to_dispinfo(getldap21pwrid(rid));
988 static struct sam_disp_info *getldapdispent(void *vp)
990 return pdb_sam_to_dispinfo(getldap21pwent(vp));
993 static struct sam_passwd *getldap21pwuid(uid_t uid)
995 return pdb_smb_to_sam(iterate_getsam21pwuid(pdb_uid_to_user_rid(uid)));
998 static struct passdb_ops ldap_ops =
1000 startldappwent,
1001 endldappwent,
1002 getldappwpos,
1003 setldappwpos,
1004 getldappwnam,
1005 getldappwuid,
1006 getldappwrid,
1007 getldappwent,
1008 add_ldappwd_entry,
1009 mod_ldappwd_entry,
1010 del_ldappwd_entry,
1011 getldap21pwent,
1012 iterate_getsam21pwnam, /* From passdb.c */
1013 iterate_getsam21pwuid, /* From passdb.c */
1014 iterate_getsam21pwrid, /* From passdb.c */
1015 add_ldap21pwd_entry,
1016 mod_ldap21pwd_entry,
1017 getldapdispnam,
1018 getldapdisprid,
1019 getldapdispent
1022 struct passdb_ops *ldap_initialize_password_db(void)
1024 return &ldap_ops;
1027 #else
1028 void dummy_function(void);
1029 void dummy_function(void) { } /* stop some compilers complaining */
1030 #endif