00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "includes.h"
00022
00023 #undef DBGC_CLASS
00024 #define DBGC_CLASS DBGC_PASSDB
00025
00026 #define LOGIN_CACHE_FILE "login_cache.tdb"
00027
00028 #define SAM_CACHE_FORMAT "dwwd"
00029
00030 static TDB_CONTEXT *cache;
00031
00032 BOOL login_cache_init(void)
00033 {
00034 char* cache_fname = NULL;
00035
00036
00037 if (cache) return True;
00038
00039 asprintf(&cache_fname, "%s/%s", lp_lockdir(), LOGIN_CACHE_FILE);
00040 if (cache_fname)
00041 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
00042 else {
00043 DEBUG(0, ("Filename allocation failed.\n"));
00044 return False;
00045 }
00046
00047 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
00048 O_RDWR|O_CREAT, 0644);
00049
00050 if (!cache)
00051 DEBUG(5, ("Attempt to open %s failed.\n", cache_fname));
00052
00053 SAFE_FREE(cache_fname);
00054
00055 return (cache ? True : False);
00056 }
00057
00058 BOOL login_cache_shutdown(void)
00059 {
00060
00061 if (!cache) return False;
00062 DEBUG(5, ("Closing cache file\n"));
00063 return tdb_close(cache) != -1;
00064 }
00065
00066
00067 LOGIN_CACHE * login_cache_read(struct samu *sampass)
00068 {
00069 TDB_DATA keybuf, databuf;
00070 LOGIN_CACHE *entry;
00071
00072 if (!login_cache_init())
00073 return NULL;
00074
00075 if (pdb_get_nt_username(sampass) == NULL) {
00076 return NULL;
00077 }
00078
00079 keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
00080 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
00081 SAFE_FREE(keybuf.dptr);
00082 return NULL;
00083 }
00084 keybuf.dsize = strlen(keybuf.dptr) + 1;
00085
00086 DEBUG(7, ("Looking up login cache for user %s\n",
00087 keybuf.dptr));
00088 databuf = tdb_fetch(cache, keybuf);
00089 SAFE_FREE(keybuf.dptr);
00090
00091 if (!(entry = SMB_MALLOC_P(LOGIN_CACHE))) {
00092 DEBUG(1, ("Unable to allocate cache entry buffer!\n"));
00093 SAFE_FREE(databuf.dptr);
00094 return NULL;
00095 }
00096
00097 if (tdb_unpack (databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
00098 &entry->entry_timestamp, &entry->acct_ctrl,
00099 &entry->bad_password_count,
00100 &entry->bad_password_time) == -1) {
00101 DEBUG(7, ("No cache entry found\n"));
00102 SAFE_FREE(entry);
00103 SAFE_FREE(databuf.dptr);
00104 return NULL;
00105 }
00106
00107 SAFE_FREE(databuf.dptr);
00108
00109 DEBUG(5, ("Found login cache entry: timestamp %12u, flags 0x%x, count %d, time %12u\n",
00110 (unsigned int)entry->entry_timestamp, entry->acct_ctrl,
00111 entry->bad_password_count, (unsigned int)entry->bad_password_time));
00112 return entry;
00113 }
00114
00115 BOOL login_cache_write(const struct samu *sampass, LOGIN_CACHE entry)
00116 {
00117
00118 TDB_DATA keybuf, databuf;
00119 BOOL ret;
00120
00121 if (!login_cache_init())
00122 return False;
00123
00124 if (pdb_get_nt_username(sampass) == NULL) {
00125 return False;
00126 }
00127
00128 keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
00129 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
00130 SAFE_FREE(keybuf.dptr);
00131 return False;
00132 }
00133 keybuf.dsize = strlen(keybuf.dptr) + 1;
00134
00135 entry.entry_timestamp = time(NULL);
00136
00137 databuf.dsize =
00138 tdb_pack(NULL, 0, SAM_CACHE_FORMAT,
00139 entry.entry_timestamp,
00140 entry.acct_ctrl,
00141 entry.bad_password_count,
00142 entry.bad_password_time);
00143 databuf.dptr = SMB_MALLOC_ARRAY(char, databuf.dsize);
00144 if (!databuf.dptr) {
00145 SAFE_FREE(keybuf.dptr);
00146 return False;
00147 }
00148
00149 if (tdb_pack(databuf.dptr, databuf.dsize, SAM_CACHE_FORMAT,
00150 entry.entry_timestamp,
00151 entry.acct_ctrl,
00152 entry.bad_password_count,
00153 entry.bad_password_time)
00154 != databuf.dsize) {
00155 SAFE_FREE(keybuf.dptr);
00156 SAFE_FREE(databuf.dptr);
00157 return False;
00158 }
00159
00160 ret = tdb_store(cache, keybuf, databuf, 0);
00161 SAFE_FREE(keybuf.dptr);
00162 SAFE_FREE(databuf.dptr);
00163 return ret == 0;
00164 }
00165
00166 BOOL login_cache_delentry(const struct samu *sampass)
00167 {
00168 int ret;
00169 TDB_DATA keybuf;
00170
00171 if (!login_cache_init())
00172 return False;
00173
00174 if (pdb_get_nt_username(sampass) == NULL) {
00175 return False;
00176 }
00177
00178 keybuf.dptr = SMB_STRDUP(pdb_get_nt_username(sampass));
00179 if (!keybuf.dptr || !strlen(keybuf.dptr)) {
00180 SAFE_FREE(keybuf.dptr);
00181 return False;
00182 }
00183 keybuf.dsize = strlen(keybuf.dptr) + 1;
00184 DEBUG(9, ("About to delete entry for %s\n", keybuf.dptr));
00185 ret = tdb_delete(cache, keybuf);
00186 DEBUG(9, ("tdb_delete returned %d\n", ret));
00187
00188 SAFE_FREE(keybuf.dptr);
00189 return ret == 0;
00190 }
00191