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 | asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
|
---|
68 | timeout_tm.tm_min, timeout_tm.tm_sec);
|
---|
69 | if (!alloc_str) {
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | timeout_str = alloc_str;
|
---|
73 | }
|
---|
74 |
|
---|
75 | d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
|
---|
76 | timeout_str, datastr, timeout > now_t ? "": "(expired)");
|
---|
77 |
|
---|
78 | SAFE_FREE(alloc_str);
|
---|
79 | }
|
---|
80 |
|
---|
81 | static void delete_cache_entry(const char* keystr, const char* datastr,
|
---|
82 | const time_t timeout, void* dptr)
|
---|
83 | {
|
---|
84 | if (!gencache_del(keystr))
|
---|
85 | d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Parse text representation of timeout value
|
---|
91 | *
|
---|
92 | * @param timeout_str string containing text representation of the timeout
|
---|
93 | * @return numeric timeout of time_t type
|
---|
94 | **/
|
---|
95 | static time_t parse_timeout(const char* timeout_str)
|
---|
96 | {
|
---|
97 | char sign = '\0', *number = NULL, unit = '\0';
|
---|
98 | int len, number_begin, number_end;
|
---|
99 | time_t timeout;
|
---|
100 |
|
---|
101 | /* sign detection */
|
---|
102 | if (timeout_str[0] == '!' || timeout_str[0] == '+') {
|
---|
103 | sign = timeout_str[0];
|
---|
104 | number_begin = 1;
|
---|
105 | } else {
|
---|
106 | number_begin = 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* unit detection */
|
---|
110 | len = strlen(timeout_str);
|
---|
111 | switch (timeout_str[len - 1]) {
|
---|
112 | case 's':
|
---|
113 | case 'm':
|
---|
114 | case 'h':
|
---|
115 | case 'd':
|
---|
116 | case 'w': unit = timeout_str[len - 1];
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* number detection */
|
---|
120 | len = (sign) ? strlen(&timeout_str[number_begin]) : len;
|
---|
121 | number_end = (unit) ? len - 1 : len;
|
---|
122 | number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
|
---|
123 |
|
---|
124 | /* calculate actual timeout value */
|
---|
125 | timeout = (time_t)atoi(number);
|
---|
126 |
|
---|
127 | switch (unit) {
|
---|
128 | case 'm': timeout *= 60; break;
|
---|
129 | case 'h': timeout *= 60*60; break;
|
---|
130 | case 'd': timeout *= 60*60*24; break;
|
---|
131 | case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
|
---|
132 | }
|
---|
133 |
|
---|
134 | switch (sign) {
|
---|
135 | case '!': timeout = time(NULL) - timeout; break;
|
---|
136 | case '+':
|
---|
137 | default: timeout += time(NULL); break;
|
---|
138 | }
|
---|
139 |
|
---|
140 | if (number) SAFE_FREE(number);
|
---|
141 | return timeout;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Add an entry to the cache. If it does exist, then set it.
|
---|
147 | *
|
---|
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(int argc, const char **argv)
|
---|
152 | {
|
---|
153 | const char *keystr, *datastr, *timeout_str;
|
---|
154 | time_t timeout;
|
---|
155 |
|
---|
156 | if (argc < 3) {
|
---|
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 argv key to delete an entry of
|
---|
187 | * @return 0 on success, otherwise failure
|
---|
188 | **/
|
---|
189 | static int net_cache_del(int argc, const char **argv)
|
---|
190 | {
|
---|
191 | const char *keystr = argv[0];
|
---|
192 |
|
---|
193 | if (argc < 1) {
|
---|
194 | d_printf("\nUsage: net cache del <key string>\n");
|
---|
195 | return -1;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if(gencache_del(keystr)) {
|
---|
199 | d_printf("Entry deleted.\n");
|
---|
200 | return 0;
|
---|
201 | }
|
---|
202 |
|
---|
203 | d_fprintf(stderr, "Couldn't delete specified entry\n");
|
---|
204 | return -1;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Get and display an entry from the cache
|
---|
210 | *
|
---|
211 | * @param argv key to search an entry of
|
---|
212 | * @return 0 on success, otherwise failure
|
---|
213 | **/
|
---|
214 | static int net_cache_get(int argc, const char **argv)
|
---|
215 | {
|
---|
216 | const char* keystr = argv[0];
|
---|
217 | char* valuestr;
|
---|
218 | time_t timeout;
|
---|
219 |
|
---|
220 | if (argc < 1) {
|
---|
221 | d_printf("\nUsage: net cache get <key>\n");
|
---|
222 | return -1;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (gencache_get(keystr, &valuestr, &timeout)) {
|
---|
226 | print_cache_entry(keystr, valuestr, timeout, NULL);
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | d_fprintf(stderr, "Failed to find entry\n");
|
---|
231 | return -1;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Search an entry/entries in the cache
|
---|
237 | *
|
---|
238 | * @param argv key pattern to match the entries to
|
---|
239 | * @return 0 on success, otherwise failure
|
---|
240 | **/
|
---|
241 | static int net_cache_search(int argc, const char **argv)
|
---|
242 | {
|
---|
243 | const char* pattern;
|
---|
244 |
|
---|
245 | if (argc < 1) {
|
---|
246 | d_printf("Usage: net cache search <pattern>\n");
|
---|
247 | return -1;
|
---|
248 | }
|
---|
249 |
|
---|
250 | pattern = argv[0];
|
---|
251 | gencache_iterate(print_cache_entry, NULL, pattern);
|
---|
252 | return 0;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * List the contents of the cache
|
---|
258 | *
|
---|
259 | * @param argv ignored in this functionailty
|
---|
260 | * @return always returns 0
|
---|
261 | **/
|
---|
262 | static int net_cache_list(int argc, const char **argv)
|
---|
263 | {
|
---|
264 | const char* pattern = "*";
|
---|
265 | gencache_iterate(print_cache_entry, NULL, pattern);
|
---|
266 | gencache_shutdown();
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Flush the whole cache
|
---|
273 | *
|
---|
274 | * @param argv ignored in this functionality
|
---|
275 | * @return always returns 0
|
---|
276 | **/
|
---|
277 | static int net_cache_flush(int argc, const char **argv)
|
---|
278 | {
|
---|
279 | const char* pattern = "*";
|
---|
280 | gencache_iterate(delete_cache_entry, NULL, pattern);
|
---|
281 | gencache_shutdown();
|
---|
282 | return 0;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Short help
|
---|
288 | *
|
---|
289 | * @param argv ignored in this functionality
|
---|
290 | * @return always returns -1
|
---|
291 | **/
|
---|
292 | static int net_cache_usage(int argc, const char **argv)
|
---|
293 | {
|
---|
294 | d_printf(" net cache add \t add add new cache entry\n");
|
---|
295 | d_printf(" net cache del \t delete existing cache entry by key\n");
|
---|
296 | d_printf(" net cache flush \t delete all entries existing in the cache\n");
|
---|
297 | d_printf(" net cache get \t get cache entry by key\n");
|
---|
298 | d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
|
---|
299 | d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
|
---|
300 | return -1;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Entry point to 'net cache' subfunctionality
|
---|
306 | *
|
---|
307 | * @param argv arguments passed to further called functions
|
---|
308 | * @return whatever further functions return
|
---|
309 | **/
|
---|
310 | int net_cache(int argc, const char **argv)
|
---|
311 | {
|
---|
312 | struct functable func[] = {
|
---|
313 | {"add", net_cache_add},
|
---|
314 | {"del", net_cache_del},
|
---|
315 | {"get", net_cache_get},
|
---|
316 | {"search", net_cache_search},
|
---|
317 | {"list", net_cache_list},
|
---|
318 | {"flush", net_cache_flush},
|
---|
319 | {NULL, NULL}
|
---|
320 | };
|
---|
321 |
|
---|
322 | return net_run_function(argc, argv, func, net_cache_usage);
|
---|
323 | }
|
---|