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 d_printf("sid_array_from_info3 failed for %s\n", sid_str
);
463 for (i
= 0; i
< num_user_sids
; i
++) {
464 d_printf("SID %2" PRIu32
": %s\n",
465 i
, sid_string_dbg(&user_sids
[i
]));
471 static int net_cache_samlogon_ndrdump(struct net_context
*c
,
475 const char *sid_str
= NULL
;
477 struct netr_SamInfo3
*info3
= NULL
;
478 struct ndr_print
*ndr_print
= NULL
;
481 if (argc
!= 1 || c
->display_usage
) {
483 "net cache samlogon ndrdump SID\n"
486 _("Show samlogon cache entry for SID."));
492 ok
= string_to_sid(&sid
, sid_str
);
494 d_printf("String to SID failed for %s\n", sid_str
);
498 info3
= netsamlogon_cache_get(c
, &sid
);
500 d_printf("SID %s not found in samlogon cache\n", sid_str
);
504 ndr_print
= talloc_zero(c
, struct ndr_print
);
505 if (ndr_print
== NULL
) {
506 d_printf("Could not allocate memory.\n");
510 ndr_print
->print
= ndr_print_printf_helper
;
511 ndr_print
->depth
= 1;
512 ndr_print_netr_SamInfo3(ndr_print
, "netr_SamInfo3", info3
);
513 TALLOC_FREE(ndr_print
);
518 static int net_cache_samlogon_delete(struct net_context
*c
,
522 const char *sid_str
= argv
[0];
526 if (argc
!= 1 || c
->display_usage
) {
528 "net cache samlogon delete SID\n"
531 _("Delete samlogon cache entry for SID."));
535 ok
= string_to_sid(&sid
, sid_str
);
537 d_printf("String to SID failed for %s\n", sid_str
);
541 netsamlogon_clear_cached_user(&sid
);
546 static int net_cache_samlogon(struct net_context
*c
, int argc
, const char **argv
)
548 struct functable func
[] = {
551 net_cache_samlogon_list
,
553 N_("List samlogon cache"),
554 N_("net cache samlogon list\n"
555 " List samlogon cachen\n")
559 net_cache_samlogon_show
,
561 N_("Show samlogon cache entry"),
562 N_("net cache samlogon show SID\n"
563 " Show samlogon cache entry\n")
567 net_cache_samlogon_ndrdump
,
569 N_("Dump the samlogon cache entry NDR blob"),
570 N_("net cache samlogon ndrdump SID\n"
571 " Dump the samlogon cache entry NDR blob\n")
575 net_cache_samlogon_delete
,
577 N_("Delete samlogon cache entry"),
578 N_("net cache samlogon delete SID\n"
579 " Delete samlogon cache entry\n")
581 {NULL
, NULL
, 0, NULL
, NULL
}
584 return net_run_function(c
, argc
, argv
, "net cache samlogon", func
);
588 * Entry point to 'net cache' subfunctionality
590 * @param c A net_context structure
591 * @param argv arguments passed to further called functions
592 * @return whatever further functions return
594 int net_cache(struct net_context
*c
, int argc
, const char **argv
)
596 struct functable func
[] = {
601 N_("Add new cache entry"),
602 N_("net cache add <key string> <data string> <timeout>\n"
603 " Add new cache entry.\n"
604 " key string\tKey string to add cache data under.\n"
605 " data string\tData to store under given key.\n"
606 " timeout\tTimeout for cache data.")
612 N_("Delete existing cache entry by key"),
613 N_("net cache del <key string>\n"
614 " Delete existing cache entry by key.\n"
615 " key string\tKey string to delete.")
621 N_("Get cache entry by key"),
622 N_("net cache get <key string>\n"
623 " Get cache entry by key.\n"
624 " key string\tKey string to look up cache entry for.")
631 N_("Search entry by pattern"),
632 N_("net cache search <pattern>\n"
633 " Search entry by pattern.\n"
634 " pattern\tPattern to search for in cache.")
640 N_("List all cache entries"),
641 N_("net cache list\n"
642 " List all cache entries")
648 N_("Delete all cache entries"),
649 N_("net cache flush\n"
650 " Delete all cache entries")
656 N_("Move transient cache content to stable storage"),
657 N_("net cache stabilize\n"
658 " Move transient cache content to stable storage")
664 N_("List contents of the samlogon cache"),
665 N_("net cache samlogon\n"
666 " List contents of the samlogon cache")
668 {NULL
, NULL
, 0, NULL
, NULL
}
671 return net_run_function(c
, argc
, argv
, "net cache", func
);