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