Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
[jra/samba/.git] / source / lib / util_sid.c
index 498919876cef51cef6a7a1e351f23274b6f694e0..222b32ed3a141f92de878e7695da8e97a45c2978 100644 (file)
@@ -172,59 +172,52 @@ const char *get_global_sam_name(void)
  Convert a SID to an ascii string.
 *****************************************************************/
 
-char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
+char *sid_to_fstring(fstring sidstr_out, const DOM_SID *sid)
 {
-       char subauth[16];
-       int i;
-       uint32 ia;
-  
-       if (!sid) {
-               fstrcpy(sidstr_out, "(NULL SID)");
-               return sidstr_out;
-       }
-
-       /*
-        * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 
-        * in a range of 2^48.
-        */
-       ia = (sid->id_auth[5]) +
-               (sid->id_auth[4] << 8 ) +
-               (sid->id_auth[3] << 16) +
-               (sid->id_auth[2] << 24);
+       char *str = sid_string_talloc(talloc_tos(), sid);
+       fstrcpy(sidstr_out, str);
+       TALLOC_FREE(str);
+       return sidstr_out;
+}
 
-       slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
+/*****************************************************************
+ Essentially a renamed dom_sid_string from librpc/ndr with a
+ panic if it didn't work
 
-       for (i = 0; i < sid->num_auths; i++) {
-               slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
-               fstrcat(sidstr_out, subauth);
-       }
+ This introduces a dependency on librpc/ndr/sid.o which can easily
+ be turned around if necessary
+*****************************************************************/
 
-       return sidstr_out;
+char *sid_string_talloc(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
+{
+       char *result = dom_sid_string(mem_ctx, sid);
+       SMB_ASSERT(result != NULL);
+       return result;
 }
 
 /*****************************************************************
  Useful function for debug lines.
-*****************************************************************/  
+*****************************************************************/
 
-const char *sid_string_static(const DOM_SID *sid)
+char *sid_string_dbg(const DOM_SID *sid)
 {
-       static fstring sid_str;
-       sid_to_string(sid_str, sid);
-       return sid_str;
+       return sid_string_talloc(debug_ctx(), sid);
 }
 
+/*****************************************************************
+ Use with care!
+*****************************************************************/
+
 char *sid_string_tos(const DOM_SID *sid)
 {
-       fstring sid_str;
-       sid_to_string(sid_str, sid);
-       return talloc_strdup(talloc_tos(), sid_str);
+       return sid_string_talloc(talloc_tos(), sid);
 }
 
 /*****************************************************************
  Convert a string to a SID. Returns True on success, False on fail.
 *****************************************************************/  
    
-BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
+bool string_to_sid(DOM_SID *sidout, const char *sidstr)
 {
        const char *p;
        char *q;
@@ -295,7 +288,7 @@ DOM_SID *string_sid_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
  Add a rid to the end of a sid
 *****************************************************************/  
 
-BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
+bool sid_append_rid(DOM_SID *sid, uint32 rid)
 {
        if (sid->num_auths < MAXSUBAUTHS) {
                sid->sub_auths[sid->num_auths++] = rid;
@@ -304,7 +297,7 @@ BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
        return False;
 }
 
-BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
+bool sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
 {
        sid_copy(dst, domain_sid);
        return sid_append_rid(dst, rid);
@@ -314,7 +307,7 @@ BOOL sid_compose(DOM_SID *dst, const DOM_SID *domain_sid, uint32 rid)
  Removes the last rid from the end of a sid
 *****************************************************************/  
 
-BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
+bool sid_split_rid(DOM_SID *sid, uint32 *rid)
 {
        if (sid->num_auths > 0) {
                sid->num_auths--;
@@ -328,7 +321,7 @@ BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
  Return the last rid from the end of a sid
 *****************************************************************/  
 
-BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
+bool sid_peek_rid(const DOM_SID *sid, uint32 *rid)
 {
        if (!sid || !rid)
                return False;           
@@ -345,7 +338,7 @@ BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
  and check the sid against the exp_dom_sid  
 *****************************************************************/  
 
-BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
+bool sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
 {
        if (!exp_dom_sid || !sid || !rid)
                return False;
@@ -385,11 +378,11 @@ void sid_copy(DOM_SID *dst, const DOM_SID *src)
  Write a sid out into on-the-wire format.
 *****************************************************************/  
 
-BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
+bool sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
 {
        size_t i;
 
-       if (len < sid_size(sid))
+       if (len < ndr_size_dom_sid(sid, 0))
                return False;
 
        SCVAL(outbuf,0,sid->sid_rev_num);
@@ -405,7 +398,7 @@ BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
  Parse a on-the-wire SID to a DOM_SID.
 *****************************************************************/  
 
-BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
+bool sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
 {
        int i;
        if (len < 8)
@@ -496,28 +489,16 @@ int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
  Compare two sids.
 *****************************************************************/  
 
-BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
+bool sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
 {
        return sid_compare(sid1, sid2) == 0;
 }
 
-/*****************************************************************
- Calculates size of a sid.
-*****************************************************************/  
-
-size_t sid_size(const DOM_SID *sid)
-{
-       if (sid == NULL)
-               return 0;
-
-       return sid->num_auths * sizeof(uint32) + 8;
-}
-
 /*****************************************************************
  Returns true if SID is internal (and non-mappable).
 *****************************************************************/
 
-BOOL non_mappable_sid(DOM_SID *sid)
+bool non_mappable_sid(DOM_SID *sid)
 {
        DOM_SID dom;
        uint32 rid;
@@ -542,7 +523,7 @@ BOOL non_mappable_sid(DOM_SID *sid)
 char *sid_binstring(const DOM_SID *sid)
 {
        char *buf, *s;
-       int len = sid_size(sid);
+       int len = ndr_size_dom_sid(sid, 0);
        buf = (char *)SMB_MALLOC(len);
        if (!buf)
                return NULL;
@@ -560,7 +541,7 @@ char *sid_binstring(const DOM_SID *sid)
 char *sid_binstring_hex(const DOM_SID *sid)
 {
        char *buf, *s;
-       int len = sid_size(sid);
+       int len = ndr_size_dom_sid(sid, 0);
        buf = (char *)SMB_MALLOC(len);
        if (!buf)
                return NULL;
@@ -592,7 +573,7 @@ DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
  Add SID to an array SIDs
 ********************************************************************/
 
-BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid, 
+bool add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid, 
                      DOM_SID **sids, size_t *num)
 {
        *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
@@ -613,7 +594,7 @@ BOOL add_sid_to_array(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
  Add SID to an array SIDs ensuring that it is not already there
 ********************************************************************/
 
-BOOL add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
+bool add_sid_to_array_unique(TALLOC_CTX *mem_ctx, const DOM_SID *sid,
                             DOM_SID **sids, size_t *num_sids)
 {
        size_t i;
@@ -655,7 +636,7 @@ void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
        return;
 }
 
-BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
+bool add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
                                    uint32 rid, uint32 **pp_rids, size_t *p_num)
 {
        size_t i;
@@ -677,7 +658,7 @@ BOOL add_rid_to_array_unique(TALLOC_CTX *mem_ctx,
        return True;
 }
 
-BOOL is_null_sid(const DOM_SID *sid)
+bool is_null_sid(const DOM_SID *sid)
 {
        static const DOM_SID null_sid = {0};
        return sid_equal(sid, &null_sid);
@@ -687,7 +668,7 @@ NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
                              const NET_USER_INFO_3 *info3,
                              DOM_SID **user_sids,
                              size_t *num_user_sids,
-                             BOOL include_user_group_rid)
+                             bool include_user_group_rid)
 {
        DOM_SID sid;
        DOM_SID *sid_array = NULL;
@@ -737,7 +718,7 @@ NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
                if (!add_sid_to_array(mem_ctx, &info3->other_sids[i].sid,
                                      &sid_array, &num_sids)) {
                        DEBUG(3, ("could not add SID to array: %s\n",
-                                 sid_string_static(&info3->other_sids[i].sid)));
+                                 sid_string_dbg(&info3->other_sids[i].sid)));
                        return NT_STATUS_NO_MEMORY;
                }
        }