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/>.
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"
29 * @brief This is part of the net tool which is basically command
30 * line wrapper for gencache.c functions (mainly for testing)
36 * These routines are used via gencache_iterate() to display the cache's contents
37 * (print_cache_entry) and to flush it (delete_cache_entry).
38 * Both of them are defined by first arg of gencache_iterate() routine.
40 static void print_cache_entry(const char* keystr, DATA_BLOB value,
41 const time_t timeout, void* dptr)
44 char *alloc_str = NULL;
46 char *datastr_free = NULL;
47 time_t now_t = time(NULL);
48 struct tm timeout_tm, now_tm;
49 struct tm *ptimeout_tm, *pnow_tm;
51 ptimeout_tm = localtime_r(&timeout, &timeout_tm);
52 if (ptimeout_tm == NULL) {
55 pnow_tm = localtime_r(&now_t, &now_tm);
56 if (pnow_tm == NULL) {
60 /* form up timeout string depending whether it's today's date or not */
61 if (timeout_tm.tm_year != now_tm.tm_year ||
62 timeout_tm.tm_mon != now_tm.tm_mon ||
63 timeout_tm.tm_mday != now_tm.tm_mday) {
65 timeout_str = asctime(&timeout_tm);
69 timeout_str[strlen(timeout_str) - 1] = '\0'; /* remove tailing CR */
71 if (asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
72 timeout_tm.tm_min, timeout_tm.tm_sec) == -1) {
75 timeout_str = alloc_str;
78 datastr = (char *)value.data;
80 if ((value.length > 0) && (value.data[value.length-1] != '\0')) {
81 datastr_free = talloc_asprintf(
82 talloc_tos(), "<binary length %d>",
84 datastr = datastr_free;
85 if (datastr == NULL) {
90 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr,
91 timeout_str, datastr, timeout > now_t ? "": _("(expired)"));
96 static void delete_cache_entry(const char* keystr, const char* datastr,
97 const time_t timeout, void* dptr)
99 if (!gencache_del(keystr))
100 d_fprintf(stderr, _("Couldn't delete entry! key = %s\n"),
106 * Parse text representation of timeout value
108 * @param timeout_str string containing text representation of the timeout
109 * @return numeric timeout of time_t type
111 static time_t parse_timeout(const char* timeout_str)
113 char sign = '\0', *number = NULL, unit = '\0';
114 int len, number_begin, number_end;
118 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
119 sign = timeout_str[0];
126 len = strlen(timeout_str);
127 switch (timeout_str[len - 1]) {
132 case 'w': unit = timeout_str[len - 1];
135 /* number detection */
136 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
137 number_end = (unit) ? len - 1 : len;
138 number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
140 /* calculate actual timeout value */
141 timeout = (time_t)atoi(number);
144 case 'm': timeout *= 60; break;
145 case 'h': timeout *= 60*60; break;
146 case 'd': timeout *= 60*60*24; break;
147 case 'w': timeout *= 60*60*24*7; break; /* that's fair enough, I think :) */
151 case '!': timeout = time(NULL) - timeout; break;
153 default: timeout += time(NULL); break;
156 if (number) SAFE_FREE(number);
162 * Add an entry to the cache. If it does exist, then set it.
164 * @param c A net_context structure
165 * @param argv key, value and timeout are passed in command line
166 * @return 0 on success, otherwise failure
168 static int net_cache_add(struct net_context *c, int argc, const char **argv)
170 const char *keystr, *datastr, *timeout_str;
173 if (argc < 3 || c->display_usage) {
176 _("net cache add <key string> <data string> "
183 timeout_str = argv[2];
185 /* parse timeout given in command line */
186 timeout = parse_timeout(timeout_str);
188 d_fprintf(stderr, _("Invalid timeout argument.\n"));
192 if (gencache_set(keystr, datastr, timeout)) {
193 d_printf(_("New cache entry stored successfully.\n"));
197 d_fprintf(stderr, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
202 * Delete an entry in the cache
204 * @param c A net_context structure
205 * @param argv key to delete an entry of
206 * @return 0 on success, otherwise failure
208 static int net_cache_del(struct net_context *c, int argc, const char **argv)
210 const char *keystr = argv[0];
212 if (argc < 1 || c->display_usage) {
215 _(" net cache del <key string>\n"));
219 if(gencache_del(keystr)) {
220 d_printf(_("Entry deleted.\n"));
224 d_fprintf(stderr, _("Couldn't delete specified entry\n"));
230 * Get and display an entry from the cache
232 * @param c A net_context structure
233 * @param argv key to search an entry of
234 * @return 0 on success, otherwise failure
236 static int net_cache_get(struct net_context *c, int argc, const char **argv)
238 const char* keystr = argv[0];
242 if (argc < 1 || c->display_usage) {
245 _(" net cache get <key>\n"));
249 if (gencache_get_data_blob(keystr, NULL, &value, &timeout, NULL)) {
250 print_cache_entry(keystr, value, timeout, NULL);
251 data_blob_free(&value);
255 d_fprintf(stderr, _("Failed to find entry\n"));
261 * Search an entry/entries in the cache
263 * @param c A net_context structure
264 * @param argv key pattern to match the entries to
265 * @return 0 on success, otherwise failure
267 static int net_cache_search(struct net_context *c, int argc, const char **argv)
271 if (argc < 1 || c->display_usage) {
274 _(" net cache search <pattern>\n"));
279 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
285 * List the contents of the cache
287 * @param c A net_context structure
288 * @param argv ignored in this functionailty
289 * @return always returns 0
291 static int net_cache_list(struct net_context *c, int argc, const char **argv)
293 const char* pattern = "*";
295 if (c->display_usage) {
300 _("List all cache entries."));
303 gencache_iterate_blobs(print_cache_entry, NULL, pattern);
309 * Flush the whole cache
311 * @param c A net_context structure
312 * @param argv ignored in this functionality
313 * @return always returns 0
315 static int net_cache_flush(struct net_context *c, int argc, const char **argv)
317 const char* pattern = "*";
318 if (c->display_usage) {
323 _("Delete all cache entries."));
326 gencache_iterate(delete_cache_entry, NULL, pattern);
330 static int net_cache_stabilize(struct net_context *c, int argc,
333 if (c->display_usage) {
335 "net cache stabilize\n"
338 _("Move transient cache content to stable storage"));
342 if (!gencache_stabilize()) {
348 static int netsamlog_cache_for_all_cb(const char *sid_str,
350 struct netr_SamInfo3 *info3,
353 struct net_context *c = (struct net_context *)private_data;
356 name = talloc_asprintf(c, "%s\\%s",
357 info3->base.logon_domain.string,
358 info3->base.account_name.string);
363 d_printf("%-50s %-40s %s\n",
366 timestring(c, when_cached));
371 static int net_cache_samlogon_list(struct net_context *c,
377 d_printf("%-50s %-40s When cached\n", "SID", "Name");
378 d_printf("------------------------------------------------------------"
379 "------------------------------------------------------------"
382 ret = netsamlog_cache_for_all(netsamlog_cache_for_all_cb, c);
390 static int net_cache_samlogon_show(struct net_context *c,
394 const char *sid_str = argv[0];
396 struct dom_sid *user_sids = NULL;
397 uint32_t num_user_sids;
398 struct netr_SamInfo3 *info3 = NULL;
404 if (argc != 1 || c->display_usage) {
406 "net cache samlogon show SID\n"
409 _("Show samlogon cache entry for SID."));
413 ok = string_to_sid(&sid, sid_str);
415 d_printf("String to SID failed for %s\n", sid_str);
419 info3 = netsamlogon_cache_get(c, &sid);
421 d_printf("SID %s not found in samlogon cache\n", sid_str);
425 name = talloc_asprintf(c, "%s\\%s",
426 info3->base.logon_domain.string,
427 info3->base.account_name.string);
432 d_printf("Name: %s\n", name);
434 status = sid_array_from_info3(c,
439 if (!NT_STATUS_IS_OK(status)) {
440 d_printf("sid_array_from_info3 failed for %s\n", sid_str);
444 for (i = 0; i < num_user_sids; i++) {
445 d_printf("SID %2" PRIu32 ": %s\n",
446 i, sid_string_dbg(&user_sids[i]));
452 static int net_cache_samlogon_ndrdump(struct net_context *c,
456 const char *sid_str = NULL;
458 struct netr_SamInfo3 *info3 = NULL;
459 struct ndr_print *ndr_print = NULL;
462 if (argc != 1 || c->display_usage) {
464 "net cache samlogon ndrdump SID\n"
467 _("Show samlogon cache entry for SID."));
473 ok = string_to_sid(&sid, sid_str);
475 d_printf("String to SID failed for %s\n", sid_str);
479 info3 = netsamlogon_cache_get(c, &sid);
481 d_printf("SID %s not found in samlogon cache\n", sid_str);
485 ndr_print = talloc_zero(c, struct ndr_print);
486 if (ndr_print == NULL) {
487 d_printf("Could not allocate memory.\n");
491 ndr_print->print = ndr_print_printf_helper;
492 ndr_print->depth = 1;
493 ndr_print_netr_SamInfo3(ndr_print, "netr_SamInfo3", info3);
494 TALLOC_FREE(ndr_print);
499 static int net_cache_samlogon_delete(struct net_context *c,
503 const char *sid_str = argv[0];
507 if (argc != 1 || c->display_usage) {
509 "net cache samlogon delete SID\n"
512 _("Delete samlogon cache entry for SID."));
516 ok = string_to_sid(&sid, sid_str);
518 d_printf("String to SID failed for %s\n", sid_str);
522 netsamlogon_clear_cached_user(&sid);
527 static int net_cache_samlogon(struct net_context *c, int argc, const char **argv)
529 struct functable func[] = {
532 net_cache_samlogon_list,
534 N_("List samlogon cache"),
535 N_("net cache samlogon list\n"
536 " List samlogon cachen\n")
540 net_cache_samlogon_show,
542 N_("Show samlogon cache entry"),
543 N_("net cache samlogon show SID\n"
544 " Show samlogon cache entry\n")
548 net_cache_samlogon_ndrdump,
550 N_("Dump the samlogon cache entry NDR blob"),
551 N_("net cache samlogon ndrdump SID\n"
552 " Dump the samlogon cache entry NDR blob\n")
556 net_cache_samlogon_delete,
558 N_("Delete samlogon cache entry"),
559 N_("net cache samlogon delete SID\n"
560 " Delete samlogon cache entry\n")
562 {NULL, NULL, 0, NULL, NULL}
565 return net_run_function(c, argc, argv, "net cache samlogon", func);
569 * Entry point to 'net cache' subfunctionality
571 * @param c A net_context structure
572 * @param argv arguments passed to further called functions
573 * @return whatever further functions return
575 int net_cache(struct net_context *c, int argc, const char **argv)
577 struct functable func[] = {
582 N_("Add new cache entry"),
583 N_("net cache add <key string> <data string> <timeout>\n"
584 " Add new cache entry.\n"
585 " key string\tKey string to add cache data under.\n"
586 " data string\tData to store under given key.\n"
587 " timeout\tTimeout for cache data.")
593 N_("Delete existing cache entry by key"),
594 N_("net cache del <key string>\n"
595 " Delete existing cache entry by key.\n"
596 " key string\tKey string to delete.")
602 N_("Get cache entry by key"),
603 N_("net cache get <key string>\n"
604 " Get cache entry by key.\n"
605 " key string\tKey string to look up cache entry for.")
612 N_("Search entry by pattern"),
613 N_("net cache search <pattern>\n"
614 " Search entry by pattern.\n"
615 " pattern\tPattern to search for in cache.")
621 N_("List all cache entries"),
622 N_("net cache list\n"
623 " List all cache entries")
629 N_("Delete all cache entries"),
630 N_("net cache flush\n"
631 " Delete all cache entries")
637 N_("Move transient cache content to stable storage"),
638 N_("net cache stabilize\n"
639 " Move transient cache content to stable storage")
645 N_("List contents of the samlogon cache"),
646 N_("net cache samlogon\n"
647 " List contents of the samlogon cache")
649 {NULL, NULL, 0, NULL, NULL}
652 return net_run_function(c, argc, argv, "net cache", func);