1 | /*
|
---|
2 | Samba Unix/Linux SMB client library
|
---|
3 | Distributed SMB/CIFS Server Management Utility
|
---|
4 | Copyright (C) Rafal Szczesniak 2002
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "net.h"
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @file net_cache.c
|
---|
25 | * @brief This is part of the net tool which is basically command
|
---|
26 | * line wrapper for gencache.c functions (mainly for testing)
|
---|
27 | *
|
---|
28 | **/
|
---|
29 |
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * These routines are used via gencache_iterate() to display the cache's contents
|
---|
33 | * (print_cache_entry) and to flush it (delete_cache_entry).
|
---|
34 | * Both of them are defined by first arg of gencache_iterate() routine.
|
---|
35 | */
|
---|
36 | static void print_cache_entry(const char* keystr, const char* datastr,
|
---|
37 | const time_t timeout, void* dptr)
|
---|
38 | {
|
---|
39 | char *timeout_str;
|
---|
40 | char *alloc_str = NULL;
|
---|
41 | time_t now_t = time(NULL);
|
---|
42 | struct tm timeout_tm, *now_tm;
|
---|
43 | /* localtime returns statically allocated pointer, so timeout_tm
|
---|
44 | has to be copied somewhere else */
|
---|
45 |
|
---|
46 | now_tm = localtime(&timeout);
|
---|
47 | if (!now_tm) {
|
---|
48 | return;
|
---|
49 | }
|
---|
50 | memcpy(&timeout_tm, now_tm, sizeof(struct tm));
|
---|
51 | now_tm = localtime(&now_t);
|
---|
52 | if (!now_tm) {
|
---|
53 | return;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /* form up timeout string depending whether it's today's date or not */
|
---|
57 | if (timeout_tm.tm_year != now_tm->tm_year ||
|
---|
58 | timeout_tm.tm_mon != now_tm->tm_mon ||
|
---|
59 | timeout_tm.tm_mday != now_tm->tm_mday) {
|
---|
60 |
|
---|
61 | timeout_str = asctime(&timeout_tm);
|
---|
62 | if (!timeout_str) {
|
---|
63 | return;
|
---|
64 | }
|
---|
65 | timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
|
---|
66 | } else {
|
---|
67 | if (asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
|
---|
68 | timeout_tm.tm_min, timeout_tm.tm_sec) == -1) {
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | timeout_str = alloc_str;
|
---|
72 | }
|
---|
73 |
|
---|
74 | d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
|
---|
75 | timeout_str, datastr, timeout > now_t ? "": "(expired)");
|
---|
76 |
|
---|
77 | SAFE_FREE(alloc_str);
|
---|
78 | }
|
---|
79 |
|
---|
80 | static void delete_cache_entry(const char* keystr, const char* datastr,
|
---|
81 | const time_t timeout, void* dptr)
|
---|
82 | {
|
---|
83 | if (!gencache_del(keystr))
|
---|
84 | d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Parse text representation of timeout value
|
---|
90 | *
|
---|
91 | * @param timeout_str string containing text representation of the timeout
|
---|
92 | * @return numeric timeout of time_t type
|
---|
93 | **/
|
---|
94 | static time_t parse_timeout(const char* timeout_str)
|
---|
95 | {
|
---|
96 | char sign = '\0', *number = NULL, unit = '\0';
|
---|
97 | int len, number_begin, number_end;
|
---|
98 | time_t timeout;
|
---|
99 |
|
---|
100 | /* sign detection */
|
---|
101 | if (timeout_str[0] == '!' || timeout_str[0] == '+') {
|
---|
102 | sign = timeout_str[0];
|
---|
103 | number_begin = 1;
|
---|
104 | } else {
|
---|
105 | number_begin = 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* unit detection */
|
---|
109 | len = strlen(timeout_str);
|
---|
110 | switch (timeout_str[len - 1]) {
|
---|
111 | case 's':
|
---|
112 | case 'm':
|
---|
113 | case 'h':
|
---|
114 | case 'd':
|
---|
115 | case 'w': unit = timeout_str[len - 1];
|
---|
116 | }
|
---|
117 |
|
---|
118 | /* number detection */
|
---|
119 | len = (sign) ? strlen(&timeout_str[number_begin]) : len;
|
---|
120 | number_end = (unit) ? len - 1 : len;
|
---|
121 | number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
|
---|
122 |
|
---|
123 | /* calculate actual timeout value */
|
---|
124 | timeout = (time_t)atoi(number);
|
---|
125 |
|
---|
126 | switch (unit) {
|
---|
127 | case 'm': timeout *= 60; break;
|
---|
128 | case 'h': timeout *= 60*60; break;
|
---|
129 | case 'd': timeout *= 60*60*24; break;
|
---|
130 | case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
|
---|
131 | }
|
---|
132 |
|
---|
133 | switch (sign) {
|
---|
134 | case '!': timeout = time(NULL) - timeout; break;
|
---|
135 | case '+':
|
---|
136 | default: timeout += time(NULL); break;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if (number) SAFE_FREE(number);
|
---|
140 | return timeout;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Add an entry to the cache. If it does exist, then set it.
|
---|
146 | *
|
---|
147 | * @param c A net_context structure
|
---|
148 | * @param argv key, value and timeout are passed in command line
|
---|
149 | * @return 0 on success, otherwise failure
|
---|
150 | **/
|
---|
151 | static int net_cache_add(struct net_context *c, int argc, const char **argv)
|
---|
152 | {
|
---|
153 | const char *keystr, *datastr, *timeout_str;
|
---|
154 | time_t timeout;
|
---|
155 |
|
---|
156 | if (argc < 3 || c->display_usage) {
|
---|
157 | d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
|
---|
158 | return -1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | keystr = argv[0];
|
---|
162 | datastr = argv[1];
|
---|
163 | timeout_str = argv[2];
|
---|
164 |
|
---|
165 | /* parse timeout given in command line */
|
---|
166 | timeout = parse_timeout(timeout_str);
|
---|
167 | if (!timeout) {
|
---|
168 | d_fprintf(stderr, "Invalid timeout argument.\n");
|
---|
169 | return -1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (gencache_set(keystr, datastr, timeout)) {
|
---|
173 | d_printf("New cache entry stored successfully.\n");
|
---|
174 | gencache_shutdown();
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | d_fprintf(stderr, "Entry couldn't be added. Perhaps there's already such a key.\n");
|
---|
179 | gencache_shutdown();
|
---|
180 | return -1;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Delete an entry in the cache
|
---|
185 | *
|
---|
186 | * @param c A net_context structure
|
---|
187 | * @param argv key to delete an entry of
|
---|
188 | * @return 0 on success, otherwise failure
|
---|
189 | **/
|
---|
190 | static int net_cache_del(struct net_context *c, int argc, const char **argv)
|
---|
191 | {
|
---|
192 | const char *keystr = argv[0];
|
---|
193 |
|
---|
194 | if (argc < 1 || c->display_usage) {
|
---|
195 | d_printf("\nUsage: net cache del <key string>\n");
|
---|
196 | return -1;
|
---|
197 | }
|
---|
198 |
|
---|
199 | if(gencache_del(keystr)) {
|
---|
200 | d_printf("Entry deleted.\n");
|
---|
201 | return 0;
|
---|
202 | }
|
---|
203 |
|
---|
204 | d_fprintf(stderr, "Couldn't delete specified entry\n");
|
---|
205 | return -1;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Get and display an entry from the cache
|
---|
211 | *
|
---|
212 | * @param c A net_context structure
|
---|
213 | * @param argv key to search an entry of
|
---|
214 | * @return 0 on success, otherwise failure
|
---|
215 | **/
|
---|
216 | static int net_cache_get(struct net_context *c, int argc, const char **argv)
|
---|
217 | {
|
---|
218 | const char* keystr = argv[0];
|
---|
219 | char* valuestr;
|
---|
220 | time_t timeout;
|
---|
221 |
|
---|
222 | if (argc < 1 || c->display_usage) {
|
---|
223 | d_printf("\nUsage: net cache get <key>\n");
|
---|
224 | return -1;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (gencache_get(keystr, &valuestr, &timeout)) {
|
---|
228 | print_cache_entry(keystr, valuestr, timeout, NULL);
|
---|
229 | return 0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | d_fprintf(stderr, "Failed to find entry\n");
|
---|
233 | return -1;
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Search an entry/entries in the cache
|
---|
239 | *
|
---|
240 | * @param c A net_context structure
|
---|
241 | * @param argv key pattern to match the entries to
|
---|
242 | * @return 0 on success, otherwise failure
|
---|
243 | **/
|
---|
244 | static int net_cache_search(struct net_context *c, int argc, const char **argv)
|
---|
245 | {
|
---|
246 | const char* pattern;
|
---|
247 |
|
---|
248 | if (argc < 1 || c->display_usage) {
|
---|
249 | d_printf("Usage: net cache search <pattern>\n");
|
---|
250 | return -1;
|
---|
251 | }
|
---|
252 |
|
---|
253 | pattern = argv[0];
|
---|
254 | gencache_iterate(print_cache_entry, NULL, pattern);
|
---|
255 | return 0;
|
---|
256 | }
|
---|
257 |
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * List the contents of the cache
|
---|
261 | *
|
---|
262 | * @param c A net_context structure
|
---|
263 | * @param argv ignored in this functionailty
|
---|
264 | * @return always returns 0
|
---|
265 | **/
|
---|
266 | static int net_cache_list(struct net_context *c, int argc, const char **argv)
|
---|
267 | {
|
---|
268 | const char* pattern = "*";
|
---|
269 |
|
---|
270 | if (c->display_usage) {
|
---|
271 | d_printf("Usage:\n"
|
---|
272 | "net cache list\n"
|
---|
273 | " List all cache entries.\n");
|
---|
274 | return 0;
|
---|
275 | }
|
---|
276 | gencache_iterate(print_cache_entry, NULL, pattern);
|
---|
277 | gencache_shutdown();
|
---|
278 | return 0;
|
---|
279 | }
|
---|
280 |
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Flush the whole cache
|
---|
284 | *
|
---|
285 | * @param c A net_context structure
|
---|
286 | * @param argv ignored in this functionality
|
---|
287 | * @return always returns 0
|
---|
288 | **/
|
---|
289 | static int net_cache_flush(struct net_context *c, int argc, const char **argv)
|
---|
290 | {
|
---|
291 | const char* pattern = "*";
|
---|
292 | if (c->display_usage) {
|
---|
293 | d_printf("Usage:\n"
|
---|
294 | "net cache flush\n"
|
---|
295 | " Delete all cache entries.\n");
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 | gencache_iterate(delete_cache_entry, NULL, pattern);
|
---|
299 | gencache_shutdown();
|
---|
300 | return 0;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * Entry point to 'net cache' subfunctionality
|
---|
305 | *
|
---|
306 | * @param c A net_context structure
|
---|
307 | * @param argv arguments passed to further called functions
|
---|
308 | * @return whatever further functions return
|
---|
309 | **/
|
---|
310 | int net_cache(struct net_context *c, int argc, const char **argv)
|
---|
311 | {
|
---|
312 | struct functable func[] = {
|
---|
313 | {
|
---|
314 | "add",
|
---|
315 | net_cache_add,
|
---|
316 | NET_TRANSPORT_LOCAL,
|
---|
317 | "Add new cache entry",
|
---|
318 | "net cache add <key string> <data string> <timeout>\n"
|
---|
319 | " Add new cache entry.\n"
|
---|
320 | " key string\tKey string to add cache data under.\n"
|
---|
321 | " data string\tData to store under given key.\n"
|
---|
322 | " timeout\tTimeout for cache data."
|
---|
323 | },
|
---|
324 | {
|
---|
325 | "del",
|
---|
326 | net_cache_del,
|
---|
327 | NET_TRANSPORT_LOCAL,
|
---|
328 | "Delete existing cache entry by key",
|
---|
329 | "net cache del <key string>\n"
|
---|
330 | " Delete existing cache entry by key.\n"
|
---|
331 | " key string\tKey string to delete."
|
---|
332 | },
|
---|
333 | {
|
---|
334 | "get",
|
---|
335 | net_cache_get,
|
---|
336 | NET_TRANSPORT_LOCAL,
|
---|
337 | "Get cache entry by key",
|
---|
338 | "net cache get <key string>\n"
|
---|
339 | " Get cache entry by key.\n"
|
---|
340 | " key string\tKey string to look up cache entry for."
|
---|
341 |
|
---|
342 | },
|
---|
343 | {
|
---|
344 | "search",
|
---|
345 | net_cache_search,
|
---|
346 | NET_TRANSPORT_LOCAL,
|
---|
347 | "Search entry by pattern",
|
---|
348 | "net cache search <pattern>\n"
|
---|
349 | " Search entry by pattern.\n"
|
---|
350 | " pattern\tPattern to search for in cache."
|
---|
351 | },
|
---|
352 | {
|
---|
353 | "list",
|
---|
354 | net_cache_list,
|
---|
355 | NET_TRANSPORT_LOCAL,
|
---|
356 | "List all cache entries",
|
---|
357 | "net cache list\n"
|
---|
358 | " List all cache entries"
|
---|
359 | },
|
---|
360 | {
|
---|
361 | "flush",
|
---|
362 | net_cache_flush,
|
---|
363 | NET_TRANSPORT_LOCAL,
|
---|
364 | "Delete all cache entries",
|
---|
365 | "net cache flush\n"
|
---|
366 | " Delete all cache entries"
|
---|
367 | },
|
---|
368 | {NULL, NULL, 0, NULL, NULL}
|
---|
369 | };
|
---|
370 |
|
---|
371 | return net_run_function(c, argc, argv, "net cache", func);
|
---|
372 | }
|
---|