LCOV - code coverage report
Current view: top level - source3/lib - util_wellknown.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 40 48 83.3 %
Date: 2021-09-23 10:06:22 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    Lookup routines for well-known SIDs
       4             :    Copyright (C) Andrew Tridgell 1992-1998
       5             :    Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
       6             :    Copyright (C) Jeremy Allison  1999
       7             :    Copyright (C) Volker Lendecke 2005
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "../libcli/security/security.h"
      25             : 
      26             : struct rid_name_map {
      27             :         uint32_t rid;
      28             :         const char *name;
      29             : };
      30             : 
      31             : struct sid_name_map_info
      32             : {
      33             :         const struct dom_sid *sid;
      34             :         const char *name;
      35             :         const struct rid_name_map *known_users;
      36             : };
      37             : 
      38             : static const struct rid_name_map everyone_users[] = {
      39             :         { 0, "Everyone" },
      40             :         { 0, NULL}};
      41             : 
      42             : static const struct rid_name_map local_authority_users[] = {
      43             :         { 0, "Local" },
      44             :         { 1, "Console Logon" },
      45             :         { 0, NULL}};
      46             : 
      47             : static const struct rid_name_map creator_owner_users[] = {
      48             :         { 0, "Creator Owner" },
      49             :         { 1, "Creator Group" },
      50             :         { 2, "Creator Owner Server" },
      51             :         { 3, "Creator Group Server" },
      52             :         { 4, "Owner Rights" },
      53             :         { 0, NULL}};
      54             : 
      55             : static const struct rid_name_map nt_authority_users[] = {
      56             :         {  1, "Dialup" },
      57             :         {  2, "Network"},
      58             :         {  3, "Batch"},
      59             :         {  4, "Interactive"},
      60             :         {  6, "Service"},
      61             :         {  7, "Anonymous Logon"},
      62             :         {  8, "Proxy"},
      63             :         {  9, "Enterprise Domain Controllers"},
      64             :         { 10, "Self"},
      65             :         { 11, "Authenticated Users"},
      66             :         { 12, "Restricted"},
      67             :         { 13, "Terminal Server User"},
      68             :         { 14, "Remote Interactive Logon"},
      69             :         { 15, "This Organization"},
      70             :         { 17, "IUSR"},
      71             :         { 18, "SYSTEM"},
      72             :         { 19, "Local Service"},
      73             :         { 20, "Network Service"},
      74             :         {  0,  NULL}};
      75             : 
      76             : static struct sid_name_map_info special_domains[] = {
      77             :         { &global_sid_World_Domain, "", everyone_users },
      78             :         { &global_sid_Local_Authority, "", local_authority_users },
      79             :         { &global_sid_Creator_Owner_Domain, "", creator_owner_users },
      80             :         { &global_sid_NT_Authority, "NT Authority", nt_authority_users },
      81             :         { NULL, NULL, NULL }};
      82             : 
      83       84648 : bool sid_check_is_wellknown_domain(const struct dom_sid *sid, const char **name)
      84             : {
      85             :         int i;
      86             : 
      87      342840 :         for (i=0; special_domains[i].sid != NULL; i++) {
      88      307806 :                 if (dom_sid_equal(sid, special_domains[i].sid)) {
      89       49614 :                         if (name != NULL) {
      90           0 :                                 *name = special_domains[i].name;
      91             :                         }
      92       49587 :                         return True;
      93             :                 }
      94             :         }
      95       34752 :         return False;
      96             : }
      97             : 
      98       57713 : bool sid_check_is_in_wellknown_domain(const struct dom_sid *sid)
      99             : {
     100             :         struct dom_sid dom_sid;
     101             : 
     102       57713 :         sid_copy(&dom_sid, sid);
     103       57713 :         sid_split_rid(&dom_sid, NULL);
     104             : 
     105       57713 :         return sid_check_is_wellknown_domain(&dom_sid, NULL);
     106             : }
     107             : 
     108             : /**************************************************************************
     109             :  Looks up a known username from one of the known domains.
     110             : ***************************************************************************/
     111             : 
     112        3181 : bool lookup_wellknown_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
     113             :                           const char **domain, const char **name)
     114             : {
     115             :         int i;
     116             :         struct dom_sid dom_sid;
     117             :         uint32_t rid;
     118        3181 :         const struct rid_name_map *users = NULL;
     119             :         struct dom_sid_buf buf;
     120             : 
     121        3181 :         sid_copy(&dom_sid, sid);
     122        3181 :         if (!sid_split_rid(&dom_sid, &rid)) {
     123           0 :                 DEBUG(2, ("Could not split rid from SID\n"));
     124           0 :                 return False;
     125             :         }
     126             : 
     127        7014 :         for (i=0; special_domains[i].sid != NULL; i++) {
     128        7014 :                 if (dom_sid_equal(&dom_sid, special_domains[i].sid)) {
     129        3181 :                         *domain = talloc_strdup(mem_ctx,
     130             :                                                 special_domains[i].name);
     131        3181 :                         users = special_domains[i].known_users;
     132        3181 :                         break;
     133             :                 }
     134             :         }
     135             : 
     136        3181 :         if (users == NULL) {
     137           0 :                 DEBUG(10, ("SID %s is no special sid\n",
     138             :                            dom_sid_str_buf(sid, &buf)));
     139           0 :                 return False;
     140             :         }
     141             : 
     142        4302 :         for (i=0; users[i].name != NULL; i++) {
     143        4302 :                 if (rid == users[i].rid) {
     144        3181 :                         *name = talloc_strdup(mem_ctx, users[i].name);
     145        3181 :                         return True;
     146             :                 }
     147             :         }
     148             : 
     149           0 :         DEBUG(10, ("RID of special SID %s not found\n",
     150             :                    dom_sid_str_buf(sid, &buf)));
     151             : 
     152           0 :         return False;
     153             : }
     154             : 
     155             : /**************************************************************************
     156             :  Try and map a name to one of the well known SIDs.
     157             : ***************************************************************************/
     158             : 
     159        2672 : bool lookup_wellknown_name(TALLOC_CTX *mem_ctx, const char *name,
     160             :                            struct dom_sid *sid, const char **pdomain)
     161             : {
     162             :         int i, j;
     163        2672 :         const char *domain = *pdomain;
     164             : 
     165        2672 :         DEBUG(10,("map_name_to_wellknown_sid: looking up %s\\%s\n", domain, name));
     166             : 
     167       13262 :         for (i=0; special_domains[i].sid != NULL; i++) {
     168       10616 :                 const struct rid_name_map *users =
     169             :                         special_domains[i].known_users;
     170             : 
     171       10616 :                 if (domain[0] != '\0') {
     172         396 :                         if (!strequal(domain, special_domains[i].name)) {
     173         394 :                                 continue;
     174             :                         }
     175             :                 }
     176             : 
     177       10222 :                 if (users == NULL)
     178           0 :                         continue;
     179             : 
     180       76526 :                 for (j=0; users[j].name != NULL; j++) {
     181       66330 :                         if ( strequal(users[j].name, name) ) {
     182          26 :                                 sid_compose(sid, special_domains[i].sid,
     183          26 :                                             users[j].rid);
     184          26 :                                 *pdomain = talloc_strdup(
     185             :                                         mem_ctx, special_domains[i].name);
     186          26 :                                 return True;
     187             :                         }
     188             :                 }
     189             :         }
     190             : 
     191        2646 :         return False;
     192             : }

Generated by: LCOV version 1.13