2 Unix SMB/CIFS implementation.
4 Trusted domain names cache on top of gencache.
6 Copyright (C) Rafal Szczesniak 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define DBGC_CLASS DBGC_ALL /* there's no proper class yet */
28 #define TDOMKEY_FMT "TDOM/%s"
32 * @file trustdom_cache.c
34 * Implementation of trusted domain names cache useful when
35 * samba acts as domain member server. In such case, caching
36 * domain names currently trusted gives a performance gain
37 * because there's no need to query PDC each time we need
38 * list of trusted domains
43 * Initialise trustdom name caching system. Call gencache
44 * initialisation routine to perform necessary activities.
46 * @return true upon successful cache initialisation or
47 * false if cache init failed
50 BOOL trustdom_cache_enable(void)
52 /* Init trustdom cache by calling gencache initialisation */
53 if (!gencache_init()) {
54 DEBUG(2, ("trustdomcache_enable: Couldn't initialise trustdom cache on top of gencache.\n"));
63 * Shutdown trustdom name caching system. Calls gencache
66 * @return true upon successful cache close or
70 BOOL trustdom_cache_shutdown(void)
72 /* Close trustdom cache by calling gencache shutdown */
73 if (!gencache_shutdown()) {
74 DEBUG(2, ("trustdomcache_shutdown: Couldn't shutdown trustdom cache on top of gencache.\n"));
83 * Form up trustdom name key. It is based only
86 * @param name trusted domain name
87 * @return cache key for use in gencache mechanism
90 static char* trustdom_cache_key(const char* name)
93 asprintf(&keystr, TDOMKEY_FMT, strupper_static(name));
100 * Store trusted domain in gencache as the domain name (key)
101 * and ip address of domain controller (value)
103 * @param name trusted domain name
104 * @param alt_name alternative trusted domain name (used in ADS domains)
105 * @param sid trusted domain's SID
106 * @param timeout cache entry expiration time
107 * @return true upon successful value storing or
108 * false if store attempt failed
111 BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
118 * we use gecache call to avoid annoying debug messages
119 * about initialised trustdom
121 if (!gencache_init()) return False;
123 DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
124 sid_string_static(sid), name));
126 key = trustdom_cache_key(name);
127 alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
129 /* Generate string representation domain SID */
130 sid_to_string(sid_string, sid);
133 * try to put the names in the cache
136 return (gencache_set(alt_key, sid_string, timeout)
137 && gencache_set(key, sid_string, timeout));
140 return gencache_set(key, sid_string, timeout);
145 * Fetch trusted domain's dc from the gencache.
146 * This routine can also be used to check whether given
147 * domain is currently trusted one.
149 * @param name trusted domain name
150 * @param sid trusted domain's SID to be returned
151 * @return true if entry is found or
152 * false if has expired/doesn't exist
155 BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
161 if (!gencache_init()) return False;
163 /* exit now if null pointers were passed as they're required further */
164 if (!sid) return False;
166 /* prepare a key and get the value */
167 key = trustdom_cache_key(name);
169 if (!gencache_get(key, &value, &timeout)) {
170 DEBUG(5, ("no entry for trusted domain %s found.\n", name));
173 DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
176 /* convert ip string representation into in_addr structure */
177 if(! string_to_sid(sid, value)) {
187 * Delete single trustdom entry. Look at the
188 * gencache_iterate definition.
192 static void flush_trustdom_name(const char* key, const char *value, time_t timeout, void* dptr)
195 DEBUG(5, ("Deleting entry %s\n", key));
200 * Flush all the trusted domains entries from the cache.
203 void trustdom_cache_flush(void)
205 if (!gencache_init())
209 * iterate through each TDOM cache's entry and flush it
210 * by flush_trustdom_name function
212 gencache_iterate(flush_trustdom_name, NULL, trustdom_cache_key("*"));
213 DEBUG(5, ("Trusted domains cache flushed\n"));