Line data Source code
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 "substitute.h"
24 : #include "system/passwd.h"
25 : #include "secrets.h"
26 : #include "auth.h"
27 : #include "lib/util/string_wrappers.h"
28 :
29 : /* Max DNS name is 253 + '\0' */
30 : #define MACHINE_NAME_SIZE 254
31 :
32 : static char local_machine[MACHINE_NAME_SIZE];
33 : static char remote_machine[MACHINE_NAME_SIZE];
34 :
35 : userdom_struct current_user_info;
36 : fstring remote_proto="UNKNOWN";
37 :
38 : /**
39 : * Set the 'local' machine name
40 : * @param local_name the name we are being called
41 : * @param if this is the 'final' name for us, not be be changed again
42 : */
43 376 : bool set_local_machine_name(const char *local_name, bool perm)
44 : {
45 : static bool already_perm = false;
46 : char tmp[MACHINE_NAME_SIZE];
47 :
48 376 : if (already_perm) {
49 0 : return true;
50 : }
51 :
52 376 : strlcpy(tmp, local_name, sizeof(tmp));
53 376 : trim_char(tmp, ' ', ' ');
54 :
55 376 : alpha_strcpy(local_machine,
56 : tmp,
57 : SAFE_NETBIOS_CHARS,
58 : sizeof(local_machine) - 1);
59 376 : if (!strlower_m(local_machine)) {
60 0 : return false;
61 : }
62 :
63 376 : already_perm = perm;
64 :
65 376 : return true;
66 : }
67 :
68 1436597 : const char *get_local_machine_name(void)
69 : {
70 1436597 : if (local_machine[0] == '\0') {
71 1271318 : return lp_netbios_name();
72 : }
73 :
74 165279 : return local_machine;
75 : }
76 :
77 : /**
78 : * Set the 'remote' machine name
79 : *
80 : * @param remote_name the name our client wants to be called by
81 : * @param if this is the 'final' name for them, not be be changed again
82 : */
83 6729 : bool set_remote_machine_name(const char *remote_name, bool perm)
84 : {
85 : static bool already_perm = False;
86 : char tmp[MACHINE_NAME_SIZE];
87 :
88 6729 : if (already_perm) {
89 395 : return true;
90 : }
91 :
92 6334 : strlcpy(tmp, remote_name, sizeof(tmp));
93 6334 : trim_char(tmp, ' ', ' ');
94 :
95 6334 : alpha_strcpy(remote_machine,
96 : tmp,
97 : SAFE_NETBIOS_CHARS,
98 : sizeof(remote_machine) - 1);
99 6334 : if (!strlower_m(remote_machine)) {
100 0 : return false;
101 : }
102 :
103 6334 : already_perm = perm;
104 :
105 6334 : return true;
106 : }
107 :
108 376 : const char *get_remote_machine_name(void)
109 : {
110 376 : return remote_machine;
111 : }
112 :
113 : static char sub_peeraddr[INET6_ADDRSTRLEN];
114 : static const char *sub_peername = NULL;
115 : static char sub_sockaddr[INET6_ADDRSTRLEN];
116 :
117 5270 : void sub_set_socket_ids(const char *peeraddr, const char *peername,
118 : const char *sockaddr)
119 : {
120 5270 : const char *addr = peeraddr;
121 :
122 5270 : if (strnequal(addr, "::ffff:", 7)) {
123 0 : addr += 7;
124 : }
125 5270 : strlcpy(sub_peeraddr, addr, sizeof(sub_peeraddr));
126 :
127 5270 : if (sub_peername != NULL &&
128 0 : sub_peername != sub_peeraddr) {
129 0 : talloc_free(discard_const_p(char,sub_peername));
130 0 : sub_peername = NULL;
131 : }
132 5270 : sub_peername = talloc_strdup(NULL, peername);
133 5270 : if (sub_peername == NULL) {
134 0 : sub_peername = sub_peeraddr;
135 : }
136 :
137 : /*
138 : * Shouldn't we do the ::ffff: cancellation here as well? The
139 : * original code in talloc_sub_basic() did not do it, so I'm
140 : * leaving it out here as well for compatibility.
141 : */
142 5270 : strlcpy(sub_sockaddr, sockaddr, sizeof(sub_sockaddr));
143 5270 : }
144 :
145 : /*******************************************************************
146 : Setup the strings used by substitutions. Called per packet. Ensure
147 : %U name is set correctly also.
148 :
149 : smb_name must be sanitized by alpha_strcpy
150 : ********************************************************************/
151 :
152 91182 : void set_current_user_info(const char *smb_name, const char *unix_name,
153 : const char *domain)
154 : {
155 : static const void *last_smb_name;
156 : static const void *last_unix_name;
157 : static const void *last_domain;
158 :
159 91182 : if (likely(last_smb_name == smb_name &&
160 : last_unix_name == unix_name &&
161 : last_domain == domain))
162 : {
163 61465 : return;
164 : }
165 :
166 29717 : fstrcpy(current_user_info.smb_name, smb_name);
167 29717 : fstrcpy(current_user_info.unix_name, unix_name);
168 29717 : fstrcpy(current_user_info.domain, domain);
169 :
170 29717 : last_smb_name = smb_name;
171 29717 : last_unix_name = unix_name;
172 29717 : last_domain = domain;
173 : }
174 :
175 : /*******************************************************************
176 : Return the current active user name.
177 : *******************************************************************/
178 :
179 1415944 : const char *get_current_username(void)
180 : {
181 1415944 : return current_user_info.smb_name;
182 : }
183 :
184 : /*******************************************************************
185 : Given a pointer to a %$(NAME) in p and the whole string in str
186 : expand it as an environment variable.
187 : str must be a talloced string.
188 : Return a new allocated and expanded string.
189 : Based on code by Branko Cibej <branko.cibej@hermes.si>
190 : When this is called p points at the '%' character.
191 : May substitute multiple occurrencies of the same env var.
192 : ********************************************************************/
193 :
194 0 : static char *realloc_expand_env_var(char *str, char *p)
195 : {
196 : char *envname;
197 : char *envval;
198 : char *q, *r;
199 : int copylen;
200 :
201 0 : if (p[0] != '%' || p[1] != '$' || p[2] != '(') {
202 0 : return str;
203 : }
204 :
205 : /*
206 : * Look for the terminating ')'.
207 : */
208 :
209 0 : if ((q = strchr_m(p,')')) == NULL) {
210 0 : DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p));
211 0 : return str;
212 : }
213 :
214 : /*
215 : * Extract the name from within the %$(NAME) string.
216 : */
217 :
218 0 : r = p + 3;
219 0 : copylen = q - r;
220 :
221 : /* reserve space for use later add %$() chars */
222 0 : if ( (envname = talloc_array(talloc_tos(), char, copylen + 1 + 4)) == NULL ) {
223 0 : return NULL;
224 : }
225 :
226 0 : strncpy(envname,r,copylen);
227 0 : envname[copylen] = '\0';
228 :
229 0 : if ((envval = getenv(envname)) == NULL) {
230 0 : DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname));
231 0 : TALLOC_FREE(envname);
232 0 : return str;
233 : }
234 :
235 : /*
236 : * Copy the full %$(NAME) into envname so it
237 : * can be replaced.
238 : */
239 :
240 0 : copylen = q + 1 - p;
241 0 : strncpy(envname,p,copylen);
242 0 : envname[copylen] = '\0';
243 0 : r = realloc_string_sub(str, envname, envval);
244 0 : TALLOC_FREE(envname);
245 :
246 0 : return r;
247 : }
248 :
249 : /****************************************************************************
250 : Do some standard substitutions in a string.
251 : len is the length in bytes of the space allowed in string str. If zero means
252 : don't allow expansions.
253 : ****************************************************************************/
254 :
255 1070658 : void standard_sub_basic(const char *smb_name, const char *domain_name,
256 : char *str, size_t len)
257 : {
258 : char *s;
259 :
260 1070658 : if ( (s = talloc_sub_basic(talloc_tos(), smb_name, domain_name, str )) != NULL ) {
261 1070658 : strncpy( str, s, len );
262 : }
263 :
264 1070658 : TALLOC_FREE( s );
265 1070658 : }
266 :
267 : /*
268 : * Limit addresses to hexalpha charactes and underscore, safe for path
269 : * components for Windows clients.
270 : */
271 0 : static void make_address_pathsafe(char *addr)
272 : {
273 0 : while(addr && *addr) {
274 0 : if(!isxdigit(*addr)) {
275 0 : *addr = '_';
276 : }
277 0 : ++addr;
278 : }
279 0 : }
280 :
281 : /****************************************************************************
282 : Do some standard substitutions in a string.
283 : This function will return a talloced string that has to be freed.
284 : ****************************************************************************/
285 :
286 1436589 : char *talloc_sub_basic(TALLOC_CTX *mem_ctx,
287 : const char *smb_name,
288 : const char *domain_name,
289 : const char *str)
290 : {
291 : char *b, *p, *s, *r, *a_string;
292 : fstring pidstr, vnnstr;
293 1436589 : const char *local_machine_name = get_local_machine_name();
294 1436589 : TALLOC_CTX *tmp_ctx = NULL;
295 :
296 : /* workaround to prevent a crash while looking at bug #687 */
297 :
298 1436589 : if (!str) {
299 0 : DEBUG(0,("talloc_sub_basic: NULL source string! This should not happen\n"));
300 0 : return NULL;
301 : }
302 :
303 1436589 : a_string = talloc_strdup(mem_ctx, str);
304 1436589 : if (a_string == NULL) {
305 0 : DEBUG(0, ("talloc_sub_basic: Out of memory!\n"));
306 0 : return NULL;
307 : }
308 :
309 1436589 : tmp_ctx = talloc_stackframe();
310 1436589 : a_string = talloc_steal(tmp_ctx, a_string);
311 :
312 1480499 : for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
313 :
314 43910 : r = NULL;
315 43910 : b = a_string;
316 :
317 43910 : switch (*(p+1)) {
318 17419 : case 'U' :
319 17419 : r = strlower_talloc(tmp_ctx, smb_name);
320 17419 : if (r == NULL) {
321 0 : goto error;
322 : }
323 17419 : a_string = realloc_string_sub(a_string, "%U", r);
324 17419 : break;
325 60 : case 'G' : {
326 : struct passwd *pass;
327 60 : bool is_domain_name = false;
328 60 : const char *sep = lp_winbind_separator();
329 :
330 120 : if (domain_name != NULL && domain_name[0] != '\0' &&
331 60 : (lp_security() == SEC_ADS ||
332 0 : lp_security() == SEC_DOMAIN)) {
333 60 : r = talloc_asprintf(tmp_ctx,
334 : "%s%c%s",
335 : domain_name,
336 60 : *sep,
337 : smb_name);
338 60 : is_domain_name = true;
339 : } else {
340 0 : r = talloc_strdup(tmp_ctx, smb_name);
341 : }
342 60 : if (r == NULL) {
343 0 : goto error;
344 : }
345 :
346 60 : pass = Get_Pwnam_alloc(tmp_ctx, r);
347 60 : if (pass != NULL) {
348 : char *group_name;
349 :
350 36 : group_name = gidtoname(pass->pw_gid);
351 36 : if (is_domain_name) {
352 : char *group_sep;
353 36 : group_sep = strchr_m(group_name, *sep);
354 36 : if (group_sep != NULL) {
355 36 : group_name = group_sep + 1;
356 : }
357 : }
358 36 : a_string = realloc_string_sub(a_string,
359 : "%G",
360 : group_name);
361 : }
362 60 : TALLOC_FREE(pass);
363 60 : break;
364 : }
365 108 : case 'D' :
366 108 : r = strupper_talloc(tmp_ctx, domain_name);
367 108 : if (r == NULL) {
368 0 : goto error;
369 : }
370 108 : a_string = realloc_string_sub(a_string, "%D", r);
371 108 : break;
372 0 : case 'I' : {
373 0 : a_string = realloc_string_sub(
374 : a_string, "%I",
375 0 : sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
376 0 : break;
377 : }
378 0 : case 'J' : {
379 0 : r = talloc_strdup(tmp_ctx,
380 0 : sub_peeraddr[0] ? sub_peeraddr : "0.0.0.0");
381 0 : make_address_pathsafe(r);
382 0 : a_string = realloc_string_sub(a_string, "%J", r);
383 0 : break;
384 : }
385 0 : case 'i':
386 0 : a_string = realloc_string_sub(
387 : a_string, "%i",
388 0 : sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
389 0 : break;
390 0 : case 'j' : {
391 0 : r = talloc_strdup(tmp_ctx,
392 0 : sub_sockaddr[0] ? sub_sockaddr : "0.0.0.0");
393 0 : make_address_pathsafe(r);
394 0 : a_string = realloc_string_sub(a_string, "%j", r);
395 0 : break;
396 : }
397 0 : case 'L' :
398 0 : if ( strncasecmp_m(p, "%LOGONSERVER%", strlen("%LOGONSERVER%")) == 0 ) {
399 0 : break;
400 : }
401 0 : if (local_machine_name && *local_machine_name) {
402 0 : a_string = realloc_string_sub(a_string, "%L", local_machine_name);
403 : } else {
404 0 : a_string = realloc_string_sub(a_string, "%L", lp_netbios_name());
405 : }
406 0 : break;
407 6542 : case 'N' :
408 6542 : a_string = realloc_string_sub(a_string,
409 : "%N",
410 : lp_netbios_name());
411 6542 : break;
412 0 : case 'M' :
413 0 : a_string = realloc_string_sub(a_string, "%M",
414 0 : sub_peername ? sub_peername : "");
415 0 : break;
416 0 : case 'R' :
417 0 : a_string = realloc_string_sub(a_string, "%R", remote_proto);
418 0 : break;
419 0 : case 'T' :
420 0 : a_string = realloc_string_sub(a_string, "%T", current_timestring(tmp_ctx, False));
421 0 : break;
422 58 : case 't' :
423 58 : a_string = realloc_string_sub(a_string, "%t",
424 58 : current_minimal_timestring(tmp_ctx, False));
425 58 : break;
426 0 : case 'a' :
427 0 : a_string = realloc_string_sub(a_string, "%a",
428 : get_remote_arch_str());
429 0 : break;
430 0 : case 'd' :
431 0 : slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)getpid());
432 0 : a_string = realloc_string_sub(a_string, "%d", pidstr);
433 0 : break;
434 0 : case 'h' :
435 0 : a_string = realloc_string_sub(a_string, "%h", myhostname());
436 0 : break;
437 19612 : case 'm' :
438 19612 : a_string = realloc_string_sub(a_string, "%m",
439 : remote_machine);
440 19612 : break;
441 0 : case 'v' :
442 0 : a_string = realloc_string_sub(a_string, "%v", samba_version_string());
443 0 : break;
444 0 : case 'w' :
445 0 : a_string = realloc_string_sub(a_string, "%w", lp_winbind_separator());
446 0 : break;
447 0 : case '$' :
448 0 : a_string = realloc_expand_env_var(a_string, p); /* Expand environment variables */
449 0 : break;
450 0 : case 'V' :
451 0 : slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
452 0 : a_string = realloc_string_sub(a_string, "%V", vnnstr);
453 0 : break;
454 111 : default:
455 111 : break;
456 : }
457 :
458 43910 : p++;
459 43910 : TALLOC_FREE(r);
460 :
461 43910 : if (a_string == NULL) {
462 0 : goto done;
463 : }
464 : }
465 :
466 1436589 : goto done;
467 :
468 0 : error:
469 0 : TALLOC_FREE(a_string);
470 :
471 847369 : done:
472 1436589 : a_string = talloc_steal(mem_ctx, a_string);
473 1436589 : TALLOC_FREE(tmp_ctx);
474 1436589 : return a_string;
475 : }
476 :
477 : /****************************************************************************
478 : Do some specific substitutions in a string.
479 : This function will return an allocated string that have to be freed.
480 : ****************************************************************************/
481 :
482 12784 : char *talloc_sub_specified(TALLOC_CTX *mem_ctx,
483 : const char *input_string,
484 : const char *username,
485 : const char *grpname,
486 : const char *domain,
487 : uid_t uid,
488 : gid_t gid)
489 : {
490 : char *a_string;
491 12784 : char *ret_string = NULL;
492 : char *b, *p, *s;
493 : TALLOC_CTX *tmp_ctx;
494 :
495 12784 : if (!(tmp_ctx = talloc_new(mem_ctx))) {
496 0 : DEBUG(0, ("talloc_new failed\n"));
497 0 : return NULL;
498 : }
499 :
500 12784 : a_string = talloc_strdup(tmp_ctx, input_string);
501 12784 : if (a_string == NULL) {
502 0 : DEBUG(0, ("talloc_sub_specified: Out of memory!\n"));
503 0 : goto done;
504 : }
505 :
506 27054 : for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
507 :
508 14270 : b = a_string;
509 :
510 14270 : switch (*(p+1)) {
511 6390 : case 'U' :
512 6390 : a_string = talloc_string_sub(
513 : tmp_ctx, a_string, "%U", username);
514 6390 : break;
515 0 : case 'u' :
516 0 : a_string = talloc_string_sub(
517 : tmp_ctx, a_string, "%u", username);
518 0 : break;
519 1490 : case 'G' :
520 1490 : if (gid != -1) {
521 : const char *name;
522 :
523 1490 : if (grpname != NULL) {
524 1490 : name = grpname;
525 : } else {
526 0 : name = gidtoname(gid);
527 : }
528 :
529 1490 : a_string = talloc_string_sub(tmp_ctx,
530 : a_string,
531 : "%G",
532 : name);
533 : } else {
534 0 : a_string = talloc_string_sub(
535 : tmp_ctx, a_string,
536 : "%G", "NO_GROUP");
537 : }
538 1490 : break;
539 0 : case 'g' :
540 0 : if (gid != -1) {
541 : const char *name;
542 :
543 0 : if (grpname != NULL) {
544 0 : name = grpname;
545 : } else {
546 0 : name = gidtoname(gid);
547 : }
548 :
549 0 : a_string = talloc_string_sub(tmp_ctx,
550 : a_string,
551 : "%g",
552 : name);
553 : } else {
554 0 : a_string = talloc_string_sub(
555 : tmp_ctx, a_string, "%g", "NO_GROUP");
556 : }
557 0 : break;
558 4554 : case 'D' :
559 4554 : a_string = talloc_string_sub(tmp_ctx, a_string,
560 : "%D", domain);
561 4554 : break;
562 1836 : case 'N' :
563 1836 : a_string = talloc_string_sub(tmp_ctx, a_string,
564 : "%N", lp_netbios_name());
565 1836 : break;
566 0 : default:
567 0 : break;
568 : }
569 :
570 14270 : p++;
571 14270 : if (a_string == NULL) {
572 0 : goto done;
573 : }
574 : }
575 :
576 : /* Watch out, using "mem_ctx" here, so all intermediate stuff goes
577 : * away with the TALLOC_FREE(tmp_ctx) further down. */
578 :
579 12784 : ret_string = talloc_sub_basic(mem_ctx, username, domain, a_string);
580 :
581 12784 : done:
582 12784 : TALLOC_FREE(tmp_ctx);
583 12784 : return ret_string;
584 : }
585 :
586 : /****************************************************************************
587 : ****************************************************************************/
588 :
589 10696 : char *talloc_sub_advanced(TALLOC_CTX *ctx,
590 : const char *servicename,
591 : const char *user,
592 : const char *connectpath,
593 : gid_t gid,
594 : const char *str)
595 : {
596 : char *a_string;
597 : char *b, *p, *s;
598 :
599 10696 : a_string = talloc_strdup(talloc_tos(), str);
600 10696 : if (a_string == NULL) {
601 0 : DEBUG(0, ("talloc_sub_advanced_only: Out of memory!\n"));
602 0 : return NULL;
603 : }
604 :
605 10700 : for (s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) {
606 :
607 4 : b = a_string;
608 :
609 4 : switch (*(p+1)) {
610 0 : case 'N':
611 0 : a_string = realloc_string_sub(a_string,
612 : "%N",
613 : lp_netbios_name());
614 0 : break;
615 0 : case 'H': {
616 : char *h;
617 0 : if ((h = get_user_home_dir(talloc_tos(), user)))
618 0 : a_string = realloc_string_sub(a_string, "%H", h);
619 0 : TALLOC_FREE(h);
620 0 : break;
621 : }
622 0 : case 'P':
623 0 : a_string = realloc_string_sub(a_string, "%P", connectpath);
624 0 : break;
625 0 : case 'S':
626 0 : a_string = realloc_string_sub(a_string, "%S", servicename);
627 0 : break;
628 2 : case 'g':
629 2 : a_string = realloc_string_sub(a_string, "%g", gidtoname(gid));
630 2 : break;
631 2 : case 'u':
632 2 : a_string = realloc_string_sub(a_string, "%u", user);
633 2 : break;
634 0 : default:
635 0 : break;
636 : }
637 :
638 4 : p++;
639 4 : if (a_string == NULL) {
640 0 : return NULL;
641 : }
642 : }
643 :
644 10696 : return a_string;
645 : }
646 :
647 10696 : char *talloc_sub_full(TALLOC_CTX *ctx,
648 : const char *servicename,
649 : const char *user,
650 : const char *connectpath,
651 : gid_t gid,
652 : const char *smb_name,
653 : const char *domain_name,
654 : const char *str)
655 : {
656 : char *a_string, *ret_string;
657 :
658 10696 : a_string = talloc_sub_advanced(ctx, servicename, user, connectpath,
659 : gid, str);
660 10696 : if (a_string == NULL) {
661 0 : return NULL;
662 : }
663 :
664 10696 : ret_string = talloc_sub_basic(ctx, smb_name, domain_name, a_string);
665 10696 : TALLOC_FREE(a_string);
666 10696 : return ret_string;
667 : }
|