examples/VFS: fix skel_transparent.c in reference to shadow_copy changes
[Samba/gebeck_regimport.git] / source3 / lib / util.c
blobb8fc319a6ff4dacd853b16bc3b5a319205b7e3b5
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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "system/passwd.h"
26 #include "system/filesys.h"
27 #include "util_tdb.h"
28 #include "ctdbd_conn.h"
29 #include "../lib/util/util_pw.h"
30 #include "messages.h"
31 #include <ccan/hash/hash.h>
33 /* Max allowable allococation - 256mb - 0x10000000 */
34 #define MAX_ALLOC_SIZE (1024*1024*256)
36 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
37 #ifdef WITH_NISPLUS_HOME
38 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
40 * The following lines are needed due to buggy include files
41 * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
42 * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
43 * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
44 * an enum in /usr/include/rpcsvc/nis.h.
47 #if defined(GROUP)
48 #undef GROUP
49 #endif
51 #if defined(GROUP_OBJ)
52 #undef GROUP_OBJ
53 #endif
55 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
57 #include <rpcsvc/nis.h>
59 #endif /* WITH_NISPLUS_HOME */
60 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
62 static enum protocol_types Protocol = PROTOCOL_COREPLUS;
64 enum protocol_types get_Protocol(void)
66 return Protocol;
69 void set_Protocol(enum protocol_types p)
71 Protocol = p;
74 static enum remote_arch_types ra_type = RA_UNKNOWN;
76 void gfree_all( void )
78 gfree_names();
79 gfree_loadparm();
80 gfree_charcnv();
81 gfree_interfaces();
82 gfree_debugsyms();
85 /*******************************************************************
86 Check if a file exists - call vfs_file_exist for samba files.
87 ********************************************************************/
89 bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf,
90 bool fake_dir_create_times)
92 SMB_STRUCT_STAT st;
93 if (!sbuf)
94 sbuf = &st;
96 if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
97 return(False);
99 return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
102 /*******************************************************************
103 Check if a unix domain socket exists - call vfs_file_exist for samba files.
104 ********************************************************************/
106 bool socket_exist(const char *fname)
108 SMB_STRUCT_STAT st;
109 if (sys_stat(fname, &st, false) != 0)
110 return(False);
112 return S_ISSOCK(st.st_ex_mode);
115 /*******************************************************************
116 Returns the size in bytes of the named given the stat struct.
117 ********************************************************************/
119 uint64_t get_file_size_stat(const SMB_STRUCT_STAT *sbuf)
121 return sbuf->st_ex_size;
124 /*******************************************************************
125 Returns the size in bytes of the named file.
126 ********************************************************************/
128 SMB_OFF_T get_file_size(char *file_name)
130 SMB_STRUCT_STAT buf;
131 buf.st_ex_size = 0;
132 if (sys_stat(file_name, &buf, false) != 0)
133 return (SMB_OFF_T)-1;
134 return get_file_size_stat(&buf);
137 /*******************************************************************
138 Show a smb message structure.
139 ********************************************************************/
141 void show_msg(const char *buf)
143 int i;
144 int bcc=0;
146 if (!DEBUGLVL(5))
147 return;
149 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
150 smb_len(buf),
151 (int)CVAL(buf,smb_com),
152 (int)CVAL(buf,smb_rcls),
153 (int)CVAL(buf,smb_reh),
154 (int)SVAL(buf,smb_err),
155 (int)CVAL(buf,smb_flg),
156 (int)SVAL(buf,smb_flg2)));
157 DEBUGADD(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\n",
158 (int)SVAL(buf,smb_tid),
159 (int)SVAL(buf,smb_pid),
160 (int)SVAL(buf,smb_uid),
161 (int)SVAL(buf,smb_mid)));
162 DEBUGADD(5,("smt_wct=%d\n",(int)CVAL(buf,smb_wct)));
164 for (i=0;i<(int)CVAL(buf,smb_wct);i++)
165 DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i,
166 SVAL(buf,smb_vwv+2*i),SVAL(buf,smb_vwv+2*i)));
168 bcc = (int)SVAL(buf,smb_vwv+2*(CVAL(buf,smb_wct)));
170 DEBUGADD(5,("smb_bcc=%d\n",bcc));
172 if (DEBUGLEVEL < 10)
173 return;
175 if (DEBUGLEVEL < 50)
176 bcc = MIN(bcc, 512);
178 dump_data(10, (const uint8 *)smb_buf_const(buf), bcc);
181 /*******************************************************************
182 Set the length and marker of an encrypted smb packet.
183 ********************************************************************/
185 void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
187 _smb_setlen(buf,len);
189 SCVAL(buf,4,0xFF);
190 SCVAL(buf,5,'E');
191 SSVAL(buf,6,enc_ctx_num);
194 /*******************************************************************
195 Set the length and marker of an smb packet.
196 ********************************************************************/
198 void smb_setlen(char *buf,int len)
200 _smb_setlen(buf,len);
202 SCVAL(buf,4,0xFF);
203 SCVAL(buf,5,'S');
204 SCVAL(buf,6,'M');
205 SCVAL(buf,7,'B');
208 /*******************************************************************
209 Setup only the byte count for a smb message.
210 ********************************************************************/
212 int set_message_bcc(char *buf,int num_bytes)
214 int num_words = CVAL(buf,smb_wct);
215 SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
216 _smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
217 return (smb_size + num_words*2 + num_bytes);
220 /*******************************************************************
221 Add a data blob to the end of a smb_buf, adjusting bcc and smb_len.
222 Return the bytes added
223 ********************************************************************/
225 ssize_t message_push_blob(uint8 **outbuf, DATA_BLOB blob)
227 size_t newlen = smb_len(*outbuf) + 4 + blob.length;
228 uint8 *tmp;
230 if (!(tmp = talloc_realloc(NULL, *outbuf, uint8, newlen))) {
231 DEBUG(0, ("talloc failed\n"));
232 return -1;
234 *outbuf = tmp;
236 memcpy(tmp + smb_len(tmp) + 4, blob.data, blob.length);
237 set_message_bcc((char *)tmp, smb_buflen(tmp) + blob.length);
238 return blob.length;
241 /*******************************************************************
242 Reduce a file name, removing .. elements.
243 ********************************************************************/
245 static char *dos_clean_name(TALLOC_CTX *ctx, const char *s)
247 char *p = NULL;
248 char *str = NULL;
250 DEBUG(3,("dos_clean_name [%s]\n",s));
252 /* remove any double slashes */
253 str = talloc_all_string_sub(ctx, s, "\\\\", "\\");
254 if (!str) {
255 return NULL;
258 /* Remove leading .\\ characters */
259 if(strncmp(str, ".\\", 2) == 0) {
260 trim_string(str, ".\\", NULL);
261 if(*str == 0) {
262 str = talloc_strdup(ctx, ".\\");
263 if (!str) {
264 return NULL;
269 while ((p = strstr_m(str,"\\..\\")) != NULL) {
270 char *s1;
272 *p = 0;
273 s1 = p+3;
275 if ((p=strrchr_m(str,'\\')) != NULL) {
276 *p = 0;
277 } else {
278 *str = 0;
280 str = talloc_asprintf(ctx,
281 "%s%s",
282 str,
283 s1);
284 if (!str) {
285 return NULL;
289 trim_string(str,NULL,"\\..");
290 return talloc_all_string_sub(ctx, str, "\\.\\", "\\");
293 /*******************************************************************
294 Reduce a file name, removing .. elements.
295 ********************************************************************/
297 char *unix_clean_name(TALLOC_CTX *ctx, const char *s)
299 char *p = NULL;
300 char *str = NULL;
302 DEBUG(3,("unix_clean_name [%s]\n",s));
304 /* remove any double slashes */
305 str = talloc_all_string_sub(ctx, s, "//","/");
306 if (!str) {
307 return NULL;
310 /* Remove leading ./ characters */
311 if(strncmp(str, "./", 2) == 0) {
312 trim_string(str, "./", NULL);
313 if(*str == 0) {
314 str = talloc_strdup(ctx, "./");
315 if (!str) {
316 return NULL;
321 while ((p = strstr_m(str,"/../")) != NULL) {
322 char *s1;
324 *p = 0;
325 s1 = p+3;
327 if ((p=strrchr_m(str,'/')) != NULL) {
328 *p = 0;
329 } else {
330 *str = 0;
332 str = talloc_asprintf(ctx,
333 "%s%s",
334 str,
335 s1);
336 if (!str) {
337 return NULL;
341 trim_string(str,NULL,"/..");
342 return talloc_all_string_sub(ctx, str, "/./", "/");
345 char *clean_name(TALLOC_CTX *ctx, const char *s)
347 char *str = dos_clean_name(ctx, s);
348 if (!str) {
349 return NULL;
351 return unix_clean_name(ctx, str);
354 /*******************************************************************
355 Write data into an fd at a given offset. Ignore seek errors.
356 ********************************************************************/
358 ssize_t write_data_at_offset(int fd, const char *buffer, size_t N, SMB_OFF_T pos)
360 size_t total=0;
361 ssize_t ret;
363 if (pos == (SMB_OFF_T)-1) {
364 return write_data(fd, buffer, N);
366 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
367 while (total < N) {
368 ret = sys_pwrite(fd,buffer + total,N - total, pos);
369 if (ret == -1 && errno == ESPIPE) {
370 return write_data(fd, buffer + total,N - total);
372 if (ret == -1) {
373 DEBUG(0,("write_data_at_offset: write failure. Error = %s\n", strerror(errno) ));
374 return -1;
376 if (ret == 0) {
377 return total;
379 total += ret;
380 pos += ret;
382 return (ssize_t)total;
383 #else
384 /* Use lseek and write_data. */
385 if (sys_lseek(fd, pos, SEEK_SET) == -1) {
386 if (errno != ESPIPE) {
387 return -1;
390 return write_data(fd, buffer, N);
391 #endif
395 NTSTATUS reinit_after_fork(struct messaging_context *msg_ctx,
396 struct event_context *ev_ctx,
397 struct server_id id,
398 bool parent_longlived)
400 NTSTATUS status = NT_STATUS_OK;
402 /* Reset the state of the random
403 * number generation system, so
404 * children do not get the same random
405 * numbers as each other */
406 set_need_random_reseed();
408 /* tdb needs special fork handling */
409 if (tdb_reopen_all(parent_longlived ? 1 : 0) != 0) {
410 DEBUG(0,("tdb_reopen_all failed.\n"));
411 status = NT_STATUS_OPEN_FAILED;
412 goto done;
415 if (ev_ctx && tevent_re_initialise(ev_ctx) != 0) {
416 smb_panic(__location__ ": Failed to re-initialise event context");
419 if (msg_ctx) {
421 * For clustering, we need to re-init our ctdbd connection after the
422 * fork
424 status = messaging_reinit(msg_ctx, id);
425 if (!NT_STATUS_IS_OK(status)) {
426 DEBUG(0,("messaging_reinit() failed: %s\n",
427 nt_errstr(status)));
430 done:
431 return status;
434 /****************************************************************************
435 (Hopefully) efficient array append.
436 ****************************************************************************/
438 void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
439 void *element, void *_array, uint32 *num_elements,
440 ssize_t *array_size)
442 void **array = (void **)_array;
444 if (*array_size < 0) {
445 return;
448 if (*array == NULL) {
449 if (*array_size == 0) {
450 *array_size = 128;
453 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
454 goto error;
457 *array = TALLOC(mem_ctx, element_size * (*array_size));
458 if (*array == NULL) {
459 goto error;
463 if (*num_elements == *array_size) {
464 *array_size *= 2;
466 if (*array_size >= MAX_ALLOC_SIZE/element_size) {
467 goto error;
470 *array = TALLOC_REALLOC(mem_ctx, *array,
471 element_size * (*array_size));
473 if (*array == NULL) {
474 goto error;
478 memcpy((char *)(*array) + element_size*(*num_elements),
479 element, element_size);
480 *num_elements += 1;
482 return;
484 error:
485 *num_elements = 0;
486 *array_size = -1;
489 /****************************************************************************
490 Get my own domain name, or "" if we have none.
491 ****************************************************************************/
493 char *get_mydnsdomname(TALLOC_CTX *ctx)
495 const char *domname;
496 char *p;
498 domname = get_mydnsfullname();
499 if (!domname) {
500 return NULL;
503 p = strchr_m(domname, '.');
504 if (p) {
505 p++;
506 return talloc_strdup(ctx, p);
507 } else {
508 return talloc_strdup(ctx, "");
512 /****************************************************************************
513 Interpret a protocol description string, with a default.
514 ****************************************************************************/
516 int interpret_protocol(const char *str,int def)
518 if (strequal(str,"NT1"))
519 return(PROTOCOL_NT1);
520 if (strequal(str,"LANMAN2"))
521 return(PROTOCOL_LANMAN2);
522 if (strequal(str,"LANMAN1"))
523 return(PROTOCOL_LANMAN1);
524 if (strequal(str,"CORE"))
525 return(PROTOCOL_CORE);
526 if (strequal(str,"COREPLUS"))
527 return(PROTOCOL_COREPLUS);
528 if (strequal(str,"CORE+"))
529 return(PROTOCOL_COREPLUS);
531 DEBUG(0,("Unrecognised protocol level %s\n",str));
533 return(def);
537 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
538 /******************************************************************
539 Remove any mount options such as -rsize=2048,wsize=2048 etc.
540 Based on a fix from <Thomas.Hepper@icem.de>.
541 Returns a malloc'ed string.
542 *******************************************************************/
544 static char *strip_mount_options(TALLOC_CTX *ctx, const char *str)
546 if (*str == '-') {
547 const char *p = str;
548 while(*p && !isspace(*p))
549 p++;
550 while(*p && isspace(*p))
551 p++;
552 if(*p) {
553 return talloc_strdup(ctx, p);
556 return NULL;
559 /*******************************************************************
560 Patch from jkf@soton.ac.uk
561 Split Luke's automount_server into YP lookup and string splitter
562 so can easily implement automount_path().
563 Returns a malloc'ed string.
564 *******************************************************************/
566 #ifdef WITH_NISPLUS_HOME
567 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
569 char *value = NULL;
571 char *nis_map = (char *)lp_nis_home_map_name();
573 char buffer[NIS_MAXATTRVAL + 1];
574 nis_result *result;
575 nis_object *object;
576 entry_obj *entry;
578 snprintf(buffer, sizeof(buffer), "[key=%s],%s", user_name, nis_map);
579 DEBUG(5, ("NIS+ querystring: %s\n", buffer));
581 if (result = nis_list(buffer, FOLLOW_PATH|EXPAND_NAME|HARD_LOOKUP, NULL, NULL)) {
582 if (result->status != NIS_SUCCESS) {
583 DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status)));
584 } else {
585 object = result->objects.objects_val;
586 if (object->zo_data.zo_type == ENTRY_OBJ) {
587 entry = &object->zo_data.objdata_u.en_data;
588 DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type));
589 DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val));
591 value = talloc_strdup(ctx,
592 entry->en_cols.en_cols_val[1].ec_value.ec_value_val);
593 if (!value) {
594 nis_freeresult(result);
595 return NULL;
597 value = talloc_string_sub(ctx,
598 value,
599 "&",
600 user_name);
604 nis_freeresult(result);
606 if (value) {
607 value = strip_mount_options(ctx, value);
608 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n",
609 user_name, value));
611 return value;
613 #else /* WITH_NISPLUS_HOME */
615 char *automount_lookup(TALLOC_CTX *ctx, const char *user_name)
617 char *value = NULL;
619 int nis_error; /* returned by yp all functions */
620 char *nis_result; /* yp_match inits this */
621 int nis_result_len; /* and set this */
622 char *nis_domain; /* yp_get_default_domain inits this */
623 char *nis_map = (char *)lp_nis_home_map_name();
625 if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) {
626 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error)));
627 return NULL;
630 DEBUG(5, ("NIS Domain: %s\n", nis_domain));
632 if ((nis_error = yp_match(nis_domain, nis_map, user_name,
633 strlen(user_name), &nis_result,
634 &nis_result_len)) == 0) {
635 if (nis_result_len > 0 && nis_result[nis_result_len] == '\n') {
636 nis_result[nis_result_len] = '\0';
638 value = talloc_strdup(ctx, nis_result);
639 if (!value) {
640 return NULL;
642 value = strip_mount_options(ctx, value);
643 } else if(nis_error == YPERR_KEY) {
644 DEBUG(3, ("YP Key not found: while looking up \"%s\" in map \"%s\"\n",
645 user_name, nis_map));
646 DEBUG(3, ("using defaults for server and home directory\n"));
647 } else {
648 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
649 yperr_string(nis_error), user_name, nis_map));
652 if (value) {
653 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, value));
655 return value;
657 #endif /* WITH_NISPLUS_HOME */
658 #endif
660 /****************************************************************************
661 Check if a process exists. Does this work on all unixes?
662 ****************************************************************************/
664 bool process_exists(const struct server_id pid)
666 if (procid_is_me(&pid)) {
667 return True;
670 if (procid_is_local(&pid)) {
671 return (kill(pid.pid,0) == 0 || errno != ESRCH);
674 #ifdef CLUSTER_SUPPORT
675 return ctdbd_process_exists(messaging_ctdbd_connection(),
676 pid.vnn, pid.pid);
677 #else
678 return False;
679 #endif
682 /*******************************************************************
683 Convert a uid into a user name.
684 ********************************************************************/
686 const char *uidtoname(uid_t uid)
688 TALLOC_CTX *ctx = talloc_tos();
689 char *name = NULL;
690 struct passwd *pass = NULL;
692 pass = getpwuid_alloc(ctx,uid);
693 if (pass) {
694 name = talloc_strdup(ctx,pass->pw_name);
695 TALLOC_FREE(pass);
696 } else {
697 name = talloc_asprintf(ctx,
698 "%ld",
699 (long int)uid);
701 return name;
704 /*******************************************************************
705 Convert a gid into a group name.
706 ********************************************************************/
708 char *gidtoname(gid_t gid)
710 struct group *grp;
712 grp = getgrgid(gid);
713 if (grp) {
714 return talloc_strdup(talloc_tos(), grp->gr_name);
716 else {
717 return talloc_asprintf(talloc_tos(),
718 "%d",
719 (int)gid);
723 /*******************************************************************
724 Convert a user name into a uid.
725 ********************************************************************/
727 uid_t nametouid(const char *name)
729 struct passwd *pass;
730 char *p;
731 uid_t u;
733 pass = Get_Pwnam_alloc(talloc_tos(), name);
734 if (pass) {
735 u = pass->pw_uid;
736 TALLOC_FREE(pass);
737 return u;
740 u = (uid_t)strtol(name, &p, 0);
741 if ((p != name) && (*p == '\0'))
742 return u;
744 return (uid_t)-1;
747 /*******************************************************************
748 Convert a name to a gid_t if possible. Return -1 if not a group.
749 ********************************************************************/
751 gid_t nametogid(const char *name)
753 struct group *grp;
754 char *p;
755 gid_t g;
757 g = (gid_t)strtol(name, &p, 0);
758 if ((p != name) && (*p == '\0'))
759 return g;
761 grp = sys_getgrnam(name);
762 if (grp)
763 return(grp->gr_gid);
764 return (gid_t)-1;
767 /*******************************************************************
768 Something really nasty happened - panic !
769 ********************************************************************/
771 void smb_panic_s3(const char *why)
773 char *cmd;
774 int result;
776 DEBUG(0,("PANIC (pid %llu): %s\n",
777 (unsigned long long)sys_getpid(), why));
778 log_stack_trace();
780 cmd = lp_panic_action();
781 if (cmd && *cmd) {
782 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
783 result = system(cmd);
785 if (result == -1)
786 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
787 strerror(errno)));
788 else
789 DEBUG(0, ("smb_panic(): action returned status %d\n",
790 WEXITSTATUS(result)));
793 dump_core();
796 /*******************************************************************
797 Print a backtrace of the stack to the debug log. This function
798 DELIBERATELY LEAKS MEMORY. The expectation is that you should
799 exit shortly after calling it.
800 ********************************************************************/
802 #ifdef HAVE_LIBUNWIND_H
803 #include <libunwind.h>
804 #endif
806 #ifdef HAVE_EXECINFO_H
807 #include <execinfo.h>
808 #endif
810 #ifdef HAVE_LIBEXC_H
811 #include <libexc.h>
812 #endif
814 void log_stack_trace(void)
816 #ifdef HAVE_LIBUNWIND
817 /* Try to use libunwind before any other technique since on ia64
818 * libunwind correctly walks the stack in more circumstances than
819 * backtrace.
821 unw_cursor_t cursor;
822 unw_context_t uc;
823 unsigned i = 0;
825 char procname[256];
826 unw_word_t ip, sp, off;
828 procname[sizeof(procname) - 1] = '\0';
830 if (unw_getcontext(&uc) != 0) {
831 goto libunwind_failed;
834 if (unw_init_local(&cursor, &uc) != 0) {
835 goto libunwind_failed;
838 DEBUG(0, ("BACKTRACE:\n"));
840 do {
841 ip = sp = 0;
842 unw_get_reg(&cursor, UNW_REG_IP, &ip);
843 unw_get_reg(&cursor, UNW_REG_SP, &sp);
845 switch (unw_get_proc_name(&cursor,
846 procname, sizeof(procname) - 1, &off) ) {
847 case 0:
848 /* Name found. */
849 case -UNW_ENOMEM:
850 /* Name truncated. */
851 DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
852 i, procname, (long long)off,
853 (long long)ip, (long long) sp));
854 break;
855 default:
856 /* case -UNW_ENOINFO: */
857 /* case -UNW_EUNSPEC: */
858 /* No symbol name found. */
859 DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
860 i, "<unknown symbol>",
861 (long long)ip, (long long) sp));
863 ++i;
864 } while (unw_step(&cursor) > 0);
866 return;
868 libunwind_failed:
869 DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
871 #elif HAVE_BACKTRACE_SYMBOLS
872 void *backtrace_stack[BACKTRACE_STACK_SIZE];
873 size_t backtrace_size;
874 char **backtrace_strings;
876 /* get the backtrace (stack frames) */
877 backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
878 backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
880 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
881 (unsigned long)backtrace_size));
883 if (backtrace_strings) {
884 int i;
886 for (i = 0; i < backtrace_size; i++)
887 DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
889 /* Leak the backtrace_strings, rather than risk what free() might do */
892 #elif HAVE_LIBEXC
894 /* The IRIX libexc library provides an API for unwinding the stack. See
895 * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
896 * since we are about to abort anyway, it hardly matters.
899 #define NAMESIZE 32 /* Arbitrary */
901 __uint64_t addrs[BACKTRACE_STACK_SIZE];
902 char * names[BACKTRACE_STACK_SIZE];
903 char namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
905 int i;
906 int levels;
908 ZERO_ARRAY(addrs);
909 ZERO_ARRAY(names);
910 ZERO_ARRAY(namebuf);
912 /* We need to be root so we can open our /proc entry to walk
913 * our stack. It also helps when we want to dump core.
915 become_root();
917 for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
918 names[i] = namebuf + (i * NAMESIZE);
921 levels = trace_back_stack(0, addrs, names,
922 BACKTRACE_STACK_SIZE, NAMESIZE - 1);
924 DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
925 for (i = 0; i < levels; i++) {
926 DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
928 #undef NAMESIZE
930 #else
931 DEBUG(0, ("unable to produce a stack trace on this platform\n"));
932 #endif
935 /*******************************************************************
936 A readdir wrapper which just returns the file name.
937 ********************************************************************/
939 const char *readdirname(SMB_STRUCT_DIR *p)
941 SMB_STRUCT_DIRENT *ptr;
942 char *dname;
944 if (!p)
945 return(NULL);
947 ptr = (SMB_STRUCT_DIRENT *)sys_readdir(p);
948 if (!ptr)
949 return(NULL);
951 dname = ptr->d_name;
953 #ifdef NEXT2
954 if (telldir(p) < 0)
955 return(NULL);
956 #endif
958 #ifdef HAVE_BROKEN_READDIR_NAME
959 /* using /usr/ucb/cc is BAD */
960 dname = dname - 2;
961 #endif
963 return talloc_strdup(talloc_tos(), dname);
966 /*******************************************************************
967 Utility function used to decide if the last component
968 of a path matches a (possibly wildcarded) entry in a namelist.
969 ********************************************************************/
971 bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensitive)
973 const char *last_component;
975 /* if we have no list it's obviously not in the path */
976 if((namelist == NULL ) || ((namelist != NULL) && (namelist[0].name == NULL))) {
977 return False;
980 DEBUG(8, ("is_in_path: %s\n", name));
982 /* Get the last component of the unix name. */
983 last_component = strrchr_m(name, '/');
984 if (!last_component) {
985 last_component = name;
986 } else {
987 last_component++; /* Go past '/' */
990 for(; namelist->name != NULL; namelist++) {
991 if(namelist->is_wild) {
992 if (mask_match(last_component, namelist->name, case_sensitive)) {
993 DEBUG(8,("is_in_path: mask match succeeded\n"));
994 return True;
996 } else {
997 if((case_sensitive && (strcmp(last_component, namelist->name) == 0))||
998 (!case_sensitive && (strcasecmp_m(last_component, namelist->name) == 0))) {
999 DEBUG(8,("is_in_path: match succeeded\n"));
1000 return True;
1004 DEBUG(8,("is_in_path: match not found\n"));
1005 return False;
1008 /*******************************************************************
1009 Strip a '/' separated list into an array of
1010 name_compare_enties structures suitable for
1011 passing to is_in_path(). We do this for
1012 speed so we can pre-parse all the names in the list
1013 and don't do it for each call to is_in_path().
1014 We also check if the entry contains a wildcard to
1015 remove a potentially expensive call to mask_match
1016 if possible.
1017 ********************************************************************/
1019 void set_namearray(name_compare_entry **ppname_array, const char *namelist_in)
1021 char *name_end;
1022 char *namelist;
1023 char *nameptr;
1024 int num_entries = 0;
1025 int i;
1027 (*ppname_array) = NULL;
1029 if((namelist_in == NULL ) || ((namelist_in != NULL) && (*namelist_in == '\0')))
1030 return;
1032 namelist = talloc_strdup(talloc_tos(), namelist_in);
1033 if (namelist == NULL) {
1034 DEBUG(0,("set_namearray: talloc fail\n"));
1035 return;
1037 nameptr = namelist;
1039 /* We need to make two passes over the string. The
1040 first to count the number of elements, the second
1041 to split it.
1044 while(*nameptr) {
1045 if ( *nameptr == '/' ) {
1046 /* cope with multiple (useless) /s) */
1047 nameptr++;
1048 continue;
1050 /* anything left? */
1051 if ( *nameptr == '\0' )
1052 break;
1054 /* find the next '/' or consume remaining */
1055 name_end = strchr_m(nameptr, '/');
1056 if (name_end == NULL)
1057 name_end = (char *)nameptr + strlen(nameptr);
1059 /* next segment please */
1060 nameptr = name_end + 1;
1061 num_entries++;
1064 if(num_entries == 0) {
1065 talloc_free(namelist);
1066 return;
1069 if(( (*ppname_array) = SMB_MALLOC_ARRAY(name_compare_entry, num_entries + 1)) == NULL) {
1070 DEBUG(0,("set_namearray: malloc fail\n"));
1071 talloc_free(namelist);
1072 return;
1075 /* Now copy out the names */
1076 nameptr = namelist;
1077 i = 0;
1078 while(*nameptr) {
1079 if ( *nameptr == '/' ) {
1080 /* cope with multiple (useless) /s) */
1081 nameptr++;
1082 continue;
1084 /* anything left? */
1085 if ( *nameptr == '\0' )
1086 break;
1088 /* find the next '/' or consume remaining */
1089 name_end = strchr_m(nameptr, '/');
1090 if (name_end)
1091 *name_end = '\0';
1092 else
1093 name_end = nameptr + strlen(nameptr);
1095 (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
1096 if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
1097 DEBUG(0,("set_namearray: malloc fail (1)\n"));
1098 talloc_free(namelist);
1099 return;
1102 /* next segment please */
1103 nameptr = name_end + 1;
1104 i++;
1107 (*ppname_array)[i].name = NULL;
1109 talloc_free(namelist);
1110 return;
1113 #undef DBGC_CLASS
1114 #define DBGC_CLASS DBGC_LOCKING
1116 /****************************************************************************
1117 Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
1118 is dealt with in posix.c
1119 Returns True if we have information regarding this lock region (and returns
1120 F_UNLCK in *ptype if the region is unlocked). False if the call failed.
1121 ****************************************************************************/
1123 bool fcntl_getlock(int fd, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
1125 SMB_STRUCT_FLOCK lock;
1126 int ret;
1128 DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
1129 fd,(double)*poffset,(double)*pcount,*ptype));
1131 lock.l_type = *ptype;
1132 lock.l_whence = SEEK_SET;
1133 lock.l_start = *poffset;
1134 lock.l_len = *pcount;
1135 lock.l_pid = 0;
1137 ret = sys_fcntl_ptr(fd,SMB_F_GETLK,&lock);
1139 if (ret == -1) {
1140 int sav = errno;
1141 DEBUG(3,("fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s)\n",
1142 (double)*poffset,(double)*pcount,*ptype,strerror(errno)));
1143 errno = sav;
1144 return False;
1147 *ptype = lock.l_type;
1148 *poffset = lock.l_start;
1149 *pcount = lock.l_len;
1150 *ppid = lock.l_pid;
1152 DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
1153 fd, (int)lock.l_type, (unsigned int)lock.l_pid));
1154 return True;
1157 #undef DBGC_CLASS
1158 #define DBGC_CLASS DBGC_ALL
1160 /*******************************************************************
1161 Is the name specified one of my netbios names.
1162 Returns true if it is equal, false otherwise.
1163 ********************************************************************/
1165 bool is_myname(const char *s)
1167 int n;
1168 bool ret = False;
1170 for (n=0; my_netbios_names(n); n++) {
1171 if (strequal(my_netbios_names(n), s)) {
1172 ret=True;
1173 break;
1176 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s, ret));
1177 return(ret);
1180 /*******************************************************************
1181 Is the name specified our workgroup/domain.
1182 Returns true if it is equal, false otherwise.
1183 ********************************************************************/
1185 bool is_myworkgroup(const char *s)
1187 bool ret = False;
1189 if (strequal(s, lp_workgroup())) {
1190 ret=True;
1193 DEBUG(8, ("is_myworkgroup(\"%s\") returns %d\n", s, ret));
1194 return(ret);
1197 /*******************************************************************
1198 we distinguish between 2K and XP by the "Native Lan Manager" string
1199 WinXP => "Windows 2002 5.1"
1200 WinXP 64bit => "Windows XP 5.2"
1201 Win2k => "Windows 2000 5.0"
1202 NT4 => "Windows NT 4.0"
1203 Win9x => "Windows 4.0"
1204 Windows 2003 doesn't set the native lan manager string but
1205 they do set the domain to "Windows 2003 5.2" (probably a bug).
1206 ********************************************************************/
1208 void ra_lanman_string( const char *native_lanman )
1210 if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
1211 set_remote_arch( RA_WINXP );
1212 else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
1213 set_remote_arch( RA_WINXP64 );
1214 else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
1215 set_remote_arch( RA_WIN2K3 );
1218 static const char *remote_arch_str;
1220 const char *get_remote_arch_str(void)
1222 if (!remote_arch_str) {
1223 return "UNKNOWN";
1225 return remote_arch_str;
1228 /*******************************************************************
1229 Set the horrid remote_arch string based on an enum.
1230 ********************************************************************/
1232 void set_remote_arch(enum remote_arch_types type)
1234 ra_type = type;
1235 switch( type ) {
1236 case RA_WFWG:
1237 remote_arch_str = "WfWg";
1238 break;
1239 case RA_OS2:
1240 remote_arch_str = "OS2";
1241 break;
1242 case RA_WIN95:
1243 remote_arch_str = "Win95";
1244 break;
1245 case RA_WINNT:
1246 remote_arch_str = "WinNT";
1247 break;
1248 case RA_WIN2K:
1249 remote_arch_str = "Win2K";
1250 break;
1251 case RA_WINXP:
1252 remote_arch_str = "WinXP";
1253 break;
1254 case RA_WINXP64:
1255 remote_arch_str = "WinXP64";
1256 break;
1257 case RA_WIN2K3:
1258 remote_arch_str = "Win2K3";
1259 break;
1260 case RA_VISTA:
1261 remote_arch_str = "Vista";
1262 break;
1263 case RA_SAMBA:
1264 remote_arch_str = "Samba";
1265 break;
1266 case RA_CIFSFS:
1267 remote_arch_str = "CIFSFS";
1268 break;
1269 case RA_OSX:
1270 remote_arch_str = "OSX";
1271 break;
1272 default:
1273 ra_type = RA_UNKNOWN;
1274 remote_arch_str = "UNKNOWN";
1275 break;
1278 DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
1279 remote_arch_str));
1282 /*******************************************************************
1283 Get the remote_arch type.
1284 ********************************************************************/
1286 enum remote_arch_types get_remote_arch(void)
1288 return ra_type;
1291 const char *tab_depth(int level, int depth)
1293 if( CHECK_DEBUGLVL(level) ) {
1294 dbgtext("%*s", depth*4, "");
1296 return "";
1299 /*****************************************************************************
1300 Provide a checksum on a string
1302 Input: s - the null-terminated character string for which the checksum
1303 will be calculated.
1305 Output: The checksum value calculated for s.
1306 *****************************************************************************/
1308 int str_checksum(const char *s)
1310 if (s == NULL)
1311 return 0;
1312 return hash(s, strlen(s), 0);
1315 /*****************************************************************
1316 Zero a memory area then free it. Used to catch bugs faster.
1317 *****************************************************************/
1319 void zero_free(void *p, size_t size)
1321 memset(p, 0, size);
1322 SAFE_FREE(p);
1325 /*****************************************************************
1326 Set our open file limit to a requested max and return the limit.
1327 *****************************************************************/
1329 int set_maxfiles(int requested_max)
1331 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1332 struct rlimit rlp;
1333 int saved_current_limit;
1335 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1336 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1337 strerror(errno) ));
1338 /* just guess... */
1339 return requested_max;
1343 * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1344 * account for the extra fd we need
1345 * as well as the log files and standard
1346 * handles etc. Save the limit we want to set in case
1347 * we are running on an OS that doesn't support this limit (AIX)
1348 * which always returns RLIM_INFINITY for rlp.rlim_max.
1351 /* Try raising the hard (max) limit to the requested amount. */
1353 #if defined(RLIM_INFINITY)
1354 if (rlp.rlim_max != RLIM_INFINITY) {
1355 int orig_max = rlp.rlim_max;
1357 if ( rlp.rlim_max < requested_max )
1358 rlp.rlim_max = requested_max;
1360 /* This failing is not an error - many systems (Linux) don't
1361 support our default request of 10,000 open files. JRA. */
1363 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1364 DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n",
1365 (int)rlp.rlim_max, strerror(errno) ));
1367 /* Set failed - restore original value from get. */
1368 rlp.rlim_max = orig_max;
1371 #endif
1373 /* Now try setting the soft (current) limit. */
1375 saved_current_limit = rlp.rlim_cur = MIN(requested_max,rlp.rlim_max);
1377 if(setrlimit(RLIMIT_NOFILE, &rlp)) {
1378 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n",
1379 (int)rlp.rlim_cur, strerror(errno) ));
1380 /* just guess... */
1381 return saved_current_limit;
1384 if(getrlimit(RLIMIT_NOFILE, &rlp)) {
1385 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1386 strerror(errno) ));
1387 /* just guess... */
1388 return saved_current_limit;
1391 #if defined(RLIM_INFINITY)
1392 if(rlp.rlim_cur == RLIM_INFINITY)
1393 return saved_current_limit;
1394 #endif
1396 if((int)rlp.rlim_cur > saved_current_limit)
1397 return saved_current_limit;
1399 return rlp.rlim_cur;
1400 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1402 * No way to know - just guess...
1404 return requested_max;
1405 #endif
1408 /*****************************************************************
1409 malloc that aborts with smb_panic on fail or zero size.
1410 *****************************************************************/
1412 void *smb_xmalloc_array(size_t size, unsigned int count)
1414 void *p;
1415 if (size == 0) {
1416 smb_panic("smb_xmalloc_array: called with zero size");
1418 if (count >= MAX_ALLOC_SIZE/size) {
1419 smb_panic("smb_xmalloc_array: alloc size too large");
1421 if ((p = SMB_MALLOC(size*count)) == NULL) {
1422 DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
1423 (unsigned long)size, (unsigned long)count));
1424 smb_panic("smb_xmalloc_array: malloc failed");
1426 return p;
1430 vasprintf that aborts on malloc fail
1433 int smb_xvasprintf(char **ptr, const char *format, va_list ap)
1435 int n;
1436 va_list ap2;
1438 va_copy(ap2, ap);
1440 n = vasprintf(ptr, format, ap2);
1441 va_end(ap2);
1442 if (n == -1 || ! *ptr) {
1443 smb_panic("smb_xvasprintf: out of memory");
1445 return n;
1448 /*****************************************************************
1449 Get local hostname and cache result.
1450 *****************************************************************/
1452 char *myhostname(void)
1454 static char *ret;
1455 if (ret == NULL) {
1456 ret = get_myname(NULL);
1458 return ret;
1461 /*****************************************************************
1462 Get local hostname and cache result.
1463 *****************************************************************/
1465 char *myhostname_upper(void)
1467 char *name;
1468 static char *ret;
1469 if (ret == NULL) {
1470 name = get_myname(talloc_tos());
1471 ret = strupper_talloc(NULL, name);
1472 talloc_free(name);
1474 return ret;
1478 * @brief Returns an absolute path to a file concatenating the provided
1479 * @a rootpath and @a basename
1481 * @param name Filename, relative to @a rootpath
1483 * @retval Pointer to a string containing the full path.
1486 static char *xx_path(const char *name, const char *rootpath)
1488 char *fname = NULL;
1490 fname = talloc_strdup(talloc_tos(), rootpath);
1491 if (!fname) {
1492 return NULL;
1494 trim_string(fname,"","/");
1496 if (!directory_exist(fname)) {
1497 if (!mkdir(fname,0755))
1498 DEBUG(1, ("Unable to create directory %s for file %s. "
1499 "Error was %s\n", fname, name, strerror(errno)));
1502 return talloc_asprintf(talloc_tos(),
1503 "%s/%s",
1504 fname,
1505 name);
1509 * @brief Returns an absolute path to a file in the Samba lock directory.
1511 * @param name File to find, relative to LOCKDIR.
1513 * @retval Pointer to a talloc'ed string containing the full path.
1516 char *lock_path(const char *name)
1518 return xx_path(name, lp_lockdir());
1522 * @brief Returns an absolute path to a file in the Samba pid directory.
1524 * @param name File to find, relative to PIDDIR.
1526 * @retval Pointer to a talloc'ed string containing the full path.
1529 char *pid_path(const char *name)
1531 return xx_path(name, lp_piddir());
1535 * @brief Returns an absolute path to a file in the Samba state directory.
1537 * @param name File to find, relative to STATEDIR.
1539 * @retval Pointer to a talloc'ed string containing the full path.
1542 char *state_path(const char *name)
1544 return xx_path(name, lp_statedir());
1548 * @brief Returns an absolute path to a file in the Samba cache directory.
1550 * @param name File to find, relative to CACHEDIR.
1552 * @retval Pointer to a talloc'ed string containing the full path.
1555 char *cache_path(const char *name)
1557 return xx_path(name, lp_cachedir());
1560 /*******************************************************************
1561 Given a filename - get its directory name
1562 ********************************************************************/
1564 bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
1565 const char **name)
1567 char *p;
1568 ptrdiff_t len;
1570 p = strrchr_m(dir, '/'); /* Find final '/', if any */
1572 if (p == NULL) {
1573 if (!(*parent = talloc_strdup(mem_ctx, "."))) {
1574 return False;
1576 if (name) {
1577 *name = dir;
1579 return True;
1582 len = p-dir;
1584 if (!(*parent = (char *)talloc_memdup(mem_ctx, dir, len+1))) {
1585 return False;
1587 (*parent)[len] = '\0';
1589 if (name) {
1590 *name = p+1;
1592 return True;
1595 /*******************************************************************
1596 Determine if a pattern contains any Microsoft wildcard characters.
1597 *******************************************************************/
1599 bool ms_has_wild(const char *s)
1601 char c;
1603 if (lp_posix_pathnames()) {
1604 /* With posix pathnames no characters are wild. */
1605 return False;
1608 while ((c = *s++)) {
1609 switch (c) {
1610 case '*':
1611 case '?':
1612 case '<':
1613 case '>':
1614 case '"':
1615 return True;
1618 return False;
1621 bool ms_has_wild_w(const smb_ucs2_t *s)
1623 smb_ucs2_t c;
1624 if (!s) return False;
1625 while ((c = *s++)) {
1626 switch (c) {
1627 case UCS2_CHAR('*'):
1628 case UCS2_CHAR('?'):
1629 case UCS2_CHAR('<'):
1630 case UCS2_CHAR('>'):
1631 case UCS2_CHAR('"'):
1632 return True;
1635 return False;
1638 /*******************************************************************
1639 A wrapper that handles case sensitivity and the special handling
1640 of the ".." name.
1641 *******************************************************************/
1643 bool mask_match(const char *string, const char *pattern, bool is_case_sensitive)
1645 if (ISDOTDOT(string))
1646 string = ".";
1647 if (ISDOT(pattern))
1648 return False;
1650 return ms_fnmatch(pattern, string, Protocol <= PROTOCOL_LANMAN2, is_case_sensitive) == 0;
1653 /*******************************************************************
1654 A wrapper that handles case sensitivity and the special handling
1655 of the ".." name. Varient that is only called by old search code which requires
1656 pattern translation.
1657 *******************************************************************/
1659 bool mask_match_search(const char *string, const char *pattern, bool is_case_sensitive)
1661 if (ISDOTDOT(string))
1662 string = ".";
1663 if (ISDOT(pattern))
1664 return False;
1666 return ms_fnmatch(pattern, string, True, is_case_sensitive) == 0;
1669 /*******************************************************************
1670 A wrapper that handles a list of patters and calls mask_match()
1671 on each. Returns True if any of the patterns match.
1672 *******************************************************************/
1674 bool mask_match_list(const char *string, char **list, int listLen, bool is_case_sensitive)
1676 while (listLen-- > 0) {
1677 if (mask_match(string, *list++, is_case_sensitive))
1678 return True;
1680 return False;
1683 /*********************************************************
1684 Recursive routine that is called by unix_wild_match.
1685 *********************************************************/
1687 static bool unix_do_match(const char *regexp, const char *str)
1689 const char *p;
1691 for( p = regexp; *p && *str; ) {
1693 switch(*p) {
1694 case '?':
1695 str++;
1696 p++;
1697 break;
1699 case '*':
1702 * Look for a character matching
1703 * the one after the '*'.
1705 p++;
1706 if(!*p)
1707 return true; /* Automatic match */
1708 while(*str) {
1710 while(*str && (*p != *str))
1711 str++;
1714 * Patch from weidel@multichart.de. In the case of the regexp
1715 * '*XX*' we want to ensure there are at least 2 'X' characters
1716 * in the string after the '*' for a match to be made.
1720 int matchcount=0;
1723 * Eat all the characters that match, but count how many there were.
1726 while(*str && (*p == *str)) {
1727 str++;
1728 matchcount++;
1732 * Now check that if the regexp had n identical characters that
1733 * matchcount had at least that many matches.
1736 while ( *(p+1) && (*(p+1) == *p)) {
1737 p++;
1738 matchcount--;
1741 if ( matchcount <= 0 )
1742 return false;
1745 str--; /* We've eaten the match char after the '*' */
1747 if(unix_do_match(p, str))
1748 return true;
1750 if(!*str)
1751 return false;
1752 else
1753 str++;
1755 return false;
1757 default:
1758 if(*str != *p)
1759 return false;
1760 str++;
1761 p++;
1762 break;
1766 if(!*p && !*str)
1767 return true;
1769 if (!*p && str[0] == '.' && str[1] == 0)
1770 return true;
1772 if (!*str && *p == '?') {
1773 while (*p == '?')
1774 p++;
1775 return(!*p);
1778 if(!*str && (*p == '*' && p[1] == '\0'))
1779 return true;
1781 return false;
1784 /*******************************************************************
1785 Simple case insensitive interface to a UNIX wildcard matcher.
1786 Returns True if match, False if not.
1787 *******************************************************************/
1789 bool unix_wild_match(const char *pattern, const char *string)
1791 TALLOC_CTX *ctx = talloc_stackframe();
1792 char *p2;
1793 char *s2;
1794 char *p;
1795 bool ret = false;
1797 p2 = talloc_strdup(ctx,pattern);
1798 s2 = talloc_strdup(ctx,string);
1799 if (!p2 || !s2) {
1800 TALLOC_FREE(ctx);
1801 return false;
1803 strlower_m(p2);
1804 strlower_m(s2);
1806 /* Remove any *? and ** from the pattern as they are meaningless */
1807 for(p = p2; *p; p++) {
1808 while( *p == '*' && (p[1] == '?' ||p[1] == '*')) {
1809 memmove(&p[1], &p[2], strlen(&p[2])+1);
1813 if (strequal(p2,"*")) {
1814 TALLOC_FREE(ctx);
1815 return true;
1818 ret = unix_do_match(p2, s2);
1819 TALLOC_FREE(ctx);
1820 return ret;
1823 /**********************************************************************
1824 Converts a name to a fully qualified domain name.
1825 Returns true if lookup succeeded, false if not (then fqdn is set to name)
1826 Note we deliberately use gethostbyname here, not getaddrinfo as we want
1827 to examine the h_aliases and I don't know how to do that with getaddrinfo.
1828 ***********************************************************************/
1830 bool name_to_fqdn(fstring fqdn, const char *name)
1832 char *full = NULL;
1833 struct hostent *hp = gethostbyname(name);
1835 if (!hp || !hp->h_name || !*hp->h_name) {
1836 DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
1837 fstrcpy(fqdn, name);
1838 return false;
1841 /* Find out if the fqdn is returned as an alias
1842 * to cope with /etc/hosts files where the first
1843 * name is not the fqdn but the short name */
1844 if (hp->h_aliases && (! strchr_m(hp->h_name, '.'))) {
1845 int i;
1846 for (i = 0; hp->h_aliases[i]; i++) {
1847 if (strchr_m(hp->h_aliases[i], '.')) {
1848 full = hp->h_aliases[i];
1849 break;
1853 if (full && (strcasecmp_m(full, "localhost.localdomain") == 0)) {
1854 DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
1855 DEBUGADD(1, (" Specifing the machine hostname for address 127.0.0.1 may lead\n"));
1856 DEBUGADD(1, (" to Kerberos authentication problems as localhost.localdomain\n"));
1857 DEBUGADD(1, (" may end up being used instead of the real machine FQDN.\n"));
1858 full = hp->h_name;
1860 if (!full) {
1861 full = hp->h_name;
1864 DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, full));
1865 fstrcpy(fqdn, full);
1866 return true;
1869 /**********************************************************************
1870 Append a DATA_BLOB to a talloc'ed object
1871 ***********************************************************************/
1873 void *talloc_append_blob(TALLOC_CTX *mem_ctx, void *buf, DATA_BLOB blob)
1875 size_t old_size = 0;
1876 char *result;
1878 if (blob.length == 0) {
1879 return buf;
1882 if (buf != NULL) {
1883 old_size = talloc_get_size(buf);
1886 result = (char *)TALLOC_REALLOC(mem_ctx, buf, old_size + blob.length);
1887 if (result == NULL) {
1888 return NULL;
1891 memcpy(result + old_size, blob.data, blob.length);
1892 return result;
1895 uint32 map_share_mode_to_deny_mode(uint32 share_access, uint32 private_options)
1897 switch (share_access & ~FILE_SHARE_DELETE) {
1898 case FILE_SHARE_NONE:
1899 return DENY_ALL;
1900 case FILE_SHARE_READ:
1901 return DENY_WRITE;
1902 case FILE_SHARE_WRITE:
1903 return DENY_READ;
1904 case FILE_SHARE_READ|FILE_SHARE_WRITE:
1905 return DENY_NONE;
1907 if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) {
1908 return DENY_DOS;
1909 } else if (private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_FCB) {
1910 return DENY_FCB;
1913 return (uint32)-1;
1916 pid_t procid_to_pid(const struct server_id *proc)
1918 return proc->pid;
1921 static uint32 my_vnn = NONCLUSTER_VNN;
1923 void set_my_vnn(uint32 vnn)
1925 DEBUG(10, ("vnn pid %d = %u\n", (int)sys_getpid(), (unsigned int)vnn));
1926 my_vnn = vnn;
1929 uint32 get_my_vnn(void)
1931 return my_vnn;
1934 static uint64_t my_unique_id = 0;
1936 void set_my_unique_id(uint64_t unique_id)
1938 my_unique_id = unique_id;
1941 struct server_id pid_to_procid(pid_t pid)
1943 struct server_id result;
1944 result.pid = pid;
1945 result.task_id = 0;
1946 result.unique_id = my_unique_id;
1947 result.vnn = my_vnn;
1948 return result;
1951 struct server_id procid_self(void)
1953 return pid_to_procid(sys_getpid());
1956 bool procid_equal(const struct server_id *p1, const struct server_id *p2)
1958 if (p1->pid != p2->pid)
1959 return False;
1960 if (p1->task_id != p2->task_id)
1961 return False;
1962 if (p1->vnn != p2->vnn)
1963 return False;
1964 return True;
1967 bool cluster_id_equal(const struct server_id *id1,
1968 const struct server_id *id2)
1970 return procid_equal(id1, id2);
1973 bool procid_is_me(const struct server_id *pid)
1975 if (pid->pid != sys_getpid())
1976 return False;
1977 if (pid->task_id != 0)
1978 return False;
1979 if (pid->vnn != my_vnn)
1980 return False;
1981 return True;
1984 struct server_id interpret_pid(const char *pid_string)
1986 struct server_id result;
1987 unsigned long long pid;
1988 unsigned int vnn, task_id = 0;
1990 ZERO_STRUCT(result);
1992 /* We accept various forms with 1, 2 or 3 component forms
1993 * because the server_id_str() can print different forms, and
1994 * we want backwards compatibility for scripts that may call
1995 * smbclient. */
1996 if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
1997 result.vnn = vnn;
1998 result.pid = pid;
1999 result.task_id = task_id;
2000 } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
2001 result.vnn = vnn;
2002 result.pid = pid;
2003 result.task_id = 0;
2004 } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
2005 result.vnn = get_my_vnn();
2006 result.pid = pid;
2007 result.task_id = task_id;
2008 } else if (sscanf(pid_string, "%llu", &pid) == 1) {
2009 result.vnn = get_my_vnn();
2010 result.pid = pid;
2011 } else {
2012 result.vnn = NONCLUSTER_VNN;
2013 result.pid = (uint64_t)-1;
2015 return result;
2018 char *procid_str_static(const struct server_id *pid)
2020 return server_id_str(talloc_tos(), pid);
2023 bool procid_valid(const struct server_id *pid)
2025 return (pid->pid != (uint64_t)-1);
2028 bool procid_is_local(const struct server_id *pid)
2030 return pid->vnn == my_vnn;
2033 /****************************************************************
2034 Check if offset/length fit into bufsize. Should probably be
2035 merged with is_offset_safe, but this would require a rewrite
2036 of lanman.c. Later :-)
2037 ****************************************************************/
2039 bool trans_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
2041 if ((offset + length < offset) || (offset + length < length)) {
2042 /* wrap */
2043 return true;
2045 if ((offset > bufsize) || (offset + length > bufsize)) {
2046 /* overflow */
2047 return true;
2049 return false;
2052 /****************************************************************
2053 Check if an offset into a buffer is safe.
2054 If this returns True it's safe to indirect into the byte at
2055 pointer ptr+off.
2056 ****************************************************************/
2058 bool is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2060 const char *end_base = buf_base + buf_len;
2061 char *end_ptr = ptr + off;
2063 if (!buf_base || !ptr) {
2064 return False;
2067 if (end_base < buf_base || end_ptr < ptr) {
2068 return False; /* wrap. */
2071 if (end_ptr < end_base) {
2072 return True;
2074 return False;
2077 /****************************************************************
2078 Return a safe pointer into a buffer, or NULL.
2079 ****************************************************************/
2081 char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2083 return is_offset_safe(buf_base, buf_len, ptr, off) ?
2084 ptr + off : NULL;
2087 /****************************************************************
2088 Return a safe pointer into a string within a buffer, or NULL.
2089 ****************************************************************/
2091 char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
2093 if (!is_offset_safe(buf_base, buf_len, ptr, off)) {
2094 return NULL;
2096 /* Check if a valid string exists at this offset. */
2097 if (skip_string(buf_base,buf_len, ptr + off) == NULL) {
2098 return NULL;
2100 return ptr + off;
2103 /****************************************************************
2104 Return an SVAL at a pointer, or failval if beyond the end.
2105 ****************************************************************/
2107 int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
2110 * Note we use off+1 here, not off+2 as SVAL accesses ptr[0] and ptr[1],
2111 * NOT ptr[2].
2113 if (!is_offset_safe(buf_base, buf_len, ptr, off+1)) {
2114 return failval;
2116 return SVAL(ptr,off);
2119 /****************************************************************
2120 Return an IVAL at a pointer, or failval if beyond the end.
2121 ****************************************************************/
2123 int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
2126 * Note we use off+3 here, not off+4 as IVAL accesses
2127 * ptr[0] ptr[1] ptr[2] ptr[3] NOT ptr[4].
2129 if (!is_offset_safe(buf_base, buf_len, ptr, off+3)) {
2130 return failval;
2132 return IVAL(ptr,off);
2135 /****************************************************************
2136 Split DOM\user into DOM and user. Do not mix with winbind variants of that
2137 call (they take care of winbind separator and other winbind specific settings).
2138 ****************************************************************/
2140 void split_domain_user(TALLOC_CTX *mem_ctx,
2141 const char *full_name,
2142 char **domain,
2143 char **user)
2145 const char *p = NULL;
2147 p = strchr_m(full_name, '\\');
2149 if (p != NULL) {
2150 *domain = talloc_strndup(mem_ctx, full_name,
2151 PTR_DIFF(p, full_name));
2152 *user = talloc_strdup(mem_ctx, p+1);
2153 } else {
2154 *domain = talloc_strdup(mem_ctx, "");
2155 *user = talloc_strdup(mem_ctx, full_name);
2159 /****************************************************************
2160 strip off leading '\\' from a hostname
2161 ****************************************************************/
2163 const char *strip_hostname(const char *s)
2165 if (!s) {
2166 return NULL;
2169 if (strlen_m(s) < 3) {
2170 return s;
2173 if (s[0] == '\\') s++;
2174 if (s[0] == '\\') s++;
2176 return s;
2179 bool tevent_req_poll_ntstatus(struct tevent_req *req,
2180 struct tevent_context *ev,
2181 NTSTATUS *status)
2183 bool ret = tevent_req_poll(req, ev);
2184 if (!ret) {
2185 *status = map_nt_error_from_unix(errno);
2187 return ret;
2190 bool any_nt_status_not_ok(NTSTATUS err1, NTSTATUS err2, NTSTATUS *result)
2192 if (!NT_STATUS_IS_OK(err1)) {
2193 *result = err1;
2194 return true;
2196 if (!NT_STATUS_IS_OK(err2)) {
2197 *result = err2;
2198 return true;
2200 return false;
2203 int timeval_to_msec(struct timeval t)
2205 return t.tv_sec * 1000 + (t.tv_usec+999) / 1000;
2208 /*******************************************************************
2209 Check a given DOS pathname is valid for a share.
2210 ********************************************************************/
2212 char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname)
2214 char *ptr = NULL;
2216 if (!dos_pathname) {
2217 return NULL;
2220 ptr = talloc_strdup(ctx, dos_pathname);
2221 if (!ptr) {
2222 return NULL;
2224 /* Convert any '\' paths to '/' */
2225 unix_format(ptr);
2226 ptr = unix_clean_name(ctx, ptr);
2227 if (!ptr) {
2228 return NULL;
2231 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
2232 if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/')
2233 ptr += 2;
2235 /* Only absolute paths allowed. */
2236 if (*ptr != '/')
2237 return NULL;
2239 return ptr;