CVE-2020-25722 pytest: Raise an error when adding a dynamic test that would overwrite...
[Samba.git] / source3 / lib / util_names.c
blob876035cbe29c1a7f5e6a3e51b2026832e14c7984
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2007
6 Copyright (C) Simo Sorce 2001
7 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
8 Copyright (C) James Peach 2006
9 Copyright (C) Andrew Bartlett 2010-2011
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
27 /***********************************************************************
28 Definitions for all names.
29 ***********************************************************************/
31 static int smb_num_netbios_names;
32 static char **smb_my_netbios_names;
34 static void free_netbios_names_array(void)
36 int i;
38 for (i = 0; i < smb_num_netbios_names; i++)
39 SAFE_FREE(smb_my_netbios_names[i]);
41 SAFE_FREE(smb_my_netbios_names);
42 smb_num_netbios_names = 0;
45 static bool allocate_my_netbios_names_array(size_t number)
47 free_netbios_names_array();
49 smb_num_netbios_names = number + 1;
50 smb_my_netbios_names = SMB_MALLOC_ARRAY( char *, smb_num_netbios_names );
52 if (!smb_my_netbios_names)
53 return False;
55 memset(smb_my_netbios_names, '\0', sizeof(char *) * smb_num_netbios_names);
56 return True;
59 static bool set_my_netbios_names(const char *name, int i)
61 SAFE_FREE(smb_my_netbios_names[i]);
64 * Don't include space for terminating '\0' in strndup,
65 * it is automatically added. This screws up if the name
66 * is greater than MAX_NETBIOSNAME_LEN-1 in the unix
67 * charset, but less than or equal to MAX_NETBIOSNAME_LEN-1
68 * in the DOS charset, but this is so old we have to live
69 * with that.
71 smb_my_netbios_names[i] = SMB_STRNDUP(name, MAX_NETBIOSNAME_LEN-1);
72 if (!smb_my_netbios_names[i])
73 return False;
74 return strupper_m(smb_my_netbios_names[i]);
77 /***********************************************************************
78 Free memory allocated to global objects
79 ***********************************************************************/
81 void gfree_names(void)
83 free_netbios_names_array();
86 const char *my_netbios_names(int i)
88 return smb_my_netbios_names[i];
91 bool set_netbios_aliases(const char **str_array)
93 size_t namecount;
95 /* Work out the max number of netbios aliases that we have */
96 for( namecount=0; str_array && (str_array[namecount] != NULL); namecount++ )
99 if ( lp_netbios_name() && *lp_netbios_name())
100 namecount++;
102 /* Allocate space for the netbios aliases */
103 if (!allocate_my_netbios_names_array(namecount))
104 return False;
106 /* Use the global_myname string first */
107 namecount=0;
108 if ( lp_netbios_name() && *lp_netbios_name()) {
109 set_my_netbios_names( lp_netbios_name(), namecount );
110 namecount++;
113 if (str_array) {
114 size_t i;
115 for ( i = 0; str_array[i] != NULL; i++) {
116 size_t n;
117 bool duplicate = False;
119 /* Look for duplicates */
120 for( n=0; n<namecount; n++ ) {
121 if( strequal( str_array[i], my_netbios_names(n) ) ) {
122 duplicate = True;
123 break;
126 if (!duplicate) {
127 if (!set_my_netbios_names(str_array[i], namecount))
128 return False;
129 namecount++;
133 return True;
136 /****************************************************************************
137 Common name initialization code.
138 ****************************************************************************/
140 bool init_names(void)
142 int n;
144 if (!set_netbios_aliases(lp_netbios_aliases())) {
145 DEBUG( 0, ( "init_names: malloc fail.\n" ) );
146 return False;
149 DEBUG( 5, ("Netbios name list:-\n") );
150 for( n=0; my_netbios_names(n); n++ ) {
151 DEBUGADD( 5, ("my_netbios_names[%d]=\"%s\"\n",
152 n, my_netbios_names(n) ) );
155 return( True );
158 /******************************************************************
159 get the default domain/netbios name to be used when dealing
160 with our passdb list of accounts
161 ******************************************************************/
163 const char *get_global_sam_name(void)
165 if (IS_DC) {
166 return lp_workgroup();
168 return lp_netbios_name();
172 /******************************************************************
173 Get the default domain/netbios name to be used when
174 testing authentication.
175 ******************************************************************/
177 const char *my_sam_name(void)
179 if (lp_server_role() == ROLE_STANDALONE) {
180 return lp_netbios_name();
183 return lp_workgroup();
186 bool is_allowed_domain(const char *domain_name)
188 const char **ignored_domains = NULL;
189 const char **dom = NULL;
191 ignored_domains = lp_parm_string_list(-1,
192 "winbind",
193 "ignore domains",
194 NULL);
196 for (dom = ignored_domains; dom != NULL && *dom != NULL; dom++) {
197 if (gen_fnmatch(*dom, domain_name) == 0) {
198 DBG_NOTICE("Ignoring domain '%s'\n", domain_name);
199 return false;
203 if (lp_allow_trusted_domains()) {
204 return true;
207 if (strequal(lp_workgroup(), domain_name)) {
208 return true;
211 if (is_myname(domain_name)) {
212 return true;
215 DBG_NOTICE("Not trusted domain '%s'\n", domain_name);
216 return false;