s3-param Remove special case for global_scope()
[ira/wip.git] / source3 / lib / util_names.c
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
10
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.
15
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.
20
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/>.
23 */
24
25 #include "includes.h"
26
27 /***********************************************************************
28  Definitions for all names.
29 ***********************************************************************/
30
31 static char *smb_myname;
32 static char *smb_myworkgroup;
33 static int smb_num_netbios_names;
34 static char **smb_my_netbios_names;
35
36 static void free_netbios_names_array(void)
37 {
38         int i;
39
40         for (i = 0; i < smb_num_netbios_names; i++)
41                 SAFE_FREE(smb_my_netbios_names[i]);
42
43         SAFE_FREE(smb_my_netbios_names);
44         smb_num_netbios_names = 0;
45 }
46
47 static bool allocate_my_netbios_names_array(size_t number)
48 {
49         free_netbios_names_array();
50
51         smb_num_netbios_names = number + 1;
52         smb_my_netbios_names = SMB_MALLOC_ARRAY( char *, smb_num_netbios_names );
53
54         if (!smb_my_netbios_names)
55                 return False;
56
57         memset(smb_my_netbios_names, '\0', sizeof(char *) * smb_num_netbios_names);
58         return True;
59 }
60
61 static bool set_my_netbios_names(const char *name, int i)
62 {
63         SAFE_FREE(smb_my_netbios_names[i]);
64
65         smb_my_netbios_names[i] = SMB_STRDUP(name);
66         if (!smb_my_netbios_names[i])
67                 return False;
68         strupper_m(smb_my_netbios_names[i]);
69         return True;
70 }
71
72 /***********************************************************************
73  Free memory allocated to global objects
74 ***********************************************************************/
75
76 void gfree_names(void)
77 {
78         gfree_netbios_names();
79         free_netbios_names_array();
80         free_local_machine_name();
81 }
82
83 const char *my_netbios_names(int i)
84 {
85         return smb_my_netbios_names[i];
86 }
87
88 bool set_netbios_aliases(const char **str_array)
89 {
90         size_t namecount;
91
92         /* Work out the max number of netbios aliases that we have */
93         for( namecount=0; str_array && (str_array[namecount] != NULL); namecount++ )
94                 ;
95
96         if ( global_myname() && *global_myname())
97                 namecount++;
98
99         /* Allocate space for the netbios aliases */
100         if (!allocate_my_netbios_names_array(namecount))
101                 return False;
102
103         /* Use the global_myname string first */
104         namecount=0;
105         if ( global_myname() && *global_myname()) {
106                 set_my_netbios_names( global_myname(), namecount );
107                 namecount++;
108         }
109
110         if (str_array) {
111                 size_t i;
112                 for ( i = 0; str_array[i] != NULL; i++) {
113                         size_t n;
114                         bool duplicate = False;
115
116                         /* Look for duplicates */
117                         for( n=0; n<namecount; n++ ) {
118                                 if( strequal( str_array[i], my_netbios_names(n) ) ) {
119                                         duplicate = True;
120                                         break;
121                                 }
122                         }
123                         if (!duplicate) {
124                                 if (!set_my_netbios_names(str_array[i], namecount))
125                                         return False;
126                                 namecount++;
127                         }
128                 }
129         }
130         return True;
131 }
132
133 /****************************************************************************
134   Common name initialization code.
135 ****************************************************************************/
136
137 bool init_names(void)
138 {
139         int n;
140
141         if (global_myname() == NULL || *global_myname() == '\0') {
142                 if (!set_global_myname(myhostname())) {
143                         DEBUG( 0, ( "init_names: malloc fail.\n" ) );
144                         return False;
145                 }
146         }
147
148         if (!set_netbios_aliases(lp_netbios_aliases())) {
149                 DEBUG( 0, ( "init_names: malloc fail.\n" ) );
150                 return False;
151         }
152
153         set_local_machine_name(global_myname(),false);
154
155         DEBUG( 5, ("Netbios name list:-\n") );
156         for( n=0; my_netbios_names(n); n++ ) {
157                 DEBUGADD( 5, ("my_netbios_names[%d]=\"%s\"\n",
158                                         n, my_netbios_names(n) ) );
159         }
160
161         return( True );
162 }
163
164 /***********************************************************************
165  Allocate and set myname. Ensure upper case.
166 ***********************************************************************/
167
168 bool set_global_myname(const char *myname)
169 {
170         SAFE_FREE(smb_myname);
171         smb_myname = SMB_STRDUP(myname);
172         if (!smb_myname)
173                 return False;
174         strupper_m(smb_myname);
175         return True;
176 }
177
178 const char *global_myname(void)
179 {
180         return smb_myname;
181 }
182
183 /******************************************************************
184  get the default domain/netbios name to be used when dealing
185  with our passdb list of accounts
186 ******************************************************************/
187
188 const char *get_global_sam_name(void)
189 {
190         if (IS_DC) {
191                 return lp_workgroup();
192         }
193         return global_myname();
194 }
195
196 void gfree_netbios_names(void)
197 {
198         SAFE_FREE( smb_myname );
199         SAFE_FREE( smb_myworkgroup );
200 }