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/>.
25 #include "system/passwd.h"
26 #include "system/filesys.h"
28 #include "ctdbd_conn.h"
29 #include "../lib/util/util_pw.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.
51 #if defined(GROUP_OBJ)
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)
69 void set_Protocol(enum protocol_types p
)
74 static enum remote_arch_types ra_type
= RA_UNKNOWN
;
76 void gfree_all( void )
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
)
96 if (sys_stat(fname
, sbuf
, fake_dir_create_times
) != 0)
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
)
109 if (sys_stat(fname
, &st
, false) != 0)
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
)
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
)
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",
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
));
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
);
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
);
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
;
230 if (!(tmp
= talloc_realloc(NULL
, *outbuf
, uint8
, newlen
))) {
231 DEBUG(0, ("talloc failed\n"));
236 memcpy(tmp
+ smb_len(tmp
) + 4, blob
.data
, blob
.length
);
237 set_message_bcc((char *)tmp
, smb_buflen(tmp
) + blob
.length
);
241 /*******************************************************************
242 Reduce a file name, removing .. elements.
243 ********************************************************************/
245 static char *dos_clean_name(TALLOC_CTX
*ctx
, const char *s
)
250 DEBUG(3,("dos_clean_name [%s]\n",s
));
252 /* remove any double slashes */
253 str
= talloc_all_string_sub(ctx
, s
, "\\\\", "\\");
258 /* Remove leading .\\ characters */
259 if(strncmp(str
, ".\\", 2) == 0) {
260 trim_string(str
, ".\\", NULL
);
262 str
= talloc_strdup(ctx
, ".\\");
269 while ((p
= strstr_m(str
,"\\..\\")) != NULL
) {
275 if ((p
=strrchr_m(str
,'\\')) != NULL
) {
280 str
= talloc_asprintf(ctx
,
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
)
302 DEBUG(3,("unix_clean_name [%s]\n",s
));
304 /* remove any double slashes */
305 str
= talloc_all_string_sub(ctx
, s
, "//","/");
310 /* Remove leading ./ characters */
311 if(strncmp(str
, "./", 2) == 0) {
312 trim_string(str
, "./", NULL
);
314 str
= talloc_strdup(ctx
, "./");
321 while ((p
= strstr_m(str
,"/../")) != NULL
) {
327 if ((p
=strrchr_m(str
,'/')) != NULL
) {
332 str
= talloc_asprintf(ctx
,
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
);
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
)
363 if (pos
== (SMB_OFF_T
)-1) {
364 return write_data(fd
, buffer
, N
);
366 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
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
);
373 DEBUG(0,("write_data_at_offset: write failure. Error = %s\n", strerror(errno
) ));
382 return (ssize_t
)total
;
384 /* Use lseek and write_data. */
385 if (sys_lseek(fd
, pos
, SEEK_SET
) == -1) {
386 if (errno
!= ESPIPE
) {
390 return write_data(fd
, buffer
, N
);
395 NTSTATUS
reinit_after_fork(struct messaging_context
*msg_ctx
,
396 struct event_context
*ev_ctx
,
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
;
415 if (ev_ctx
&& tevent_re_initialise(ev_ctx
) != 0) {
416 smb_panic(__location__
": Failed to re-initialise event context");
421 * For clustering, we need to re-init our ctdbd connection after the
424 status
= messaging_reinit(msg_ctx
, id
);
425 if (!NT_STATUS_IS_OK(status
)) {
426 DEBUG(0,("messaging_reinit() failed: %s\n",
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
,
442 void **array
= (void **)_array
;
444 if (*array_size
< 0) {
448 if (*array
== NULL
) {
449 if (*array_size
== 0) {
453 if (*array_size
>= MAX_ALLOC_SIZE
/element_size
) {
457 *array
= TALLOC(mem_ctx
, element_size
* (*array_size
));
458 if (*array
== NULL
) {
463 if (*num_elements
== *array_size
) {
466 if (*array_size
>= MAX_ALLOC_SIZE
/element_size
) {
470 *array
= TALLOC_REALLOC(mem_ctx
, *array
,
471 element_size
* (*array_size
));
473 if (*array
== NULL
) {
478 memcpy((char *)(*array
) + element_size
*(*num_elements
),
479 element
, element_size
);
489 /****************************************************************************
490 Get my own domain name, or "" if we have none.
491 ****************************************************************************/
493 char *get_mydnsdomname(TALLOC_CTX
*ctx
)
498 domname
= get_mydnsfullname();
503 p
= strchr_m(domname
, '.');
506 return talloc_strdup(ctx
, p
);
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
));
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
)
548 while(*p
&& !isspace(*p
))
550 while(*p
&& isspace(*p
))
553 return talloc_strdup(ctx
, p
);
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
)
571 char *nis_map
= (char *)lp_nis_home_map_name();
573 char buffer
[NIS_MAXATTRVAL
+ 1];
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
)));
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
);
594 nis_freeresult(result
);
597 value
= talloc_string_sub(ctx
,
604 nis_freeresult(result
);
607 value
= strip_mount_options(ctx
, value
);
608 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n",
613 #else /* WITH_NISPLUS_HOME */
615 char *automount_lookup(TALLOC_CTX
*ctx
, const char *user_name
)
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
)));
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
);
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"));
648 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
649 yperr_string(nis_error
), user_name
, nis_map
));
653 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name
, value
));
657 #endif /* WITH_NISPLUS_HOME */
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
)) {
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(),
682 /*******************************************************************
683 Convert a uid into a user name.
684 ********************************************************************/
686 const char *uidtoname(uid_t uid
)
688 TALLOC_CTX
*ctx
= talloc_tos();
690 struct passwd
*pass
= NULL
;
692 pass
= getpwuid_alloc(ctx
,uid
);
694 name
= talloc_strdup(ctx
,pass
->pw_name
);
697 name
= talloc_asprintf(ctx
,
704 /*******************************************************************
705 Convert a gid into a group name.
706 ********************************************************************/
708 char *gidtoname(gid_t gid
)
714 return talloc_strdup(talloc_tos(), grp
->gr_name
);
717 return talloc_asprintf(talloc_tos(),
723 /*******************************************************************
724 Convert a user name into a uid.
725 ********************************************************************/
727 uid_t
nametouid(const char *name
)
733 pass
= Get_Pwnam_alloc(talloc_tos(), name
);
740 u
= (uid_t
)strtol(name
, &p
, 0);
741 if ((p
!= name
) && (*p
== '\0'))
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
)
757 g
= (gid_t
)strtol(name
, &p
, 0);
758 if ((p
!= name
) && (*p
== '\0'))
761 grp
= sys_getgrnam(name
);
767 /*******************************************************************
768 Something really nasty happened - panic !
769 ********************************************************************/
771 void smb_panic_s3(const char *why
)
776 DEBUG(0,("PANIC (pid %llu): %s\n",
777 (unsigned long long)sys_getpid(), why
));
780 cmd
= lp_panic_action();
782 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd
));
783 result
= system(cmd
);
786 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
789 DEBUG(0, ("smb_panic(): action returned status %d\n",
790 WEXITSTATUS(result
)));
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>
806 #ifdef HAVE_EXECINFO_H
807 #include <execinfo.h>
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
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"));
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
) ) {
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
));
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
));
864 } while (unw_step(&cursor
) > 0);
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
) {
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 */
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
];
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.
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
]));
931 DEBUG(0, ("unable to produce a stack trace on this platform\n"));
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
;
947 ptr
= (SMB_STRUCT_DIRENT
*)sys_readdir(p
);
958 #ifdef HAVE_BROKEN_READDIR_NAME
959 /* using /usr/ucb/cc is BAD */
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
))) {
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
;
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"));
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"));
1004 DEBUG(8,("is_in_path: match not found\n"));
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
1017 ********************************************************************/
1019 void set_namearray(name_compare_entry
**ppname_array
, const char *namelist_in
)
1024 int num_entries
= 0;
1027 (*ppname_array
) = NULL
;
1029 if((namelist_in
== NULL
) || ((namelist_in
!= NULL
) && (*namelist_in
== '\0')))
1032 namelist
= talloc_strdup(talloc_tos(), namelist_in
);
1033 if (namelist
== NULL
) {
1034 DEBUG(0,("set_namearray: talloc fail\n"));
1039 /* We need to make two passes over the string. The
1040 first to count the number of elements, the second
1045 if ( *nameptr
== '/' ) {
1046 /* cope with multiple (useless) /s) */
1050 /* anything left? */
1051 if ( *nameptr
== '\0' )
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;
1064 if(num_entries
== 0) {
1065 talloc_free(namelist
);
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
);
1075 /* Now copy out the names */
1079 if ( *nameptr
== '/' ) {
1080 /* cope with multiple (useless) /s) */
1084 /* anything left? */
1085 if ( *nameptr
== '\0' )
1088 /* find the next '/' or consume remaining */
1089 name_end
= strchr_m(nameptr
, '/');
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
);
1102 /* next segment please */
1103 nameptr
= name_end
+ 1;
1107 (*ppname_array
)[i
].name
= NULL
;
1109 talloc_free(namelist
);
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
;
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
;
1137 ret
= sys_fcntl_ptr(fd
,SMB_F_GETLK
,&lock
);
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
)));
1147 *ptype
= lock
.l_type
;
1148 *poffset
= lock
.l_start
;
1149 *pcount
= lock
.l_len
;
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
));
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
)
1170 for (n
=0; my_netbios_names(n
); n
++) {
1171 if (strequal(my_netbios_names(n
), s
)) {
1176 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s
, 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
)
1189 if (strequal(s
, lp_workgroup())) {
1193 DEBUG(8, ("is_myworkgroup(\"%s\") returns %d\n", s
, 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
) {
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
)
1237 remote_arch_str
= "WfWg";
1240 remote_arch_str
= "OS2";
1243 remote_arch_str
= "Win95";
1246 remote_arch_str
= "WinNT";
1249 remote_arch_str
= "Win2K";
1252 remote_arch_str
= "WinXP";
1255 remote_arch_str
= "WinXP64";
1258 remote_arch_str
= "Win2K3";
1261 remote_arch_str
= "Vista";
1264 remote_arch_str
= "Samba";
1267 remote_arch_str
= "CIFSFS";
1270 remote_arch_str
= "OSX";
1273 ra_type
= RA_UNKNOWN
;
1274 remote_arch_str
= "UNKNOWN";
1278 DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
1282 /*******************************************************************
1283 Get the remote_arch type.
1284 ********************************************************************/
1286 enum remote_arch_types
get_remote_arch(void)
1291 const char *tab_depth(int level
, int depth
)
1293 if( CHECK_DEBUGLVL(level
) ) {
1294 dbgtext("%*s", depth
*4, "");
1299 /*****************************************************************************
1300 Provide a checksum on a string
1302 Input: s - the null-terminated character string for which the checksum
1305 Output: The checksum value calculated for s.
1306 *****************************************************************************/
1308 int str_checksum(const char *s
)
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
)
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))
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",
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
;
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
) ));
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",
1388 return saved_current_limit
;
1391 #if defined(RLIM_INFINITY)
1392 if(rlp
.rlim_cur
== RLIM_INFINITY
)
1393 return saved_current_limit
;
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
;
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
)
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");
1430 vasprintf that aborts on malloc fail
1433 int smb_xvasprintf(char **ptr
, const char *format
, va_list ap
)
1440 n
= vasprintf(ptr
, format
, ap2
);
1442 if (n
== -1 || ! *ptr
) {
1443 smb_panic("smb_xvasprintf: out of memory");
1448 /*****************************************************************
1449 Get local hostname and cache result.
1450 *****************************************************************/
1452 char *myhostname(void)
1456 ret
= get_myname(NULL
);
1461 /*****************************************************************
1462 Get local hostname and cache result.
1463 *****************************************************************/
1465 char *myhostname_upper(void)
1470 name
= get_myname(talloc_tos());
1471 ret
= strupper_talloc(NULL
, name
);
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
)
1490 fname
= talloc_strdup(talloc_tos(), rootpath
);
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(),
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
,
1570 p
= strrchr_m(dir
, '/'); /* Find final '/', if any */
1573 if (!(*parent
= talloc_strdup(mem_ctx
, "."))) {
1584 if (!(*parent
= (char *)talloc_memdup(mem_ctx
, dir
, len
+1))) {
1587 (*parent
)[len
] = '\0';
1595 /*******************************************************************
1596 Determine if a pattern contains any Microsoft wildcard characters.
1597 *******************************************************************/
1599 bool ms_has_wild(const char *s
)
1603 if (lp_posix_pathnames()) {
1604 /* With posix pathnames no characters are wild. */
1608 while ((c
= *s
++)) {
1621 bool ms_has_wild_w(const smb_ucs2_t
*s
)
1624 if (!s
) return False
;
1625 while ((c
= *s
++)) {
1627 case UCS2_CHAR('*'):
1628 case UCS2_CHAR('?'):
1629 case UCS2_CHAR('<'):
1630 case UCS2_CHAR('>'):
1631 case UCS2_CHAR('"'):
1638 /*******************************************************************
1639 A wrapper that handles case sensitivity and the special handling
1641 *******************************************************************/
1643 bool mask_match(const char *string
, const char *pattern
, bool is_case_sensitive
)
1645 if (ISDOTDOT(string
))
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
))
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
))
1683 /*********************************************************
1684 Recursive routine that is called by unix_wild_match.
1685 *********************************************************/
1687 static bool unix_do_match(const char *regexp
, const char *str
)
1691 for( p
= regexp
; *p
&& *str
; ) {
1702 * Look for a character matching
1703 * the one after the '*'.
1707 return true; /* Automatic match */
1710 while(*str
&& (*p
!= *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.
1723 * Eat all the characters that match, but count how many there were.
1726 while(*str
&& (*p
== *str
)) {
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
)) {
1741 if ( matchcount
<= 0 )
1745 str
--; /* We've eaten the match char after the '*' */
1747 if(unix_do_match(p
, str
))
1769 if (!*p
&& str
[0] == '.' && str
[1] == 0)
1772 if (!*str
&& *p
== '?') {
1778 if(!*str
&& (*p
== '*' && p
[1] == '\0'))
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();
1797 p2
= talloc_strdup(ctx
,pattern
);
1798 s2
= talloc_strdup(ctx
,string
);
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
,"*")) {
1818 ret
= unix_do_match(p2
, s2
);
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
)
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
);
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
, '.'))) {
1846 for (i
= 0; hp
->h_aliases
[i
]; i
++) {
1847 if (strchr_m(hp
->h_aliases
[i
], '.')) {
1848 full
= hp
->h_aliases
[i
];
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"));
1864 DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name
, full
));
1865 fstrcpy(fqdn
, full
);
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;
1878 if (blob
.length
== 0) {
1883 old_size
= talloc_get_size(buf
);
1886 result
= (char *)TALLOC_REALLOC(mem_ctx
, buf
, old_size
+ blob
.length
);
1887 if (result
== NULL
) {
1891 memcpy(result
+ old_size
, blob
.data
, blob
.length
);
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
:
1900 case FILE_SHARE_READ
:
1902 case FILE_SHARE_WRITE
:
1904 case FILE_SHARE_READ
|FILE_SHARE_WRITE
:
1907 if (private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) {
1909 } else if (private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
) {
1916 pid_t
procid_to_pid(const struct server_id
*proc
)
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
));
1929 uint32
get_my_vnn(void)
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
;
1946 result
.unique_id
= my_unique_id
;
1947 result
.vnn
= my_vnn
;
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
)
1960 if (p1
->task_id
!= p2
->task_id
)
1962 if (p1
->vnn
!= p2
->vnn
)
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())
1977 if (pid
->task_id
!= 0)
1979 if (pid
->vnn
!= my_vnn
)
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
1996 if (sscanf(pid_string
, "%u:%llu.%u", &vnn
, &pid
, &task_id
) == 3) {
1999 result
.task_id
= task_id
;
2000 } else if (sscanf(pid_string
, "%u:%llu", &vnn
, &pid
) == 2) {
2004 } else if (sscanf(pid_string
, "%llu.%u", &pid
, &task_id
) == 2) {
2005 result
.vnn
= get_my_vnn();
2007 result
.task_id
= task_id
;
2008 } else if (sscanf(pid_string
, "%llu", &pid
) == 1) {
2009 result
.vnn
= get_my_vnn();
2012 result
.vnn
= NONCLUSTER_VNN
;
2013 result
.pid
= (uint64_t)-1;
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
)) {
2045 if ((offset
> bufsize
) || (offset
+ length
> bufsize
)) {
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
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
) {
2067 if (end_base
< buf_base
|| end_ptr
< ptr
) {
2068 return False
; /* wrap. */
2071 if (end_ptr
< end_base
) {
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
) ?
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
)) {
2096 /* Check if a valid string exists at this offset. */
2097 if (skip_string(buf_base
,buf_len
, ptr
+ off
) == NULL
) {
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],
2113 if (!is_offset_safe(buf_base
, buf_len
, ptr
, off
+1)) {
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)) {
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
,
2145 const char *p
= NULL
;
2147 p
= strchr_m(full_name
, '\\');
2150 *domain
= talloc_strndup(mem_ctx
, full_name
,
2151 PTR_DIFF(p
, full_name
));
2152 *user
= talloc_strdup(mem_ctx
, p
+1);
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
)
2169 if (strlen_m(s
) < 3) {
2173 if (s
[0] == '\\') s
++;
2174 if (s
[0] == '\\') s
++;
2179 bool tevent_req_poll_ntstatus(struct tevent_req
*req
,
2180 struct tevent_context
*ev
,
2183 bool ret
= tevent_req_poll(req
, ev
);
2185 *status
= map_nt_error_from_unix(errno
);
2190 bool any_nt_status_not_ok(NTSTATUS err1
, NTSTATUS err2
, NTSTATUS
*result
)
2192 if (!NT_STATUS_IS_OK(err1
)) {
2196 if (!NT_STATUS_IS_OK(err2
)) {
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
)
2216 if (!dos_pathname
) {
2220 ptr
= talloc_strdup(ctx
, dos_pathname
);
2224 /* Convert any '\' paths to '/' */
2226 ptr
= unix_clean_name(ctx
, ptr
);
2231 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
2232 if (strlen(ptr
) > 2 && ptr
[1] == ':' && ptr
[0] != '/')
2235 /* Only absolute paths allowed. */