diff options
author | Volker Lendecke <vl@samba.org> | 2011-03-04 05:38:04 +0100 |
---|---|---|
committer | Volker Lendecke <vl@samba.org> | 2011-03-05 14:29:32 +0100 |
commit | afb6752fa7903a63c2a1cef704bb9da9bab4b251 (patch) | |
tree | 47f184c42bf3740d4c30f85ad9c2af8be9ae2945 /nsswitch | |
parent | 181b23ceed77de50c2b9d05a92a795cce1beabb9 (diff) | |
download | samba-afb6752fa7903a63c2a1cef704bb9da9bab4b251.tar.gz samba-afb6752fa7903a63c2a1cef704bb9da9bab4b251.tar.bz2 samba-afb6752fa7903a63c2a1cef704bb9da9bab4b251.zip |
libwbclient: Add wbcSidToStringBuf
Diffstat (limited to 'nsswitch')
-rw-r--r-- | nsswitch/libwbclient/wbc_sid.c | 50 | ||||
-rw-r--r-- | nsswitch/libwbclient/wbclient.h | 16 |
2 files changed, 50 insertions, 16 deletions
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 * |