r5235: Fix compile warning.
[ira/wip.git] / source3 / lib / account_pol.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  account policy storage
4  *  Copyright (C) Jean François Micouleau      1998-2001.
5  *  Copyright (C) Andrew Bartlett              2002
6  *  Copyright (C) Guenther Deschner            2004-2005
7  *  
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.
12  *  
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.
17  *  
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.
21  */
22
23 #include "includes.h"
24 static TDB_CONTEXT *tdb; 
25
26 /* cache all entries for 60 seconds for to save ldap-queries (cache is updated
27  * after this period if admins do not use pdbedit or usermanager but manipulate
28  * ldap directly) - gd */
29
30 #define DATABASE_VERSION        3
31 #define AP_LASTSET              "LAST_CACHE_UPDATE"
32 #define AP_MIGRATED             "ACCOUNT POLICIES WERE MIGRATED TO PASSDB"
33 #define AP_TTL                  60
34
35 extern DOM_SID global_sid_World;
36 extern DOM_SID global_sid_Builtin_Administrators;
37 extern DOM_SID global_sid_Builtin_Account_Operators;
38 extern DOM_SID global_sid_Builtin_Server_Operators;
39 extern DOM_SID global_sid_Builtin_Print_Operators;
40 extern DOM_SID global_sid_Builtin_Backup_Operators;
41
42
43 struct ap_table {
44         int field;
45         const char *string;
46         uint32 default_val;
47         const char *comment;
48 };
49
50 static const struct ap_table account_policy_names[] = {
51         {AP_MIN_PASSWORD_LEN, "min password length", MINPASSWDLENGTH, 
52                 "Minimal password length (default: 5)"},
53         {AP_PASSWORD_HISTORY, "password history", 0,
54                 "Length of Password History Entries (default: 0 => off)" },
55         {AP_USER_MUST_LOGON_TO_CHG_PASS, "user must logon to change password", 0,
56                 "Force Users to logon for password change (default: 0 => off, 2 => on)"},
57         {AP_MAX_PASSWORD_AGE, "maximum password age", (uint32)-1,
58                 "Maximum password age, in seconds (default: -1 => never expire passwords)"},
59         {AP_MIN_PASSWORD_AGE,"minimum password age", 0,
60                 "Minimal password age, in seconds (default: 0 => allow immediate password change)"},
61         {AP_LOCK_ACCOUNT_DURATION, "lockout duration", 30,
62                 "Lockout duration in minutes (default: 30, -1 => forever)"},
63         {AP_RESET_COUNT_TIME, "reset count minutes", 30,
64                 "Reset time after lockout in minutes (default: 30)"},
65         {AP_BAD_ATTEMPT_LOCKOUT, "bad lockout attempt", 0,
66                 "Lockout users after bad logon attempts (default: 0 => off)"},
67         {AP_TIME_TO_LOGOUT, "disconnect time", -1,
68                 "Disconnect Users outside logon hours (default: -1 => off, 0 => on)"}, 
69         {AP_REFUSE_MACHINE_PW_CHANGE, "refuse machine password change", 0,
70                 "Allow Machine Password changes (default: 0 => off)"},
71         {0, NULL, 0, ""}
72 };
73
74 char *account_policy_names_list(void)
75 {
76         char *nl, *p;
77         int i;
78         size_t len = 0;
79
80         for (i=0; account_policy_names[i].string; i++) {
81                 len += strlen(account_policy_names[i].string) + 1;
82         }
83         len++;
84         nl = SMB_MALLOC(len);
85         if (!nl) {
86                 return NULL;
87         }
88         p = nl;
89         for (i=0; account_policy_names[i].string; i++) {
90                 memcpy(p, account_policy_names[i].string, strlen(account_policy_names[i].string) + 1);
91                 p[strlen(account_policy_names[i].string)] = '\n';
92                 p += strlen(account_policy_names[i].string) + 1;
93         }
94         *p = '\0';
95         return nl;
96 }
97
98 /****************************************************************************
99 Get the account policy name as a string from its #define'ed number
100 ****************************************************************************/
101
102 const char *decode_account_policy_name(int field)
103 {
104         int i;
105         for (i=0; account_policy_names[i].string; i++) {
106                 if (field == account_policy_names[i].field)
107                         return account_policy_names[i].string;
108         }
109         return NULL;
110
111 }
112
113 /****************************************************************************
114 Get the account policy comment as a string from its #define'ed number
115 ****************************************************************************/
116
117 const char *account_policy_get_comment(int field)
118 {
119         int i;
120         for (i=0; account_policy_names[i].string; i++) {
121                 if (field == account_policy_names[i].field)
122                         return account_policy_names[i].comment;
123         }
124         return NULL;
125
126 }
127
128 /****************************************************************************
129 Get the account policy name as a string from its #define'ed number
130 ****************************************************************************/
131
132 int account_policy_name_to_fieldnum(const char *name)
133 {
134         int i;
135         for (i=0; account_policy_names[i].string; i++) {
136                 if (strcmp(name, account_policy_names[i].string) == 0)
137                         return account_policy_names[i].field;
138         }
139         return 0;
140
141 }
142
143 /*****************************************************************************
144 Update LAST-Set counter inside the cache
145 *****************************************************************************/
146
147 static BOOL account_policy_cache_timestamp(uint32 *value, BOOL update)
148 {
149         pstring key;
150         uint32 val = 0;
151         time_t now;
152
153         slprintf(key, sizeof(key)-1, "%s", AP_LASTSET);
154
155         if (!init_account_policy())
156                 return False;
157
158         if (!tdb_fetch_uint32(tdb, key, &val) && !update) {
159                 DEBUG(10,("failed to get last set timestamp of cache\n"));
160                 return False;
161         }
162
163         *value = val;
164
165         DEBUG(10, ("account policy cache lastset was: %s\n", http_timestring(val)));
166
167         if (update) {
168
169                 now = time(NULL);
170
171                 if (!tdb_store_uint32(tdb, key, (uint32)now)) {
172                         DEBUG(1, ("tdb_store_uint32 failed for %s\n", key));
173                         return False;
174                 }
175                 DEBUG(10, ("account policy cache lastset now: %s\n", http_timestring(now)));
176                 *value = now;
177         }
178
179         return True;
180 }
181
182 /*****************************************************************************
183 Get default value for account policy
184 *****************************************************************************/
185
186 BOOL account_policy_get_default(int account_policy, uint32 *val)
187 {
188         int i;
189         for (i=0; account_policy_names[i].field; i++) {
190                 if (account_policy_names[i].field == account_policy) {
191                         *val = account_policy_names[i].default_val;
192                         return True;
193                 }
194         }
195         DEBUG(0,("no default for account_policy index %d found. This should never happen\n", 
196                 account_policy));
197         return False;
198 }
199
200 /*****************************************************************************
201  Set default for a field if it is empty
202 *****************************************************************************/
203
204 static BOOL account_policy_set_default_on_empty(int account_policy)
205 {
206
207         uint32 value;
208
209         if (!account_policy_get(account_policy, &value) && 
210             !account_policy_get_default(account_policy, &value)) {
211                 return False;
212         }
213
214         return account_policy_set(account_policy, value);
215 }
216
217 /*****************************************************************************
218 Check migration success, set marker if required
219 *****************************************************************************/
220
221 static BOOL already_migrated_account_policies(BOOL store_migration_success)
222 {
223         pstring key;
224         uint32 value;
225
226         slprintf(key, sizeof(key)-1, "%s", AP_MIGRATED);
227
228         if (tdb_fetch_uint32(tdb, key, &value)) {
229                 return True;
230         }
231
232         if (store_migration_success) {
233
234                 if (!tdb_store_uint32(tdb, key, 1)) {
235                         DEBUG(1, ("tdb_store_uint32 failed for %s\n", key));
236                         return False;
237                 }
238                 return True;
239         }
240
241         return False;
242 }
243
244 /*****************************************************************************
245 Migrate account policies to passdb
246 *****************************************************************************/
247
248 static BOOL migrate_account_policy_names_to_passdb(void)
249 {
250         int i, tmp_val;
251         BOOL got_pol;
252
253         if (already_migrated_account_policies(False)) {
254                 return True;
255         }
256
257         DEBUG(1,("start migrating account policies into passdb\n"));
258
259         for (i=1; decode_account_policy_name(i) != NULL; i++) {
260
261                 got_pol = False;
262
263                 if (pdb_get_account_policy(i, &tmp_val)) {
264                         DEBUG(10,("account policy already in passdb\n"));
265                         got_pol = True;
266                 }
267
268                 if (!got_pol && !account_policy_get(i, &tmp_val)) {
269                         DEBUG(0,("very weird: could not get value for account policy\n"));
270                         return False;
271                 }
272
273                 DEBUGADD(1,("\tmigrating account policy (#%d: %s with value: %d) to passdb\n", 
274                         i, decode_account_policy_name(i), tmp_val));
275
276                 /* set policy via new passdb api */
277                 if (!pdb_set_account_policy(i, tmp_val)) {
278                         DEBUG(0,("failed to set account_policy\n"));
279                         return False;
280                 }
281
282         }
283
284         if (!already_migrated_account_policies(True)) {
285                 DEBUG(0,("could not store marker for account policy migration in the tdb\n"));
286                 return False;
287         }
288                 
289         DEBUGADD(1,("succesfully migrated account policies into passdb\n"));
290
291         return True;
292 }
293
294 /*****************************************************************************
295  Open the account policy tdb.
296 ***`*************************************************************************/
297
298 BOOL init_account_policy(void)
299 {
300
301         const char *vstring = "INFO/version";
302         uint32 version;
303         int i;
304
305         if (tdb)
306                 return True;
307
308         tdb = tdb_open_log(lock_path("account_policy.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
309         if (!tdb) {
310                 DEBUG(0,("Failed to open account policy database\n"));
311                 return False;
312         }
313
314         /* handle a Samba upgrade */
315         tdb_lock_bystring(tdb, vstring,0);
316         if (!tdb_fetch_uint32(tdb, vstring, &version) || version != DATABASE_VERSION) {
317
318                 tdb_store_uint32(tdb, vstring, DATABASE_VERSION);
319
320                 for (i=0; account_policy_names[i].field; i++) {
321
322                         if (!account_policy_set_default_on_empty(account_policy_names[i].field)) {
323                                 DEBUG(0,("failed to set default value in account policy tdb\n"));
324                                 return False;
325                         }
326                 }
327         }
328
329         if (!migrate_account_policy_names_to_passdb()) {
330                 DEBUG(0,("Could not migrate account policy tdb to passdb.\n"));
331                 return False;
332         }
333
334         tdb_unlock_bystring(tdb, vstring);
335
336         /* These exist by default on NT4 in [HKLM\SECURITY\Policy\Accounts] */
337
338         privilege_create_account( &global_sid_World );
339         privilege_create_account( &global_sid_Builtin_Administrators );
340         privilege_create_account( &global_sid_Builtin_Account_Operators );
341         privilege_create_account( &global_sid_Builtin_Server_Operators );
342         privilege_create_account( &global_sid_Builtin_Print_Operators );
343         privilege_create_account( &global_sid_Builtin_Backup_Operators );
344
345         return True;
346 }
347
348 /*****************************************************************************
349 Internal function
350 *****************************************************************************/
351
352 BOOL account_policy_get(int field, uint32 *value)
353 {
354         fstring name;
355         uint32 regval;
356
357         if (!init_account_policy())
358                 return False;
359
360         if (value)
361                 *value = 0;
362
363         fstrcpy(name, decode_account_policy_name(field));
364         if (!*name) {
365                 DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type!  Cannot get, returning 0.\n", field));
366                 return False;
367         }
368         if (!tdb_fetch_uint32(tdb, name, &regval)) {
369                 DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for field %d (%s), returning 0\n", field, name));
370                 return False;
371         }
372         if (value)
373                 *value = regval;
374
375         DEBUG(10,("account_policy_get: %s:%d\n", name, regval));
376         return True;
377 }
378
379
380 /****************************************************************************
381 Get an account policy from a (migrated tdb)
382 ****************************************************************************/
383
384 BOOL account_policy_set(int field, uint32 value)
385 {
386         fstring name;
387
388         if (!init_account_policy())
389                 return False;
390
391         fstrcpy(name, decode_account_policy_name(field));
392         if (!*name) {
393                 DEBUG(1, ("Field %d is not a valid account policy type!  Cannot set.\n", field));
394                 return False;
395         }
396
397         if (!tdb_store_uint32(tdb, name, value)) {
398                 DEBUG(1, ("tdb_store_uint32 failed for field %d (%s) on value %u\n", field, name, value));
399                 return False;
400         }
401
402         DEBUG(10,("account_policy_set: %s:%d\n", name, value));
403         
404         return True;
405 }
406
407 /****************************************************************************
408 Set an account policy in the cache 
409 ****************************************************************************/
410
411 BOOL cache_account_policy_set(int field, uint32 value)
412 {
413         uint32 lastset, i;
414
415         for (i=0; account_policy_names[i].field; i++) {
416
417                 if (account_policy_names[i].field == field) {
418
419                         DEBUG(10,("cache_account_policy_set: updating account pol cache\n"));
420
421                         if (!account_policy_set(field, value)) {
422                                 return False;
423                         }
424
425                         if (!account_policy_cache_timestamp(&lastset, True)) {
426                                 DEBUG(10,("cache_account_policy_set: failed to get lastest cache update timestamp\n"));
427                                 return False;
428                         }
429
430                         DEBUG(10,("cache_account_policy_set: cache valid until: %s\n", http_timestring(lastset+AP_TTL)));
431                 }
432         }
433
434         return True;
435 }
436
437 /*****************************************************************************
438 Get an account policy from the cache 
439 *****************************************************************************/
440
441 BOOL cache_account_policy_get(int field, uint32 *value)
442 {
443         uint32 lastset, i;
444
445         if (!account_policy_cache_timestamp(&lastset, False)) {
446                 DEBUG(10,("cache_account_policy_get: failed to get latest cache update timestamp\n"));
447                 return False;
448         }
449
450         if ((lastset + AP_TTL) < (uint32)time(NULL) ) {
451                 DEBUG(10,("cache_account_policy_get: no valid cache entry (cache expired)\n"));
452                 return False;
453         } 
454
455         for (i=0; account_policy_names[i].field; i++) {
456                 if (account_policy_names[i].field == field) {
457                         return account_policy_get(field, value);
458                 }
459         }
460
461         return False;
462 }
463
464
465 /****************************************************************************
466 ****************************************************************************/
467
468 TDB_CONTEXT *get_account_pol_tdb( void )
469 {
470
471         if ( !tdb ) {
472                 if ( !init_account_policy() )
473                         return NULL;
474         }
475
476         return tdb;
477 }
478