00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024
00025 #undef DBGC_CLASS
00026 #define DBGC_CLASS DBGC_ALL
00027
00028 #define TDOMKEY_FMT "TDOM/%s"
00029 #define TDOMTSKEY "TDOMCACHE/TIMESTAMP"
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 BOOL trustdom_cache_enable(void)
00052 {
00053
00054 if (!gencache_init()) {
00055 DEBUG(2, ("trustdomcache_enable: Couldn't initialise trustdom cache on top of gencache.\n"));
00056 return False;
00057 }
00058
00059 return True;
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 BOOL trustdom_cache_shutdown(void)
00072 {
00073
00074 if (!gencache_shutdown()) {
00075 DEBUG(2, ("trustdomcache_shutdown: Couldn't shutdown trustdom cache on top of gencache.\n"));
00076 return False;
00077 }
00078
00079 return True;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 static char* trustdom_cache_key(const char* name)
00092 {
00093 char* keystr = NULL;
00094 asprintf(&keystr, TDOMKEY_FMT, strupper_static(name));
00095
00096 return keystr;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
00113 time_t timeout)
00114 {
00115 char *key, *alt_key;
00116 fstring sid_string;
00117 BOOL ret;
00118
00119
00120
00121
00122
00123 if (!gencache_init())
00124 return False;
00125
00126 DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
00127 sid_string_static(sid), name));
00128
00129 key = trustdom_cache_key(name);
00130 alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
00131
00132
00133 sid_to_string(sid_string, sid);
00134
00135
00136
00137
00138 if (alt_key) {
00139 ret = gencache_set(alt_key, sid_string, timeout);
00140 if ( ret ) {
00141 ret = gencache_set(key, sid_string, timeout);
00142 }
00143 SAFE_FREE(alt_key);
00144 SAFE_FREE(key);
00145 return ret;
00146 }
00147
00148 ret = gencache_set(key, sid_string, timeout);
00149 SAFE_FREE(key);
00150 return ret;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
00166 {
00167 char *key = NULL, *value = NULL;
00168 time_t timeout;
00169
00170
00171 if (!gencache_init())
00172 return False;
00173
00174
00175 if (!sid)
00176 return False;
00177
00178
00179 key = trustdom_cache_key(name);
00180 if (!key)
00181 return False;
00182
00183 if (!gencache_get(key, &value, &timeout)) {
00184 DEBUG(5, ("no entry for trusted domain %s found.\n", name));
00185 SAFE_FREE(key);
00186 return False;
00187 } else {
00188 SAFE_FREE(key);
00189 DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
00190 }
00191
00192
00193 if(! string_to_sid(sid, value)) {
00194 sid = NULL;
00195 SAFE_FREE(value);
00196 return False;
00197 }
00198
00199 SAFE_FREE(value);
00200 return True;
00201 }
00202
00203
00204
00205
00206
00207
00208 uint32 trustdom_cache_fetch_timestamp( void )
00209 {
00210 char *value = NULL;
00211 time_t timeout;
00212 uint32 timestamp;
00213
00214
00215 if (!gencache_init())
00216 return False;
00217
00218 if (!gencache_get(TDOMTSKEY, &value, &timeout)) {
00219 DEBUG(5, ("no timestamp for trusted domain cache located.\n"));
00220 SAFE_FREE(value);
00221 return 0;
00222 }
00223
00224 timestamp = atoi(value);
00225
00226 SAFE_FREE(value);
00227 return timestamp;
00228 }
00229
00230
00231
00232
00233
00234 BOOL trustdom_cache_store_timestamp( uint32 t, time_t timeout )
00235 {
00236 fstring value;
00237
00238
00239 if (!gencache_init())
00240 return False;
00241
00242 fstr_sprintf(value, "%d", t );
00243
00244 if (!gencache_set(TDOMTSKEY, value, timeout)) {
00245 DEBUG(5, ("failed to set timestamp for trustdom_cache\n"));
00246 return False;
00247 }
00248
00249 return True;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259 static void flush_trustdom_name(const char* key, const char *value, time_t timeout, void* dptr)
00260 {
00261 gencache_del(key);
00262 DEBUG(5, ("Deleting entry %s\n", key));
00263 }
00264
00265
00266
00267
00268
00269
00270 void trustdom_cache_flush(void)
00271 {
00272 if (!gencache_init())
00273 return;
00274
00275
00276
00277
00278
00279 gencache_iterate(flush_trustdom_name, NULL, trustdom_cache_key("*"));
00280 DEBUG(5, ("Trusted domains cache flushed\n"));
00281 }
00282
00283
00284
00285
00286 #define TRUSTDOM_UPDATE_INTERVAL 600
00287
00288 void update_trustdom_cache( void )
00289 {
00290 char **domain_names;
00291 DOM_SID *dom_sids;
00292 uint32 num_domains;
00293 uint32 last_check;
00294 int time_diff;
00295 TALLOC_CTX *mem_ctx = NULL;
00296 time_t now = time(NULL);
00297 int i;
00298
00299
00300 if ( (last_check = trustdom_cache_fetch_timestamp()) == 0 )
00301 trustdom_cache_store_timestamp(0, now+TRUSTDOM_UPDATE_INTERVAL);
00302
00303 time_diff = (int) (now - last_check);
00304
00305 if ( (time_diff > 0) && (time_diff < TRUSTDOM_UPDATE_INTERVAL) ) {
00306 DEBUG(10,("update_trustdom_cache: not time to update trustdom_cache yet\n"));
00307 return;
00308 }
00309
00310
00311
00312
00313 trustdom_cache_store_timestamp(now, now+TRUSTDOM_UPDATE_INTERVAL);
00314
00315 if ( !(mem_ctx = talloc_init("update_trustdom_cache")) ) {
00316 DEBUG(0,("update_trustdom_cache: talloc_init() failed!\n"));
00317 goto done;
00318 }
00319
00320
00321
00322 if ( enumerate_domain_trusts(mem_ctx, lp_workgroup(), &domain_names,
00323 &num_domains, &dom_sids)) {
00324 for ( i=0; i<num_domains; i++ ) {
00325 trustdom_cache_store( domain_names[i], NULL, &dom_sids[i],
00326 now+TRUSTDOM_UPDATE_INTERVAL);
00327 }
00328 } else {
00329
00330
00331 trustdom_cache_store_timestamp(last_check,
00332 last_check+TRUSTDOM_UPDATE_INTERVAL);
00333 }
00334
00335 done:
00336 talloc_destroy( mem_ctx );
00337
00338 return;
00339 }