126ee291d887c105aff162d9850da5ff62cc0a1d
[samba.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
31 BOOL init_account_policy(void)
32 {
33         static pid_t local_pid;
34         char *vstring = "INFO/version";
35
36         if (tdb && local_pid == sys_getpid())
37                 return True;
38         tdb = tdb_open_log(lock_path("account_policy.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
39         if (!tdb) {
40                 DEBUG(0,("Failed to open account policy database\n"));
41                 return False;
42         }
43
44         local_pid = sys_getpid();
45
46         /* handle a Samba upgrade */
47         tdb_lock_bystring(tdb, vstring);
48         if (tdb_fetch_int32(tdb, vstring) != DATABASE_VERSION) {
49                 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
50                 tdb_store_int32(tdb, vstring, DATABASE_VERSION);
51                 
52                 account_policy_set(AP_MIN_PASSWORD_LEN, MINPASSWDLENGTH);   /* 5 chars minimum             */
53                 account_policy_set(AP_PASSWORD_HISTORY, 0);                 /* don't keep any old password */
54                 account_policy_set(AP_USER_MUST_LOGON_TO_CHG_PASS, 0);      /* don't force user to logon   */
55                 account_policy_set(AP_MAX_PASSWORD_AGE, MAX_PASSWORD_AGE);  /* 21 days                     */
56                 account_policy_set(AP_MIN_PASSWORD_AGE, 0);                 /* 0 days                      */
57                 account_policy_set(AP_LOCK_ACCOUNT_DURATION, 0);            /* lockout for 0 minutes       */
58                 account_policy_set(AP_RESET_COUNT_TIME, 0);                 /* reset immediatly            */
59                 account_policy_set(AP_BAD_ATTEMPT_LOCKOUT, 0);              /* don't lockout               */
60                 account_policy_set(AP_TIME_TO_LOGOUT, -1);                  /* don't force logout          */
61         }
62         tdb_unlock_bystring(tdb, vstring);
63
64         return True;
65 }
66
67 /****************************************************************************
68 ****************************************************************************/
69
70 static char *decode_account_policy_name(int field)
71 {
72         switch (field) {
73                 case AP_MIN_PASSWORD_LEN:
74                         return "min password length";
75                 case AP_PASSWORD_HISTORY:
76                         return "password history";
77                 case AP_USER_MUST_LOGON_TO_CHG_PASS:
78                         return "user must logon to change password";
79                 case AP_MAX_PASSWORD_AGE:
80                         return "maximum password age";
81                 case AP_MIN_PASSWORD_AGE:
82                         return "minimum password age";
83                 case AP_LOCK_ACCOUNT_DURATION:
84                         return "lockout duration";
85                 case AP_RESET_COUNT_TIME:
86                         return "reset count minutes";
87                 case AP_BAD_ATTEMPT_LOCKOUT:
88                         return "bad lockout attempt";
89                 case AP_TIME_TO_LOGOUT:
90                         return "disconnect time";
91                 default:
92                         return "undefined value";
93         }
94 }
95
96
97 /****************************************************************************
98 ****************************************************************************/
99 BOOL account_policy_get(int field, uint32 *value)
100 {
101         fstring name;
102
103         init_account_policy();
104
105         fstrcpy(name, decode_account_policy_name(field));
106         *value=tdb_fetch_int32(tdb, name);
107         DEBUG(10,("account_policy_get: %s:%d\n", name, *value));
108         return True;
109 }
110
111
112 /****************************************************************************
113 ****************************************************************************/
114 BOOL account_policy_set(int field, uint32 value)
115 {
116         fstring name;
117
118         init_account_policy();
119
120         fstrcpy(name, decode_account_policy_name(field));
121         if ( tdb_store_int32(tdb, name, value)== -1)
122                 return False;
123         DEBUG(10,("account_policy_set: %s:%d\n", name, value));
124         
125         return True;
126 }
127