move to SAFE_FREE()
[samba.git] / source3 / smbwrapper / smbw_cache.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4    SMB wrapper directory functions
5    Copyright (C) Tim Potter 2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* We cache lists of workgroups, lists of servers in workgroups, and lists
25    of shares exported by servers. */
26
27 #define CACHE_TIMEOUT 30
28
29 struct name_list {
30         struct name_list *prev, *next;
31         char *name;
32         uint32 stype;
33         char *comment;
34 };
35
36 struct cached_names {
37         struct cached_names *prev, *next;
38         char *key;
39         struct name_list *name_list;
40         time_t cache_timeout;
41         int result;
42 };
43
44 static struct cached_names *cached_names = NULL;
45
46 /* Find a list of cached name for a workgroup, server or share list */
47
48 static struct cached_names *find_cached_names(char *key)
49 {
50         struct cached_names *tmp;
51
52         for (tmp = cached_names; tmp; tmp = tmp->next) {
53                 if (strequal(tmp->key, key)) {
54                         return tmp;
55                 }
56         }
57
58         return NULL;
59 }
60
61 /* Add a name to a list stored in the state variable */
62
63 static void add_cached_names(const char *name, uint32 stype, 
64                              const char *comment, void *state)
65 {
66         struct name_list **name_list = (struct name_list **)state;
67         struct name_list *new_name;
68
69         new_name = (struct name_list *)malloc(sizeof(struct name_list));
70         if (!new_name) return;
71
72         ZERO_STRUCTP(new_name);
73
74         new_name->name = strdup(name);
75         new_name->stype = stype;
76         new_name->comment = strdup(comment);
77
78         DLIST_ADD(*name_list, new_name);
79 }
80
81 static void free_name_list(struct name_list *name_list)
82 {
83         struct name_list *tmp = name_list;
84
85         while(tmp) {
86                 struct name_list *next;
87
88                 next = tmp->next;
89
90                 SAFE_FREE(tmp->name);
91                 SAFE_FREE(tmp->comment);
92                 SAFE_FREE(tmp);
93                 
94                 tmp = next;
95         }
96 }
97
98 /* Wrapper for NetServerEnum function */
99
100 BOOL smbw_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
101                         void (*fn)(const char *, uint32, const char *, void *),
102                         void *state)
103 {
104         struct cached_names *names;
105         struct name_list *tmp;
106         time_t now = time(NULL);
107         char key[PATH_MAX];
108         BOOL result = True;
109
110         slprintf(key, PATH_MAX - 1, "%s/%s#%s", cli->desthost, 
111                  workgroup, (stype == SV_TYPE_DOMAIN_ENUM ? "DOM" : "SRV"));
112
113         names = find_cached_names(key);
114
115         if (names == NULL || (now - names->cache_timeout) > CACHE_TIMEOUT) {
116                 struct cached_names *new_names = NULL;
117
118                 /* No names cached for this workgroup */
119
120                 if (names == NULL) {
121                         new_names = (struct cached_names *)
122                                 malloc(sizeof(struct cached_names));
123
124                         ZERO_STRUCTP(new_names);
125                         DLIST_ADD(cached_names, new_names);
126
127                 } else {
128
129                         /* Dispose of out of date name list */
130
131                         free_name_list(names->name_list);
132                         names->name_list = NULL;
133
134                         new_names = names;
135                 }               
136
137                 result = cli_NetServerEnum(cli, workgroup, stype, 
138                                            add_cached_names, 
139                                            &new_names->name_list);
140                                            
141                 new_names->cache_timeout = now;
142                 new_names->result = result;
143                 new_names->key = strdup(key);
144
145                 names = new_names;
146         }
147
148         /* Return names by running callback function. */
149
150         for (tmp = names->name_list; tmp; tmp = tmp->next)
151                 fn(tmp->name, stype, tmp->comment, state);
152         
153         return names->result;
154 }
155
156 /* Wrapper for RNetShareEnum function */
157
158 int smbw_RNetShareEnum(struct cli_state *cli, 
159                        void (*fn)(const char *, uint32, const char *, void *), 
160                        void *state)
161 {
162         struct cached_names *names;
163         struct name_list *tmp;
164         time_t now = time(NULL);
165         char key[PATH_MAX];
166
167         slprintf(key, PATH_MAX - 1, "SHARE/%s", cli->desthost);
168
169         names = find_cached_names(key);
170
171         if (names == NULL || (now - names->cache_timeout) > CACHE_TIMEOUT) {
172                 struct cached_names *new_names = NULL;
173
174                 /* No names cached for this server */
175
176                 if (names == NULL) {
177                         new_names = (struct cached_names *)
178                                 malloc(sizeof(struct cached_names));
179
180                         ZERO_STRUCTP(new_names);
181                         DLIST_ADD(cached_names, new_names);
182
183                 } else {
184
185                         /* Dispose of out of date name list */
186
187                         free_name_list(names->name_list);
188                         names->name_list = NULL;
189
190                         new_names = names;
191                 }
192
193                 new_names->result = cli_RNetShareEnum(cli, add_cached_names, 
194                                                       &new_names->name_list);
195                 
196                 new_names->cache_timeout = now;
197                 new_names->key = strdup(key);
198
199                 names = new_names;
200         }
201
202         /* Return names by running callback function. */
203
204         for (tmp = names->name_list; tmp; tmp = tmp->next)
205                 fn(tmp->name, tmp->stype, tmp->comment, state);
206         
207         return names->result;
208 }