1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2006
9 Copyright (C) Jeremy Allison 1992-2007
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 #include "lib/param/loadparm.h"
27
28 static const char toupper_ascii_fast_table[128] = {
29 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
30 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
31 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
32 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
33 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
34 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
35 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
36 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
37 };
38
39 /**
40 * Compare 2 strings up to and including the nth char.
41 *
42 * @note The comparison is case-insensitive.
43 **/
strnequal(const char * s1,const char * s2,size_t n)44 bool strnequal(const char *s1,const char *s2,size_t n)
45 {
46 if (s1 == s2)
47 return(true);
48 if (!s1 || !s2 || !n)
49 return(false);
50
51 return(strncasecmp_m(s1,s2,n)==0);
52 }
53
54 /**
55 Convert a string to "normal" form.
56 **/
57
strnorm(char * s,int case_default)58 bool strnorm(char *s, int case_default)
59 {
60 if (case_default == CASE_UPPER)
61 return strupper_m(s);
62 else
63 return strlower_m(s);
64 }
65
66 /**
67 * Skip past some strings in a buffer - old version - no checks.
68 * **/
69
push_skip_string(char * buf)70 char *push_skip_string(char *buf)
71 {
72 buf += strlen(buf) + 1;
73 return(buf);
74 }
75
76 /**
77 Skip past a string in a buffer. Buffer may not be
78 null terminated. end_ptr points to the first byte after
79 then end of the buffer.
80 **/
81
skip_string(const char * base,size_t len,char * buf)82 char *skip_string(const char *base, size_t len, char *buf)
83 {
84 const char *end_ptr = base + len;
85
86 if (end_ptr < base || !base || !buf || buf >= end_ptr) {
87 return NULL;
88 }
89
90 /* Skip the string */
91 while (*buf) {
92 buf++;
93 if (buf >= end_ptr) {
94 return NULL;
95 }
96 }
97 /* Skip the '\0' */
98 buf++;
99 return buf;
100 }
101
102 /**
103 Count the number of characters in a string. Normally this will
104 be the same as the number of bytes in a string for single byte strings,
105 but will be different for multibyte.
106 **/
107
str_charnum(const char * s)108 size_t str_charnum(const char *s)
109 {
110 size_t ret, converted_size;
111 smb_ucs2_t *tmpbuf2 = NULL;
112 if (!push_ucs2_talloc(talloc_tos(), &tmpbuf2, s, &converted_size)) {
113 return 0;
114 }
115 ret = strlen_w(tmpbuf2);
116 TALLOC_FREE(tmpbuf2);
117 return ret;
118 }
119
trim_char(char * s,char cfront,char cback)120 bool trim_char(char *s,char cfront,char cback)
121 {
122 bool ret = false;
123 char *ep;
124 char *fp = s;
125
126 /* Ignore null or empty strings. */
127 if (!s || (s[0] == '\0'))
128 return false;
129
130 if (cfront) {
131 while (*fp && *fp == cfront)
132 fp++;
133 if (!*fp) {
134 /* We ate the string. */
135 s[0] = '\0';
136 return true;
137 }
138 if (fp != s)
139 ret = true;
140 }
141
142 ep = fp + strlen(fp) - 1;
143 if (cback) {
144 /* Attempt ascii only. Bail for mb strings. */
145 while ((ep >= fp) && (*ep == cback)) {
146 ret = true;
147 if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
148 /* Could be mb... bail back to tim_string. */
149 char fs[2], bs[2];
150 if (cfront) {
151 fs[0] = cfront;
152 fs[1] = '\0';
153 }
154 bs[0] = cback;
155 bs[1] = '\0';
156 return trim_string(s, cfront ? fs : NULL, bs);
157 } else {
158 ep--;
159 }
160 }
161 if (ep < fp) {
162 /* We ate the string. */
163 s[0] = '\0';
164 return true;
165 }
166 }
167
168 ep[1] = '\0';
169 memmove(s, fp, ep-fp+2);
170 return ret;
171 }
172
173 /**
174 Check if a string is part of a list.
175 **/
176
in_list(const char * s,const char * list,bool casesensitive)177 bool in_list(const char *s, const char *list, bool casesensitive)
178 {
179 char *tok = NULL;
180 bool ret = false;
181 TALLOC_CTX *frame;
182
183 if (!list) {
184 return false;
185 }
186
187 frame = talloc_stackframe();
188 while (next_token_talloc(frame, &list, &tok,LIST_SEP)) {
189 if (casesensitive) {
190 if (strcmp(tok,s) == 0) {
191 ret = true;
192 break;
193 }
194 } else {
195 if (strcasecmp_m(tok,s) == 0) {
196 ret = true;
197 break;
198 }
199 }
200 }
201 TALLOC_FREE(frame);
202 return ret;
203 }
204
205 /*
206 * Internal guts of talloc_string_sub and talloc_all_string_sub.
207 * talloc version of string_sub2.
208 */
209
talloc_string_sub2(TALLOC_CTX * mem_ctx,const char * src,const char * pattern,const char * insert,bool remove_unsafe_characters,bool replace_once,bool allow_trailing_dollar)210 char *talloc_string_sub2(TALLOC_CTX *mem_ctx, const char *src,
211 const char *pattern,
212 const char *insert,
213 bool remove_unsafe_characters,
214 bool replace_once,
215 bool allow_trailing_dollar)
216 {
217 char *p, *in;
218 char *s;
219 char *string;
220 ssize_t ls,lp,li,ld, i;
221
222 if (!insert || !pattern || !*pattern || !src) {
223 return NULL;
224 }
225
226 string = talloc_strdup(mem_ctx, src);
227 if (string == NULL) {
228 DEBUG(0, ("talloc_string_sub2: "
229 "talloc_strdup failed\n"));
230 return NULL;
231 }
232
233 s = string;
234
235 in = talloc_strdup(mem_ctx, insert);
236 if (!in) {
237 DEBUG(0, ("talloc_string_sub2: ENOMEM\n"));
238 return NULL;
239 }
240 ls = (ssize_t)strlen(s);
241 lp = (ssize_t)strlen(pattern);
242 li = (ssize_t)strlen(insert);
243 ld = li - lp;
244
245 for (i=0;i<li;i++) {
246 switch (in[i]) {
247 case '$':
248 /* allow a trailing $
249 * (as in machine accounts) */
250 if (allow_trailing_dollar && (i == li - 1 )) {
251 break;
252 }
253
254 FALL_THROUGH;
255 case '`':
256 case '"':
257 case '\'':
258 case ';':
259 case '%':
260 case '\r':
261 case '\n':
262 if (remove_unsafe_characters) {
263 in[i] = '_';
264 break;
265 }
266
267 FALL_THROUGH;
268 default:
269 /* ok */
270 break;
271 }
272 }
273
274 while ((p = strstr_m(s,pattern))) {
275 if (ld > 0) {
276 int offset = PTR_DIFF(s,string);
277 string = (char *)TALLOC_REALLOC(mem_ctx, string,
278 ls + ld + 1);
279 if (!string) {
280 DEBUG(0, ("talloc_string_sub: out of "
281 "memory!\n"));
282 TALLOC_FREE(in);
283 return NULL;
284 }
285 p = string + offset + (p - s);
286 }
287 if (li != lp) {
288 memmove(p+li,p+lp,strlen(p+lp)+1);
289 }
290 memcpy(p, in, li);
291 s = p + li;
292 ls += ld;
293
294 if (replace_once) {
295 break;
296 }
297 }
298 TALLOC_FREE(in);
299 return string;
300 }
301
302 /* Same as string_sub, but returns a talloc'ed string */
303
talloc_string_sub(TALLOC_CTX * mem_ctx,const char * src,const char * pattern,const char * insert)304 char *talloc_string_sub(TALLOC_CTX *mem_ctx,
305 const char *src,
306 const char *pattern,
307 const char *insert)
308 {
309 return talloc_string_sub2(mem_ctx, src, pattern, insert,
310 true, false, false);
311 }
312
talloc_all_string_sub(TALLOC_CTX * ctx,const char * src,const char * pattern,const char * insert)313 char *talloc_all_string_sub(TALLOC_CTX *ctx,
314 const char *src,
315 const char *pattern,
316 const char *insert)
317 {
318 return talloc_string_sub2(ctx, src, pattern, insert,
319 false, false, false);
320 }
321
322 /**
323 Write an octal as a string.
324 **/
325
octal_string(int i)326 char *octal_string(int i)
327 {
328 char *result;
329 if (i == -1) {
330 result = talloc_strdup(talloc_tos(), "-1");
331 }
332 else {
333 result = talloc_asprintf(talloc_tos(), "0%o", i);
334 }
335 SMB_ASSERT(result != NULL);
336 return result;
337 }
338
339
340 /**
341 Truncate a string at a specified length.
342 **/
343
string_truncate(char * s,unsigned int length)344 char *string_truncate(char *s, unsigned int length)
345 {
346 if (s && strlen(s) > length)
347 s[length] = 0;
348 return s;
349 }
350
351
352 /***********************************************************************
353 Return the equivalent of doing strrchr 'n' times - always going
354 backwards.
355 ***********************************************************************/
356
strnrchr_m(const char * s,char c,unsigned int n)357 char *strnrchr_m(const char *s, char c, unsigned int n)
358 {
359 smb_ucs2_t *ws = NULL;
360 char *s2 = NULL;
361 smb_ucs2_t *p;
362 char *ret;
363 size_t converted_size;
364
365 if (!push_ucs2_talloc(talloc_tos(), &ws, s, &converted_size)) {
366 /* Too hard to try and get right. */
367 return NULL;
368 }
369 p = strnrchr_w(ws, UCS2_CHAR(c), n);
370 if (!p) {
371 TALLOC_FREE(ws);
372 return NULL;
373 }
374 *p = 0;
375 if (!pull_ucs2_talloc(talloc_tos(), &s2, ws, &converted_size)) {
376 TALLOC_FREE(ws);
377 /* Too hard to try and get right. */
378 return NULL;
379 }
380 ret = discard_const_p(char, (s+strlen(s2)));
381 TALLOC_FREE(ws);
382 TALLOC_FREE(s2);
383 return ret;
384 }
385
unix_strlower(const char * src,size_t srclen,char * dest,size_t destlen)386 static bool unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
387 {
388 size_t size;
389 smb_ucs2_t *buffer = NULL;
390 bool ret;
391
392 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE, src, srclen,
393 (void **)(void *)&buffer, &size))
394 {
395 return false;
396 }
397 if (!strlower_w(buffer) && (dest == src)) {
398 TALLOC_FREE(buffer);
399 return true;
400 }
401 ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
402 TALLOC_FREE(buffer);
403 return ret;
404 }
405
406 #if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
407
408 /**
409 Convert a string to lower case.
410 **/
411 _PUBLIC_ void strlower_m(char *s)
412 {
413 char *d;
414 struct smb_iconv_handle *iconv_handle;
415
416 iconv_handle = get_iconv_handle();
417
418 d = s;
419
420 while (*s) {
421 size_t c_size, c_size2;
422 codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
423 c_size2 = push_codepoint_handle(iconv_handle, d, tolower_m(c));
424 if (c_size2 > c_size) {
425 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
426 c, tolower_m(c), (int)c_size, (int)c_size2));
427 smb_panic("codepoint expansion in strlower_m\n");
428 }
429 s += c_size;
430 d += c_size2;
431 }
432 *d = 0;
433 }
434
435 #endif
436
437 /**
438 Convert a string to lower case.
439 **/
440
strlower_m(char * s)441 bool strlower_m(char *s)
442 {
443 size_t len;
444 int errno_save;
445 bool ret = false;
446
447 /* this is quite a common operation, so we want it to be
448 fast. We optimise for the ascii case, knowing that all our
449 supported multi-byte character sets are ascii-compatible
450 (ie. they match for the first 128 chars) */
451
452 while (*s && !(((unsigned char)s[0]) & 0x80)) {
453 *s = tolower_m((unsigned char)*s);
454 s++;
455 }
456
457 if (!*s)
458 return true;
459
460 /* I assume that lowercased string takes the same number of bytes
461 * as source string even in UTF-8 encoding. (VIV) */
462 len = strlen(s) + 1;
463 errno_save = errno;
464 errno = 0;
465 ret = unix_strlower(s,len,s,len);
466 /* Catch mb conversion errors that may not terminate. */
467 if (errno) {
468 s[len-1] = '\0';
469 }
470 errno = errno_save;
471 return ret;
472 }
473
unix_strupper(const char * src,size_t srclen,char * dest,size_t destlen)474 static bool unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
475 {
476 size_t size;
477 smb_ucs2_t *buffer;
478 bool ret;
479
480 if (!push_ucs2_talloc(talloc_tos(), &buffer, src, &size)) {
481 return false;
482 }
483
484 if (!strupper_w(buffer) && (dest == src)) {
485 TALLOC_FREE(buffer);
486 return true;
487 }
488
489 ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
490 TALLOC_FREE(buffer);
491 return ret;
492 }
493
494 #if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
495
496 /**
497 Convert a string to UPPER case.
498 **/
499 _PUBLIC_ void strupper_m(char *s)
500 {
501 char *d;
502 struct smb_iconv_handle *iconv_handle;
503
504 iconv_handle = get_iconv_handle();
505
506 d = s;
507
508 while (*s) {
509 size_t c_size, c_size2;
510 codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
511 c_size2 = push_codepoint_handle(iconv_handle, d, toupper_m(c));
512 if (c_size2 > c_size) {
513 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
514 c, toupper_m(c), (int)c_size, (int)c_size2));
515 smb_panic("codepoint expansion in strupper_m\n");
516 }
517 s += c_size;
518 d += c_size2;
519 }
520 *d = 0;
521 }
522
523 #endif
524
525 /**
526 Convert a string to upper case.
527 **/
528
strupper_m(char * s)529 bool strupper_m(char *s)
530 {
531 size_t len;
532 bool ret = false;
533
534 /* this is quite a common operation, so we want it to be
535 fast. We optimise for the ascii case, knowing that all our
536 supported multi-byte character sets are ascii-compatible
537 (ie. they match for the first 128 chars) */
538
539 while (*s && !(((unsigned char)s[0]) & 0x80)) {
540 *s = toupper_ascii_fast_table[(unsigned char)s[0]];
541 s++;
542 }
543
544 if (!*s)
545 return true;
546
547 /* I assume that uppercased string takes the same number of bytes
548 * as source string even in multibyte encoding. (VIV) */
549 len = strlen(s) + 1;
550 ret = unix_strupper(s,len,s,len);
551 /* Catch mb conversion errors that may not terminate. */
552 if (!ret) {
553 s[len-1] = '\0';
554 }
555 return ret;
556 }
557
558 /**
559 Just a typesafety wrapper for snprintf into a fstring.
560 **/
561
fstr_sprintf(fstring s,const char * fmt,...)562 int fstr_sprintf(fstring s, const char *fmt, ...)
563 {
564 va_list ap;
565 int ret;
566
567 va_start(ap, fmt);
568 ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
569 va_end(ap);
570 return ret;
571 }
572
573 #define IPSTR_LIST_SEP ","
574 #define IPSTR_LIST_CHAR ','
575
576 /**
577 * Add ip string representation to ipstr list. Used also
578 * as part of @function ipstr_list_make
579 *
580 * @param ipstr_list pointer to string containing ip list;
581 * MUST BE already allocated and IS reallocated if necessary
582 * @param ipstr_size pointer to current size of ipstr_list (might be changed
583 * as a result of reallocation)
584 * @param ip IP address which is to be added to list
585 * @return pointer to string appended with new ip and possibly
586 * reallocated to new length
587 **/
588
ipstr_list_add(char ** ipstr_list,const struct ip_service * service)589 static char *ipstr_list_add(char **ipstr_list, const struct ip_service *service)
590 {
591 char *new_ipstr = NULL;
592 char addr_buf[INET6_ADDRSTRLEN];
593 int ret;
594
595 /* arguments checking */
596 if (!ipstr_list || !service) {
597 return NULL;
598 }
599
600 print_sockaddr(addr_buf,
601 sizeof(addr_buf),
602 &service->ss);
603
604 /* attempt to convert ip to a string and append colon separator to it */
605 if (*ipstr_list) {
606 if (service->ss.ss_family == AF_INET) {
607 /* IPv4 */
608 ret = asprintf(&new_ipstr, "%s%s%s:%d", *ipstr_list,
609 IPSTR_LIST_SEP, addr_buf,
610 service->port);
611 } else {
612 /* IPv6 */
613 ret = asprintf(&new_ipstr, "%s%s[%s]:%d", *ipstr_list,
614 IPSTR_LIST_SEP, addr_buf,
615 service->port);
616 }
617 SAFE_FREE(*ipstr_list);
618 } else {
619 if (service->ss.ss_family == AF_INET) {
620 /* IPv4 */
621 ret = asprintf(&new_ipstr, "%s:%d", addr_buf,
622 service->port);
623 } else {
624 /* IPv6 */
625 ret = asprintf(&new_ipstr, "[%s]:%d", addr_buf,
626 service->port);
627 }
628 }
629 if (ret == -1) {
630 return NULL;
631 }
632 *ipstr_list = new_ipstr;
633 return *ipstr_list;
634 }
635
636 /**
637 * Allocate and initialise an ipstr list using ip adresses
638 * passed as arguments.
639 *
640 * @param ipstr_list pointer to string meant to be allocated and set
641 * @param ip_list array of ip addresses to place in the list
642 * @param ip_count number of addresses stored in ip_list
643 * @return pointer to allocated ip string
644 **/
645
ipstr_list_make(char ** ipstr_list,const struct ip_service * ip_list,int ip_count)646 char *ipstr_list_make(char **ipstr_list,
647 const struct ip_service *ip_list,
648 int ip_count)
649 {
650 int i;
651
652 /* arguments checking */
653 if (!ip_list || !ipstr_list) {
654 return 0;
655 }
656
657 *ipstr_list = NULL;
658
659 /* process ip addresses given as arguments */
660 for (i = 0; i < ip_count; i++) {
661 *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
662 }
663
664 return (*ipstr_list);
665 }
666
667
668 /**
669 * Parse given ip string list into array of ip addresses
670 * (as ip_service structures)
671 * e.g. [IPv6]:port,192.168.1.100:389,192.168.1.78, ...
672 *
673 * @param ipstr ip string list to be parsed
674 * @param ip_list pointer to array of ip addresses which is
675 * allocated by this function and must be freed by caller
676 * @return number of successfully parsed addresses
677 **/
678
ipstr_list_parse(const char * ipstr_list,struct ip_service ** ip_list)679 int ipstr_list_parse(const char *ipstr_list, struct ip_service **ip_list)
680 {
681 TALLOC_CTX *frame;
682 char *token_str = NULL;
683 size_t count;
684 int i;
685
686 if (!ipstr_list || !ip_list)
687 return 0;
688
689 count = count_chars(ipstr_list, IPSTR_LIST_CHAR) + 1;
690 if ( (*ip_list = SMB_MALLOC_ARRAY(struct ip_service, count)) == NULL ) {
691 DEBUG(0,("ipstr_list_parse: malloc failed for %lu entries\n",
692 (unsigned long)count));
693 return 0;
694 }
695
696 frame = talloc_stackframe();
697 for ( i=0; next_token_talloc(frame, &ipstr_list, &token_str,
698 IPSTR_LIST_SEP) && i<count; i++ ) {
699 char *s = token_str;
700 char *p = strrchr(token_str, ':');
701
702 if (p) {
703 *p = 0;
704 (*ip_list)[i].port = atoi(p+1);
705 }
706
707 /* convert single token to ip address */
708 if (token_str[0] == '[') {
709 /* IPv6 address. */
710 s++;
711 p = strchr(token_str, ']');
712 if (!p) {
713 continue;
714 }
715 *p = '\0';
716 }
717 if (!interpret_string_addr(&(*ip_list)[i].ss,
718 s,
719 AI_NUMERICHOST)) {
720 continue;
721 }
722 }
723 TALLOC_FREE(frame);
724 return count;
725 }
726
727 /**
728 * Safely free ip string list
729 *
730 * @param ipstr_list ip string list to be freed
731 **/
732
ipstr_list_free(char * ipstr_list)733 void ipstr_list_free(char* ipstr_list)
734 {
735 SAFE_FREE(ipstr_list);
736 }
737
738 /* read a SMB_BIG_UINT from a string */
STR_TO_SMB_BIG_UINT(const char * nptr,const char ** entptr)739 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
740 {
741
742 uint64_t val = (uint64_t)-1;
743 const char *p = nptr;
744
745 if (!p) {
746 if (entptr) {
747 *entptr = p;
748 }
749 return val;
750 }
751
752 while (*p && isspace(*p))
753 p++;
754
755 sscanf(p,"%"SCNu64,&val);
756 if (entptr) {
757 while (*p && isdigit(*p))
758 p++;
759 *entptr = p;
760 }
761
762 return val;
763 }
764
765 /* Convert a size specification to a count of bytes. We accept the following
766 * suffixes:
767 * bytes if there is no suffix
768 * kK kibibytes
769 * mM mebibytes
770 * gG gibibytes
771 * tT tibibytes
772 * pP whatever the ISO name for petabytes is
773 *
774 * Returns 0 if the string can't be converted.
775 */
conv_str_size(const char * str)776 uint64_t conv_str_size(const char * str)
777 {
778 uint64_t lval;
779 char *end;
780 int error = 0;
781
782 if (str == NULL || *str == '\0') {
783 return 0;
784 }
785
786 lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
787
788 if (error != 0) {
789 return 0;
790 }
791
792 if (*end == '\0') {
793 return lval;
794 }
795
796 if (strwicmp(end, "K") == 0) {
797 lval *= 1024ULL;
798 } else if (strwicmp(end, "M") == 0) {
799 lval *= (1024ULL * 1024ULL);
800 } else if (strwicmp(end, "G") == 0) {
801 lval *= (1024ULL * 1024ULL *
802 1024ULL);
803 } else if (strwicmp(end, "T") == 0) {
804 lval *= (1024ULL * 1024ULL *
805 1024ULL * 1024ULL);
806 } else if (strwicmp(end, "P") == 0) {
807 lval *= (1024ULL * 1024ULL *
808 1024ULL * 1024ULL *
809 1024ULL);
810 } else {
811 return 0;
812 }
813
814 return lval;
815 }
816
817 /*
818 * asprintf into a string and strupper_m it after that.
819 */
820
asprintf_strupper_m(char ** strp,const char * fmt,...)821 int asprintf_strupper_m(char **strp, const char *fmt, ...)
822 {
823 va_list ap;
824 char *result;
825 int ret;
826
827 va_start(ap, fmt);
828 ret = vasprintf(&result, fmt, ap);
829 va_end(ap);
830
831 if (ret == -1)
832 return -1;
833
834 if (!strupper_m(result)) {
835 SAFE_FREE(result);
836 return -1;
837 }
838
839 *strp = result;
840 return ret;
841 }
842
talloc_asprintf_strupper_m(TALLOC_CTX * t,const char * fmt,...)843 char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...)
844 {
845 va_list ap;
846 char *ret;
847
848 va_start(ap, fmt);
849 ret = talloc_vasprintf(t, fmt, ap);
850 va_end(ap);
851
852 if (ret == NULL) {
853 return NULL;
854 }
855 if (!strupper_m(ret)) {
856 TALLOC_FREE(ret);
857 return NULL;
858 }
859 return ret;
860 }
861
talloc_asprintf_strlower_m(TALLOC_CTX * t,const char * fmt,...)862 char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...)
863 {
864 va_list ap;
865 char *ret;
866
867 va_start(ap, fmt);
868 ret = talloc_vasprintf(t, fmt, ap);
869 va_end(ap);
870
871 if (ret == NULL) {
872 return NULL;
873 }
874 if (!strlower_m(ret)) {
875 TALLOC_FREE(ret);
876 return NULL;
877 }
878 return ret;
879 }
880
881
882 /********************************************************************
883 Check a string for any occurrences of a specified list of invalid
884 characters.
885 ********************************************************************/
886
validate_net_name(const char * name,const char * invalid_chars,int max_len)887 bool validate_net_name( const char *name,
888 const char *invalid_chars,
889 int max_len)
890 {
891 int i;
892
893 if (!name) {
894 return false;
895 }
896
897 for ( i=0; i<max_len && name[i]; i++ ) {
898 /* fail if strchr_m() finds one of the invalid characters */
899 if ( name[i] && strchr_m( invalid_chars, name[i] ) ) {
900 return false;
901 }
902 }
903
904 return true;
905 }
906
907
908 /*******************************************************************
909 Add a shell escape character '\' to any character not in a known list
910 of characters. UNIX charset format.
911 *******************************************************************/
912
913 #define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
914 #define INSIDE_DQUOTE_LIST "$`\n\"\\"
915
escape_shell_string(const char * src)916 char *escape_shell_string(const char *src)
917 {
918 size_t srclen = strlen(src);
919 char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1);
920 char *dest = ret;
921 bool in_s_quote = false;
922 bool in_d_quote = false;
923 bool next_escaped = false;
924
925 if (!ret) {
926 return NULL;
927 }
928
929 while (*src) {
930 size_t c_size;
931 codepoint_t c = next_codepoint(src, &c_size);
932
933 if (c == INVALID_CODEPOINT) {
934 SAFE_FREE(ret);
935 return NULL;
936 }
937
938 if (c_size > 1) {
939 memcpy(dest, src, c_size);
940 src += c_size;
941 dest += c_size;
942 next_escaped = false;
943 continue;
944 }
945
946 /*
947 * Deal with backslash escaped state.
948 * This only lasts for one character.
949 */
950
951 if (next_escaped) {
952 *dest++ = *src++;
953 next_escaped = false;
954 continue;
955 }
956
957 /*
958 * Deal with single quote state. The
959 * only thing we care about is exiting
960 * this state.
961 */
962
963 if (in_s_quote) {
964 if (*src == '\'') {
965 in_s_quote = false;
966 }
967 *dest++ = *src++;
968 continue;
969 }
970
971 /*
972 * Deal with double quote state. The most
973 * complex state. We must cope with \, meaning
974 * possibly escape next char (depending what it
975 * is), ", meaning exit this state, and possibly
976 * add an \ escape to any unprotected character
977 * (listed in INSIDE_DQUOTE_LIST).
978 */
979
980 if (in_d_quote) {
981 if (*src == '\\') {
982 /*
983 * Next character might be escaped.
984 * We have to peek. Inside double
985 * quotes only INSIDE_DQUOTE_LIST
986 * characters are escaped by a \.
987 */
988
989 char nextchar;
990
991 c = next_codepoint(&src[1], &c_size);
992 if (c == INVALID_CODEPOINT) {
993 SAFE_FREE(ret);
994 return NULL;
995 }
996 if (c_size > 1) {
997 /*
998 * Don't escape the next char.
999 * Just copy the \.
1000 */
1001 *dest++ = *src++;
1002 continue;
1003 }
1004
1005 nextchar = src[1];
1006
1007 if (nextchar && strchr(INSIDE_DQUOTE_LIST,
1008 (int)nextchar)) {
1009 next_escaped = true;
1010 }
1011 *dest++ = *src++;
1012 continue;
1013 }
1014
1015 if (*src == '\"') {
1016 /* Exit double quote state. */
1017 in_d_quote = false;
1018 *dest++ = *src++;
1019 continue;
1020 }
1021
1022 /*
1023 * We know the character isn't \ or ",
1024 * so escape it if it's any of the other
1025 * possible unprotected characters.
1026 */
1027
1028 if (strchr(INSIDE_DQUOTE_LIST, (int)*src)) {
1029 *dest++ = '\\';
1030 }
1031 *dest++ = *src++;
1032 continue;
1033 }
1034
1035 /*
1036 * From here to the end of the loop we're
1037 * not in the single or double quote state.
1038 */
1039
1040 if (*src == '\\') {
1041 /* Next character must be escaped. */
1042 next_escaped = true;
1043 *dest++ = *src++;
1044 continue;
1045 }
1046
1047 if (*src == '\'') {
1048 /* Go into single quote state. */
1049 in_s_quote = true;
1050 *dest++ = *src++;
1051 continue;
1052 }
1053
1054 if (*src == '\"') {
1055 /* Go into double quote state. */
1056 in_d_quote = true;
1057 *dest++ = *src++;
1058 continue;
1059 }
1060
1061 /* Check if we need to escape the character. */
1062
1063 if (!strchr(INCLUDE_LIST, (int)*src)) {
1064 *dest++ = '\\';
1065 }
1066 *dest++ = *src++;
1067 }
1068 *dest++ = '\0';
1069 return ret;
1070 }
1071
1072 /*
1073 * This routine improves performance for operations temporarily acting on a
1074 * full path. It is equivalent to the much more expensive
1075 *
1076 * talloc_asprintf(talloc_tos(), "%s/%s", dir, name)
1077 *
1078 * This actually does make a difference in metadata-heavy workloads (i.e. the
1079 * "standard" client.txt nbench run.
1080 */
1081
full_path_tos(const char * dir,const char * name,char * tmpbuf,size_t tmpbuf_len,char ** pdst,char ** to_free)1082 ssize_t full_path_tos(const char *dir, const char *name,
1083 char *tmpbuf, size_t tmpbuf_len,
1084 char **pdst, char **to_free)
1085 {
1086 size_t dirlen, namelen, len;
1087 char *dst;
1088
1089 dirlen = strlen(dir);
1090 namelen = strlen(name);
1091 len = dirlen + namelen + 1;
1092
1093 if (len < tmpbuf_len) {
1094 dst = tmpbuf;
1095 *to_free = NULL;
1096 } else {
1097 dst = talloc_array(talloc_tos(), char, len+1);
1098 if (dst == NULL) {
1099 return -1;
1100 }
1101 *to_free = dst;
1102 }
1103
1104 memcpy(dst, dir, dirlen);
1105 dst[dirlen] = '/';
1106 memcpy(dst+dirlen+1, name, namelen+1);
1107 *pdst = dst;
1108 return len;
1109 }
1110