libwbclient: Add wbcSidToStringBuf
authorVolker Lendecke <vl@samba.org>
Fri, 4 Mar 2011 04:38:04 +0000 (05:38 +0100)
committerVolker Lendecke <vl@samba.org>
Sat, 5 Mar 2011 13:29:32 +0000 (14:29 +0100)
nsswitch/libwbclient/wbc_sid.c
nsswitch/libwbclient/wbclient.h

index 73bd4162472c1f3cfbfb1d29122807f8f24201ca..6ea9e0a668d79ee87ae4f49d9313f5c94b842c4a 100644 (file)
 #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;
index bd5a51ccaa6a2ab561806b2511cc5d23a164589b..0286e5b5ec94773283d4ee8cb1dbe76391b8aae6 100644 (file)
@@ -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
  *