📄 tdbutil.c
字号:
break; case 'f': /* null-terminated string */ s = va_arg(ap,char *); w = strlen(s); len = w + 1; if (bufsize && bufsize >= len) memcpy(buf, s, len); break; case 'B': /* fixed-length string */ i = va_arg(ap, int); s = va_arg(ap, char *); len = 4+i; if (bufsize && bufsize >= len) { SIVAL(buf, 0, i); memcpy(buf+4, s, i); } break; default: DEBUG(0,("Unknown tdb_pack format %c in %s\n", c, fmt)); len = 0; break; } buf += len; if (bufsize) bufsize -= len; if (bufsize < 0) bufsize = 0; } DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", fmt0, bufsize0, (int)PTR_DIFF(buf, buf0))); return PTR_DIFF(buf, buf0);}size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...){ va_list ap; size_t result; va_start(ap, fmt); result = tdb_pack_va(buf, bufsize, fmt, ap); va_end(ap); return result;}BOOL tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len, const char *fmt, ...){ va_list ap; size_t len1, len2; va_start(ap, fmt); len1 = tdb_pack_va(NULL, 0, fmt, ap); va_end(ap); if (mem_ctx != NULL) *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8, (*len) + len1); else *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1); if (*buf == NULL) return False; va_start(ap, fmt); len2 = tdb_pack_va((char *)(*buf)+(*len), len1, fmt, ap); va_end(ap); if (len1 != len2) return False; *len += len2; return True;}/**************************************************************************** Useful pair of routines for packing/unpacking data consisting of integers and strings.****************************************************************************/int tdb_unpack(char *buf, int bufsize, const char *fmt, ...){ va_list ap; uint8 *bt; uint16 *w; uint32 *d; int len; int *i; void **p; char *s, **b; char c; char *buf0 = buf; const char *fmt0 = fmt; int bufsize0 = bufsize; va_start(ap, fmt); while (*fmt) { switch ((c=*fmt++)) { case 'b': len = 1; bt = va_arg(ap, uint8 *); if (bufsize < len) goto no_space; *bt = SVAL(buf, 0); break; case 'w': len = 2; w = va_arg(ap, uint16 *); if (bufsize < len) goto no_space; *w = SVAL(buf, 0); break; case 'd': len = 4; d = va_arg(ap, uint32 *); if (bufsize < len) goto no_space; *d = IVAL(buf, 0); break; case 'p': len = 4; p = va_arg(ap, void **); if (bufsize < len) goto no_space; /* * This isn't a real pointer - only a token (1 or 0) * to mark the fact a pointer is present. */ *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL); break; case 'P': s = va_arg(ap,char *); len = strlen(buf) + 1; if (bufsize < len || len > sizeof(pstring)) goto no_space; memcpy(s, buf, len); break; case 'f': s = va_arg(ap,char *); len = strlen(buf) + 1; if (bufsize < len || len > sizeof(fstring)) goto no_space; memcpy(s, buf, len); break; case 'B': i = va_arg(ap, int *); b = va_arg(ap, char **); len = 4; if (bufsize < len) goto no_space; *i = IVAL(buf, 0); if (! *i) { *b = NULL; break; } len += *i; if (bufsize < len) goto no_space; *b = (char *)SMB_MALLOC(*i); if (! *b) goto no_space; memcpy(*b, buf+4, *i); break; default: DEBUG(0,("Unknown tdb_unpack format %c in %s\n", c, fmt)); len = 0; break; } buf += len; bufsize -= len; } va_end(ap); DEBUG(18,("tdb_unpack(%s, %d) -> %d\n", fmt0, bufsize0, (int)PTR_DIFF(buf, buf0))); return PTR_DIFF(buf, buf0); no_space: return -1;}/** * Pack SID passed by pointer * * @param pack_buf pointer to buffer which is to be filled with packed data * @param bufsize size of packing buffer * @param sid pointer to sid to be packed * * @return length of the packed representation of the whole structure **/size_t tdb_sid_pack(char* pack_buf, int bufsize, DOM_SID* sid){ int idx; size_t len = 0; if (!sid || !pack_buf) return -1; len += tdb_pack(pack_buf + len, bufsize - len, "bb", sid->sid_rev_num, sid->num_auths); for (idx = 0; idx < 6; idx++) { len += tdb_pack(pack_buf + len, bufsize - len, "b", sid->id_auth[idx]); } for (idx = 0; idx < MAXSUBAUTHS; idx++) { len += tdb_pack(pack_buf + len, bufsize - len, "d", sid->sub_auths[idx]); } return len;}/** * Unpack SID into a pointer * * @param pack_buf pointer to buffer with packed representation * @param bufsize size of the buffer * @param sid pointer to sid structure to be filled with unpacked data * * @return size of structure unpacked from buffer **/size_t tdb_sid_unpack(char* pack_buf, int bufsize, DOM_SID* sid){ int idx, len = 0; if (!sid || !pack_buf) return -1; len += tdb_unpack(pack_buf + len, bufsize - len, "bb", &sid->sid_rev_num, &sid->num_auths); for (idx = 0; idx < 6; idx++) { len += tdb_unpack(pack_buf + len, bufsize - len, "b", &sid->id_auth[idx]); } for (idx = 0; idx < MAXSUBAUTHS; idx++) { len += tdb_unpack(pack_buf + len, bufsize - len, "d", &sid->sub_auths[idx]); } return len;}/** * Pack TRUSTED_DOM_PASS passed by pointer * * @param pack_buf pointer to buffer which is to be filled with packed data * @param bufsize size of the buffer * @param pass pointer to trusted domain password to be packed * * @return length of the packed representation of the whole structure **/size_t tdb_trusted_dom_pass_pack(char* pack_buf, int bufsize, TRUSTED_DOM_PASS* pass){ int idx, len = 0; if (!pack_buf || !pass) return -1; /* packing unicode domain name and password */ len += tdb_pack(pack_buf + len, bufsize - len, "d", pass->uni_name_len); for (idx = 0; idx < 32; idx++) len += tdb_pack(pack_buf + len, bufsize - len, "w", pass->uni_name[idx]); len += tdb_pack(pack_buf + len, bufsize - len, "dPd", pass->pass_len, pass->pass, pass->mod_time); /* packing SID structure */ len += tdb_sid_pack(pack_buf + len, bufsize - len, &pass->domain_sid); return len;}/** * Unpack TRUSTED_DOM_PASS passed by pointer * * @param pack_buf pointer to buffer with packed representation * @param bufsize size of the buffer * @param pass pointer to trusted domain password to be filled with unpacked data * * @return size of structure unpacked from buffer **/size_t tdb_trusted_dom_pass_unpack(char* pack_buf, int bufsize, TRUSTED_DOM_PASS* pass){ int idx, len = 0; if (!pack_buf || !pass) return -1; /* unpack unicode domain name and plaintext password */ len += tdb_unpack(pack_buf, bufsize - len, "d", &pass->uni_name_len); for (idx = 0; idx < 32; idx++) len += tdb_unpack(pack_buf + len, bufsize - len, "w", &pass->uni_name[idx]); len += tdb_unpack(pack_buf + len, bufsize - len, "dPd", &pass->pass_len, &pass->pass, &pass->mod_time); /* unpack domain sid */ len += tdb_sid_unpack(pack_buf + len, bufsize - len, &pass->domain_sid); return len; }/**************************************************************************** Log tdb messages via DEBUG().****************************************************************************/static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...){ va_list ap; char *ptr = NULL; va_start(ap, format); vasprintf(&ptr, format, ap); va_end(ap); if (!ptr || !*ptr) return; DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr)); SAFE_FREE(ptr);}/**************************************************************************** Like tdb_open() but also setup a logging function that redirects to the samba DEBUG() system.****************************************************************************/TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode){ TDB_CONTEXT *tdb; if (!lp_use_mmap()) tdb_flags |= TDB_NOMMAP; tdb = tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, tdb_log, NULL); if (!tdb) return NULL; return tdb;}/**************************************************************************** Allow tdb_delete to be used as a tdb_traversal_fn.****************************************************************************/int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state){ return tdb_delete(the_tdb, key);}/** * Search across the whole tdb for keys that match the given pattern * return the result as a list of keys * * @param tdb pointer to opened tdb file context * @param pattern searching pattern used by fnmatch(3) functions * * @return list of keys found by looking up with given pattern **/TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern){ TDB_DATA key, next; TDB_LIST_NODE *list = NULL; TDB_LIST_NODE *rec = NULL; TDB_LIST_NODE *tmp = NULL; for (key = tdb_firstkey(tdb); key.dptr; key = next) { /* duplicate key string to ensure null-termination */ char *key_str = (char*) SMB_STRNDUP(key.dptr, key.dsize); if (!key_str) { DEBUG(0, ("tdb_search_keys: strndup() failed!\n")); smb_panic("strndup failed!\n"); } DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern)); next = tdb_nextkey(tdb, key); /* do the pattern checking */ if (fnmatch(pattern, key_str, 0) == 0) { rec = SMB_MALLOC_P(TDB_LIST_NODE); ZERO_STRUCTP(rec); rec->node_key = key; DLIST_ADD_END(list, rec, tmp); DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern)); } else { free(key.dptr); } /* free duplicated key string */ free(key_str); } return list;}/** * Free the list returned by tdb_search_keys * * @param node list of results found by tdb_search_keys **/void tdb_search_list_free(TDB_LIST_NODE* node){ TDB_LIST_NODE *next_node; while (node) { next_node = node->next; SAFE_FREE(node->node_key.dptr); SAFE_FREE(node); node = next_node; };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -