From afb6752fa7903a63c2a1cef704bb9da9bab4b251 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 4 Mar 2011 05:38:04 +0100 Subject: libwbclient: Add wbcSidToStringBuf --- nsswitch/libwbclient/wbc_sid.c | 50 ++++++++++++++++++++++++++++------------- nsswitch/libwbclient/wbclient.h | 16 ++++++++++++- 2 files changed, 50 insertions(+), 16 deletions(-) (limited to 'nsswitch/libwbclient') diff --git a/nsswitch/libwbclient/wbc_sid.c b/nsswitch/libwbclient/wbc_sid.c index 73bd416247..6ea9e0a668 100644 --- a/nsswitch/libwbclient/wbc_sid.c +++ b/nsswitch/libwbclient/wbc_sid.c @@ -27,23 +27,17 @@ #include "libwbclient.h" #include "../winbind_client.h" -/* Convert a binary SID to a character string */ -wbcErr wbcSidToString(const struct wbcDomainSid *sid, - char **sid_string) +/* Convert a sid to a string into a buffer. Return the string + * length. If buflen is too small, return the string length that would + * result if it was long enough. */ +int wbcSidToStringBuf(const struct wbcDomainSid *sid, char *buf, int buflen) { uint32_t id_auth; - int i, ofs, maxlen; - char *result; + int i, ofs; if (!sid) { - return WBC_ERR_INVALID_SID; - } - - maxlen = sid->num_auths * 11 + 25; - - result = (char *)wbcAllocateMemory(maxlen, 1, NULL); - if (result == NULL) { - return WBC_ERR_NO_MEMORY; + strlcpy(buf, "(NULL SID)", buflen); + return 10; /* strlen("(NULL SID)") */ } /* @@ -56,13 +50,39 @@ wbcErr wbcSidToString(const struct wbcDomainSid *sid, (sid->id_auth[3] << 16) + (sid->id_auth[2] << 24); - ofs = snprintf(result, maxlen, "S-%u-%lu", + ofs = snprintf(buf, buflen, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)id_auth); for (i = 0; i < sid->num_auths; i++) { - ofs += snprintf(result + ofs, maxlen - ofs, "-%lu", + ofs += snprintf(buf + ofs, MAX(buflen - ofs, 0), "-%lu", (unsigned long)sid->sub_auths[i]); } + return ofs; +} + +/* Convert a binary SID to a character string */ +wbcErr wbcSidToString(const struct wbcDomainSid *sid, + char **sid_string) +{ + char buf[WBC_SID_STRING_BUFLEN]; + char *result; + int len; + + if (!sid) { + return WBC_ERR_INVALID_SID; + } + + len = wbcSidToStringBuf(sid, buf, sizeof(buf)); + + if (len+1 > sizeof(buf)) { + return WBC_ERR_INVALID_SID; + } + + result = (char *)wbcAllocateMemory(len+1, 1, NULL); + if (result == NULL) { + return WBC_ERR_NO_MEMORY; + } + memcpy(result, buf, len+1); *sid_string = result; return WBC_ERR_SUCCESS; diff --git a/nsswitch/libwbclient/wbclient.h b/nsswitch/libwbclient/wbclient.h index bd5a51ccaa..0286e5b5ec 100644 --- a/nsswitch/libwbclient/wbclient.h +++ b/nsswitch/libwbclient/wbclient.h @@ -66,9 +66,10 @@ const char *wbcErrorString(wbcErr error); * 0.4: Added wbcSidTypeString() * 0.5: Added wbcChangeTrustCredentials() * 0.6: Made struct wbcInterfaceDetails char* members non-const + * 0.7: Added wbcSidToStringBuf() **/ #define WBCLIENT_MAJOR_VERSION 0 -#define WBCLIENT_MINOR_VERSION 6 +#define WBCLIENT_MINOR_VERSION 7 #define WBCLIENT_VENDOR_VERSION "Samba libwbclient" struct wbcLibraryDetails { uint16_t major_version; @@ -529,6 +530,19 @@ void wbcFreeMemory(void*); */ const char* wbcSidTypeString(enum wbcSidType type); +#define WBC_SID_STRING_BUFLEN (15*11+25) + +/* + * @brief Print a sid into a buffer + * + * @param sid Binary Security Identifier + * @param buf Target buffer + * @param buflen Target buffer length + * + * @return Resulting string length. + */ +int wbcSidToStringBuf(const struct wbcDomainSid *sid, char *buf, int buflen); + /** * @brief Convert a binary SID to a character string * -- cgit