a66c48d396733ba5bf16b2afd6cc291bac1350ed
[samba.git] / source3 / passdb / 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 3 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, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "dbwrap.h"
24 static struct db_context *db;
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_TTL                  60
32
33
34 struct ap_table {
35         enum pdb_policy_type type;
36         const char *string;
37         uint32 default_val;
38         const char *description;
39         const char *ldap_attr;
40 };
41
42 static const struct ap_table account_policy_names[] = {
43         {PDB_POLICY_MIN_PASSWORD_LEN, "min password length", MINPASSWDLENGTH,
44                 "Minimal password length (default: 5)",
45                 "sambaMinPwdLength" },
46
47         {PDB_POLICY_PASSWORD_HISTORY, "password history", 0,
48                 "Length of Password History Entries (default: 0 => off)",
49                 "sambaPwdHistoryLength" },
50
51         {PDB_POLICY_USER_MUST_LOGON_TO_CHG_PASS, "user must logon to change password", 0,
52                 "Force Users to logon for password change (default: 0 => off, 2 => on)",
53                 "sambaLogonToChgPwd" },
54
55         {PDB_POLICY_MAX_PASSWORD_AGE, "maximum password age", (uint32) -1,
56                 "Maximum password age, in seconds (default: -1 => never expire passwords)",
57                 "sambaMaxPwdAge" },
58
59         {PDB_POLICY_MIN_PASSWORD_AGE,"minimum password age", 0,
60                 "Minimal password age, in seconds (default: 0 => allow immediate password change)",
61                 "sambaMinPwdAge" },
62
63         {PDB_POLICY_LOCK_ACCOUNT_DURATION, "lockout duration", 30,
64                 "Lockout duration in minutes (default: 30, -1 => forever)",
65                 "sambaLockoutDuration" },
66
67         {PDB_POLICY_RESET_COUNT_TIME, "reset count minutes", 30,
68                 "Reset time after lockout in minutes (default: 30)",
69                 "sambaLockoutObservationWindow" },
70
71         {PDB_POLICY_BAD_ATTEMPT_LOCKOUT, "bad lockout attempt", 0,
72                 "Lockout users after bad logon attempts (default: 0 => off)",
73                 "sambaLockoutThreshold" },
74
75         {PDB_POLICY_TIME_TO_LOGOUT, "disconnect time", (uint32) -1,
76                 "Disconnect Users outside logon hours (default: -1 => off, 0 => on)",
77                 "sambaForceLogoff" },
78
79         {PDB_POLICY_REFUSE_MACHINE_PW_CHANGE, "refuse machine password change", 0,
80                 "Allow Machine Password changes (default: 0 => off)",
81                 "sambaRefuseMachinePwdChange" },
82
83         {0, NULL, 0, "", NULL}
84 };
85
86 void account_policy_names_list(const char ***names, int *num_names)
87 {
88         const char **nl;
89         int i, count;
90
91         for (count=0; account_policy_names[count].string; count++) {
92         }
93         nl = SMB_MALLOC_ARRAY(const char *, count);
94         if (!nl) {
95                 *num_names = 0;
96                 return;
97         }
98         for (i=0; account_policy_names[i].string; i++) {
99                 nl[i] = account_policy_names[i].string;
100         }
101         *num_names = count;
102         *names = nl;
103         return;
104 }
105
106 /****************************************************************************
107 Get the account policy name as a string from its #define'ed number
108 ****************************************************************************/
109
110 const char *decode_account_policy_name(enum pdb_policy_type type)
111 {
112         int i;
113         for (i=0; account_policy_names[i].string; i++) {
114                 if (type == account_policy_names[i].type) {
115                         return account_policy_names[i].string;
116                 }
117         }
118         return NULL;
119 }
120
121 /****************************************************************************
122 Get the account policy LDAP attribute as a string from its #define'ed number
123 ****************************************************************************/
124
125 const char *get_account_policy_attr(enum pdb_policy_type type)
126 {
127         int i;
128         for (i=0; account_policy_names[i].type; i++) {
129                 if (type == account_policy_names[i].type) {
130                         return account_policy_names[i].ldap_attr;
131                 }
132         }
133         return NULL;
134 }
135
136 /****************************************************************************
137 Get the account policy description as a string from its #define'ed number
138 ****************************************************************************/
139
140 const char *account_policy_get_desc(enum pdb_policy_type type)
141 {
142         int i;
143         for (i=0; account_policy_names[i].string; i++) {
144                 if (type == account_policy_names[i].type) {
145                         return account_policy_names[i].description;
146                 }
147         }
148         return NULL;
149 }
150
151 /****************************************************************************
152 Get the account policy name as a string from its #define'ed number
153 ****************************************************************************/
154
155 enum pdb_policy_type account_policy_name_to_typenum(const char *name)
156 {
157         int i;
158         for (i=0; account_policy_names[i].string; i++) {
159                 if (strcmp(name, account_policy_names[i].string) == 0) {
160                         return account_policy_names[i].type;
161                 }
162         }
163         return 0;
164 }
165
166 /*****************************************************************************
167 Get default value for account policy
168 *****************************************************************************/
169
170 bool account_policy_get_default(enum pdb_policy_type type, uint32_t *val)
171 {
172         int i;
173         for (i=0; account_policy_names[i].type; i++) {
174                 if (account_policy_names[i].type == type) {
175                         *val = account_policy_names[i].default_val;
176                         return True;
177                 }
178         }
179         DEBUG(0,("no default for account_policy index %d found. This should never happen\n",
180                 type));
181         return False;
182 }
183
184 /*****************************************************************************
185  Set default for a type if it is empty
186 *****************************************************************************/
187
188 static bool account_policy_set_default_on_empty(enum pdb_policy_type type)
189 {
190
191         uint32 value;
192
193         if (!account_policy_get(type, &value) &&
194             !account_policy_get_default(type, &value)) {
195                 return False;
196         }
197
198         return account_policy_set(type, value);
199 }
200
201 /*****************************************************************************
202  Open the account policy tdb.
203 ***`*************************************************************************/
204
205 bool init_account_policy(void)
206 {
207
208         const char *vstring = "INFO/version";
209         uint32 version;
210         int i;
211
212         if (db != NULL) {
213                 return True;
214         }
215
216         db = db_open(NULL, state_path("account_policy.tdb"), 0, TDB_DEFAULT,
217                      O_RDWR, 0600);
218
219         if (db == NULL) { /* the account policies files does not exist or open
220                            * failed, try to create a new one */
221                 db = db_open(NULL, state_path("account_policy.tdb"), 0,
222                              TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
223                 if (db == NULL) {
224                         DEBUG(0,("Failed to open account policy database\n"));
225                         return False;
226                 }
227         }
228
229         version = dbwrap_fetch_int32(db, vstring);
230         if (version == DATABASE_VERSION) {
231                 return true;
232         }
233
234         /* handle a Samba upgrade */
235
236         if (db->transaction_start(db) != 0) {
237                 DEBUG(0, ("transaction_start failed\n"));
238                 TALLOC_FREE(db);
239                 return false;
240         }
241
242         version = dbwrap_fetch_int32(db, vstring);
243         if (version == DATABASE_VERSION) {
244                 /*
245                  * Race condition
246                  */
247                 if (db->transaction_cancel(db)) {
248                         smb_panic("transaction_cancel failed");
249                 }
250                 return true;
251         }
252
253         if (version != DATABASE_VERSION) {
254                 if (dbwrap_store_uint32(db, vstring, DATABASE_VERSION) != 0) {
255                         DEBUG(0, ("dbwrap_store_uint32 failed\n"));
256                         goto cancel;
257                 }
258
259                 for (i=0; account_policy_names[i].type; i++) {
260
261                         if (!account_policy_set_default_on_empty(account_policy_names[i].type)) {
262                                 DEBUG(0,("failed to set default value in account policy tdb\n"));
263                                 goto cancel;
264                         }
265                 }
266         }
267
268         /* These exist by default on NT4 in [HKLM\SECURITY\Policy\Accounts] */
269
270         privilege_create_account( &global_sid_World );
271         privilege_create_account( &global_sid_Builtin_Account_Operators );
272         privilege_create_account( &global_sid_Builtin_Server_Operators );
273         privilege_create_account( &global_sid_Builtin_Print_Operators );
274         privilege_create_account( &global_sid_Builtin_Backup_Operators );
275
276         /* BUILTIN\Administrators get everything -- *always* */
277
278         if ( lp_enable_privileges() ) {
279                 if ( !grant_all_privileges( &global_sid_Builtin_Administrators ) ) {
280                         DEBUG(1,("init_account_policy: Failed to grant privileges "
281                                 "to BUILTIN\\Administrators!\n"));
282                 }
283         }
284
285         if (db->transaction_commit(db) != 0) {
286                 DEBUG(0, ("transaction_commit failed\n"));
287                 TALLOC_FREE(db);
288                 return false;
289         }
290
291         return True;
292
293  cancel:
294         if (db->transaction_cancel(db)) {
295                 smb_panic("transaction_cancel failed");
296         }
297         TALLOC_FREE(db);
298
299         return false;
300 }
301
302 /*****************************************************************************
303 Get an account policy (from tdb)
304 *****************************************************************************/
305
306 bool account_policy_get(enum pdb_policy_type type, uint32_t *value)
307 {
308         const char *name;
309         uint32 regval;
310
311         if (!init_account_policy()) {
312                 return False;
313         }
314
315         if (value) {
316                 *value = 0;
317         }
318
319         name = decode_account_policy_name(type);
320         if (name == NULL) {
321                 DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type!  Cannot get, returning 0.\n", type));
322                 return False;
323         }
324
325         if (!dbwrap_fetch_uint32(db, name, &regval)) {
326                 DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for type %d (%s), returning 0\n", type, name));
327                 return False;
328         }
329
330         if (value) {
331                 *value = regval;
332         }
333
334         DEBUG(10,("account_policy_get: name: %s, val: %d\n", name, regval));
335         return True;
336 }
337
338
339 /****************************************************************************
340 Set an account policy (in tdb)
341 ****************************************************************************/
342
343 bool account_policy_set(enum pdb_policy_type type, uint32_t value)
344 {
345         const char *name;
346         NTSTATUS status;
347
348         if (!init_account_policy()) {
349                 return False;
350         }
351
352         name = decode_account_policy_name(type);
353         if (name == NULL) {
354                 DEBUG(1, ("Field %d is not a valid account policy type!  Cannot set.\n", type));
355                 return False;
356         }
357
358         status = dbwrap_trans_store_uint32(db, name, value);
359         if (!NT_STATUS_IS_OK(status)) {
360                 DEBUG(1, ("store_uint32 failed for type %d (%s) on value "
361                           "%u: %s\n", type, name, value, nt_errstr(status)));
362                 return False;
363         }
364
365         DEBUG(10,("account_policy_set: name: %s, value: %d\n", name, value));
366
367         return True;
368 }
369
370 /****************************************************************************
371 Set an account policy in the cache
372 ****************************************************************************/
373
374 bool cache_account_policy_set(enum pdb_policy_type type, uint32_t value)
375 {
376         const char *policy_name = NULL;
377         char *cache_key = NULL;
378         char *cache_value = NULL;
379         bool ret = False;
380
381         policy_name = decode_account_policy_name(type);
382         if (policy_name == NULL) {
383                 DEBUG(0,("cache_account_policy_set: no policy found\n"));
384                 return False;
385         }
386
387         if (asprintf(&cache_key, "ACCT_POL/%s", policy_name) < 0) {
388                 DEBUG(0, ("asprintf failed\n"));
389                 goto done;
390         }
391
392         if (asprintf(&cache_value, "%lu\n", (unsigned long)value) < 0) {
393                 DEBUG(0, ("asprintf failed\n"));
394                 goto done;
395         }
396
397         DEBUG(10,("cache_account_policy_set: updating account pol cache\n"));
398
399         ret = gencache_set(cache_key, cache_value, time(NULL)+AP_TTL);
400
401  done:
402         SAFE_FREE(cache_key);
403         SAFE_FREE(cache_value);
404         return ret;
405 }
406
407 /*****************************************************************************
408 Get an account policy from the cache
409 *****************************************************************************/
410
411 bool cache_account_policy_get(enum pdb_policy_type type, uint32_t *value)
412 {
413         const char *policy_name = NULL;
414         char *cache_key = NULL;
415         char *cache_value = NULL;
416         bool ret = False;
417
418         policy_name = decode_account_policy_name(type);
419         if (policy_name == NULL) {
420                 DEBUG(0,("cache_account_policy_set: no policy found\n"));
421                 return False;
422         }
423
424         if (asprintf(&cache_key, "ACCT_POL/%s", policy_name) < 0) {
425                 DEBUG(0, ("asprintf failed\n"));
426                 goto done;
427         }
428
429         if (gencache_get(cache_key, &cache_value, NULL)) {
430                 uint32 tmp = strtoul(cache_value, NULL, 10);
431                 *value = tmp;
432                 ret = True;
433         }
434
435  done:
436         SAFE_FREE(cache_key);
437         SAFE_FREE(cache_value);
438         return ret;
439 }
440
441 /****************************************************************************
442 ****************************************************************************/
443
444 struct db_context *get_account_pol_db( void )
445 {
446
447         if ( db == NULL ) {
448                 if ( !init_account_policy() ) {
449                         return NULL;
450                 }
451         }
452
453         return db;
454 }