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