r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
[obnox/samba/samba-obnox.git] / source / lib / 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  *  
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         const char *vstring = "INFO/version";
34         uint32 version;
35
36         if (tdb)
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         /* handle a Samba upgrade */
45         tdb_lock_bystring(tdb, vstring,0);
46         if (!tdb_fetch_uint32(tdb, vstring, &version) || version != DATABASE_VERSION) {
47                 tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
48                 tdb_store_uint32(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, (uint32)-1);        /* don't expire                */
54                 account_policy_set(AP_MIN_PASSWORD_AGE, 0);                 /* 0 days                      */
55                 account_policy_set(AP_LOCK_ACCOUNT_DURATION, 30);           /* lockout for 30 minutes      */
56                 account_policy_set(AP_RESET_COUNT_TIME, 30);                /* reset after 30 minutes      */
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         return True;
63 }
64
65 static const struct {
66         int field;
67         const char *string;
68 } account_policy_names[] = {
69         {AP_MIN_PASSWORD_LEN, "min password length"},
70         {AP_PASSWORD_HISTORY, "password history"},
71         {AP_USER_MUST_LOGON_TO_CHG_PASS, "user must logon to change password"},
72         {AP_MAX_PASSWORD_AGE, "maximum password age"},
73         {AP_MIN_PASSWORD_AGE,"minimum password age"},
74         {AP_LOCK_ACCOUNT_DURATION, "lockout duration"},
75         {AP_RESET_COUNT_TIME, "reset count minutes"},
76         {AP_BAD_ATTEMPT_LOCKOUT, "bad lockout attempt"},
77         {AP_TIME_TO_LOGOUT, "disconnect time"},
78         {0, NULL}
79 };
80
81 char *account_policy_names_list(void)
82 {
83         char *nl, *p;
84         int i;
85         size_t len = 0;
86
87         for (i=0; account_policy_names[i].string; i++) {
88                 len += strlen(account_policy_names[i].string) + 1;
89         }
90         len++;
91         nl = SMB_MALLOC(len);
92         if (!nl) {
93                 return NULL;
94         }
95         p = nl;
96         for (i=0; account_policy_names[i].string; i++) {
97                 memcpy(p, account_policy_names[i].string, strlen(account_policy_names[i].string) + 1);
98                 p[strlen(account_policy_names[i].string)] = '\n';
99                 p += strlen(account_policy_names[i].string) + 1;
100         }
101         *p = '\0';
102         return nl;
103 }
104
105 /****************************************************************************
106 Get the account policy name as a string from its #define'ed number
107 ****************************************************************************/
108
109 static const char *decode_account_policy_name(int field)
110 {
111         int i;
112         for (i=0; account_policy_names[i].string; i++) {
113                 if (field == account_policy_names[i].field)
114                         return account_policy_names[i].string;
115         }
116         return NULL;
117
118 }
119
120 /****************************************************************************
121 Get the account policy name as a string from its #define'ed number
122 ****************************************************************************/
123
124 int account_policy_name_to_fieldnum(const char *name)
125 {
126         int i;
127         for (i=0; account_policy_names[i].string; i++) {
128                 if (strcmp(name, account_policy_names[i].string) == 0)
129                         return account_policy_names[i].field;
130         }
131         return 0;
132
133 }
134
135 /****************************************************************************
136 ****************************************************************************/
137
138 BOOL account_policy_get(int field, uint32 *value)
139 {
140         fstring name;
141
142         if(!init_account_policy())return False;
143
144         *value = 0;
145
146         fstrcpy(name, decode_account_policy_name(field));
147         if (!*name) {
148                 DEBUG(1, ("account_policy_get: Field %d is not a valid account policy type!  Cannot get, returning 0.\n", field));
149                 return False;
150         }
151         if (!tdb_fetch_uint32(tdb, name, value)) {
152                 DEBUG(1, ("account_policy_get: tdb_fetch_uint32 failed for field %d (%s), returning 0\n", field, name));
153                 return False;
154         }
155         DEBUG(10,("account_policy_get: %s:%d\n", name, *value));
156         return True;
157 }
158
159
160 /****************************************************************************
161 ****************************************************************************/
162 BOOL account_policy_set(int field, uint32 value)
163 {
164         fstring name;
165
166         if(!init_account_policy())return False;
167
168         fstrcpy(name, decode_account_policy_name(field));
169         if (!*name) {
170                 DEBUG(1, ("Field %d is not a valid account policy type!  Cannot set.\n", field));
171                 return False;
172         }
173
174         if (!tdb_store_uint32(tdb, name, value)) {
175                 DEBUG(1, ("tdb_store_uint32 failed for field %d (%s) on value %u", field, name, value));
176                 return False;
177         }
178
179         DEBUG(10,("account_policy_set: %s:%d\n", name, value));
180         
181         return True;
182 }