1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | string substitution functions
|
---|
4 | Copyright (C) Andrew Tridgell 1992-2000
|
---|
5 | Copyright (C) Gerald Carter 2006
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "system/passwd.h"
|
---|
24 | #include "secrets.h"
|
---|
25 | #include "auth.h"
|
---|
26 |
|
---|
27 | userdom_struct current_user_info;
|
---|
28 | fstring remote_proto="UNKNOWN";
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Set the 'local' machine name
|
---|
32 | * @param local_name the name we are being called
|
---|
33 | * @param if this is the 'final' name for us, not be be changed again
|
---|
34 | */
|
---|
35 |
|
---|
36 | static char *local_machine;
|
---|
37 |
|
---|
38 | void free_local_machine_name(void)
|
---|
39 | {
|
---|
40 | TALLOC_FREE(local_machine);
|
---|
41 | }
|
---|
42 |
|
---|
43 | bool set_local_machine_name(const char *local_name, bool perm)
|
---|
44 | {
|
---|
45 | static bool already_perm = false;
|
---|
46 | char *tmp_local_machine = NULL;
|
---|
47 | size_t len;
|
---|
48 |
|
---|
49 | if (already_perm) {
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 |
|
---|
53 | tmp_local_machine = talloc_strdup(NULL, local_name);
|
---|
54 | if (!tmp_local_machine) {
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | trim_char(tmp_local_machine,' ',' ');
|
---|
58 |
|
---|
59 | TALLOC_FREE(local_machine);
|
---|
60 | len = strlen(tmp_local_machine);
|
---|
61 | local_machine = (char *)TALLOC_ZERO(NULL, len+1);
|
---|
62 | if (!local_machine) {
|
---|
63 | TALLOC_FREE(tmp_local_machine);
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
67 | alpha_strcpy(local_machine,tmp_local_machine,
|
---|
68 | SAFE_NETBIOS_CHARS,len+1);
|
---|
69 | if (!strlower_m(local_machine)) {
|
---|
70 | TALLOC_FREE(tmp_local_machine);
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | TALLOC_FREE(tmp_local_machine);
|
---|
74 |
|
---|
75 | already_perm = perm;
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | const char *get_local_machine_name(void)
|
---|
81 | {
|
---|
82 | if (!local_machine || !*local_machine) {
|
---|
83 | return lp_netbios_name();
|
---|
84 | }
|
---|
85 |
|
---|
86 | return local_machine;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Set the 'remote' machine name
|
---|
91 | * @param remote_name the name our client wants to be called by
|
---|
92 | * @param if this is the 'final' name for them, not be be changed again
|
---|
93 | */
|
---|
94 |
|
---|
95 | static char *remote_machine;
|
---|
96 |
|
---|
97 | bool set_remote_machine_name(const char *remote_name, bool perm)
|
---|
98 | {
|
---|
99 | static bool already_perm = False;
|
---|
100 | char *tmp_remote_machine;
|
---|
101 | size_t len;
|
---|
102 |
|
---|
103 | if (already_perm) {
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 |
|
---|
107 | tmp_remote_machine = talloc_strdup(NULL, remote_name);
|
---|
108 | if (!tmp_remote_machine) {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | trim_char(tmp_remote_machine,' ',' ');
|
---|
112 |
|
---|
113 | TALLOC_FREE(remote_machine);
|
---|
114 | len = strlen(tmp_remote_machine);
|
---|
115 | remote_machine = (char *)TALLOC_ZERO(NULL, len+1);
|
---|
116 | if (!remote_machine) {
|
---|
117 | TALLOC_FREE(tmp_remote_machine);
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
122 | alpha_strcpy(remote_machine,tmp_remote_machine,
|
---|
123 | SAFE_NETBIOS_CHARS,len+1);
|
---|
124 | if (!strlower_m(remote_machine)) {
|
---|
125 | TALLOC_FREE(tmp_remote_machine);
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 | TALLOC_FREE(tmp_remote_machine);
|
---|
129 |
|
---|
130 | already_perm = perm;
|
---|
131 |
|
---|
132 | return true;
|
---|
133 | }
|
---|
134 |
|
---|
135 | const char *get_remote_machine_name(void)
|
---|
136 | {
|
---|
137 | return remote_machine ? remote_machine : "";
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*******************************************************************
|
---|
141 | Setup the string used by %U substitution.
|
---|
142 | ********************************************************************/
|
---|
143 |
|
---|
144 | static char *smb_user_name;
|
---|
145 |
|
---|
146 | void sub_set_smb_name(const char *name)
|
---|
147 | {
|
---|
148 | char *tmp;
|
---|
149 | size_t len;
|
---|
150 | bool is_machine_account = false;
|
---|
151 |
|
---|
152 | /* don't let anonymous logins override the name */
|
---|
153 | if (!name || !*name) {
|
---|
154 | return;
|
---|
155 | }
|
---|
156 |
|
---|
157 | tmp = talloc_strdup(NULL, name);
|
---|
158 | if (!tmp) {
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | trim_char(tmp, ' ', ' ');
|
---|
162 | if (!strlower_m(tmp)) {
|
---|
163 | TALLOC_FREE(tmp);
|
---|
164 | return;
|
---|
165 | }
|
---|
166 |
|
---|
167 | len = strlen(tmp);
|
---|
168 |
|
---|
169 | if (len == 0) {
|
---|
170 | TALLOC_FREE(tmp);
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* long story but here goes....we have to allow usernames
|
---|
175 | ending in '$' as they are valid machine account names.
|
---|
176 | So check for a machine account and re-add the '$'
|
---|
177 | at the end after the call to alpha_strcpy(). --jerry */
|
---|
178 |
|
---|
179 | if (tmp[len-1] == '$') {
|
---|
180 | is_machine_account = True;
|
---|
181 | }
|
---|
182 |
|
---|
183 | TALLOC_FREE(smb_user_name);
|
---|
184 | smb_user_name = (char *)TALLOC_ZERO(NULL, len+1);
|
---|
185 | if (!smb_user_name) {
|
---|
186 | TALLOC_FREE(tmp);
|
---|
187 | return;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* alpha_strcpy includes the space for the terminating nul. */
|
---|
191 | alpha_strcpy(smb_user_name, tmp,
|
---|
192 | SAFE_NETBIOS_CHARS,
|
---|
193 | len+1);
|
---|
194 |
|
---|
195 | TALLOC_FREE(tmp);
|
---|
196 |
|
---|
197 | if (is_machine_account) {
|
---|
198 | len = strlen(smb_user_name);
|
---|
199 | smb_user_name[len-1] = '$';
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | static char sub_peeraddr[INET6_ADDRSTRLEN];
|
---|
204 | static const char *sub_peername = NULL;
|
---|
205 | static char sub_sockaddr[INET6_ADDRSTRLEN];
|
---|
206 |
|
---|
207 | void sub_set_socket_ids(const char *peeraddr, const char *peername,
|
---|
208 | const char *sockaddr)
|
---|
209 | {
|
---|
210 | const char *addr = peeraddr;
|
---|
211 |
|
---|
212 | if (strnequal(addr, "::ffff:", 7)) {
|
---|
213 | addr += 7;
|
---|
214 | }
|
---|
215 | strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
|
---|
216 |
|
---|
217 | if (sub_peername != NULL &&
|
---|
218 | sub_peername != sub_peeraddr) {
|
---|
219 | talloc_free(discard_const_p(char,sub_peername));
|
---|
220 | sub_peername = NULL;
|
---|
221 | }
|
---|
222 | sub_peername = talloc_strdup(NULL, peername);
|
---|
223 | if (sub_peername == NULL) {
|
---|
224 | sub_peername = sub_peeraddr;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Shouldn't we do the ::ffff: cancellation here as well? The
|
---|
229 | * original code in talloc_sub_basic() did not do it, so I'm
|
---|
230 | * leaving it out here as well for compatibility.
|
---|
231 | */
|
---|
232 | strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
|
---|
233 | }
|
---|
234 |
|
---|
235 | static const char *get_smb_user_name(void)
|
---|
236 | {
|
---|
237 | return smb_user_name ? smb_user_name : "";
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*******************************************************************
|
---|
241 | Setup the strings used by substitutions. Called per packet. Ensure
|
---|
242 | %U name is set correctly also.
|
---|
243 |
|
---|
244 | smb_name must be sanitized by alpha_strcpy
|
---|
245 | ********************************************************************/
|
---|
246 |
|
---|
247 | void set_current_user_info(const char *smb_name, const char *unix_name,
|
---|
248 | const char *domain)
|
---|
249 | {
|
---|
250 | fstrcpy(current_user_info.smb_name, smb_name);
|
---|
251 | fstrcpy(current_user_info.unix_name, unix_name);
|
---|
252 | fstrcpy(current_user_info.domain, domain);
|
---|
253 |
|
---|
254 | /* The following is safe as current_user_info.smb_name
|
---|
255 | * has already been sanitised in register_existing_vuid. */
|
---|
256 |
|
---|
257 | sub_set_smb_name(current_user_info.smb_name);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*******************************************************************
|
---|
261 | Return the current active user name.
|
---|
262 | *******************************************************************/
|
---|
263 |
|
---|
264 | const char *get_current_username(void)
|
---|
265 | {
|
---|
266 | if (current_user_info.smb_name[0] == '\0' ) {
|
---|
267 | return get_smb_user_name();
|
---|
268 | }
|
---|
269 |
|
---|
270 | return current_user_info.smb_name;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*******************************************************************
|
---|
274 | Given a pointer to a %$(NAME) in p and the whole string in str
|
---|
275 | expand it as an environment variable.
|
---|
276 | str must be a talloced string.
|
---|
277 | Return a new allocated and expanded string.
|
---|
278 | Based on code by Branko Cibej <branko.cibej@hermes.si>
|
---|
279 | When this is called p points at the '%' character.
|
---|
280 | May substitute multiple occurrencies of the same env var.
|
---|
281 | ********************************************************************/
|
---|
282 |
|
---|
283 | static char *realloc_expand_env_var(char *str, char *p)
|
---|
284 | {
|
---|
285 | char *envname;
|
---|
286 | char *envval;
|
---|
287 | char *q, *r;
|
---|
288 | int copylen;
|
---|
289 |
|
---|
290 | if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
|
---|
291 | return str;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*
|
---|
295 | * Look for the terminating ')'.
|
---|
296 | */
|
---|
297 |
|
---|
298 | if ((q = strchr_m(p,')')) == NULL) {
|
---|
299 | DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
|
---|
300 | return str;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Extract the name from within the %$(NAME) string.
|
---|
305 | */
|
---|
306 |
|
---|
307 | r = p + 3;
|
---|
308 | copylen = q - r;
|
---|
309 |
|
---|
310 | /* reserve space for use later add %$() chars */
|
---|
311 | if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
|
---|
312 | return NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | strncpy(envname,r,copylen);
|
---|
316 | envname[copylen] = '\0';
|
---|
317 |
|
---|
318 | if ((envval = getenv(envname)) == NULL) {
|
---|
319 | DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
|
---|
320 | TALLOC_FREE(envname);
|
---|
321 | return str;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Copy the full %$(NAME) into envname so it
|
---|
326 | * can be replaced.
|
---|
327 | */
|
---|
328 |
|
---|
329 | copylen = q + 1 - p;
|
---|
330 | strncpy(envname,p,copylen);
|
---|
331 | envname[copylen] = '\0';
|
---|
332 | r = realloc_string_sub(str, envname, envval);
|
---|
333 | TALLOC_FREE(envname);
|
---|
334 |
|
---|
335 | return r;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /*******************************************************************
|
---|
339 | Patch from jkf@soton.ac.uk
|
---|
340 | Added this to implement %p (NIS auto-map version of %H)
|
---|
341 | *******************************************************************/
|
---|
342 |
|
---|
343 | static const char *automount_path(const char *user_name)
|
---|
344 | {
|
---|
345 | TALLOC_CTX *ctx = talloc_tos();
|
---|
346 | const char *server_path;
|
---|
347 |
|
---|
348 | /* use the passwd entry as the default */
|
---|
349 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
350 |
|
---|
351 | server_path = talloc_strdup(ctx, get_user_home_dir(ctx, user_name));
|
---|
352 | if (!server_path) {
|
---|
353 | return "";
|
---|
354 | }
|
---|
355 |
|
---|
356 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
357 |
|
---|
358 | if (lp_nis_homedir()) {
|
---|
359 | const char *home_path_start;
|
---|
360 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
361 |
|
---|
362 | if(automount_value && strlen(automount_value) > 0) {
|
---|
363 | home_path_start = strchr_m(automount_value,':');
|
---|
364 | if (home_path_start != NULL) {
|
---|
365 | DEBUG(5, ("NIS lookup succeeded. "
|
---|
366 | "Home path is: %s\n",
|
---|
367 | home_path_start ?
|
---|
368 | (home_path_start+1):""));
|
---|
369 | server_path = talloc_strdup(ctx,
|
---|
370 | home_path_start+1);
|
---|
371 | if (!server_path) {
|
---|
372 | server_path = "";
|
---|
373 | }
|
---|
374 | }
|
---|
375 | } else {
|
---|
376 | /* NIS key lookup failed: default to
|
---|
377 | * user home directory from password file */
|
---|
378 | DEBUG(5, ("NIS lookup failed. Using Home path from "
|
---|
379 | "passwd file. Home path is: %s\n", server_path ));
|
---|
380 | }
|
---|
381 | }
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | DEBUG(4,("Home server path: %s\n", server_path));
|
---|
385 | return server_path;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /*******************************************************************
|
---|
389 | Patch from jkf@soton.ac.uk
|
---|
390 | This is Luke's original function with the NIS lookup code
|
---|
391 | moved out to a separate function.
|
---|
392 | *******************************************************************/
|
---|
393 |
|
---|
394 | static const char *automount_server(const char *user_name)
|
---|
395 | {
|
---|
396 | TALLOC_CTX *ctx = talloc_tos();
|
---|
397 | const char *server_name;
|
---|
398 | const char *local_machine_name = get_local_machine_name();
|
---|
399 |
|
---|
400 | /* use the local machine name as the default */
|
---|
401 | /* this will be the default if WITH_AUTOMOUNT is not used or fails */
|
---|
402 | if (local_machine_name && *local_machine_name) {
|
---|
403 | server_name = talloc_strdup(ctx, local_machine_name);
|
---|
404 | } else {
|
---|
405 | server_name = talloc_strdup(ctx, lp_netbios_name());
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (!server_name) {
|
---|
409 | return "";
|
---|
410 | }
|
---|
411 |
|
---|
412 | #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
|
---|
413 | if (lp_nis_homedir()) {
|
---|
414 | char *p;
|
---|
415 | char *srv;
|
---|
416 | char *automount_value = automount_lookup(ctx, user_name);
|
---|
417 | if (!automount_value) {
|
---|
418 | return "";
|
---|
419 | }
|
---|
420 | srv = talloc_strdup(ctx, automount_value);
|
---|
421 | if (!srv) {
|
---|
422 | return "";
|
---|
423 | }
|
---|
424 | p = strchr_m(srv, ':');
|
---|
425 | if (!p) {
|
---|
426 | return "";
|
---|
427 | }
|
---|
428 | *p = '\0';
|
---|
429 | server_name = srv;
|
---|
430 | DEBUG(5, ("NIS lookup succeeded. Home server %s\n",
|
---|
431 | server_name));
|
---|
432 | }
|
---|
433 | #endif
|
---|
434 |
|
---|
435 | DEBUG(4,("Home server: %s\n", server_name));
|
---|
436 | return server_name;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /****************************************************************************
|
---|
440 | Do some standard substitutions in a string.
|
---|
441 | len is the length in bytes of the space allowed in string str. If zero means
|
---|
442 | don't allow expansions.
|
---|
443 | ****************************************************************************/
|
---|
444 |
|
---|
445 | void standard_sub_basic(const char *smb_name, const char *domain_name,
|
---|
446 | char *str, size_t len)
|
---|
447 | {
|
---|
448 | char *s;
|
---|
449 |
|
---|
450 | if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
|
---|
451 | strncpy( str, s, len );
|
---|
452 | }
|
---|
453 |
|
---|
454 | TALLOC_FREE( s );
|
---|
455 | }
|
---|
456 |
|
---|
457 | /****************************************************************************
|
---|
458 | Do some standard substitutions in a string.
|
---|
459 | This function will return an talloced string that has to be freed.
|
---|
460 | ****************************************************************************/
|
---|
461 |
|
---|
462 | char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
|
---|
463 | const char *smb_name,
|
---|
464 | const char *domain_name,
|
---|
465 | const char *str)
|
---|
466 | {
|
---|
467 | char *b, *p, *s, *r, *a_string;
|
---|
468 | fstring pidstr, vnnstr;
|
---|
469 | const char *local_machine_name = get_local_machine_name();
|
---|
470 | TALLOC_CTX *tmp_ctx = NULL;
|
---|
471 |
|
---|
472 | /* workaround to prevent a crash while looking at bug #687 */
|
---|
473 |
|
---|
474 | if (!str) {
|
---|
475 | DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
|
---|
476 | return NULL;
|
---|
477 | }
|
---|
478 |
|
---|
479 | a_string = talloc_strdup(mem_ctx, str);
|
---|
480 | if (a_string == NULL) {
|
---|
481 | DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
|
---|
482 | return NULL;
|
---|
483 | }
|
---|
484 |
|
---|
485 | tmp_ctx = talloc_stackframe();
|
---|
486 |
|
---|
487 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
488 |
|
---|
489 | r = NULL;
|
---|
490 | b = a_string;
|
---|
491 |
|
---|
492 | switch (*(p+1)) {
|
---|
493 | case 'U' :
|
---|
494 | r = strlower_talloc(tmp_ctx, smb_name);
|
---|
495 | if (r == NULL) {
|
---|
496 | goto error;
|
---|
497 | }
|
---|
498 | a_string = realloc_string_sub(a_string, "%U", r);
|
---|
499 | break;
|
---|
500 | case 'G' : {
|
---|
501 | struct passwd *pass;
|
---|
502 | bool is_domain_name = false;
|
---|
503 | const char *sep = lp_winbind_separator();
|
---|
504 |
|
---|
505 | if (domain_name != NULL && domain_name[0] != '\0' &&
|
---|
506 | (lp_security() == SEC_ADS ||
|
---|
507 | lp_security() == SEC_DOMAIN)) {
|
---|
508 | r = talloc_asprintf(tmp_ctx,
|
---|
509 | "%s%c%s",
|
---|
510 | domain_name,
|
---|
511 | *sep,
|
---|
512 | smb_name);
|
---|
513 | is_domain_name = true;
|
---|
514 | } else {
|
---|
515 | r = talloc_strdup(tmp_ctx, smb_name);
|
---|
516 | }
|
---|
517 | if (r == NULL) {
|
---|
518 | goto error;
|
---|
519 | }
|
---|
520 |
|
---|
521 | pass = Get_Pwnam_alloc(tmp_ctx, r);
|
---|
522 | if (pass != NULL) {
|
---|
523 | char *group_name;
|
---|
524 |
|
---|
525 | group_name = gidtoname(pass->pw_gid);
|
---|
526 | if (is_domain_name) {
|
---|
527 | p = strchr_m(group_name, *sep);
|
---|
528 | if (p != NULL) {
|
---|
529 | group_name = p + 1;
|
---|
530 | }
|
---|
531 | }
|
---|
532 | a_string = realloc_string_sub(a_string,
|
---|
533 | "%G",
|
---|
534 | group_name);
|
---|
535 | }
|
---|
536 | TALLOC_FREE(pass);
|
---|
537 | break;
|
---|
538 | }
|
---|
539 | case 'D' :
|
---|
540 | r = strupper_talloc(tmp_ctx, domain_name);
|
---|
541 | if (r == NULL) {
|
---|
542 | goto error;
|
---|
543 | }
|
---|
544 | a_string = realloc_string_sub(a_string, "%D", r);
|
---|
545 | break;
|
---|
546 | case 'I' : {
|
---|
547 | a_string = realloc_string_sub(
|
---|
548 | a_string, "%I",
|
---|
549 | sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
|
---|
550 | break;
|
---|
551 | }
|
---|
552 | case 'i':
|
---|
553 | a_string = realloc_string_sub(
|
---|
554 | a_string, "%i",
|
---|
555 | sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
|
---|
556 | break;
|
---|
557 | case 'L' :
|
---|
558 | if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
|
---|
559 | break;
|
---|
560 | }
|
---|
561 | if (local_machine_name && *local_machine_name) {
|
---|
562 | a_string = realloc_string_sub(a_string, "%L", local_machine_name);
|
---|
563 | } else {
|
---|
564 | a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
|
---|
565 | }
|
---|
566 | break;
|
---|
567 | case 'N':
|
---|
568 | a_string = realloc_string_sub(a_string, "%N", automount_server(smb_name));
|
---|
569 | break;
|
---|
570 | case 'M' :
|
---|
571 | a_string = realloc_string_sub(a_string, "%M",
|
---|
572 | sub_peername ? sub_peername : "");
|
---|
573 | break;
|
---|
574 | case 'R' :
|
---|
575 | a_string = realloc_string_sub(a_string, "%R", remote_proto);
|
---|
576 | break;
|
---|
577 | case 'T' :
|
---|
578 | a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
|
---|
579 | break;
|
---|
580 | case 'a' :
|
---|
581 | a_string = realloc_string_sub(a_string, "%a",
|
---|
582 | get_remote_arch_str());
|
---|
583 | break;
|
---|
584 | case 'd' :
|
---|
585 | slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
|
---|
586 | a_string = realloc_string_sub(a_string, "%d", pidstr);
|
---|
587 | break;
|
---|
588 | case 'h' :
|
---|
589 | a_string = realloc_string_sub(a_string, "%h", myhostname());
|
---|
590 | break;
|
---|
591 | case 'm' :
|
---|
592 | a_string = realloc_string_sub(a_string, "%m",
|
---|
593 | remote_machine
|
---|
594 | ? remote_machine
|
---|
595 | : "");
|
---|
596 | break;
|
---|
597 | case 'v' :
|
---|
598 | a_string = realloc_string_sub(a_string, "%v", samba_version_string());
|
---|
599 | break;
|
---|
600 | case 'w' :
|
---|
601 | a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
|
---|
602 | break;
|
---|
603 | case '$' :
|
---|
604 | a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
|
---|
605 | break;
|
---|
606 | case 'V' :
|
---|
607 | slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
|
---|
608 | a_string = realloc_string_sub(a_string, "%V", vnnstr);
|
---|
609 | break;
|
---|
610 | default:
|
---|
611 | break;
|
---|
612 | }
|
---|
613 |
|
---|
614 | p++;
|
---|
615 | TALLOC_FREE(r);
|
---|
616 |
|
---|
617 | if (a_string == NULL) {
|
---|
618 | goto done;
|
---|
619 | }
|
---|
620 | }
|
---|
621 |
|
---|
622 | goto done;
|
---|
623 |
|
---|
624 | error:
|
---|
625 | TALLOC_FREE(a_string);
|
---|
626 |
|
---|
627 | done:
|
---|
628 | TALLOC_FREE(tmp_ctx);
|
---|
629 | return a_string;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /****************************************************************************
|
---|
633 | Do some specific substitutions in a string.
|
---|
634 | This function will return an allocated string that have to be freed.
|
---|
635 | ****************************************************************************/
|
---|
636 |
|
---|
637 | char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
|
---|
638 | const char *input_string,
|
---|
639 | const char *username,
|
---|
640 | const char *grpname,
|
---|
641 | const char *domain,
|
---|
642 | uid_t uid,
|
---|
643 | gid_t gid)
|
---|
644 | {
|
---|
645 | char *a_string;
|
---|
646 | char *ret_string = NULL;
|
---|
647 | char *b, *p, *s;
|
---|
648 | TALLOC_CTX *tmp_ctx;
|
---|
649 |
|
---|
650 | if (!(tmp_ctx = talloc_new(mem_ctx))) {
|
---|
651 | DEBUG(0, ("talloc_new failed\n"));
|
---|
652 | return NULL;
|
---|
653 | }
|
---|
654 |
|
---|
655 | a_string = talloc_strdup(tmp_ctx, input_string);
|
---|
656 | if (a_string == NULL) {
|
---|
657 | DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
|
---|
658 | goto done;
|
---|
659 | }
|
---|
660 |
|
---|
661 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
662 |
|
---|
663 | b = a_string;
|
---|
664 |
|
---|
665 | switch (*(p+1)) {
|
---|
666 | case 'U' :
|
---|
667 | a_string = talloc_string_sub(
|
---|
668 | tmp_ctx, a_string, "%U", username);
|
---|
669 | break;
|
---|
670 | case 'u' :
|
---|
671 | a_string = talloc_string_sub(
|
---|
672 | tmp_ctx, a_string, "%u", username);
|
---|
673 | break;
|
---|
674 | case 'G' :
|
---|
675 | if (gid != -1) {
|
---|
676 | const char *name;
|
---|
677 |
|
---|
678 | if (grpname != NULL) {
|
---|
679 | name = grpname;
|
---|
680 | } else {
|
---|
681 | name = gidtoname(gid);
|
---|
682 | }
|
---|
683 |
|
---|
684 | a_string = talloc_string_sub(tmp_ctx,
|
---|
685 | a_string,
|
---|
686 | "%G",
|
---|
687 | name);
|
---|
688 | } else {
|
---|
689 | a_string = talloc_string_sub(
|
---|
690 | tmp_ctx, a_string,
|
---|
691 | "%G", "NO_GROUP");
|
---|
692 | }
|
---|
693 | break;
|
---|
694 | case 'g' :
|
---|
695 | if (gid != -1) {
|
---|
696 | const char *name;
|
---|
697 |
|
---|
698 | if (grpname != NULL) {
|
---|
699 | name = grpname;
|
---|
700 | } else {
|
---|
701 | name = gidtoname(gid);
|
---|
702 | }
|
---|
703 |
|
---|
704 | a_string = talloc_string_sub(tmp_ctx,
|
---|
705 | a_string,
|
---|
706 | "%g",
|
---|
707 | name);
|
---|
708 | } else {
|
---|
709 | a_string = talloc_string_sub(
|
---|
710 | tmp_ctx, a_string, "%g", "NO_GROUP");
|
---|
711 | }
|
---|
712 | break;
|
---|
713 | case 'D' :
|
---|
714 | a_string = talloc_string_sub(tmp_ctx, a_string,
|
---|
715 | "%D", domain);
|
---|
716 | break;
|
---|
717 | case 'N' :
|
---|
718 | a_string = talloc_string_sub(
|
---|
719 | tmp_ctx, a_string, "%N",
|
---|
720 | automount_server(username));
|
---|
721 | break;
|
---|
722 | default:
|
---|
723 | break;
|
---|
724 | }
|
---|
725 |
|
---|
726 | p++;
|
---|
727 | if (a_string == NULL) {
|
---|
728 | goto done;
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
|
---|
733 | * away with the TALLOC_FREE(tmp_ctx) further down. */
|
---|
734 |
|
---|
735 | ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
|
---|
736 |
|
---|
737 | done:
|
---|
738 | TALLOC_FREE(tmp_ctx);
|
---|
739 | return ret_string;
|
---|
740 | }
|
---|
741 |
|
---|
742 | /****************************************************************************
|
---|
743 | ****************************************************************************/
|
---|
744 |
|
---|
745 | char *talloc_sub_advanced(TALLOC_CTX *ctx,
|
---|
746 | const char *servicename,
|
---|
747 | const char *user,
|
---|
748 | const char *connectpath,
|
---|
749 | gid_t gid,
|
---|
750 | const char *smb_name,
|
---|
751 | const char *domain_name,
|
---|
752 | const char *str)
|
---|
753 | {
|
---|
754 | char *a_string, *ret_string;
|
---|
755 | char *b, *p, *s;
|
---|
756 |
|
---|
757 | a_string = talloc_strdup(talloc_tos(), str);
|
---|
758 | if (a_string == NULL) {
|
---|
759 | DEBUG(0, ("talloc_sub_advanced: Out of memory!\n"));
|
---|
760 | return NULL;
|
---|
761 | }
|
---|
762 |
|
---|
763 | for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
|
---|
764 |
|
---|
765 | b = a_string;
|
---|
766 |
|
---|
767 | switch (*(p+1)) {
|
---|
768 | case 'N' :
|
---|
769 | a_string = realloc_string_sub(a_string, "%N", automount_server(user));
|
---|
770 | break;
|
---|
771 | case 'H': {
|
---|
772 | char *h;
|
---|
773 | if ((h = get_user_home_dir(talloc_tos(), user)))
|
---|
774 | a_string = realloc_string_sub(a_string, "%H", h);
|
---|
775 | TALLOC_FREE(h);
|
---|
776 | break;
|
---|
777 | }
|
---|
778 | case 'P':
|
---|
779 | a_string = realloc_string_sub(a_string, "%P", connectpath);
|
---|
780 | break;
|
---|
781 | case 'S':
|
---|
782 | a_string = realloc_string_sub(a_string, "%S", servicename);
|
---|
783 | break;
|
---|
784 | case 'g':
|
---|
785 | a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
|
---|
786 | break;
|
---|
787 | case 'u':
|
---|
788 | a_string = realloc_string_sub(a_string, "%u", user);
|
---|
789 | break;
|
---|
790 |
|
---|
791 | /* Patch from jkf@soton.ac.uk Left the %N (NIS
|
---|
792 | * server name) in standard_sub_basic as it is
|
---|
793 | * a feature for logon servers, hence uses the
|
---|
794 | * username. The %p (NIS server path) code is
|
---|
795 | * here as it is used instead of the default
|
---|
796 | * "path =" string in [homes] and so needs the
|
---|
797 | * service name, not the username. */
|
---|
798 | case 'p':
|
---|
799 | a_string = realloc_string_sub(a_string, "%p",
|
---|
800 | automount_path(servicename));
|
---|
801 | break;
|
---|
802 |
|
---|
803 | default:
|
---|
804 | break;
|
---|
805 | }
|
---|
806 |
|
---|
807 | p++;
|
---|
808 | if (a_string == NULL) {
|
---|
809 | return NULL;
|
---|
810 | }
|
---|
811 | }
|
---|
812 |
|
---|
813 | ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
|
---|
814 | TALLOC_FREE(a_string);
|
---|
815 | return ret_string;
|
---|
816 | }
|
---|
817 |
|
---|
818 | void standard_sub_advanced(const char *servicename, const char *user,
|
---|
819 | const char *connectpath, gid_t gid,
|
---|
820 | const char *smb_name, const char *domain_name,
|
---|
821 | char *str, size_t len)
|
---|
822 | {
|
---|
823 | char *s = talloc_sub_advanced(talloc_tos(),
|
---|
824 | servicename, user, connectpath,
|
---|
825 | gid, smb_name, domain_name, str);
|
---|
826 |
|
---|
827 | if (!s) {
|
---|
828 | return;
|
---|
829 | }
|
---|
830 | strlcpy( str, s, len );
|
---|
831 | TALLOC_FREE( s );
|
---|
832 | }
|
---|
833 |
|
---|
834 | /******************************************************************************
|
---|
835 | version of standard_sub_basic() for string lists; uses talloc_sub_basic()
|
---|
836 | for the work
|
---|
837 | *****************************************************************************/
|
---|
838 |
|
---|
839 | bool str_list_sub_basic( char **list, const char *smb_name,
|
---|
840 | const char *domain_name )
|
---|
841 | {
|
---|
842 | TALLOC_CTX *ctx = list;
|
---|
843 | char *s, *tmpstr;
|
---|
844 |
|
---|
845 | while ( *list ) {
|
---|
846 | s = *list;
|
---|
847 | tmpstr = talloc_sub_basic(ctx, smb_name, domain_name, s);
|
---|
848 | if ( !tmpstr ) {
|
---|
849 | DEBUG(0,("str_list_sub_basic: "
|
---|
850 | "talloc_sub_basic() return NULL!\n"));
|
---|
851 | return false;
|
---|
852 | }
|
---|
853 |
|
---|
854 | TALLOC_FREE(*list);
|
---|
855 | *list = tmpstr;
|
---|
856 |
|
---|
857 | list++;
|
---|
858 | }
|
---|
859 |
|
---|
860 | return true;
|
---|
861 | }
|
---|