s4-dsdb Add ability to force a particular SID in the upgrade case
[idra/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 "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(const char ***names, int *num_names)
93 {
94         const char **nl;
95         int i, count;
96
97         for (count=0; account_policy_names[count].string; count++) {
98         }
99         nl = SMB_MALLOC_ARRAY(const char *, count);
100         if (!nl) {
101                 *num_names = 0;
102                 return;
103         }
104         for (i=0; account_policy_names[i].string; i++) {
105                 nl[i] = account_policy_names[i].string;
106         }
107         *num_names = count;
108         *names = nl;
109         return;
110 }
111
112 /****************************************************************************
113 Get the account policy name as a string from its #define'ed number
114 ****************************************************************************/
115
116 const char *decode_account_policy_name(enum pdb_policy_type type)
117 {
118         int i;
119         for (i=0; account_policy_names[i].string; i++) {
120                 if (type == account_policy_names[i].type) {
121                         return account_policy_names[i].string;
122                 }
123         }
124         return NULL;
125 }
126
127 /****************************************************************************
128 Get the account policy LDAP attribute as a string from its #define'ed number
129 ****************************************************************************/
130
131 const char *get_account_policy_attr(enum pdb_policy_type type)
132 {
133         int i;
134         for (i=0; account_policy_names[i].type; i++) {
135                 if (type == account_policy_names[i].type) {
136                         return account_policy_names[i].ldap_attr;
137                 }
138         }
139         return NULL;
140 }
141
142 /****************************************************************************
143 Get the account policy description as a string from its #define'ed number
144 ****************************************************************************/
145
146 const char *account_policy_get_desc(enum pdb_policy_type type)
147 {
148         int i;
149         for (i=0; account_policy_names[i].string; i++) {
150                 if (type == account_policy_names[i].type) {
151                         return account_policy_names[i].description;
152                 }
153         }
154         return NULL;
155 }
156
157 /****************************************************************************
158 Get the account policy name as a string from its #define'ed number
159 ****************************************************************************/
160
161 enum pdb_policy_type account_policy_name_to_typenum(const char *name)
162 {
163         int i;
164         for (i=0; account_policy_names[i].string; i++) {
165                 if (strcmp(name, account_policy_names[i].string) == 0) {
166                         return account_policy_names[i].type;
167                 }
168         }
169         return 0;
170 }
171
172 /*****************************************************************************
173 Get default value for account policy
174 *****************************************************************************/
175
176 bool account_policy_get_default(enum pdb_policy_type type, uint32_t *val)
177 {
178         int i;
179         for (i=0; account_policy_names[i].type; i++) {
180                 if (account_policy_names[i].type == type) {
181                         *val = account_policy_names[i].default_val;
182                         return True;
183                 }
184         }
185         DEBUG(0,("no default for account_policy index %d found. This should never happen\n",
186                 type));
187         return False;
188 }
189
190 /*****************************************************************************
191  Set default for a type if it is empty
192 *****************************************************************************/
193
194 static bool account_policy_set_default_on_empty(enum pdb_policy_type type)
195 {
196
197         uint32 value;
198
199         if (!account_policy_get(type, &value) &&
200             !account_policy_get_default(type, &value)) {
201                 return False;
202         }
203
204         return account_policy_set(type, value);
205 }
206
207 /*****************************************************************************
208  Open the account policy tdb.
209 ***`*************************************************************************/
210
211 bool init_account_policy(void)
212 {
213
214         const char *vstring = "INFO/version";
215         uint32 version;
216         int i;
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);
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                 if (db == NULL) {
230                         DEBUG(0,("Failed to open account policy database\n"));
231                         return False;
232                 }
233         }
234
235         version = dbwrap_fetch_int32(db, vstring);
236         if (version == DATABASE_VERSION) {
237                 return true;
238         }
239
240         /* handle a Samba upgrade */
241
242         if (db->transaction_start(db) != 0) {
243                 DEBUG(0, ("transaction_start failed\n"));
244                 TALLOC_FREE(db);
245                 return false;
246         }
247
248         version = dbwrap_fetch_int32(db, vstring);
249         if (version == DATABASE_VERSION) {
250                 /*
251                  * Race condition
252                  */
253                 if (db->transaction_cancel(db)) {
254                         smb_panic("transaction_cancel failed");
255                 }
256                 return true;
257         }
258
259         if (version != DATABASE_VERSION) {
260                 if (dbwrap_store_uint32(db, vstring, DATABASE_VERSION) != 0) {
261                         DEBUG(0, ("dbwrap_store_uint32 failed\n"));
262                         goto cancel;
263                 }
264
265                 for (i=0; account_policy_names[i].type; i++) {
266
267                         if (!account_policy_set_default_on_empty(account_policy_names[i].type)) {
268                                 DEBUG(0,("failed to set default value in account policy tdb\n"));
269                                 goto cancel;
270                         }
271                 }
272         }
273
274         /* These exist by default on NT4 in [HKLM\SECURITY\Policy\Accounts] */
275
276         privilege_create_account( &global_sid_World );
277         privilege_create_account( &global_sid_Builtin_Account_Operators );
278         privilege_create_account( &global_sid_Builtin_Server_Operators );
279         privilege_create_account( &global_sid_Builtin_Print_Operators );
280         privilege_create_account( &global_sid_Builtin_Backup_Operators );
281
282         /* BUILTIN\Administrators get everything -- *always* */
283
284         if ( lp_enable_privileges() ) {
285                 if ( !grant_all_privileges( &global_sid_Builtin_Administrators ) ) {
286                         DEBUG(1,("init_account_policy: Failed to grant privileges "
287                                 "to BUILTIN\\Administrators!\n"));
288                 }
289         }
290
291         if (db->transaction_commit(db) != 0) {
292                 DEBUG(0, ("transaction_commit failed\n"));
293                 TALLOC_FREE(db);
294                 return false;
295         }
296
297         return True;
298
299  cancel:
300         if (db->transaction_cancel(db)) {
301                 smb_panic("transaction_cancel failed");
302         }
303         TALLOC_FREE(db);
304
305         return false;
306 }
307
308 /*****************************************************************************
309 Get an account policy (from tdb)
310 *****************************************************************************/
311
312 bool account_policy_get(enum pdb_policy_type type, uint32_t *value)
313 {
314         const char *name;
315         uint32 regval;
316
317         if (!init_account_policy()) {
318                 return False;
319         }
320
321         if (value) {
322                 *value = 0;
323         }
324
325         name = decode_account_policy_name(type);
326         if (name == NULL) {
327                 DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type!  Cannot get, returning 0.\n", type));
328                 return False;
329         }
330
331         if (!dbwrap_fetch_uint32(db, name, &regval)) {
332                 DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for type %d (%s), returning 0\n", type, name));
333                 return False;
334         }
335
336         if (value) {
337                 *value = regval;
338         }
339
340         DEBUG(10,("account_policy_get: name: %s, val: %d\n", name, regval));
341         return True;
342 }
343
344
345 /****************************************************************************
346 Set an account policy (in tdb)
347 ****************************************************************************/
348
349 bool account_policy_set(enum pdb_policy_type type, uint32_t value)
350 {
351         const char *name;
352         NTSTATUS status;
353
354         if (!init_account_policy()) {
355                 return False;
356         }
357
358         name = decode_account_policy_name(type);
359         if (name == NULL) {
360                 DEBUG(1, ("Field %d is not a valid account policy type!  Cannot set.\n", type));
361                 return False;
362         }
363
364         status = dbwrap_trans_store_uint32(db, name, value);
365         if (!NT_STATUS_IS_OK(status)) {
366                 DEBUG(1, ("store_uint32 failed for type %d (%s) on value "
367                           "%u: %s\n", type, name, value, nt_errstr(status)));
368                 return False;
369         }
370
371         DEBUG(10,("account_policy_set: name: %s, value: %d\n", name, value));
372
373         return True;
374 }
375
376 /****************************************************************************
377 Set an account policy in the cache
378 ****************************************************************************/
379
380 bool cache_account_policy_set(enum pdb_policy_type type, uint32_t value)
381 {
382         const char *policy_name = NULL;
383         char *cache_key = NULL;
384         char *cache_value = NULL;
385         bool ret = False;
386
387         policy_name = decode_account_policy_name(type);
388         if (policy_name == NULL) {
389                 DEBUG(0,("cache_account_policy_set: no policy found\n"));
390                 return False;
391         }
392
393         if (asprintf(&cache_key, "ACCT_POL/%s", policy_name) < 0) {
394                 DEBUG(0, ("asprintf failed\n"));
395                 goto done;
396         }
397
398         if (asprintf(&cache_value, "%lu\n", (unsigned long)value) < 0) {
399                 DEBUG(0, ("asprintf failed\n"));
400                 goto done;
401         }
402
403         DEBUG(10,("cache_account_policy_set: updating account pol cache\n"));
404
405         ret = gencache_set(cache_key, cache_value, time(NULL)+AP_TTL);
406
407  done:
408         SAFE_FREE(cache_key);
409         SAFE_FREE(cache_value);
410         return ret;
411 }
412
413 /*****************************************************************************
414 Get an account policy from the cache
415 *****************************************************************************/
416
417 bool cache_account_policy_get(enum pdb_policy_type type, uint32_t *value)
418 {
419         const char *policy_name = NULL;
420         char *cache_key = NULL;
421         char *cache_value = NULL;
422         bool ret = False;
423
424         policy_name = decode_account_policy_name(type);
425         if (policy_name == NULL) {
426                 DEBUG(0,("cache_account_policy_set: no policy found\n"));
427                 return False;
428         }
429
430         if (asprintf(&cache_key, "ACCT_POL/%s", policy_name) < 0) {
431                 DEBUG(0, ("asprintf failed\n"));
432                 goto done;
433         }
434
435         if (gencache_get(cache_key, &cache_value, NULL)) {
436                 uint32 tmp = strtoul(cache_value, NULL, 10);
437                 *value = tmp;
438                 ret = True;
439         }
440
441  done:
442         SAFE_FREE(cache_key);
443         SAFE_FREE(cache_value);
444         return ret;
445 }
446
447 /****************************************************************************
448 ****************************************************************************/
449
450 struct db_context *get_account_pol_db( void )
451 {
452
453         if ( db == NULL ) {
454                 if ( !init_account_policy() ) {
455                         return NULL;
456                 }
457         }
458
459         return db;
460 }