Replace sid_string_static by sid_string_dbg in DEBUGs
[samba.git] / source3 / lib / util_sid.c
index b6952fca81c45e38c693d35765a5d1a760976b43..e27c72dbc4cb88234fdd10631dcbbc3701ecdc45 100644 (file)
@@ -10,7 +10,7 @@
       
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 
 /*
- * Some useful sids
+ * Some useful sids, more well known sids can be found at
+ * http://support.microsoft.com/kb/243330/EN-US/
  */
 
 
@@ -44,6 +44,11 @@ const DOM_SID global_sid_NULL =                      /* NULL sid */
 { 1, 1, {0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
 const DOM_SID global_sid_Authenticated_Users = /* All authenticated rids */
 { 1, 1, {0,0,0,0,0,5}, {11,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
+#if 0
+/* for documentation */
+const DOM_SID global_sid_Restriced =                   /* Restriced Code */
+{ 1, 1, {0,0,0,0,0,5}, {12,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
+#endif
 const DOM_SID global_sid_Network =                     /* Network rids */
 { 1, 1, {0,0,0,0,0,5}, {2,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
 
@@ -208,11 +213,31 @@ const char *sid_string_static(const DOM_SID *sid)
        return sid_str;
 }
 
+char *sid_string_talloc(TALLOC_CTX *mem_ctx, const DOM_SID *sid)
+{
+       fstring sid_str;
+       char *result;
+       sid_to_string(sid_str, sid);
+       result = talloc_strdup(mem_ctx, sid_str);
+       SMB_ASSERT(result != NULL);
+       return result;
+}
+
+char *sid_string_dbg(const DOM_SID *sid)
+{
+       return sid_string_talloc(debug_ctx(), sid);
+}
+
+char *sid_string_tos(const DOM_SID *sid)
+{
+       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;
@@ -283,7 +308,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;
@@ -292,7 +317,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);
@@ -302,7 +327,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--;
@@ -316,7 +341,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;           
@@ -333,7 +358,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;
@@ -373,7 +398,7 @@ 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;
 
@@ -393,7 +418,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)
@@ -484,7 +509,7 @@ 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;
 }
@@ -505,7 +530,7 @@ size_t sid_size(const DOM_SID *sid)
  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;
@@ -580,24 +605,20 @@ DOM_SID *sid_dup_talloc(TALLOC_CTX *ctx, const DOM_SID *src)
  Add SID to an array SIDs
 ********************************************************************/
 
-void 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)
 {
-       if (mem_ctx != NULL) {
-               *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
+       *sids = TALLOC_REALLOC_ARRAY(mem_ctx, *sids, DOM_SID,
                                             (*num)+1);
-       } else {
-               *sids = SMB_REALLOC_ARRAY(*sids, DOM_SID, (*num)+1);
-       }
-
        if (*sids == NULL) {
-               return;
+               *num = 0;
+               return False;
        }
 
        sid_copy(&((*sids)[*num]), sid);
        *num += 1;
 
-       return;
+       return True;
 }
 
 
@@ -605,17 +626,17 @@ void 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
 ********************************************************************/
 
-void 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;
 
        for (i=0; i<(*num_sids); i++) {
                if (sid_compare(sid, &(*sids)[i]) == 0)
-                       return;
+                       return True;
        }
 
-       add_sid_to_array(mem_ctx, sid, sids, num_sids);
+       return add_sid_to_array(mem_ctx, sid, sids, num_sids);
 }
 
 /********************************************************************
@@ -647,27 +668,95 @@ void del_sid_from_array(const DOM_SID *sid, DOM_SID **sids, size_t *num)
        return;
 }
 
-void 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;
 
        for (i=0; i<*p_num; i++) {
                if ((*pp_rids)[i] == rid)
-                       return;
+                       return True;
        }
        
        *pp_rids = TALLOC_REALLOC_ARRAY(mem_ctx, *pp_rids, uint32, *p_num+1);
 
-       if (*pp_rids == NULL)
-               return;
+       if (*pp_rids == NULL) {
+               *p_num = 0;
+               return False;
+       }
 
        (*pp_rids)[*p_num] = rid;
        *p_num += 1;
+       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);
 }
+
+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)
+{
+       DOM_SID sid;
+       DOM_SID *sid_array = NULL;
+       size_t num_sids = 0;
+       int i;
+
+       if (include_user_group_rid) {
+
+               if (!sid_compose(&sid, &(info3->dom_sid.sid),
+                                info3->user_rid)
+                   || !add_sid_to_array(mem_ctx, &sid,
+                                        &sid_array, &num_sids)) {
+                       DEBUG(3,("could not add user SID from rid 0x%x\n",
+                                info3->user_rid));                     
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               if (!sid_compose(&sid, &(info3->dom_sid.sid),
+                                info3->group_rid)
+                   || !add_sid_to_array(mem_ctx, &sid, 
+                                        &sid_array, &num_sids)) {
+                       DEBUG(3,("could not append additional group rid 0x%x\n",
+                                info3->group_rid));                    
+                       
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+       }
+
+       for (i = 0; i < info3->num_groups2; i++) {
+               if (!sid_compose(&sid, &(info3->dom_sid.sid),
+                                info3->gids[i].g_rid)
+                   || !add_sid_to_array(mem_ctx, &sid,
+                                        &sid_array, &num_sids)) {
+                       DEBUG(3,("could not append additional group rid 0x%x\n",
+                                info3->gids[i].g_rid));        
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+       }
+
+       /* Copy 'other' sids.  We need to do sid filtering here to
+          prevent possible elevation of privileges.  See:
+
+           http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
+         */
+
+       for (i = 0; i < info3->num_other_sids; i++) {
+               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_dbg(&info3->other_sids[i].sid)));
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       *user_sids = sid_array;
+       *num_user_sids = num_sids;
+
+       return NT_STATUS_OK;
+}