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