r10656: BIG merge from trunk. Features not copied over
[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_TTL                  60
33
34
35 struct ap_table {
36         int field;
37         const char *string;
38         uint32 default_val;
39         const char *description;
40         const char *ldap_attr;
41 };
42
43 static const struct ap_table account_policy_names[] = {
44         {AP_MIN_PASSWORD_LEN, "min password length", MINPASSWDLENGTH, 
45                 "Minimal password length (default: 5)", 
46                 "sambaMinPwdLength" },
47
48         {AP_PASSWORD_HISTORY, "password history", 0,
49                 "Length of Password History Entries (default: 0 => off)", 
50                 "sambaPwdHistoryLength" },
51                 
52         {AP_USER_MUST_LOGON_TO_CHG_PASS, "user must logon to change password", 0,
53                 "Force Users to logon for password change (default: 0 => off, 2 => on)",
54                 "sambaLogonToChgPwd" },
55         
56         {AP_MAX_PASSWORD_AGE, "maximum password age", (uint32) -1,
57                 "Maximum password age, in seconds (default: -1 => never expire passwords)", 
58                 "sambaMaxPwdAge" },
59                 
60         {AP_MIN_PASSWORD_AGE,"minimum password age", 0,
61                 "Minimal password age, in seconds (default: 0 => allow immediate password change)", 
62                 "sambaMinPwdAge" },
63                 
64         {AP_LOCK_ACCOUNT_DURATION, "lockout duration", 30,
65                 "Lockout duration in minutes (default: 30, -1 => forever)",
66                 "sambaLockoutDuration" },
67                 
68         {AP_RESET_COUNT_TIME, "reset count minutes", 30,
69                 "Reset time after lockout in minutes (default: 30)", 
70                 "sambaLockoutObservationWindow" },
71                 
72         {AP_BAD_ATTEMPT_LOCKOUT, "bad lockout attempt", 0,
73                 "Lockout users after bad logon attempts (default: 0 => off)", 
74                 "sambaLockoutThreshold" },
75                 
76         {AP_TIME_TO_LOGOUT, "disconnect time", -1,
77                 "Disconnect Users outside logon hours (default: -1 => off, 0 => on)", 
78                 "sambaForceLogoff" }, 
79                 
80         {AP_REFUSE_MACHINE_PW_CHANGE, "refuse machine password change", 0,
81                 "Allow Machine Password changes (default: 0 => off)",
82                 "sambaRefuseMachinePwdChange" },
83                 
84         {0, NULL, 0, "", NULL}
85 };
86
87 char *account_policy_names_list(void)
88 {
89         char *nl, *p;
90         int i;
91         size_t len = 0;
92
93         for (i=0; account_policy_names[i].string; i++) {
94                 len += strlen(account_policy_names[i].string) + 1;
95         }
96         len++;
97         nl = SMB_MALLOC(len);
98         if (!nl) {
99                 return NULL;
100         }
101         p = nl;
102         for (i=0; account_policy_names[i].string; i++) {
103                 memcpy(p, account_policy_names[i].string, strlen(account_policy_names[i].string) + 1);
104                 p[strlen(account_policy_names[i].string)] = '\n';
105                 p += strlen(account_policy_names[i].string) + 1;
106         }
107         *p = '\0';
108         return nl;
109 }
110
111 /****************************************************************************
112 Get the account policy name as a string from its #define'ed number
113 ****************************************************************************/
114
115 const char *decode_account_policy_name(int field)
116 {
117         int i;
118         for (i=0; account_policy_names[i].string; i++) {
119                 if (field == account_policy_names[i].field)
120                         return account_policy_names[i].string;
121         }
122         return NULL;
123 }
124
125 /****************************************************************************
126 Get the account policy LDAP attribute as a string from its #define'ed number
127 ****************************************************************************/
128
129 const char *get_account_policy_attr(int field)
130 {
131         int i;
132         for (i=0; account_policy_names[i].field; i++) {
133                 if (field == account_policy_names[i].field)
134                         return account_policy_names[i].ldap_attr;
135         }
136         return NULL;
137 }
138
139 /****************************************************************************
140 Get the account policy description as a string from its #define'ed number
141 ****************************************************************************/
142
143 const char *account_policy_get_desc(int field)
144 {
145         int i;
146         for (i=0; account_policy_names[i].string; i++) {
147                 if (field == account_policy_names[i].field)
148                         return account_policy_names[i].description;
149         }
150         return NULL;
151 }
152
153 /****************************************************************************
154 Get the account policy name as a string from its #define'ed number
155 ****************************************************************************/
156
157 int account_policy_name_to_fieldnum(const char *name)
158 {
159         int i;
160         for (i=0; account_policy_names[i].string; i++) {
161                 if (strcmp(name, account_policy_names[i].string) == 0)
162                         return account_policy_names[i].field;
163         }
164         return 0;
165 }
166
167 /*****************************************************************************
168 Update LAST-Set counter inside the cache
169 *****************************************************************************/
170
171 static BOOL account_policy_cache_timestamp(uint32 *value, BOOL update, 
172                                            const char *ap_name)
173 {
174         pstring key;
175         uint32 val = 0;
176         time_t now;
177
178         if (ap_name == NULL)
179                 return False;
180                 
181         slprintf(key, sizeof(key)-1, "%s/%s", ap_name, AP_LASTSET);
182
183         if (!init_account_policy())
184                 return False;
185
186         if (!tdb_fetch_uint32(tdb, key, &val) && !update) {
187                 DEBUG(10,("failed to get last set timestamp of cache\n"));
188                 return False;
189         }
190
191         *value = val;
192
193         DEBUG(10, ("account policy cache lastset was: %s\n", http_timestring(val)));
194
195         if (update) {
196
197                 now = time(NULL);
198
199                 if (!tdb_store_uint32(tdb, key, (uint32)now)) {
200                         DEBUG(1, ("tdb_store_uint32 failed for %s\n", key));
201                         return False;
202                 }
203                 DEBUG(10, ("account policy cache lastset now: %s\n", http_timestring(now)));
204                 *value = now;
205         }
206
207         return True;
208 }
209
210 /*****************************************************************************
211 Get default value for account policy
212 *****************************************************************************/
213
214 BOOL account_policy_get_default(int account_policy, uint32 *val)
215 {
216         int i;
217         for (i=0; account_policy_names[i].field; i++) {
218                 if (account_policy_names[i].field == account_policy) {
219                         *val = account_policy_names[i].default_val;
220                         return True;
221                 }
222         }
223         DEBUG(0,("no default for account_policy index %d found. This should never happen\n", 
224                 account_policy));
225         return False;
226 }
227
228 /*****************************************************************************
229  Set default for a field if it is empty
230 *****************************************************************************/
231
232 static BOOL account_policy_set_default_on_empty(int account_policy)
233 {
234
235         uint32 value;
236
237         if (!account_policy_get(account_policy, &value) && 
238             !account_policy_get_default(account_policy, &value)) {
239                 return False;
240         }
241
242         return account_policy_set(account_policy, value);
243 }
244
245 /*****************************************************************************
246  Open the account policy tdb.
247 ***`*************************************************************************/
248
249 BOOL init_account_policy(void)
250 {
251
252         const char *vstring = "INFO/version";
253         uint32 version;
254         int i;
255
256         if (tdb)
257                 return True;
258
259         tdb = tdb_open_log(lock_path("account_policy.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
260         if (!tdb) {
261                 DEBUG(0,("Failed to open account policy database\n"));
262                 return False;
263         }
264
265         /* handle a Samba upgrade */
266         tdb_lock_bystring(tdb, vstring,0);
267         if (!tdb_fetch_uint32(tdb, vstring, &version) || version != DATABASE_VERSION) {
268
269                 tdb_store_uint32(tdb, vstring, DATABASE_VERSION);
270
271                 for (i=0; account_policy_names[i].field; i++) {
272
273                         if (!account_policy_set_default_on_empty(account_policy_names[i].field)) {
274                                 DEBUG(0,("failed to set default value in account policy tdb\n"));
275                                 return False;
276                         }
277                 }
278         }
279
280         tdb_unlock_bystring(tdb, vstring);
281
282         /* These exist by default on NT4 in [HKLM\SECURITY\Policy\Accounts] */
283
284         privilege_create_account( &global_sid_World );
285         privilege_create_account( &global_sid_Builtin_Administrators );
286         privilege_create_account( &global_sid_Builtin_Account_Operators );
287         privilege_create_account( &global_sid_Builtin_Server_Operators );
288         privilege_create_account( &global_sid_Builtin_Print_Operators );
289         privilege_create_account( &global_sid_Builtin_Backup_Operators );
290
291         return True;
292 }
293
294 /*****************************************************************************
295 Get an account policy (from tdb) 
296 *****************************************************************************/
297
298 BOOL account_policy_get(int field, uint32 *value)
299 {
300         fstring name;
301         uint32 regval;
302
303         if (!init_account_policy())
304                 return False;
305
306         if (value)
307                 *value = 0;
308
309         fstrcpy(name, decode_account_policy_name(field));
310         if (!*name) {
311                 DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type!  Cannot get, returning 0.\n", field));
312                 return False;
313         }
314         if (!tdb_fetch_uint32(tdb, name, &regval)) {
315                 DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for field %d (%s), returning 0\n", field, name));
316                 return False;
317         }
318         if (value)
319                 *value = regval;
320
321         DEBUG(10,("account_policy_get: name: %s, val: %d\n", name, regval));
322         return True;
323 }
324
325
326 /****************************************************************************
327 Set an account policy (in tdb) 
328 ****************************************************************************/
329
330 BOOL account_policy_set(int field, uint32 value)
331 {
332         fstring name;
333
334         if (!init_account_policy())
335                 return False;
336
337         fstrcpy(name, decode_account_policy_name(field));
338         if (!*name) {
339                 DEBUG(1, ("Field %d is not a valid account policy type!  Cannot set.\n", field));
340                 return False;
341         }
342
343         if (!tdb_store_uint32(tdb, name, value)) {
344                 DEBUG(1, ("tdb_store_uint32 failed for field %d (%s) on value %u\n", field, name, value));
345                 return False;
346         }
347
348         DEBUG(10,("account_policy_set: name: %s, value: %d\n", name, value));
349         
350         return True;
351 }
352
353 /****************************************************************************
354 Set an account policy in the cache 
355 ****************************************************************************/
356
357 BOOL cache_account_policy_set(int field, uint32 value)
358 {
359         uint32 lastset;
360         const char *policy_name = NULL;
361
362         policy_name = decode_account_policy_name(field);
363         if (policy_name == NULL) {
364                 DEBUG(0,("cache_account_policy_set: no policy found\n"));
365                 return False;
366         }
367
368         DEBUG(10,("cache_account_policy_set: updating account pol cache\n"));
369
370         if (!account_policy_set(field, value)) {
371                 return False;
372         }
373
374         if (!account_policy_cache_timestamp(&lastset, True, policy_name)) 
375         {
376                 DEBUG(10,("cache_account_policy_set: failed to get lastest cache update timestamp\n"));
377                 return False;
378         }
379
380         DEBUG(10,("cache_account_policy_set: cache valid until: %s\n", http_timestring(lastset+AP_TTL)));
381
382         return True;
383 }
384
385 /*****************************************************************************
386 Get an account policy from the cache 
387 *****************************************************************************/
388
389 BOOL cache_account_policy_get(int field, uint32 *value)
390 {
391         uint32 lastset;
392
393         if (!account_policy_cache_timestamp(&lastset, False, 
394                                             decode_account_policy_name(field))) 
395         {
396                 DEBUG(10,("cache_account_policy_get: failed to get latest cache update timestamp\n"));
397                 return False;
398         }
399
400         if ((lastset + AP_TTL) < (uint32)time(NULL) ) {
401                 DEBUG(10,("cache_account_policy_get: no valid cache entry (cache expired)\n"));
402                 return False;
403         } 
404
405         return account_policy_get(field, value);
406 }
407
408
409 /****************************************************************************
410 ****************************************************************************/
411
412 TDB_CONTEXT *get_account_pol_tdb( void )
413 {
414
415         if ( !tdb ) {
416                 if ( !init_account_policy() )
417                         return NULL;
418         }
419
420         return tdb;
421 }
422