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"
26 #include "lib/util/strv.h"
30 * @brief This is part of the net tool which is basically command
31 * line wrapper for gencache.c functions (mainly for testing)
37 * These routines are used via gencache_iterate() to display the cache's contents
38 * (print_cache_entry) and to flush it (delete_cache_entry).
39 * Both of them are defined by first arg of gencache_iterate() routine.
41 static void print_cache_entry(const char* keystr
, DATA_BLOB value
,
42 const time_t timeout
, void* dptr
)
45 char *alloc_str
= NULL
;
47 char *datastr_free
= NULL
;
48 time_t now_t
= time(NULL
);
49 struct tm timeout_tm
, now_tm
;
50 struct tm
*ptimeout_tm
, *pnow_tm
;
52 ptimeout_tm
= localtime_r(&timeout
, &timeout_tm
);
53 if (ptimeout_tm
== NULL
) {
56 pnow_tm
= localtime_r(&now_t
, &now_tm
);
57 if (pnow_tm
== NULL
) {
61 /* form up timeout string depending whether it's today's date or not */
62 if (timeout_tm
.tm_year
!= now_tm
.tm_year
||
63 timeout_tm
.tm_mon
!= now_tm
.tm_mon
||
64 timeout_tm
.tm_mday
!= now_tm
.tm_mday
) {
66 timeout_str
= asctime(&timeout_tm
);
70 timeout_str
[strlen(timeout_str
) - 1] = '\0'; /* remove tailing CR */
72 if (asprintf(&alloc_str
, "%.2d:%.2d:%.2d", timeout_tm
.tm_hour
,
73 timeout_tm
.tm_min
, timeout_tm
.tm_sec
) == -1) {
76 timeout_str
= alloc_str
;
79 datastr
= (char *)value
.data
;
81 if (strnequal(keystr
, "NAME2SID/", strlen("NAME2SID/"))) {
82 const char *strv
= (char *)value
.data
;
83 size_t strv_len
= value
.length
;
84 const char *sid
= strv_len_next(strv
, strv_len
, NULL
);
85 const char *type
= strv_len_next(strv
, strv_len
, sid
);
86 datastr
= talloc_asprintf(talloc_tos(), "%s (%s)", sid
, type
);
89 if (strnequal(keystr
, "SID2NAME/", strlen("SID2NAME/"))) {
90 const char *strv
= (char *)value
.data
;
91 size_t strv_len
= value
.length
;
92 const char *domain
= strv_len_next(strv
, strv_len
, NULL
);
93 const char *name
= strv_len_next(strv
, strv_len
, domain
);
94 const char *type
= strv_len_next(strv
, strv_len
, name
);
95 datastr
= talloc_asprintf(talloc_tos(), "%s\\%s (%s)",
99 if ((value
.length
> 0) && (value
.data
[value
.length
-1] != '\0')) {
100 datastr_free
= talloc_asprintf(
101 talloc_tos(), "<binary length %d>",
103 datastr
= datastr_free
;
104 if (datastr
== NULL
) {
105 datastr
= "<binary>";
109 d_printf(_("Key: %s\t Timeout: %s\t Value: %s %s\n"), keystr
,
110 timeout_str
, datastr
, timeout
> now_t
? "": _("(expired)"));
112 SAFE_FREE(alloc_str
);
115 static void delete_cache_entry(const char* keystr
, const char* datastr
,
116 const time_t timeout
, void* dptr
)
118 if (!gencache_del(keystr
))
119 d_fprintf(stderr
, _("Couldn't delete entry! key = %s\n"),
125 * Parse text representation of timeout value
127 * @param timeout_str string containing text representation of the timeout
128 * @return numeric timeout of time_t type
130 static time_t parse_timeout(const char* timeout_str
)
132 char sign
= '\0', *number
= NULL
, unit
= '\0';
133 int len
, number_begin
, number_end
;
137 if (timeout_str
[0] == '!' || timeout_str
[0] == '+') {
138 sign
= timeout_str
[0];
145 len
= strlen(timeout_str
);
146 switch (timeout_str
[len
- 1]) {
151 case 'w': unit
= timeout_str
[len
- 1];
154 /* number detection */
155 len
= (sign
) ? strlen(&timeout_str
[number_begin
]) : len
;
156 number_end
= (unit
) ? len
- 1 : len
;
157 number
= SMB_STRNDUP(&timeout_str
[number_begin
], number_end
);
159 /* calculate actual timeout value */
160 timeout
= (time_t)atoi(number
);
163 case 'm': timeout
*= 60; break;
164 case 'h': timeout
*= 60*60; break;
165 case 'd': timeout
*= 60*60*24; break;
166 case 'w': timeout
*= 60*60*24*7; break; /* that's fair enough, I think :) */
170 case '!': timeout
= time(NULL
) - timeout
; break;
172 default: timeout
+= time(NULL
); break;
175 if (number
) SAFE_FREE(number
);
181 * Add an entry to the cache. If it does exist, then set it.
183 * @param c A net_context structure
184 * @param argv key, value and timeout are passed in command line
185 * @return 0 on success, otherwise failure
187 static int net_cache_add(struct net_context
*c
, int argc
, const char **argv
)
189 const char *keystr
, *datastr
, *timeout_str
;
192 if (argc
< 3 || c
->display_usage
) {
195 _("net cache add <key string> <data string> "
202 timeout_str
= argv
[2];
204 /* parse timeout given in command line */
205 timeout
= parse_timeout(timeout_str
);
207 d_fprintf(stderr
, _("Invalid timeout argument.\n"));
211 if (gencache_set(keystr
, datastr
, timeout
)) {
212 d_printf(_("New cache entry stored successfully.\n"));
216 d_fprintf(stderr
, _("Entry couldn't be added. Perhaps there's already such a key.\n"));
221 * Delete an entry in the cache
223 * @param c A net_context structure
224 * @param argv key to delete an entry of
225 * @return 0 on success, otherwise failure
227 static int net_cache_del(struct net_context
*c
, int argc
, const char **argv
)
229 const char *keystr
= argv
[0];
231 if (argc
< 1 || c
->display_usage
) {
234 _(" net cache del <key string>\n"));
238 if(gencache_del(keystr
)) {
239 d_printf(_("Entry deleted.\n"));
243 d_fprintf(stderr
, _("Couldn't delete specified entry\n"));
249 * Get and display an entry from the cache
251 * @param c A net_context structure
252 * @param argv key to search an entry of
253 * @return 0 on success, otherwise failure
255 static int net_cache_get(struct net_context
*c
, int argc
, const char **argv
)
257 const char* keystr
= argv
[0];
261 if (argc
< 1 || c
->display_usage
) {
264 _(" net cache get <key>\n"));
268 if (gencache_get_data_blob(keystr
, NULL
, &value
, &timeout
, NULL
)) {
269 print_cache_entry(keystr
, value
, timeout
, NULL
);
270 data_blob_free(&value
);
274 d_fprintf(stderr
, _("Failed to find entry\n"));
280 * Search an entry/entries in the cache
282 * @param c A net_context structure
283 * @param argv key pattern to match the entries to
284 * @return 0 on success, otherwise failure
286 static int net_cache_search(struct net_context
*c
, int argc
, const char **argv
)
290 if (argc
< 1 || c
->display_usage
) {
293 _(" net cache search <pattern>\n"));
298 gencache_iterate_blobs(print_cache_entry
, NULL
, pattern
);
304 * List the contents of the cache
306 * @param c A net_context structure
307 * @param argv ignored in this functionailty
308 * @return always returns 0
310 static int net_cache_list(struct net_context
*c
, int argc
, const char **argv
)
312 const char* pattern
= "*";
314 if (c
->display_usage
) {
319 _("List all cache entries."));
322 gencache_iterate_blobs(print_cache_entry
, NULL
, pattern
);
328 * Flush the whole cache
330 * @param c A net_context structure
331 * @param argv ignored in this functionality
332 * @return always returns 0
334 static int net_cache_flush(struct net_context
*c
, int argc
, const char **argv
)
336 const char* pattern
= "*";
337 if (c
->display_usage
) {
342 _("Delete all cache entries."));
345 gencache_iterate(delete_cache_entry
, NULL
, pattern
);
349 static int net_cache_stabilize(struct net_context
*c
, int argc
,
352 if (c
->display_usage
) {
354 "net cache stabilize\n"
357 _("Move transient cache content to stable storage"));
361 if (!gencache_stabilize()) {
367 static int netsamlog_cache_for_all_cb(const char *sid_str
,
369 struct netr_SamInfo3
*info3
,
372 struct net_context
*c
= (struct net_context
*)private_data
;
375 name
= talloc_asprintf(c
, "%s\\%s",
376 info3
->base
.logon_domain
.string
,
377 info3
->base
.account_name
.string
);
382 d_printf("%-50s %-40s %s\n",
385 timestring(c
, when_cached
));
390 static int net_cache_samlogon_list(struct net_context
*c
,
396 d_printf("%-50s %-40s When cached\n", "SID", "Name");
397 d_printf("------------------------------------------------------------"
398 "------------------------------------------------------------"
401 ret
= netsamlog_cache_for_all(netsamlog_cache_for_all_cb
, c
);
409 static int net_cache_samlogon_show(struct net_context
*c
,
413 const char *sid_str
= argv
[0];
415 struct dom_sid
*user_sids
= NULL
;
416 uint32_t num_user_sids
;
417 struct netr_SamInfo3
*info3
= NULL
;
423 if (argc
!= 1 || c
->display_usage
) {
425 "net cache samlogon show SID\n"
428 _("Show samlogon cache entry for SID."));
432 ok
= string_to_sid(&sid
, sid_str
);
434 d_printf("String to SID failed for %s\n", sid_str
);
438 info3
= netsamlogon_cache_get(c
, &sid
);
440 d_printf("SID %s not found in samlogon cache\n", sid_str
);
444 name
= talloc_asprintf(c
, "%s\\%s",
445 info3
->base
.logon_domain
.string
,
446 info3
->base
.account_name
.string
);
451 d_printf("Name: %s\n", name
);
453 status
= sid_array_from_info3(c
,
458 if (!NT_STATUS_IS_OK(status
)) {
459 TALLOC_FREE(user_sids
);
460 d_printf("sid_array_from_info3 failed for %s\n", sid_str
);
464 for (i
= 0; i
< num_user_sids
; i
++) {
465 char buf
[DOM_SID_STR_BUFLEN
];
466 dom_sid_string_buf(&user_sids
[i
], buf
, sizeof(buf
));
467 d_printf("SID %2" PRIu32
": %s\n", i
, buf
);
470 TALLOC_FREE(user_sids
);
475 static int net_cache_samlogon_ndrdump(struct net_context
*c
,
479 const char *sid_str
= NULL
;
481 struct netr_SamInfo3
*info3
= NULL
;
482 struct ndr_print
*ndr_print
= NULL
;
485 if (argc
!= 1 || c
->display_usage
) {
487 "net cache samlogon ndrdump SID\n"
490 _("Show samlogon cache entry for SID."));
496 ok
= string_to_sid(&sid
, sid_str
);
498 d_printf("String to SID failed for %s\n", sid_str
);
502 info3
= netsamlogon_cache_get(c
, &sid
);
504 d_printf("SID %s not found in samlogon cache\n", sid_str
);
508 ndr_print
= talloc_zero(c
, struct ndr_print
);
509 if (ndr_print
== NULL
) {
510 d_printf("Could not allocate memory.\n");
514 ndr_print
->print
= ndr_print_printf_helper
;
515 ndr_print
->depth
= 1;
516 ndr_print_netr_SamInfo3(ndr_print
, "netr_SamInfo3", info3
);
517 TALLOC_FREE(ndr_print
);
522 static int net_cache_samlogon_delete(struct net_context
*c
,
526 const char *sid_str
= argv
[0];
530 if (argc
!= 1 || c
->display_usage
) {
532 "net cache samlogon delete SID\n"
535 _("Delete samlogon cache entry for SID."));
539 ok
= string_to_sid(&sid
, sid_str
);
541 d_printf("String to SID failed for %s\n", sid_str
);
545 netsamlogon_clear_cached_user(&sid
);
550 static int net_cache_samlogon(struct net_context
*c
, int argc
, const char **argv
)
552 struct functable func
[] = {
555 net_cache_samlogon_list
,
557 N_("List samlogon cache"),
558 N_("net cache samlogon list\n"
559 " List samlogon cachen\n")
563 net_cache_samlogon_show
,
565 N_("Show samlogon cache entry"),
566 N_("net cache samlogon show SID\n"
567 " Show samlogon cache entry\n")
571 net_cache_samlogon_ndrdump
,
573 N_("Dump the samlogon cache entry NDR blob"),
574 N_("net cache samlogon ndrdump SID\n"
575 " Dump the samlogon cache entry NDR blob\n")
579 net_cache_samlogon_delete
,
581 N_("Delete samlogon cache entry"),
582 N_("net cache samlogon delete SID\n"
583 " Delete samlogon cache entry\n")
585 {NULL
, NULL
, 0, NULL
, NULL
}
588 return net_run_function(c
, argc
, argv
, "net cache samlogon", func
);
592 * Entry point to 'net cache' subfunctionality
594 * @param c A net_context structure
595 * @param argv arguments passed to further called functions
596 * @return whatever further functions return
598 int net_cache(struct net_context
*c
, int argc
, const char **argv
)
600 struct functable func
[] = {
605 N_("Add new cache entry"),
606 N_("net cache add <key string> <data string> <timeout>\n"
607 " Add new cache entry.\n"
608 " key string\tKey string to add cache data under.\n"
609 " data string\tData to store under given key.\n"
610 " timeout\tTimeout for cache data.")
616 N_("Delete existing cache entry by key"),
617 N_("net cache del <key string>\n"
618 " Delete existing cache entry by key.\n"
619 " key string\tKey string to delete.")
625 N_("Get cache entry by key"),
626 N_("net cache get <key string>\n"
627 " Get cache entry by key.\n"
628 " key string\tKey string to look up cache entry for.")
635 N_("Search entry by pattern"),
636 N_("net cache search <pattern>\n"
637 " Search entry by pattern.\n"
638 " pattern\tPattern to search for in cache.")
644 N_("List all cache entries"),
645 N_("net cache list\n"
646 " List all cache entries")
652 N_("Delete all cache entries"),
653 N_("net cache flush\n"
654 " Delete all cache entries")
660 N_("Move transient cache content to stable storage"),
661 N_("net cache stabilize\n"
662 " Move transient cache content to stable storage")
668 N_("List contents of the samlogon cache"),
669 N_("net cache samlogon\n"
670 " List contents of the samlogon cache")
672 {NULL
, NULL
, 0, NULL
, NULL
}
675 return net_run_function(c
, argc
, argv
, "net cache", func
);