Fixed nasty cast of tdb_delete in traversals.
[ira/wip.git] / source3 / lib / account_pol.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  account policy storage
5  *  Copyright (C) Jean François Micouleau      1998-2001.
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *  
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "includes.h"
23 static TDB_CONTEXT *tdb; /* used for driver files */
24
25 #define DATABASE_VERSION 1
26
27 /****************************************************************************
28 open the account policy tdb
29 ****************************************************************************/
30 BOOL init_account_policy(void)
31 {
32         static pid_t local_pid;
33         char *vstring = "INFO/version";
34
35         if (tdb && local_pid == sys_getpid()) return True;
36         tdb = tdb_open_log(lock_path("account_policy.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
37         if (!tdb) {
38                 DEBUG(0,("Failed to open account policy database\n"));
39                 return False;
40         }
41
42         local_pid = sys_getpid();
43
44         /* handle a Samba upgrade */
45         tdb_lock_bystring(tdb, vstring);
46         if (tdb_fetch_int(tdb, vstring) != DATABASE_VERSION) {
47                 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
48                 tdb_store_int(tdb, vstring, DATABASE_VERSION);
49                 
50                 account_policy_set(AP_MIN_PASSWORD_LEN, MINPASSWDLENGTH);   /* 5 chars minimum             */
51                 account_policy_set(AP_PASSWORD_HISTORY, 0);                 /* don't keep any old password */
52                 account_policy_set(AP_USER_MUST_LOGON_TO_CHG_PASS, 0);      /* don't force user to logon   */
53                 account_policy_set(AP_MAX_PASSWORD_AGE, MAX_PASSWORD_AGE);  /* 21 days                     */
54                 account_policy_set(AP_MIN_PASSWORD_AGE, 0);                 /* 0 days                      */
55                 account_policy_set(AP_LOCK_ACCOUNT_DURATION, 0);            /* lockout for 0 minutes       */
56                 account_policy_set(AP_RESET_COUNT_TIME, 0);                 /* reset immediatly            */
57                 account_policy_set(AP_BAD_ATTEMPT_LOCKOUT, 0);              /* don't lockout               */
58                 account_policy_set(AP_TIME_TO_LOGOUT, -1);                  /* don't force logout          */
59         }
60         tdb_unlock_bystring(tdb, vstring);
61
62
63         return True;
64 }
65
66 /****************************************************************************
67 ****************************************************************************/
68
69 static char *decode_account_policy_name(int field)
70 {
71         switch (field) {
72                 case AP_MIN_PASSWORD_LEN:
73                         return "min password length";
74                 case AP_PASSWORD_HISTORY:
75                         return "password history";
76                 case AP_USER_MUST_LOGON_TO_CHG_PASS:
77                         return "user must logon to change password";
78                 case AP_MAX_PASSWORD_AGE:
79                         return "maximum password age";
80                 case AP_MIN_PASSWORD_AGE:
81                         return "minimum password age";
82                 case AP_LOCK_ACCOUNT_DURATION:
83                         return "lockout duration";
84                 case AP_RESET_COUNT_TIME:
85                         return "reset count minutes";
86                 case AP_BAD_ATTEMPT_LOCKOUT:
87                         return "bad lockout attempt";
88                 case AP_TIME_TO_LOGOUT:
89                         return "disconnect time";
90                 default:
91                         return "undefined value";
92         }
93 }
94
95
96 /****************************************************************************
97 ****************************************************************************/
98 BOOL account_policy_get(int field, uint32 *value)
99 {
100         fstring name;
101
102         init_account_policy();
103
104         fstrcpy(name, decode_account_policy_name(field));
105         *value=tdb_fetch_int(tdb, name);
106         DEBUG(10,("account_policy_get: %s:%d\n", name, *value));
107         return True;
108 }
109
110
111 /****************************************************************************
112 ****************************************************************************/
113 BOOL account_policy_set(int field, uint32 value)
114 {
115         fstring name;
116
117         init_account_policy();
118
119         fstrcpy(name, decode_account_policy_name(field));
120         if ( tdb_store_int(tdb, name, value)== -1)
121                 return False;
122         DEBUG(10,("account_policy_set: %s:%d\n", name, value));
123         
124         return True;
125 }
126