2 Unix SMB/Netbios implementation.
4 LDAP protocol helper functions for SAMBA
5 Copyright (C) Matthew Chapman 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.
30 extern int DEBUGLEVEL;
33 extern LDAP *ldap_struct;
34 extern LDAPMessage *ldap_results;
35 extern LDAPMessage *ldap_entry;
38 /*******************************************************************
39 NT name/RID search functions.
40 ******************************************************************/
42 BOOL ldap_search_by_rid(uint32 rid)
46 slprintf(filter, sizeof(filter)-1,
47 "(&(rid=%x)(objectclass=sambaAccount))", rid);
48 return ldap_search_for(filter);
51 BOOL ldap_search_by_ntname(const char *ntname)
55 slprintf(filter, sizeof(filter)-1,
56 "(&(ntuid=%s)(objectclass=sambaAccount))", ntname);
57 return ldap_search_for(filter);
61 /*******************************************************************
62 Store NTTIMEs as time_t's.
63 ******************************************************************/
65 static void ldap_save_time(LDAPMod ***modlist, int modop, char *attribute,
71 t = nt_time_to_unix(nttime);
76 slprintf(tstr, sizeof(tstr)-1, "%08X", t);
77 ldap_make_mod(modlist, modop, attribute, tstr);
80 static void ldap_read_time(char *attribute, NTTIME *nttime)
85 if(ldap_get_attribute(attribute, timestr))
87 t = (time_t)strtol(timestr, NULL, 16);
88 unix_to_nt_time(nttime, t);
93 /*******************************************************************
94 Contruct a sam_passwd structure.
95 ******************************************************************/
97 static struct sam_passwd *ldapsam_getsam()
99 static pstring full_name;
100 static pstring acct_desc;
101 static pstring home_dir;
102 static pstring home_drive;
103 static pstring logon_script;
104 static pstring profile_path;
105 static pstring workstations;
107 struct sam_passwd *sam21;
108 struct smb_passwd *smbpw;
113 smbpw = ldap_getpw();
114 sam21 = pwdb_smb_to_sam(smbpw);
116 if(ldap_get_attribute("gidNumber", temp))
117 sam21->unix_gid = atoi(temp);
119 if(ldap_get_attribute("grouprid", temp))
120 sam21->group_rid = strtol(temp, NULL, 16);
122 if(ldap_get_attribute("cn", full_name))
123 sam21->full_name = full_name;
125 if(ldap_get_attribute("description", acct_desc))
126 sam21->acct_desc = acct_desc;
128 if(ldap_get_attribute("smbHome", home_dir))
129 sam21->home_dir = home_dir;
131 if(ldap_get_attribute("homeDrive", home_drive))
132 sam21->dir_drive = home_drive;
134 if(ldap_get_attribute("script", logon_script))
135 sam21->logon_script = logon_script;
137 if(ldap_get_attribute("profile", profile_path))
138 sam21->profile_path = profile_path;
140 if(ldap_get_attribute("workstations", workstations))
141 sam21->workstations = workstations;
143 ldap_read_time("pwdCanChange", &sam21->pass_can_change_time);
144 ldap_read_time("pwdMustChange", &sam21->pass_must_change_time);
145 ldap_read_time("logonTime", &sam21->logon_time);
146 ldap_read_time("logoffTime", &sam21->logoff_time);
147 ldap_read_time("kickoffTime", &sam21->kickoff_time);
149 sam21->unknown_3 = 0xffffff; /* don't know */
150 sam21->logon_divs = 168; /* hours per week */
151 sam21->hours_len = 21; /* 21 times 8 bits = 168 */
152 memset(sam21->hours, 0xff, sam21->hours_len); /* all hours */
153 sam21->unknown_5 = 0x00020000; /* don't know */
154 sam21->unknown_6 = 0x000004ec; /* don't know */
155 sam21->unknown_str = NULL;
156 sam21->munged_dial = NULL;
158 ldap_entry = ldap_next_entry(ldap_struct, ldap_entry);
163 /*******************************************************************
164 Contruct a sam_disp_info structure.
165 ******************************************************************/
167 static struct sam_disp_info *ldapsam_getdispinfo()
169 static struct sam_disp_info dispinfo;
170 static pstring nt_name;
171 static pstring full_name;
177 if(!ldap_get_attribute("ntuid", nt_name) &&
178 !ldap_get_attribute("uid", nt_name)) {
179 DEBUG(0,("Missing uid\n"));
181 dispinfo.nt_name = nt_name;
183 DEBUG(2,("Retrieving account [%s]\n",nt_name));
185 if(ldap_get_attribute("rid", temp))
186 dispinfo.user_rid = strtol(temp, NULL, 16);
188 DEBUG(0,("Missing rid\n"));
191 if(ldap_get_attribute("cn", full_name))
192 dispinfo.full_name = full_name;
194 dispinfo.full_name = NULL;
196 ldap_entry = ldap_next_entry(ldap_struct, ldap_entry);
201 /************************************************************************
202 Queues the necessary modifications to save a sam_passwd structure
203 ************************************************************************/
205 static void ldapsam_sammods(struct sam_passwd *newpwd, LDAPMod ***mods,
208 struct smb_passwd *smbpw;
211 smbpw = pwdb_sam_to_smb(newpwd);
212 ldap_smbpwmods(smbpw, mods, operation);
214 slprintf(temp, sizeof(temp)-1, "%d", newpwd->unix_gid);
215 ldap_make_mod(mods, operation, "gidNumber", temp);
217 slprintf(temp, sizeof(temp)-1, "%x", newpwd->group_rid);
218 ldap_make_mod(mods, operation, "grouprid", temp);
220 ldap_make_mod(mods, operation, "cn", newpwd->full_name);
221 ldap_make_mod(mods, operation, "description", newpwd->acct_desc);
222 ldap_make_mod(mods, operation, "smbHome", newpwd->home_dir);
223 ldap_make_mod(mods, operation, "homeDrive", newpwd->dir_drive);
224 ldap_make_mod(mods, operation, "script", newpwd->logon_script);
225 ldap_make_mod(mods, operation, "profile", newpwd->profile_path);
226 ldap_make_mod(mods, operation, "workstations", newpwd->workstations);
228 ldap_save_time(mods, operation, "pwdCanChange",
229 &newpwd->pass_can_change_time);
230 ldap_save_time(mods, operation, "pwdMustChange",
231 &newpwd->pass_must_change_time);
232 ldap_save_time(mods, operation, "logonTime",
233 &newpwd->logon_time);
234 ldap_save_time(mods, operation, "logoffTime",
235 &newpwd->logoff_time);
236 ldap_save_time(mods, operation, "kickoffTime",
237 &newpwd->kickoff_time);
241 /***************************************************************
242 Begin/end account enumeration.
243 ****************************************************************/
245 static void *ldapsam_enumfirst(BOOL update)
250 ldap_search_for("objectclass=sambaAccount");
255 static void ldapsam_enumclose(void *vp)
261 /*************************************************************************
262 Save/restore the current position in a query
263 *************************************************************************/
265 static SMB_BIG_UINT ldapsam_getdbpos(void *vp)
267 return (SMB_BIG_UINT)((ulong)ldap_entry);
270 static BOOL ldapsam_setdbpos(void *vp, SMB_BIG_UINT tok)
272 ldap_entry = (LDAPMessage *)((ulong)tok);
277 /*************************************************************************
278 Return sam_passwd information.
279 *************************************************************************/
281 static struct sam_passwd *ldapsam_getsambynam(const char *name)
283 struct sam_passwd *ret;
288 ldap_search_by_ntname(name);
289 ret = ldapsam_getsam();
295 static struct sam_passwd *ldapsam_getsambyuid(uid_t userid)
297 struct sam_passwd *ret;
302 ldap_search_by_uid(userid);
303 ret = ldapsam_getsam();
309 static struct sam_passwd *ldapsam_getsambyrid(uint32 user_rid)
311 struct sam_passwd *ret;
316 ldap_search_by_rid(user_rid);
317 ret = ldapsam_getsam();
323 static struct sam_passwd *ldapsam_getcurrentsam(void *vp)
325 return ldapsam_getsam();
329 /************************************************************************
330 Modify user information given a sam_passwd struct.
331 *************************************************************************/
333 static BOOL ldapsam_addsam(struct sam_passwd *newpwd)
337 if (!newpwd || !ldap_allocaterid(&newpwd->user_rid))
340 ldapsam_sammods(newpwd, &mods, LDAP_MOD_ADD);
341 return ldap_makemods("uid", newpwd->unix_name, mods, True);
344 static BOOL ldapsam_modsam(struct sam_passwd *pwd, BOOL override)
351 ldapsam_sammods(pwd, &mods, LDAP_MOD_REPLACE);
352 return ldap_makemods("uid", pwd->unix_name, mods, False);
356 /*************************************************************************
357 Return sam_disp_info information.
358 *************************************************************************/
360 static struct sam_disp_info *ldapsam_getdispbynam(const char *name)
362 struct sam_disp_info *ret;
367 ldap_search_by_ntname(name);
368 ret = ldapsam_getdispinfo();
374 static struct sam_disp_info *ldapsam_getdispbyrid(uint32 user_rid)
376 struct sam_disp_info *ret;
381 ldap_search_by_rid(user_rid);
382 ret = ldapsam_getdispinfo();
388 static struct sam_disp_info *ldapsam_getcurrentdisp(void *vp)
390 return ldapsam_getdispinfo();
395 static struct sam_passdb_ops ldapsam_ops =
405 ldapsam_getcurrentsam,
409 ldapsam_getdispbynam,
410 ldapsam_getdispbyrid,
411 ldapsam_getcurrentdisp
414 struct sam_passdb_ops *ldap_initialise_sam_password_db(void)
420 void sampassldap_dummy_function(void);
421 void sampassldap_dummy_function(void) { } /* stop some compilers complaining */