forgot that i should run autogen.sh as part of release
[gd/samba/.git] / source / libsmb / trustdom_cache.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Trusted domain names cache on top of gencache.
5
6    Copyright (C) Rafal Szczesniak       2002
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_ALL     /* there's no proper class yet */
27
28 #define TDOMKEY_FMT  "TDOM/%s"
29
30
31 /**
32  * @file trustdom_cache.c
33  *
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
39  **/
40
41  
42 /**
43  * Initialise trustdom name caching system. Call gencache
44  * initialisation routine to perform necessary activities.
45  *
46  * @return true upon successful cache initialisation or
47  *         false if cache init failed
48  **/
49  
50 BOOL trustdom_cache_enable(void)
51 {
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"));
55                 return False;
56         }
57
58         return True;
59 }
60
61
62 /**
63  * Shutdown trustdom name caching system. Calls gencache
64  * shutdown function.
65  *
66  * @return true upon successful cache close or
67  *         false if it failed
68  **/
69  
70 BOOL trustdom_cache_shutdown(void)
71 {
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"));
75                 return False;
76         }
77         
78         return True;
79 }
80
81
82 /**
83  * Form up trustdom name key. It is based only
84  * on domain name now.
85  *
86  * @param name trusted domain name
87  * @return cache key for use in gencache mechanism
88  **/
89
90 static char* trustdom_cache_key(const char* name)
91 {
92         char* keystr;
93         asprintf(&keystr, TDOMKEY_FMT, strupper_static(name));
94         
95         return keystr;
96 }
97
98
99 /**
100  * Store trusted domain in gencache as the domain name (key)
101  * and ip address of domain controller (value)
102  *
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
109  **/
110  
111 BOOL trustdom_cache_store(char* name, char* alt_name, const DOM_SID *sid,
112                           time_t timeout)
113 {
114         char *key, *alt_key;
115         fstring sid_string;
116
117         /*
118          * we use gecache call to avoid annoying debug messages
119          * about initialised trustdom 
120          */
121         if (!gencache_init()) return False;
122
123         DEBUG(5, ("trustdom_store: storing SID %s of domain %s\n",
124                   sid_string_static(sid), name));
125
126         key = trustdom_cache_key(name);
127         alt_key = alt_name ? trustdom_cache_key(alt_name) : NULL;
128
129         /* Generate string representation domain SID */
130         sid_to_string(sid_string, sid);
131
132         /*
133          * try to put the names in the cache
134          */
135         if (alt_key) {
136                 return (gencache_set(alt_key, sid_string, timeout)
137                         && gencache_set(key, sid_string, timeout));
138         }
139                  
140         return gencache_set(key, sid_string, timeout);
141 }
142
143
144 /**
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.
148  *
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
153  **/
154  
155 BOOL trustdom_cache_fetch(const char* name, DOM_SID* sid)
156 {
157         char *key, *value;
158         time_t timeout;
159
160         /* init the cache */
161         if (!gencache_init()) return False;
162         
163         /* exit now if null pointers were passed as they're required further */
164         if (!sid) return False;
165
166         /* prepare a key and get the value */
167         key = trustdom_cache_key(name);
168         
169         if (!gencache_get(key, &value, &timeout)) {
170                 DEBUG(5, ("no entry for trusted domain %s found.\n", name));
171                 return False;
172         } else {
173                 DEBUG(5, ("trusted domain %s found (%s)\n", name, value));
174         }
175
176         /* convert ip string representation into in_addr structure */
177         if(! string_to_sid(sid, value)) {
178                 sid = NULL;
179                 return False;
180         }
181         
182         return True;
183 }
184
185
186 /**
187  * Delete single trustdom entry. Look at the
188  * gencache_iterate definition.
189  *
190  **/
191
192 static void flush_trustdom_name(const char* key, const char *value, time_t timeout, void* dptr)
193 {
194         gencache_del(key);
195         DEBUG(5, ("Deleting entry %s\n", key));
196 }
197
198
199 /**
200  * Flush all the trusted domains entries from the cache.
201  **/
202
203 void trustdom_cache_flush(void)
204 {
205         if (!gencache_init())
206                 return;
207
208         /* 
209          * iterate through each TDOM cache's entry and flush it
210          * by flush_trustdom_name function
211          */
212         gencache_iterate(flush_trustdom_name, NULL, trustdom_cache_key("*"));
213         DEBUG(5, ("Trusted domains cache flushed\n"));
214 }
215