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