Actually check the return value of the account_policy_get() call.
[ira/wip.git] / source3 / lib / account_pol.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  account policy storage
4  *  Copyright (C) Jean François Micouleau      1998-2001.
5  *  
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22 static TDB_CONTEXT *tdb; /* used for driver files */
23
24 #define DATABASE_VERSION 1
25
26 /****************************************************************************
27  Open the account policy tdb.
28 ****************************************************************************/
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())
36                 return True;
37         tdb = tdb_open_log(lock_path("account_policy.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
38         if (!tdb) {
39                 DEBUG(0,("Failed to open account policy database\n"));
40                 return False;
41         }
42
43         local_pid = sys_getpid();
44
45         /* handle a Samba upgrade */
46         tdb_lock_bystring(tdb, vstring);
47         if (tdb_fetch_int32(tdb, vstring) != DATABASE_VERSION) {
48                 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
49                 tdb_store_int32(tdb, vstring, DATABASE_VERSION);
50                 
51                 account_policy_set(AP_MIN_PASSWORD_LEN, MINPASSWDLENGTH);   /* 5 chars minimum             */
52                 account_policy_set(AP_PASSWORD_HISTORY, 0);                 /* don't keep any old password */
53                 account_policy_set(AP_USER_MUST_LOGON_TO_CHG_PASS, 0);      /* don't force user to logon   */
54                 account_policy_set(AP_MAX_PASSWORD_AGE, MAX_PASSWORD_AGE);  /* 21 days                     */
55                 account_policy_set(AP_MIN_PASSWORD_AGE, 0);                 /* 0 days                      */
56                 account_policy_set(AP_LOCK_ACCOUNT_DURATION, 0);            /* lockout for 0 minutes       */
57                 account_policy_set(AP_RESET_COUNT_TIME, 0);                 /* reset immediatly            */
58                 account_policy_set(AP_BAD_ATTEMPT_LOCKOUT, 0);              /* don't lockout               */
59                 account_policy_set(AP_TIME_TO_LOGOUT, -1);                  /* don't force logout          */
60         }
61         tdb_unlock_bystring(tdb, vstring);
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_int32(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_int32(tdb, name, value)== -1)
121                 return False;
122         DEBUG(10,("account_policy_set: %s:%d\n", name, value));
123         
124         return True;
125 }
126