python:tests: Store keys as bytes rather than as lists of ints
[samba.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 int smb_num_netbios_names;
32 static char **smb_my_netbios_names;
33
34 static void free_netbios_names_array(void)
35 {
36         int i;
37
38         for (i = 0; i < smb_num_netbios_names; i++)
39                 SAFE_FREE(smb_my_netbios_names[i]);
40
41         SAFE_FREE(smb_my_netbios_names);
42         smb_num_netbios_names = 0;
43 }
44
45 static bool allocate_my_netbios_names_array(size_t number)
46 {
47         free_netbios_names_array();
48
49         smb_num_netbios_names = number + 1;
50         smb_my_netbios_names = SMB_MALLOC_ARRAY( char *, smb_num_netbios_names );
51
52         if (!smb_my_netbios_names)
53                 return False;
54
55         memset(smb_my_netbios_names, '\0', sizeof(char *) * smb_num_netbios_names);
56         return True;
57 }
58
59 static bool set_my_netbios_names(const char *name, int i)
60 {
61         SAFE_FREE(smb_my_netbios_names[i]);
62
63         /*
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.
70          */
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]);
75 }
76
77 /***********************************************************************
78  Free memory allocated to global objects
79 ***********************************************************************/
80
81 void gfree_names(void)
82 {
83         free_netbios_names_array();
84         free_local_machine_name();
85 }
86
87 const char *my_netbios_names(int i)
88 {
89         return smb_my_netbios_names[i];
90 }
91
92 bool set_netbios_aliases(const char **str_array)
93 {
94         size_t namecount;
95
96         /* Work out the max number of netbios aliases that we have */
97         for( namecount=0; str_array && (str_array[namecount] != NULL); namecount++ )
98                 ;
99
100         if ( lp_netbios_name() && *lp_netbios_name())
101                 namecount++;
102
103         /* Allocate space for the netbios aliases */
104         if (!allocate_my_netbios_names_array(namecount))
105                 return False;
106
107         /* Use the global_myname string first */
108         namecount=0;
109         if ( lp_netbios_name() && *lp_netbios_name()) {
110                 set_my_netbios_names( lp_netbios_name(), namecount );
111                 namecount++;
112         }
113
114         if (str_array) {
115                 size_t i;
116                 for ( i = 0; str_array[i] != NULL; i++) {
117                         size_t n;
118                         bool duplicate = False;
119
120                         /* Look for duplicates */
121                         for( n=0; n<namecount; n++ ) {
122                                 if( strequal( str_array[i], my_netbios_names(n) ) ) {
123                                         duplicate = True;
124                                         break;
125                                 }
126                         }
127                         if (!duplicate) {
128                                 if (!set_my_netbios_names(str_array[i], namecount))
129                                         return False;
130                                 namecount++;
131                         }
132                 }
133         }
134         return True;
135 }
136
137 /****************************************************************************
138   Common name initialization code.
139 ****************************************************************************/
140
141 bool init_names(void)
142 {
143         int n;
144
145         if (!set_netbios_aliases(lp_netbios_aliases())) {
146                 DEBUG( 0, ( "init_names: malloc fail.\n" ) );
147                 return False;
148         }
149
150         set_local_machine_name(lp_netbios_name(),false);
151
152         DEBUG( 5, ("Netbios name list:-\n") );
153         for( n=0; my_netbios_names(n); n++ ) {
154                 DEBUGADD( 5, ("my_netbios_names[%d]=\"%s\"\n",
155                                         n, my_netbios_names(n) ) );
156         }
157
158         return( True );
159 }
160
161 /******************************************************************
162  get the default domain/netbios name to be used when dealing
163  with our passdb list of accounts
164 ******************************************************************/
165
166 const char *get_global_sam_name(void)
167 {
168         if (IS_DC) {
169                 return lp_workgroup();
170         }
171         return lp_netbios_name();
172 }
173
174
175 /******************************************************************
176  Get the default domain/netbios name to be used when
177  testing authentication.
178 ******************************************************************/
179
180 const char *my_sam_name(void)
181 {
182         if (lp_server_role() == ROLE_STANDALONE) {
183                 return lp_netbios_name();
184         }
185
186         return lp_workgroup();
187 }