The branch, master has been updated via 8ea51f4135d s3: libsmb: Cleanup - Make ipstr_list_make() talloc rather than malloc. via 1d712add994 s3: libsmb: Cleanup - Move DEBUG -> DBG_XXX() macros. via bd205f2a6cc s3: libsmb: Cleanup - make namecache_status_record_key() use talloc. via 56d5cbe8bf6 s3: libsmb: Cleanup - make namecache_key() use talloc. via df0e54bea30 s3: libsmb: Cleanup - namecache_store() - use common out. via 06f0a7e911d s3: libsmb: Cleanup - namecache_store() initialize stack variables. via 67ea64d27e6 s3: libsmb: Cleanup - move talloc frame out of inner scope. via be41035127d s3: libsmb: Cleanup modern coding standards. 'True/False' -> 'true/false'. via c7e8c3d427d s3: lib: Cleanup - make ipstr_list_make() and ipstr_list_parse() private to the only user. via 2371c45f11c s3: lib: Cleanup - nothing uses ipstr_list_free(). Remove it. via 527d7df08ee s3: lib: Cleanup - all the ipstr_XXX() functions are only used in namecache.c. from 0e287127cb8 ctdb-tools: Improve onnode's ShellCheck credibility
https://git.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 8ea51f4135d21f0cf70215521818eeb55e7bdbaa Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 15:02:02 2020 -0700 s3: libsmb: Cleanup - Make ipstr_list_make() talloc rather than malloc. Remove the excessive and unneeded ipstr_list_add() function, fold it into ipstr_list_make() to make it much clearer what we're doing. The only use of MALLOC now is in ipstr_list_parse() returned by namecache_fetch(). We need to fix the caller before we can move that to talloc. As that is used inside internal_resolve_name() which is designed to return a MALLOC'ed ip list from all name resolution mechanisms leave that fix for another day. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> Autobuild-User(master): Volker Lendecke <v...@samba.org> Autobuild-Date(master): Thu Jul 16 08:16:31 UTC 2020 on sn-devel-184 commit 1d712add994f8426a90589788be53c1ce2e54c17 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 14:41:45 2020 -0700 s3: libsmb: Cleanup - Move DEBUG -> DBG_XXX() macros. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit bd205f2a6cc6ca23bab8eb15d57c16994641d711 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 14:38:23 2020 -0700 s3: libsmb: Cleanup - make namecache_status_record_key() use talloc. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit 56d5cbe8bf6f4cd65b673b7923f955a457f8e0c7 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 13:37:59 2020 -0700 s3: libsmb: Cleanup - make namecache_key() use talloc. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit df0e54bea307b459433e1fdf9dc547875ee494cc Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 13:33:27 2020 -0700 s3: libsmb: Cleanup - namecache_store() - use common out. Prepare for moving malloc values to talloc. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit 06f0a7e911da4f3279066f5ffaf4e03d01b354af Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 13:31:38 2020 -0700 s3: libsmb: Cleanup - namecache_store() initialize stack variables. Preparing for common out: exit. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit 67ea64d27e6ede6993025e9598162f385d2937f7 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 13:28:33 2020 -0700 s3: libsmb: Cleanup - move talloc frame out of inner scope. Make it available thoughout the function. Prepare to use talloc for namecache_key(). Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit be41035127d3cc7f8144b10dcbc1894dff4d587f Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 12:12:23 2020 -0700 s3: libsmb: Cleanup modern coding standards. 'True/False' -> 'true/false'. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit c7e8c3d427d39f275588c495c92f3e2eeca2b832 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 11:58:45 2020 -0700 s3: lib: Cleanup - make ipstr_list_make() and ipstr_list_parse() private to the only user. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit 2371c45f11c91ef16b02d86cdc6f6d4f7624b356 Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 11:48:30 2020 -0700 s3: lib: Cleanup - nothing uses ipstr_list_free(). Remove it. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> commit 527d7df08eef1ab8a3ecbed3e16d4ffcbd93db1c Author: Jeremy Allison <j...@samba.org> Date: Wed Jul 15 11:43:03 2020 -0700 s3: lib: Cleanup - all the ipstr_XXX() functions are only used in namecache.c. Move them there. Will remove from the global namespace next. Signed-off-by: Jeremy Allison <j...@samba.org> Reviewed-by: Volker Lendecke <v...@samba.org> ----------------------------------------------------------------------- Summary of changes: source3/include/proto.h | 5 - source3/lib/util_str.c | 164 --------------------------- source3/libsmb/namecache.c | 275 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 212 insertions(+), 232 deletions(-) Changeset truncated at 500 lines: diff --git a/source3/include/proto.h b/source3/include/proto.h index 12aa392abae..d349e22aa6b 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -631,11 +631,6 @@ size_t strlen_m_term(const char *s); size_t strlen_m_term_null(const char *s); int fstr_sprintf(fstring s, const char *fmt, ...); -char *ipstr_list_make(char **ipstr_list, - const struct ip_service *ip_list, - int ip_count); -int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list); -void ipstr_list_free(char* ipstr_list); uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr); uint64_t conv_str_size(const char * str); int asprintf_strupper_m(char **strp, const char *fmt, ...) diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index c62cbe16a59..c31667c8821 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -560,170 +560,6 @@ int fstr_sprintf(fstring s, const char *fmt, ...) return ret; } -#define IPSTR_LIST_SEP "," -#define IPSTR_LIST_CHAR ',' - -/** - * Add ip string representation to ipstr list. Used also - * as part of @function ipstr_list_make - * - * @param ipstr_list pointer to string containing ip list; - * MUST BE already allocated and IS reallocated if necessary - * @param ipstr_size pointer to current size of ipstr_list (might be changed - * as a result of reallocation) - * @param ip IP address which is to be added to list - * @return pointer to string appended with new ip and possibly - * reallocated to new length - **/ - -static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service) -{ - char *new_ipstr = NULL; - char addr_buf[INET6_ADDRSTRLEN]; - int ret; - - /* arguments checking */ - if (!ipstr_list || !service) { - return NULL; - } - - print_sockaddr(addr_buf, - sizeof(addr_buf), - &service->ss); - - /* attempt to convert ip to a string and append colon separator to it */ - if (*ipstr_list) { - if (service->ss.ss_family == AF_INET) { - /* IPv4 */ - ret = asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list, - IPSTR_LIST_SEP, addr_buf, - service->port); - } else { - /* IPv6 */ - ret = asprintf(&new_ipstr, "%s%s[%s]:%d", *ipstr_list, - IPSTR_LIST_SEP, addr_buf, - service->port); - } - SAFE_FREE(*ipstr_list); - } else { - if (service->ss.ss_family == AF_INET) { - /* IPv4 */ - ret = asprintf(&new_ipstr, "%s:%d", addr_buf, - service->port); - } else { - /* IPv6 */ - ret = asprintf(&new_ipstr, "[%s]:%d", addr_buf, - service->port); - } - } - if (ret == -1) { - return NULL; - } - *ipstr_list = new_ipstr; - return *ipstr_list; -} - -/** - * Allocate and initialise an ipstr list using ip adresses - * passed as arguments. - * - * @param ipstr_list pointer to string meant to be allocated and set - * @param ip_list array of ip addresses to place in the list - * @param ip_count number of addresses stored in ip_list - * @return pointer to allocated ip string - **/ - -char *ipstr_list_make(char **ipstr_list, - const struct ip_service *ip_list, - int ip_count) -{ - int i; - - /* arguments checking */ - if (!ip_list || !ipstr_list) { - return 0; - } - - *ipstr_list = NULL; - - /* process ip addresses given as arguments */ - for (i = 0; i < ip_count; i++) { - *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]); - } - - return (*ipstr_list); -} - - -/** - * Parse given ip string list into array of ip addresses - * (as ip_service structures) - * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ... - * - * @param ipstr ip string list to be parsed - * @param ip_list pointer to array of ip addresses which is - * allocated by this function and must be freed by caller - * @return number of successfully parsed addresses - **/ - -int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) -{ - TALLOC_CTX *frame; - char *token_str = NULL; - size_t i, count; - - if (!ipstr_list || !ip_list) - return 0; - - count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; - if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { - DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n", - (unsigned long)count)); - return 0; - } - - frame = talloc_stackframe(); - for ( i=0; next_token_talloc(frame, &ipstr_list, &token_str, - IPSTR_LIST_SEP) && i<count; i++ ) { - char *s = token_str; - char *p = strrchr(token_str, ':'); - - if (p) { - *p = 0; - (*ip_list)[i].port = atoi(p+1); - } - - /* convert single token to ip address */ - if (token_str[0] == '[') { - /* IPv6 address. */ - s++; - p = strchr(token_str, ']'); - if (!p) { - continue; - } - *p = '\0'; - } - if (!interpret_string_addr(&(*ip_list)[i].ss, - s, - AI_NUMERICHOST)) { - continue; - } - } - TALLOC_FREE(frame); - return count; -} - -/** - * Safely free ip string list - * - * @param ipstr_list ip string list to be freed - **/ - -void ipstr_list_free(char* ipstr_list) -{ - SAFE_FREE(ipstr_list); -} - /* read a SMB_BIG_UINT from a string */ uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr) { diff --git a/source3/libsmb/namecache.c b/source3/libsmb/namecache.c index 082f256bc02..7f534587263 100644 --- a/source3/libsmb/namecache.c +++ b/source3/libsmb/namecache.c @@ -24,6 +24,147 @@ #include "includes.h" #include "lib/gencache.h" +#define IPSTR_LIST_SEP "," +#define IPSTR_LIST_CHAR ',' + +/** + * Allocate and initialise an ipstr list using ip adresses + * passed as arguments. + * + * @param ctx TALLOC_CTX to use + * @param ip_list array of ip addresses to place in the list + * @param ip_count number of addresses stored in ip_list + * @return pointer to allocated ip string + **/ + +static char *ipstr_list_make(TALLOC_CTX *ctx, + const struct ip_service *ip_list, + int ip_count) +{ + char *ipstr_list = NULL; + int i; + + /* arguments checking */ + if (ip_list == NULL) { + return NULL; + } + + /* process ip addresses given as arguments */ + for (i = 0; i < ip_count; i++) { + char addr_buf[INET6_ADDRSTRLEN]; + char *new_str = NULL; + + print_sockaddr(addr_buf, + sizeof(addr_buf), + &ip_list[i].ss); + + if (ip_list->ss.ss_family == AF_INET) { + /* IPv4 */ + new_str = talloc_asprintf(ctx, + "%s:%d", + addr_buf, + ip_list[i].port); + } else { + /* IPv6 */ + new_str = talloc_asprintf(ctx, + "[%s]:%d", + addr_buf, + ip_list[i].port); + } + if (new_str == NULL) { + TALLOC_FREE(ipstr_list); + return NULL; + } + + if (ipstr_list == NULL) { + /* First ip address. */ + ipstr_list = new_str; + } else { + /* + * Append the separator "," and then the new + * ip address to the existing list. + * + * The efficiency here is horrible, but + * ip_count should be small enough we can + * live with it. + */ + char *tmp = talloc_asprintf(ctx, + "%s%s%s", + ipstr_list, + IPSTR_LIST_SEP, + new_str); + if (tmp == NULL) { + TALLOC_FREE(new_str); + TALLOC_FREE(ipstr_list); + return NULL; + } + TALLOC_FREE(new_str); + TALLOC_FREE(ipstr_list); + ipstr_list = tmp; + } + } + + return ipstr_list; +} + +/** + * Parse given ip string list into array of ip addresses + * (as ip_service structures) + * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ... + * + * @param ipstr ip string list to be parsed + * @param ip_list pointer to array of ip addresses which is + * allocated by this function and must be freed by caller + * @return number of successfully parsed addresses + **/ + +static int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list) +{ + TALLOC_CTX *frame; + char *token_str = NULL; + size_t i, count; + + if (!ipstr_list || !ip_list) + return 0; + + count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1; + if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) { + DBG_ERR("malloc failed for %lu entries\n", + (unsigned long)count); + return 0; + } + + frame = talloc_stackframe(); + for ( i=0; next_token_talloc(frame, &ipstr_list, &token_str, + IPSTR_LIST_SEP) && i<count; i++ ) { + char *s = token_str; + char *p = strrchr(token_str, ':'); + + if (p) { + *p = 0; + (*ip_list)[i].port = atoi(p+1); + } + + /* convert single token to ip address */ + if (token_str[0] == '[') { + /* IPv6 address. */ + s++; + p = strchr(token_str, ']'); + if (!p) { + continue; + } + *p = '\0'; + } + if (!interpret_string_addr(&(*ip_list)[i].ss, + s, + AI_NUMERICHOST)) { + continue; + } + } + TALLOC_FREE(frame); + return count; +} + #define NBTKEY_FMT "NBT/%s#%02X" /** @@ -38,13 +179,14 @@ * type number */ -static char *namecache_key(const char *name, - int name_type) +static char *namecache_key(TALLOC_CTX *ctx, + const char *name, + int name_type) { - char *keystr = NULL; - asprintf_strupper_m(&keystr, NBTKEY_FMT, name, name_type); - - return keystr; + return talloc_asprintf_strupper_m(ctx, + NBTKEY_FMT, + name, + name_type); } /** @@ -63,23 +205,25 @@ bool namecache_store(const char *name, struct ip_service *ip_list) { time_t expiry; - char *key, *value_string; + char *key = NULL; + char *value_string = NULL; int i; - bool ret; + bool ret = false; + TALLOC_CTX *frame = talloc_stackframe(); if (name_type > 255) { - return False; /* Don't store non-real name types. */ + /* Don't store non-real name types. */ + goto out; } if ( DEBUGLEVEL >= 5 ) { - TALLOC_CTX *ctx = talloc_stackframe(); char *addr = NULL; - DEBUG(5, ("namecache_store: storing %d address%s for %s#%02x: ", - num_names, num_names == 1 ? "": "es", name, name_type)); + DBG_INFO("storing %d address%s for %s#%02x: ", + num_names, num_names == 1 ? "": "es", name, name_type); for (i = 0; i < num_names; i++) { - addr = print_canonical_sockaddr(ctx, + addr = print_canonical_sockaddr(frame, &ip_list[i].ss); if (!addr) { continue; @@ -89,31 +233,31 @@ bool namecache_store(const char *name, } DEBUGADD(5, ("\n")); - TALLOC_FREE(ctx); } - key = namecache_key(name, name_type); + key = namecache_key(frame, name, name_type); if (!key) { - return False; + goto out; } expiry = time(NULL) + lp_name_cache_timeout(); /* * Generate string representation of ip addresses list - * First, store the number of ip addresses and then - * place each single ip */ - if (!ipstr_list_make(&value_string, ip_list, num_names)) { - SAFE_FREE(key); - SAFE_FREE(value_string); - return false; + value_string = ipstr_list_make(frame, ip_list, num_names); + if (value_string == NULL) { + goto out; } /* set the entry */ ret = gencache_set(key, value_string, expiry); - SAFE_FREE(key); - SAFE_FREE(value_string); + + out: + + TALLOC_FREE(key); + TALLOC_FREE(value_string); + TALLOC_FREE(frame); return ret; } @@ -140,11 +284,11 @@ bool namecache_fetch(const char *name, /* exit now if null pointers were passed as they're required further */ if (!ip_list || !num_names) { - return False; + return false; } if (name_type > 255) { - return False; /* Don't fetch non-real name types. */ + return false; /* Don't fetch non-real name types. */ } *num_names = 0; @@ -152,25 +296,25 @@ bool namecache_fetch(const char *name, /* * Use gencache interface - lookup the key */ - key = namecache_key(name, name_type); + key = namecache_key(talloc_tos(), name, name_type); if (!key) { - return False; + return false; } if (!gencache_get(key, talloc_tos(), &value, &timeout)) { - DEBUG(5, ("no entry for %s#%02X found.\n", name, name_type)); - SAFE_FREE(key); - return False; + DBG_INFO("no entry for %s#%02X found.\n", name, name_type); + TALLOC_FREE(key); + return false; } - DEBUG(5, ("name %s#%02X found.\n", name, name_type)); + DBG_INFO("name %s#%02X found.\n", name, name_type); /* * Split up the stored value into the list of IP adresses */ *num_names = ipstr_list_parse(value, ip_list); - SAFE_FREE(key); + TALLOC_FREE(key); TALLOC_FREE(value); return *num_names > 0; /* true only if some ip has been fetched */ @@ -187,15 +331,15 @@ bool namecache_delete(const char *name, int name_type) char *key; if (name_type > 255) { - return False; /* Don't fetch non-real name types. */ + return false; /* Don't fetch non-real name types. */ } - key = namecache_key(name, name_type); + key = namecache_key(talloc_tos(), name, name_type); if (!key) { - return False; + return false; -- Samba Shared Repository