fixed word-order issue in password set and password change.
[Samba.git] / source / lib / util_pwdb.c
blobab5adf34bef979e2b414a55adada6c46b0e6b14b
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Password and authentication handling
5 Copyright (C) Jeremy Allison 1996-1998
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
24 #include "nterr.h"
25 #include "sids.h"
27 extern int DEBUGLEVEL;
29 extern fstring global_myworkgroup;
31 extern pstring global_myname;
33 typedef struct
35 uint32 rid;
36 char *defaultname;
37 char *name;
38 } rid_name;
41 * A list of the rids of well known BUILTIN and Domain users
42 * and groups.
45 static rid_name builtin_alias_rids[] =
47 { BUILTIN_ALIAS_RID_ADMINS , "Administrators" , NULL },
48 { BUILTIN_ALIAS_RID_USERS , "Users" , NULL },
49 { BUILTIN_ALIAS_RID_GUESTS , "Guests" , NULL },
50 { BUILTIN_ALIAS_RID_POWER_USERS , "Power Users" , NULL },
52 { BUILTIN_ALIAS_RID_ACCOUNT_OPS , "Account Operators" , NULL },
53 { BUILTIN_ALIAS_RID_SYSTEM_OPS , "System Operators" , NULL },
54 { BUILTIN_ALIAS_RID_PRINT_OPS , "Print Operators" , NULL },
55 { BUILTIN_ALIAS_RID_BACKUP_OPS , "Backup Operators" , NULL },
56 { BUILTIN_ALIAS_RID_REPLICATOR , "Replicator" , NULL },
57 { 0 , NULL , NULL}
60 /* array lookup of well-known Domain RID users. */
61 static rid_name domain_user_rids[] =
63 { DOMAIN_USER_RID_ADMIN , "Administrator" , NULL },
64 { DOMAIN_USER_RID_GUEST , "Guest" , NULL },
65 { 0 , NULL , NULL}
68 /* array lookup of well-known Domain RID groups. */
69 static rid_name domain_group_rids[] =
71 { DOMAIN_GROUP_RID_ADMINS , "Domain Admins" , NULL },
72 { DOMAIN_GROUP_RID_USERS , "Domain Users" , NULL },
73 { DOMAIN_GROUP_RID_GUESTS , "Domain Guests" , NULL },
74 { 0 , NULL , NULL}
77 /*******************************************************************
78 make an entry in wk name map
79 the name is strdup()ed!
80 *******************************************************************/
81 static BOOL make_alias_entry(rid_name *map, char *defaultname, char *name)
83 if(isdigit(*defaultname))
85 long rid = -1;
86 char *s;
88 if(*defaultname == '0')
90 if(defaultname[1] == 'x')
92 s = "%lx";
93 defaultname += 2;
95 else
97 s = "%lo";
100 else
102 s = "%ld";
105 sscanf(defaultname, s, &rid);
107 for( ; map->rid; map++)
109 if(map->rid == rid) {
110 map->name = strdup(name);
111 DEBUG(5, ("make_alias_entry: mapping %s (rid 0x%x) to %s\n",
112 map->defaultname, map->rid, map->name));
113 return True;
116 return False;
119 for( ; map->rid; map++)
121 if(!StrCaseCmp(map->name, defaultname)) {
122 map->name = strdup(name);
123 DEBUG(5, ("make_alias_entry: mapping %s (rid 0x%x) to %s\n",
124 map->defaultname, map->rid, map->name));
125 return True;
128 return False;
131 /*******************************************************************
132 reset wk map to default values
133 *******************************************************************/
134 static void reset_wk_map(rid_name *map)
136 for( ; map->rid; map++)
138 if(map->name != NULL && map->name != map->defaultname)
139 free(map->name);
140 map->name = map->defaultname;
144 /*******************************************************************
145 reset all wk maps
146 *******************************************************************/
147 static void reset_wk_maps(void)
149 DEBUG(4, ("reset_wk_maps: Initializing maps\n"));
150 reset_wk_map(builtin_alias_rids);
151 reset_wk_map(domain_user_rids);
152 reset_wk_map(domain_group_rids);
155 /*******************************************************************
156 Load builtin alias map
157 *******************************************************************/
158 static BOOL load_wk_rid_map(void)
160 static int map_initialized = 0;
161 static time_t builtin_rid_file_last_modified = (time_t)0;
162 char *builtin_rid_file = lp_builtinrid_file();
164 FILE *fp;
165 char *s;
166 pstring buf;
168 if (!map_initialized)
170 reset_wk_maps();
171 map_initialized = 1;
174 if (!*builtin_rid_file)
176 return False;
179 fp = open_file_if_modified(builtin_rid_file, "r", &builtin_rid_file_last_modified);
180 if(!fp)
182 DEBUG(0,("load_wk_rid_map: can't open name map %s. Error was %s\n",
183 builtin_rid_file, strerror(errno)));
184 return False;
187 reset_wk_maps();
188 DEBUG(4,("load_wk_rid_map: Scanning builtin rid map %s\n",builtin_rid_file));
190 while ((s = fgets_slash(buf, sizeof(buf), fp)) != NULL)
192 pstring defaultname;
193 pstring name;
195 DEBUG(10,("Read line |%s|\n", s));
197 if (!*s || strchr("#;",*s))
198 continue;
200 if (!next_token(&s,name, "\t\n\r=", sizeof(defaultname)))
201 continue;
203 if (!next_token(&s,defaultname, "\t\n\r=", sizeof(name)))
204 continue;
206 trim_string(defaultname, " ", " ");
207 trim_string(name, " ", " ");
209 if (!*defaultname || !*name)
210 continue;
212 if(make_alias_entry(builtin_alias_rids, defaultname, name))
213 continue;
214 if(make_alias_entry(domain_user_rids, defaultname, name))
215 continue;
216 if(make_alias_entry(domain_group_rids, defaultname, name))
217 continue;
219 DEBUG(0,("load_wk_rid_map: Unknown alias %s in map %s\n",
220 defaultname, builtin_rid_file));
223 fclose(fp);
224 return True;
227 /*******************************************************************
228 lookup_wk_group_name
229 ********************************************************************/
230 uint32 lookup_wk_group_name(const char *group_name, const char *domain,
231 DOM_SID *sid, uint32 *type)
233 char *grp_name;
234 int i = -1; /* start do loop at -1 */
235 uint32 rid;
237 if (strequal(domain, global_sam_name))
239 sid_copy(sid, &global_sam_sid);
241 else if (strequal(domain, "BUILTIN"))
243 sid_copy(sid, global_sid_builtin);
245 else
247 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
250 load_wk_rid_map();
252 do /* find, if it exists, a group rid for the group name */
254 i++;
255 rid = domain_group_rids[i].rid;
256 grp_name = domain_group_rids[i].name;
258 if (strequal(grp_name, group_name))
260 sid_append_rid(sid, rid);
261 (*type) = SID_NAME_DOM_GRP;
263 return 0x0;
266 } while (grp_name != NULL);
268 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
271 /*******************************************************************
272 lookup_wk_user_name
273 ********************************************************************/
274 uint32 lookup_wk_user_name(const char *user_name, const char *domain,
275 DOM_SID *sid, uint32 *type)
277 char *usr_name;
278 int i = -1; /* start do loop at -1 */
280 if (strequal(domain, global_sam_name))
282 sid_copy(sid, &global_sam_sid);
284 else if (strequal(domain, "BUILTIN"))
286 sid_copy(sid, global_sid_builtin);
288 else
290 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
293 load_wk_rid_map();
295 do /* find, if it exists, a alias rid for the alias name */
297 i++;
298 usr_name = domain_user_rids[i].name;
300 } while (usr_name != NULL && !strequal(usr_name, user_name));
302 if (usr_name != NULL)
304 sid_append_rid(sid, domain_user_rids[i].rid);
305 (*type) = SID_NAME_USER;
306 return 0;
309 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
312 /*******************************************************************
313 lookup_builtin_alias_name
314 ********************************************************************/
315 uint32 lookup_builtin_alias_name(const char *alias_name, const char *domain,
316 DOM_SID *sid, uint32 *type)
318 char *als_name;
319 int i = 0;
320 uint32 rid;
322 if (strequal(domain, "BUILTIN"))
324 if (sid != NULL)
326 sid_copy(sid, global_sid_builtin);
329 else
331 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
334 load_wk_rid_map();
336 do /* find, if it exists, a alias rid for the alias name*/
338 rid = builtin_alias_rids[i].rid;
339 als_name = builtin_alias_rids[i].name;
341 if (strequal(als_name, alias_name))
343 if (sid != NULL)
345 sid_append_rid(sid, rid);
348 if (type != NULL)
350 (*type) = SID_NAME_ALIAS;
353 return 0x0;
356 i++;
358 } while (als_name != NULL);
360 return 0xC0000000 | NT_STATUS_NONE_MAPPED;
363 /*************************************************************
364 the following functions lookup wk rid's.
365 these may be unnecessary...
366 **************************************************************/
367 static char *lookup_wk_rid(uint32 rid, rid_name *table)
369 load_wk_rid_map();
370 for( ; table->rid ; table++)
372 if(table->rid == rid)
374 return table->name;
377 return NULL;
380 char *lookup_wk_alias_rid(uint32 rid)
382 return lookup_wk_rid(rid, builtin_alias_rids);
385 char *lookup_wk_user_rid(uint32 rid)
387 return lookup_wk_rid(rid, domain_user_rids);
390 char *lookup_wk_group_rid(uint32 rid)
392 return lookup_wk_rid(rid, domain_group_rids);