CVE-2018-16852 dcerpc dnsserver: Verification tests
[Samba.git] / source3 / utils / net_cache.c
blob5691f04d8d6827db011e308c7da7615085c96b6f
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
4 Copyright (C) Rafal Szczesniak 2002
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.
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.
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/>.
20 #include "includes.h"
21 #include "net.h"
22 #include "libsmb/samlogon_cache.h"
23 #include "../librpc/gen_ndr/netlogon.h"
24 #include "../librpc/gen_ndr/ndr_netlogon.h"
25 #include "libcli/security/dom_sid.h"
26 #include "lib/util/strv.h"
27 #include "lib/gencache.h"
29 /**
30 * @file net_cache.c
31 * @brief This is part of the net tool which is basically command
32 * line wrapper for gencache.c functions (mainly for testing)
34 **/
38 * These routines are used via gencache_iterate() to display the cache's contents
39 * (print_cache_entry) and to flush it (delete_cache_entry).
40 * Both of them are defined by first arg of gencache_iterate() routine.
42 static void print_cache_entry(const char* keystr, DATA_BLOB value,
43 const time_t timeout, void* dptr)
45 char *timeout_str;
46 char *alloc_str = NULL;
47 const char *datastr;
48 char *datastr_free = NULL;
49 time_t now_t = time(NULL);
50 struct tm timeout_tm, now_tm;
51 struct tm *ptimeout_tm, *pnow_tm;
53 ptimeout_tm = localtime_r(&timeout, &timeout_tm);
54 if (ptimeout_tm == NULL) {
55 return;
57 pnow_tm = localtime_r(&now_t, &now_tm);
58 if (pnow_tm == NULL) {
59 return;
62 /* form up timeout string depending whether it's today's date or not */
63 if (timeout_tm.tm_year != now_tm.tm_year ||
64 timeout_tm.tm_mon != now_tm.tm_mon ||
65 timeout_tm.tm_mday != now_tm.tm_mday) {
67 timeout_str = asctime(&timeout_tm);
68 if (!timeout_str) {
69 return;
71 timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
72 } else {
73 if (asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
74 timeout_tm.tm_min, timeout_tm.tm_sec) == -1) {
75 return;
77 timeout_str = alloc_str;
80 datastr = (char *)value.data;
82 if (strnequal(keystr, "NAME2SID/", strlen("NAME2SID/"))) {
83 const char *strv = (char *)value.data;
84 size_t strv_len = value.length;
85 const char *sid = strv_len_next(strv, strv_len, NULL);
86 const char *type = strv_len_next(strv, strv_len, sid);
87 datastr = talloc_asprintf(talloc_tos(), "%s (%s)", sid, type);
90 if (strnequal(keystr, "SID2NAME/", strlen("SID2NAME/"))) {
91 const char *strv = (char *)value.data;
92 size_t strv_len = value.length;
93 const char *domain = strv_len_next(strv, strv_len, NULL);
94 const char *name = strv_len_next(strv, strv_len, domain);
95 const char *type = strv_len_next(strv, strv_len, name);
96 datastr = talloc_asprintf(talloc_tos(), "%s\\%s (%s)",
97 domain, name, type);
100 if ((value.length > 0) && (value.data[value.length-1] != '\0')) {
101 datastr_free = talloc_asprintf(
102 talloc_tos(), "<binary length %d>",
103 (int)value.length);
104 datastr = datastr_free;
105 if (datastr == NULL) {
106 datastr = "<binary>";
110 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
111 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
113 SAFE_FREE(alloc_str);
116 static void delete_cache_entry(const char* keystr, const char* datastr,
117 const time_t timeout, void* dptr)
119 if (!gencache_del(keystr))
120 d_fprintf(stderr, _("Couldn't delete entry! key = %s\n"),
121 keystr);
126 * Parse text representation of timeout value
128 * @param timeout_str string containing text representation of the timeout
129 * @return numeric timeout of time_t type
131 static time_t parse_timeout(const char* timeout_str)
133 char sign = '\0', *number = NULL, unit = '\0';
134 int len, number_begin, number_end;
135 time_t timeout;
137 /* sign detection */
138 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
139 sign = timeout_str[0];
140 number_begin = 1;
141 } else {
142 number_begin = 0;
145 /* unit detection */
146 len = strlen(timeout_str);
147 switch (timeout_str[len - 1]) {
148 case 's':
149 case 'm':
150 case 'h':
151 case 'd':
152 case 'w': unit = timeout_str[len - 1];
155 /* number detection */
156 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
157 number_end = (unit) ? len - 1 : len;
158 number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
160 /* calculate actual timeout value */
161 timeout = (time_t)atoi(number);
163 switch (unit) {
164 case 'm': timeout *= 60; break;
165 case 'h': timeout *= 60*60; break;
166 case 'd': timeout *= 60*60*24; break;
167 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
170 switch (sign) {
171 case '!': timeout = time(NULL) - timeout; break;
172 case '+':
173 default: timeout += time(NULL); break;
176 if (number) SAFE_FREE(number);
177 return timeout;
182 * Add an entry to the cache. If it does exist, then set it.
184 * @param c A net_context structure
185 * @param argv key, value and timeout are passed in command line
186 * @return 0 on success, otherwise failure
188 static int net_cache_add(struct net_context *c, int argc, const char **argv)
190 const char *keystr, *datastr, *timeout_str;
191 time_t timeout;
193 if (argc < 3 || c->display_usage) {
194 d_printf("%s\n%s",
195 _("Usage:"),
196 _("net cache add <key string> <data string> "
197 "<timeout>\n"));
198 return -1;
201 keystr = argv[0];
202 datastr = argv[1];
203 timeout_str = argv[2];
205 /* parse timeout given in command line */
206 timeout = parse_timeout(timeout_str);
207 if (!timeout) {
208 d_fprintf(stderr, _("Invalid timeout argument.\n"));
209 return -1;
212 if (gencache_set(keystr, datastr, timeout)) {
213 d_printf(_("New cache entry stored successfully.\n"));
214 return 0;
217 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
218 return -1;
222 * Delete an entry in the cache
224 * @param c A net_context structure
225 * @param argv key to delete an entry of
226 * @return 0 on success, otherwise failure
228 static int net_cache_del(struct net_context *c, int argc, const char **argv)
230 const char *keystr = argv[0];
232 if (argc < 1 || c->display_usage) {
233 d_printf("%s\n%s",
234 _("Usage:"),
235 _(" net cache del <key string>\n"));
236 return -1;
239 if(gencache_del(keystr)) {
240 d_printf(_("Entry deleted.\n"));
241 return 0;
244 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
245 return -1;
250 * Get and display an entry from the cache
252 * @param c A net_context structure
253 * @param argv key to search an entry of
254 * @return 0 on success, otherwise failure
256 static int net_cache_get(struct net_context *c, int argc, const char **argv)
258 const char* keystr = argv[0];
259 DATA_BLOB value;
260 time_t timeout;
262 if (argc < 1 || c->display_usage) {
263 d_printf("%s\n%s",
264 _("Usage:"),
265 _(" net cache get <key>\n"));
266 return -1;
269 if (gencache_get_data_blob(keystr, NULL, &value, &timeout, NULL)) {
270 print_cache_entry(keystr, value, timeout, NULL);
271 data_blob_free(&value);
272 return 0;
275 d_fprintf(stderr, _("Failed to find entry\n"));
276 return -1;
281 * Search an entry/entries in the cache
283 * @param c A net_context structure
284 * @param argv key pattern to match the entries to
285 * @return 0 on success, otherwise failure
287 static int net_cache_search(struct net_context *c, int argc, const char **argv)
289 const char* pattern;
291 if (argc < 1 || c->display_usage) {
292 d_printf("%s\n%s",
293 _("Usage:"),
294 _(" net cache search <pattern>\n"));
295 return -1;
298 pattern = argv[0];
299 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
300 return 0;
305 * List the contents of the cache
307 * @param c A net_context structure
308 * @param argv ignored in this functionailty
309 * @return always returns 0
311 static int net_cache_list(struct net_context *c, int argc, const char **argv)
313 const char* pattern = "*";
315 if (c->display_usage) {
316 d_printf( "%s\n"
317 "net cache list\n"
318 " %s\n",
319 _("Usage:"),
320 _("List all cache entries."));
321 return 0;
323 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
324 return 0;
329 * Flush the whole cache
331 * @param c A net_context structure
332 * @param argv ignored in this functionality
333 * @return always returns 0
335 static int net_cache_flush(struct net_context *c, int argc, const char **argv)
337 const char* pattern = "*";
338 if (c->display_usage) {
339 d_printf( "%s\n"
340 "net cache flush\n"
341 " %s",
342 _("Usage:"),
343 _("Delete all cache entries."));
344 return 0;
346 gencache_iterate(delete_cache_entry, NULL, pattern);
347 return 0;
350 static int netsamlog_cache_for_all_cb(const char *sid_str,
351 time_t when_cached,
352 struct netr_SamInfo3 *info3,
353 void *private_data)
355 struct net_context *c = (struct net_context *)private_data;
356 char *name = NULL;
358 name = talloc_asprintf(c, "%s\\%s",
359 info3->base.logon_domain.string,
360 info3->base.account_name.string);
361 if (name == NULL) {
362 return -1;
365 d_printf("%-50s %-40s %s\n",
366 sid_str,
367 name,
368 timestring(c, when_cached));
370 return 0;
373 static int net_cache_samlogon_list(struct net_context *c,
374 int argc,
375 const char **argv)
377 int ret;
379 d_printf("%-50s %-40s When cached\n", "SID", "Name");
380 d_printf("------------------------------------------------------------"
381 "------------------------------------------------------------"
382 "----\n");
384 ret = netsamlog_cache_for_all(netsamlog_cache_for_all_cb, c);
385 if (ret == -1) {
386 return -1;
389 return 0;
392 static int net_cache_samlogon_show(struct net_context *c,
393 int argc,
394 const char **argv)
396 const char *sid_str = argv[0];
397 struct dom_sid sid;
398 struct dom_sid *user_sids = NULL;
399 uint32_t num_user_sids;
400 struct netr_SamInfo3 *info3 = NULL;
401 char *name = NULL;
402 uint32_t i;
403 NTSTATUS status;
404 bool ok;
406 if (argc != 1 || c->display_usage) {
407 d_printf("%s\n"
408 "net cache samlogon show SID\n"
409 " %s\n",
410 _("Usage:"),
411 _("Show samlogon cache entry for SID."));
412 return 0;
415 ok = string_to_sid(&sid, sid_str);
416 if (!ok) {
417 d_printf("String to SID failed for %s\n", sid_str);
418 return -1;
421 info3 = netsamlogon_cache_get(c, &sid);
422 if (info3 == NULL) {
423 d_printf("SID %s not found in samlogon cache\n", sid_str);
424 return -1;
427 name = talloc_asprintf(c, "%s\\%s",
428 info3->base.logon_domain.string,
429 info3->base.account_name.string);
430 if (name == NULL) {
431 return -1;
434 d_printf("Name: %s\n", name);
436 status = sid_array_from_info3(c,
437 info3,
438 &user_sids,
439 &num_user_sids,
440 true);
441 if (!NT_STATUS_IS_OK(status)) {
442 TALLOC_FREE(user_sids);
443 d_printf("sid_array_from_info3 failed for %s\n", sid_str);
444 return -1;
447 for (i = 0; i < num_user_sids; i++) {
448 struct dom_sid_buf buf;
449 d_printf("SID %2" PRIu32 ": %s\n",
451 dom_sid_str_buf(&user_sids[i], &buf));
454 TALLOC_FREE(user_sids);
456 return 0;
459 static int net_cache_samlogon_ndrdump(struct net_context *c,
460 int argc,
461 const char **argv)
463 const char *sid_str = NULL;
464 struct dom_sid sid;
465 struct netr_SamInfo3 *info3 = NULL;
466 struct ndr_print *ndr_print = NULL;
467 bool ok;
469 if (argc != 1 || c->display_usage) {
470 d_printf( "%s\n"
471 "net cache samlogon ndrdump SID\n"
472 " %s\n",
473 _("Usage:"),
474 _("Show samlogon cache entry for SID."));
475 return 0;
478 sid_str = argv[0];
480 ok = string_to_sid(&sid, sid_str);
481 if (!ok) {
482 d_printf("String to SID failed for %s\n", sid_str);
483 return -1;
486 info3 = netsamlogon_cache_get(c, &sid);
487 if (info3 == NULL) {
488 d_printf("SID %s not found in samlogon cache\n", sid_str);
489 return -1;
492 ndr_print = talloc_zero(c, struct ndr_print);
493 if (ndr_print == NULL) {
494 d_printf("Could not allocate memory.\n");
495 return -1;
498 ndr_print->print = ndr_print_printf_helper;
499 ndr_print->depth = 1;
500 ndr_print_netr_SamInfo3(ndr_print, "netr_SamInfo3", info3);
501 TALLOC_FREE(ndr_print);
503 return 0;
506 static int net_cache_samlogon_delete(struct net_context *c,
507 int argc,
508 const char **argv)
510 const char *sid_str = argv[0];
511 struct dom_sid sid;
512 bool ok;
514 if (argc != 1 || c->display_usage) {
515 d_printf( "%s\n"
516 "net cache samlogon delete SID\n"
517 " %s\n",
518 _("Usage:"),
519 _("Delete samlogon cache entry for SID."));
520 return 0;
523 ok = string_to_sid(&sid, sid_str);
524 if (!ok) {
525 d_printf("String to SID failed for %s\n", sid_str);
526 return -1;
529 netsamlogon_clear_cached_user(&sid);
531 return 0;
534 static int net_cache_samlogon(struct net_context *c, int argc, const char **argv)
536 struct functable func[] = {
538 "list",
539 net_cache_samlogon_list,
540 NET_TRANSPORT_LOCAL,
541 N_("List samlogon cache"),
542 N_("net cache samlogon list\n"
543 " List samlogon cachen\n")
546 "show",
547 net_cache_samlogon_show,
548 NET_TRANSPORT_LOCAL,
549 N_("Show samlogon cache entry"),
550 N_("net cache samlogon show SID\n"
551 " Show samlogon cache entry\n")
554 "ndrdump",
555 net_cache_samlogon_ndrdump,
556 NET_TRANSPORT_LOCAL,
557 N_("Dump the samlogon cache entry NDR blob"),
558 N_("net cache samlogon ndrdump SID\n"
559 " Dump the samlogon cache entry NDR blob\n")
562 "delete",
563 net_cache_samlogon_delete,
564 NET_TRANSPORT_LOCAL,
565 N_("Delete samlogon cache entry"),
566 N_("net cache samlogon delete SID\n"
567 " Delete samlogon cache entry\n")
569 {NULL, NULL, 0, NULL, NULL}
572 return net_run_function(c, argc, argv, "net cache samlogon", func);
576 * Entry point to 'net cache' subfunctionality
578 * @param c A net_context structure
579 * @param argv arguments passed to further called functions
580 * @return whatever further functions return
582 int net_cache(struct net_context *c, int argc, const char **argv)
584 struct functable func[] = {
586 "add",
587 net_cache_add,
588 NET_TRANSPORT_LOCAL,
589 N_("Add new cache entry"),
590 N_("net cache add <key string> <data string> <timeout>\n"
591 " Add new cache entry.\n"
592 " key string\tKey string to add cache data under.\n"
593 " data string\tData to store under given key.\n"
594 " timeout\tTimeout for cache data.")
597 "del",
598 net_cache_del,
599 NET_TRANSPORT_LOCAL,
600 N_("Delete existing cache entry by key"),
601 N_("net cache del <key string>\n"
602 " Delete existing cache entry by key.\n"
603 " key string\tKey string to delete.")
606 "get",
607 net_cache_get,
608 NET_TRANSPORT_LOCAL,
609 N_("Get cache entry by key"),
610 N_("net cache get <key string>\n"
611 " Get cache entry by key.\n"
612 " key string\tKey string to look up cache entry for.")
616 "search",
617 net_cache_search,
618 NET_TRANSPORT_LOCAL,
619 N_("Search entry by pattern"),
620 N_("net cache search <pattern>\n"
621 " Search entry by pattern.\n"
622 " pattern\tPattern to search for in cache.")
625 "list",
626 net_cache_list,
627 NET_TRANSPORT_LOCAL,
628 N_("List all cache entries"),
629 N_("net cache list\n"
630 " List all cache entries")
633 "flush",
634 net_cache_flush,
635 NET_TRANSPORT_LOCAL,
636 N_("Delete all cache entries"),
637 N_("net cache flush\n"
638 " Delete all cache entries")
641 "samlogon",
642 net_cache_samlogon,
643 NET_TRANSPORT_LOCAL,
644 N_("List contents of the samlogon cache"),
645 N_("net cache samlogon\n"
646 " List contents of the samlogon cache")
648 {NULL, NULL, 0, NULL, NULL}
651 return net_run_function(c, argc, argv, "net cache", func);