~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/passdb/login_cache.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   Unix SMB/CIFS implementation.
 
3
   struct samu local cache for 
 
4
   Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2004.
 
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 3 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, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "includes.h"
 
21
 
 
22
#undef DBGC_CLASS
 
23
#define DBGC_CLASS DBGC_PASSDB
 
24
 
 
25
#define LOGIN_CACHE_FILE "login_cache.tdb"
 
26
 
 
27
#define SAM_CACHE_FORMAT "dwwd"
 
28
 
 
29
static TDB_CONTEXT *cache;
 
30
 
 
31
bool login_cache_init(void)
 
32
{
 
33
        char* cache_fname = NULL;
 
34
        
 
35
        /* skip file open if it's already opened */
 
36
        if (cache) return True;
 
37
 
 
38
        cache_fname = cache_path(LOGIN_CACHE_FILE);
 
39
        if (cache_fname == NULL) {
 
40
                DEBUG(0, ("Filename allocation failed.\n"));
 
41
                return False;
 
42
        }
 
43
 
 
44
        DEBUG(5, ("Opening cache file at %s\n", cache_fname));
 
45
 
 
46
        cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
 
47
                             O_RDWR|O_CREAT, 0644);
 
48
 
 
49
        if (!cache)
 
50
                DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
 
51
 
 
52
        TALLOC_FREE(cache_fname);
 
53
 
 
54
        return (cache ? True : False);
 
55
}
 
56
 
 
57
bool login_cache_shutdown(void)
 
58
{
 
59
        /* tdb_close routine returns -1 on error */
 
60
        if (!cache) return False;
 
61
        DEBUG(5, ("Closing cache file\n"));
 
62
        return tdb_close(cache) != -1;
 
63
}
 
64
 
 
65
/* if we can't read the cache, oh well, no need to return anything */
 
66
LOGIN_CACHE * login_cache_read(struct samu *sampass)
 
67
{
 
68
        char *keystr;
 
69
        TDB_DATA databuf;
 
70
        LOGIN_CACHE *entry;
 
71
        uint32_t entry_timestamp = 0, bad_password_time = 0;
 
72
 
 
73
        if (!login_cache_init())
 
74
                return NULL;
 
75
 
 
76
        if (pdb_get_nt_username(sampass) == NULL) {
 
77
                return NULL;
 
78
        }
 
79
 
 
80
        keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
 
81
        if (!keystr || !keystr[0]) {
 
82
                SAFE_FREE(keystr);
 
83
                return NULL;
 
84
        }
 
85
 
 
86
        DEBUG(7, ("Looking up login cache for user %s\n",
 
87
                  keystr));
 
88
        databuf = tdb_fetch_bystring(cache, keystr);
 
89
        SAFE_FREE(keystr);
 
90
 
 
91
        if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
 
92
                DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
 
93
                SAFE_FREE(databuf.dptr);
 
94
                return NULL;
 
95
        }
 
96
        ZERO_STRUCTP(entry);
 
97
 
 
98
        if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
 
99
                        &entry_timestamp,
 
100
                        &entry->acct_ctrl,
 
101
                        &entry->bad_password_count,
 
102
                        &bad_password_time) == -1) {
 
103
                DEBUG(7, ("No cache entry found\n"));
 
104
                SAFE_FREE(entry);
 
105
                SAFE_FREE(databuf.dptr);
 
106
                return NULL;
 
107
        }
 
108
 
 
109
        /* Deal with possible 64-bit time_t. */
 
110
        entry->entry_timestamp = (time_t)entry_timestamp;
 
111
        entry->bad_password_time = (time_t)bad_password_time;
 
112
 
 
113
        SAFE_FREE(databuf.dptr);
 
114
 
 
115
        DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
 
116
                  (unsigned int)entry->entry_timestamp, entry->acct_ctrl, 
 
117
                  entry->bad_password_count, (unsigned int)entry->bad_password_time));
 
118
        return entry;
 
119
}
 
120
 
 
121
bool login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
 
122
{
 
123
        char *keystr;
 
124
        TDB_DATA databuf;
 
125
        bool ret;
 
126
        uint32_t entry_timestamp;
 
127
        uint32_t bad_password_time = (uint32_t)entry.bad_password_time;
 
128
 
 
129
        if (!login_cache_init())
 
130
                return False;
 
131
 
 
132
        if (pdb_get_nt_username(sampass) == NULL) {
 
133
                return False;
 
134
        }
 
135
 
 
136
        keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
 
137
        if (!keystr || !keystr[0]) {
 
138
                SAFE_FREE(keystr);
 
139
                return False;
 
140
        }
 
141
 
 
142
        entry_timestamp = (uint32_t)time(NULL);
 
143
 
 
144
        databuf.dsize = 
 
145
                tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
 
146
                         entry_timestamp,
 
147
                         entry.acct_ctrl,
 
148
                         entry.bad_password_count,
 
149
                         bad_password_time);
 
150
        databuf.dptr = SMB_MALLOC_ARRAY(uint8, databuf.dsize);
 
151
        if (!databuf.dptr) {
 
152
                SAFE_FREE(keystr);
 
153
                return False;
 
154
        }
 
155
                         
 
156
        if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
 
157
                         entry_timestamp,
 
158
                         entry.acct_ctrl,
 
159
                         entry.bad_password_count,
 
160
                         bad_password_time)
 
161
            != databuf.dsize) {
 
162
                SAFE_FREE(keystr);
 
163
                SAFE_FREE(databuf.dptr);
 
164
                return False;
 
165
        }
 
166
 
 
167
        ret = tdb_store_bystring(cache, keystr, databuf, 0);
 
168
        SAFE_FREE(keystr);
 
169
        SAFE_FREE(databuf.dptr);
 
170
        return ret == 0;
 
171
}
 
172
 
 
173
bool login_cache_delentry(const struct samu *sampass)
 
174
{
 
175
        int ret;
 
176
        char *keystr;
 
177
        
 
178
        if (!login_cache_init()) 
 
179
                return False;   
 
180
 
 
181
        if (pdb_get_nt_username(sampass) == NULL) {
 
182
                return False;
 
183
        }
 
184
 
 
185
        keystr = SMB_STRDUP(pdb_get_nt_username(sampass));
 
186
        if (!keystr || !keystr[0]) {
 
187
                SAFE_FREE(keystr);
 
188
                return False;
 
189
        }
 
190
 
 
191
        DEBUG(9, ("About to delete entry for %s\n", keystr));
 
192
        ret = tdb_delete_bystring(cache, keystr);
 
193
        DEBUG(9, ("tdb_delete returned %d\n", ret));
 
194
        
 
195
        SAFE_FREE(keystr);
 
196
        return ret == 0;
 
197
}