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 "libcli/security/security.h"
33 extern char *global_clobber_region_function
;
34 extern unsigned int global_clobber_region_line
;
36 /* Max allowable allococation - 256mb - 0x10000000 */
37 #define MAX_ALLOC_SIZE (1024*1024*256)
39 #if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT))
40 #ifdef WITH_NISPLUS_HOME
41 #ifdef BROKEN_NISPLUS_INCLUDE_FILES
43 * The following lines are needed due to buggy include files
44 * in Solaris 2.6 which define GROUP in both /usr/include/sys/acl.h and
45 * also in /usr/include/rpcsvc/nis.h. The definitions conflict. JRA.
46 * Also GROUP_OBJ is defined as 0x4 in /usr/include/sys/acl.h and as
47 * an enum in /usr/include/rpcsvc/nis.h.
54 #if defined(GROUP_OBJ)
58 #endif /* BROKEN_NISPLUS_INCLUDE_FILES */
60 #include <rpcsvc/nis.h>
62 #endif /* WITH_NISPLUS_HOME */
63 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
65 static enum protocol_types Protocol
= PROTOCOL_COREPLUS
;
67 enum protocol_types
get_Protocol(void)
72 void set_Protocol(enum protocol_types p
)
77 static enum remote_arch_types ra_type
= RA_UNKNOWN
;
79 /***********************************************************************
80 Definitions for all names.
81 ***********************************************************************/
83 static char *smb_scope
;
84 static int smb_num_netbios_names
;
85 static char **smb_my_netbios_names
;
87 /***********************************************************************
88 Allocate and set scope. Ensure upper case.
89 ***********************************************************************/
91 bool set_global_scope(const char *scope
)
94 smb_scope
= SMB_STRDUP(scope
);
97 strupper_m(smb_scope
);
101 /*********************************************************************
102 Ensure scope is never null string.
103 *********************************************************************/
105 const char *global_scope(void)
108 set_global_scope("");
112 static void free_netbios_names_array(void)
116 for (i
= 0; i
< smb_num_netbios_names
; i
++)
117 SAFE_FREE(smb_my_netbios_names
[i
]);
119 SAFE_FREE(smb_my_netbios_names
);
120 smb_num_netbios_names
= 0;
123 static bool allocate_my_netbios_names_array(size_t number
)
125 free_netbios_names_array();
127 smb_num_netbios_names
= number
+ 1;
128 smb_my_netbios_names
= SMB_MALLOC_ARRAY( char *, smb_num_netbios_names
);
130 if (!smb_my_netbios_names
)
133 memset(smb_my_netbios_names
, '\0', sizeof(char *) * smb_num_netbios_names
);
137 static bool set_my_netbios_names(const char *name
, int i
)
139 SAFE_FREE(smb_my_netbios_names
[i
]);
141 smb_my_netbios_names
[i
] = SMB_STRDUP(name
);
142 if (!smb_my_netbios_names
[i
])
144 strupper_m(smb_my_netbios_names
[i
]);
148 /***********************************************************************
149 Free memory allocated to global objects
150 ***********************************************************************/
152 void gfree_names(void)
154 gfree_netbios_names();
155 SAFE_FREE( smb_scope
);
156 free_netbios_names_array();
157 free_local_machine_name();
160 void gfree_all( void )
170 const char *my_netbios_names(int i
)
172 return smb_my_netbios_names
[i
];
175 bool set_netbios_aliases(const char **str_array
)
179 /* Work out the max number of netbios aliases that we have */
180 for( namecount
=0; str_array
&& (str_array
[namecount
] != NULL
); namecount
++ )
183 if ( global_myname() && *global_myname())
186 /* Allocate space for the netbios aliases */
187 if (!allocate_my_netbios_names_array(namecount
))
190 /* Use the global_myname string first */
192 if ( global_myname() && *global_myname()) {
193 set_my_netbios_names( global_myname(), namecount
);
199 for ( i
= 0; str_array
[i
] != NULL
; i
++) {
201 bool duplicate
= False
;
203 /* Look for duplicates */
204 for( n
=0; n
<namecount
; n
++ ) {
205 if( strequal( str_array
[i
], my_netbios_names(n
) ) ) {
211 if (!set_my_netbios_names(str_array
[i
], namecount
))
220 /****************************************************************************
221 Common name initialization code.
222 ****************************************************************************/
224 bool init_names(void)
228 if (global_myname() == NULL
|| *global_myname() == '\0') {
229 if (!set_global_myname(myhostname())) {
230 DEBUG( 0, ( "init_names: malloc fail.\n" ) );
235 if (!set_netbios_aliases(lp_netbios_aliases())) {
236 DEBUG( 0, ( "init_names: malloc fail.\n" ) );
240 set_local_machine_name(global_myname(),false);
242 DEBUG( 5, ("Netbios name list:-\n") );
243 for( n
=0; my_netbios_names(n
); n
++ ) {
244 DEBUGADD( 5, ("my_netbios_names[%d]=\"%s\"\n",
245 n
, my_netbios_names(n
) ) );
251 /*******************************************************************
252 Check if a file exists - call vfs_file_exist for samba files.
253 ********************************************************************/
255 bool file_exist_stat(const char *fname
,SMB_STRUCT_STAT
*sbuf
,
256 bool fake_dir_create_times
)
262 if (sys_stat(fname
, sbuf
, fake_dir_create_times
) != 0)
265 return((S_ISREG(sbuf
->st_ex_mode
)) || (S_ISFIFO(sbuf
->st_ex_mode
)));
268 /*******************************************************************
269 Check if a unix domain socket exists - call vfs_file_exist for samba files.
270 ********************************************************************/
272 bool socket_exist(const char *fname
)
275 if (sys_stat(fname
, &st
, false) != 0)
278 return S_ISSOCK(st
.st_ex_mode
);
281 /*******************************************************************
282 Returns the size in bytes of the named given the stat struct.
283 ********************************************************************/
285 uint64_t get_file_size_stat(const SMB_STRUCT_STAT
*sbuf
)
287 return sbuf
->st_ex_size
;
290 /*******************************************************************
291 Returns the size in bytes of the named file.
292 ********************************************************************/
294 SMB_OFF_T
get_file_size(char *file_name
)
298 if (sys_stat(file_name
, &buf
, false) != 0)
299 return (SMB_OFF_T
)-1;
300 return get_file_size_stat(&buf
);
303 /*******************************************************************
304 Return a string representing an attribute for a file.
305 ********************************************************************/
307 char *attrib_string(uint16 mode
)
313 if (mode
& FILE_ATTRIBUTE_VOLUME
) fstrcat(attrstr
,"V");
314 if (mode
& FILE_ATTRIBUTE_DIRECTORY
) fstrcat(attrstr
,"D");
315 if (mode
& FILE_ATTRIBUTE_ARCHIVE
) fstrcat(attrstr
,"A");
316 if (mode
& FILE_ATTRIBUTE_HIDDEN
) fstrcat(attrstr
,"H");
317 if (mode
& FILE_ATTRIBUTE_SYSTEM
) fstrcat(attrstr
,"S");
318 if (mode
& FILE_ATTRIBUTE_READONLY
) fstrcat(attrstr
,"R");
320 return talloc_strdup(talloc_tos(), attrstr
);
323 /*******************************************************************
324 Show a smb message structure.
325 ********************************************************************/
327 void show_msg(char *buf
)
335 DEBUG(5,("size=%d\nsmb_com=0x%x\nsmb_rcls=%d\nsmb_reh=%d\nsmb_err=%d\nsmb_flg=%d\nsmb_flg2=%d\n",
337 (int)CVAL(buf
,smb_com
),
338 (int)CVAL(buf
,smb_rcls
),
339 (int)CVAL(buf
,smb_reh
),
340 (int)SVAL(buf
,smb_err
),
341 (int)CVAL(buf
,smb_flg
),
342 (int)SVAL(buf
,smb_flg2
)));
343 DEBUGADD(5,("smb_tid=%d\nsmb_pid=%d\nsmb_uid=%d\nsmb_mid=%d\n",
344 (int)SVAL(buf
,smb_tid
),
345 (int)SVAL(buf
,smb_pid
),
346 (int)SVAL(buf
,smb_uid
),
347 (int)SVAL(buf
,smb_mid
)));
348 DEBUGADD(5,("smt_wct=%d\n",(int)CVAL(buf
,smb_wct
)));
350 for (i
=0;i
<(int)CVAL(buf
,smb_wct
);i
++)
351 DEBUGADD(5,("smb_vwv[%2d]=%5d (0x%X)\n",i
,
352 SVAL(buf
,smb_vwv
+2*i
),SVAL(buf
,smb_vwv
+2*i
)));
354 bcc
= (int)SVAL(buf
,smb_vwv
+2*(CVAL(buf
,smb_wct
)));
356 DEBUGADD(5,("smb_bcc=%d\n",bcc
));
364 dump_data(10, (uint8
*)smb_buf(buf
), bcc
);
367 /*******************************************************************
368 Set the length and marker of an encrypted smb packet.
369 ********************************************************************/
371 void smb_set_enclen(char *buf
,int len
,uint16 enc_ctx_num
)
373 _smb_setlen(buf
,len
);
377 SSVAL(buf
,6,enc_ctx_num
);
380 /*******************************************************************
381 Set the length and marker of an smb packet.
382 ********************************************************************/
384 void smb_setlen(char *buf
,int len
)
386 _smb_setlen(buf
,len
);
394 /*******************************************************************
395 Setup only the byte count for a smb message.
396 ********************************************************************/
398 int set_message_bcc(char *buf
,int num_bytes
)
400 int num_words
= CVAL(buf
,smb_wct
);
401 SSVAL(buf
,smb_vwv
+ num_words
*SIZEOFWORD
,num_bytes
);
402 _smb_setlen(buf
,smb_size
+ num_words
*2 + num_bytes
- 4);
403 return (smb_size
+ num_words
*2 + num_bytes
);
406 /*******************************************************************
407 Add a data blob to the end of a smb_buf, adjusting bcc and smb_len.
408 Return the bytes added
409 ********************************************************************/
411 ssize_t
message_push_blob(uint8
**outbuf
, DATA_BLOB blob
)
413 size_t newlen
= smb_len(*outbuf
) + 4 + blob
.length
;
416 if (!(tmp
= TALLOC_REALLOC_ARRAY(NULL
, *outbuf
, uint8
, newlen
))) {
417 DEBUG(0, ("talloc failed\n"));
422 memcpy(tmp
+ smb_len(tmp
) + 4, blob
.data
, blob
.length
);
423 set_message_bcc((char *)tmp
, smb_buflen(tmp
) + blob
.length
);
427 /*******************************************************************
428 Reduce a file name, removing .. elements.
429 ********************************************************************/
431 static char *dos_clean_name(TALLOC_CTX
*ctx
, const char *s
)
436 DEBUG(3,("dos_clean_name [%s]\n",s
));
438 /* remove any double slashes */
439 str
= talloc_all_string_sub(ctx
, s
, "\\\\", "\\");
444 /* Remove leading .\\ characters */
445 if(strncmp(str
, ".\\", 2) == 0) {
446 trim_string(str
, ".\\", NULL
);
448 str
= talloc_strdup(ctx
, ".\\");
455 while ((p
= strstr_m(str
,"\\..\\")) != NULL
) {
461 if ((p
=strrchr_m(str
,'\\')) != NULL
) {
466 str
= talloc_asprintf(ctx
,
475 trim_string(str
,NULL
,"\\..");
476 return talloc_all_string_sub(ctx
, str
, "\\.\\", "\\");
479 /*******************************************************************
480 Reduce a file name, removing .. elements.
481 ********************************************************************/
483 char *unix_clean_name(TALLOC_CTX
*ctx
, const char *s
)
488 DEBUG(3,("unix_clean_name [%s]\n",s
));
490 /* remove any double slashes */
491 str
= talloc_all_string_sub(ctx
, s
, "//","/");
496 /* Remove leading ./ characters */
497 if(strncmp(str
, "./", 2) == 0) {
498 trim_string(str
, "./", NULL
);
500 str
= talloc_strdup(ctx
, "./");
507 while ((p
= strstr_m(str
,"/../")) != NULL
) {
513 if ((p
=strrchr_m(str
,'/')) != NULL
) {
518 str
= talloc_asprintf(ctx
,
527 trim_string(str
,NULL
,"/..");
528 return talloc_all_string_sub(ctx
, str
, "/./", "/");
531 char *clean_name(TALLOC_CTX
*ctx
, const char *s
)
533 char *str
= dos_clean_name(ctx
, s
);
537 return unix_clean_name(ctx
, str
);
540 /*******************************************************************
541 Write data into an fd at a given offset. Ignore seek errors.
542 ********************************************************************/
544 ssize_t
write_data_at_offset(int fd
, const char *buffer
, size_t N
, SMB_OFF_T pos
)
549 if (pos
== (SMB_OFF_T
)-1) {
550 return write_data(fd
, buffer
, N
);
552 #if defined(HAVE_PWRITE) || defined(HAVE_PRWITE64)
554 ret
= sys_pwrite(fd
,buffer
+ total
,N
- total
, pos
);
555 if (ret
== -1 && errno
== ESPIPE
) {
556 return write_data(fd
, buffer
+ total
,N
- total
);
559 DEBUG(0,("write_data_at_offset: write failure. Error = %s\n", strerror(errno
) ));
568 return (ssize_t
)total
;
570 /* Use lseek and write_data. */
571 if (sys_lseek(fd
, pos
, SEEK_SET
) == -1) {
572 if (errno
!= ESPIPE
) {
576 return write_data(fd
, buffer
, N
);
581 NTSTATUS
reinit_after_fork(struct messaging_context
*msg_ctx
,
582 struct event_context
*ev_ctx
,
584 bool parent_longlived
)
586 NTSTATUS status
= NT_STATUS_OK
;
588 /* Reset the state of the random
589 * number generation system, so
590 * children do not get the same random
591 * numbers as each other */
592 set_need_random_reseed();
594 /* tdb needs special fork handling */
595 if (tdb_reopen_all(parent_longlived
? 1 : 0) == -1) {
596 DEBUG(0,("tdb_reopen_all failed.\n"));
597 status
= NT_STATUS_OPEN_FAILED
;
601 if (ev_ctx
&& tevent_re_initialise(ev_ctx
) != 0) {
602 smb_panic(__location__
": Failed to re-initialise event context");
607 * For clustering, we need to re-init our ctdbd connection after the
610 status
= messaging_reinit(msg_ctx
, id
);
611 if (!NT_STATUS_IS_OK(status
)) {
612 DEBUG(0,("messaging_reinit() failed: %s\n",
620 #if defined(PARANOID_MALLOC_CHECKER)
622 /****************************************************************************
623 Internal malloc wrapper. Externally visible.
624 ****************************************************************************/
626 void *malloc_(size_t size
)
633 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
636 /****************************************************************************
637 Internal calloc wrapper. Not externally visible.
638 ****************************************************************************/
640 static void *calloc_(size_t count
, size_t size
)
642 if (size
== 0 || count
== 0) {
646 return calloc(count
, size
);
647 #define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
650 /****************************************************************************
651 Internal realloc wrapper. Not externally visible.
652 ****************************************************************************/
654 static void *realloc_(void *ptr
, size_t size
)
657 return realloc(ptr
, size
);
658 #define realloc(p,s) __ERROR_DONT_USE_RELLOC_DIRECTLY
661 #endif /* PARANOID_MALLOC_CHECKER */
663 /****************************************************************************
665 ****************************************************************************/
667 void *memalign_array(size_t el_size
, size_t align
, unsigned int count
)
669 if (count
>= MAX_ALLOC_SIZE
/el_size
) {
673 return sys_memalign(align
, el_size
*count
);
676 /****************************************************************************
678 ****************************************************************************/
680 void *calloc_array(size_t size
, size_t nmemb
)
682 if (nmemb
>= MAX_ALLOC_SIZE
/size
) {
685 if (size
== 0 || nmemb
== 0) {
688 #if defined(PARANOID_MALLOC_CHECKER)
689 return calloc_(nmemb
, size
);
691 return calloc(nmemb
, size
);
695 /****************************************************************************
696 Expand a pointer to be a particular size.
697 Note that this version of Realloc has an extra parameter that decides
698 whether to free the passed in storage on allocation failure or if the
701 This is designed for use in the typical idiom of :
703 p = SMB_REALLOC(p, size)
708 and not to have to keep track of the old 'p' contents to free later, nor
709 to worry if the size parameter was zero. In the case where NULL is returned
710 we guarentee that p has been freed.
712 If free later semantics are desired, then pass 'free_old_on_error' as False which
713 guarentees that the old contents are not freed on error, even if size == 0. To use
716 tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
724 Changes were instigated by Coverity error checking. JRA.
725 ****************************************************************************/
727 void *Realloc(void *p
, size_t size
, bool free_old_on_error
)
732 if (free_old_on_error
) {
735 DEBUG(2,("Realloc asked for 0 bytes\n"));
739 #if defined(PARANOID_MALLOC_CHECKER)
741 ret
= (void *)malloc_(size
);
743 ret
= (void *)realloc_(p
,size
);
747 ret
= (void *)malloc(size
);
749 ret
= (void *)realloc(p
,size
);
754 if (free_old_on_error
&& p
) {
757 DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size
));
763 /****************************************************************************
764 (Hopefully) efficient array append.
765 ****************************************************************************/
767 void add_to_large_array(TALLOC_CTX
*mem_ctx
, size_t element_size
,
768 void *element
, void *_array
, uint32
*num_elements
,
771 void **array
= (void **)_array
;
773 if (*array_size
< 0) {
777 if (*array
== NULL
) {
778 if (*array_size
== 0) {
782 if (*array_size
>= MAX_ALLOC_SIZE
/element_size
) {
786 *array
= TALLOC(mem_ctx
, element_size
* (*array_size
));
787 if (*array
== NULL
) {
792 if (*num_elements
== *array_size
) {
795 if (*array_size
>= MAX_ALLOC_SIZE
/element_size
) {
799 *array
= TALLOC_REALLOC(mem_ctx
, *array
,
800 element_size
* (*array_size
));
802 if (*array
== NULL
) {
807 memcpy((char *)(*array
) + element_size
*(*num_elements
),
808 element
, element_size
);
818 /****************************************************************************
819 Get my own domain name, or "" if we have none.
820 ****************************************************************************/
822 char *get_mydnsdomname(TALLOC_CTX
*ctx
)
827 domname
= get_mydnsfullname();
832 p
= strchr_m(domname
, '.');
835 return talloc_strdup(ctx
, p
);
837 return talloc_strdup(ctx
, "");
841 /****************************************************************************
842 Interpret a protocol description string, with a default.
843 ****************************************************************************/
845 int interpret_protocol(const char *str
,int def
)
847 if (strequal(str
,"NT1"))
848 return(PROTOCOL_NT1
);
849 if (strequal(str
,"LANMAN2"))
850 return(PROTOCOL_LANMAN2
);
851 if (strequal(str
,"LANMAN1"))
852 return(PROTOCOL_LANMAN1
);
853 if (strequal(str
,"CORE"))
854 return(PROTOCOL_CORE
);
855 if (strequal(str
,"COREPLUS"))
856 return(PROTOCOL_COREPLUS
);
857 if (strequal(str
,"CORE+"))
858 return(PROTOCOL_COREPLUS
);
860 DEBUG(0,("Unrecognised protocol level %s\n",str
));
866 #if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))
867 /******************************************************************
868 Remove any mount options such as -rsize=2048,wsize=2048 etc.
869 Based on a fix from <Thomas.Hepper@icem.de>.
870 Returns a malloc'ed string.
871 *******************************************************************/
873 static char *strip_mount_options(TALLOC_CTX
*ctx
, const char *str
)
877 while(*p
&& !isspace(*p
))
879 while(*p
&& isspace(*p
))
882 return talloc_strdup(ctx
, p
);
888 /*******************************************************************
889 Patch from jkf@soton.ac.uk
890 Split Luke's automount_server into YP lookup and string splitter
891 so can easily implement automount_path().
892 Returns a malloc'ed string.
893 *******************************************************************/
895 #ifdef WITH_NISPLUS_HOME
896 char *automount_lookup(TALLOC_CTX
*ctx
, const char *user_name
)
900 char *nis_map
= (char *)lp_nis_home_map_name();
902 char buffer
[NIS_MAXATTRVAL
+ 1];
907 snprintf(buffer
, sizeof(buffer
), "[key=%s],%s", user_name
, nis_map
);
908 DEBUG(5, ("NIS+ querystring: %s\n", buffer
));
910 if (result
= nis_list(buffer
, FOLLOW_PATH
|EXPAND_NAME
|HARD_LOOKUP
, NULL
, NULL
)) {
911 if (result
->status
!= NIS_SUCCESS
) {
912 DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result
->status
)));
914 object
= result
->objects
.objects_val
;
915 if (object
->zo_data
.zo_type
== ENTRY_OBJ
) {
916 entry
= &object
->zo_data
.objdata_u
.en_data
;
917 DEBUG(5, ("NIS+ entry type: %s\n", entry
->en_type
));
918 DEBUG(3, ("NIS+ result: %s\n", entry
->en_cols
.en_cols_val
[1].ec_value
.ec_value_val
));
920 value
= talloc_strdup(ctx
,
921 entry
->en_cols
.en_cols_val
[1].ec_value
.ec_value_val
);
923 nis_freeresult(result
);
926 value
= talloc_string_sub(ctx
,
933 nis_freeresult(result
);
936 value
= strip_mount_options(ctx
, value
);
937 DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n",
942 #else /* WITH_NISPLUS_HOME */
944 char *automount_lookup(TALLOC_CTX
*ctx
, const char *user_name
)
948 int nis_error
; /* returned by yp all functions */
949 char *nis_result
; /* yp_match inits this */
950 int nis_result_len
; /* and set this */
951 char *nis_domain
; /* yp_get_default_domain inits this */
952 char *nis_map
= (char *)lp_nis_home_map_name();
954 if ((nis_error
= yp_get_default_domain(&nis_domain
)) != 0) {
955 DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error
)));
959 DEBUG(5, ("NIS Domain: %s\n", nis_domain
));
961 if ((nis_error
= yp_match(nis_domain
, nis_map
, user_name
,
962 strlen(user_name
), &nis_result
,
963 &nis_result_len
)) == 0) {
964 if (nis_result_len
> 0 && nis_result
[nis_result_len
] == '\n') {
965 nis_result
[nis_result_len
] = '\0';
967 value
= talloc_strdup(ctx
, nis_result
);
971 value
= strip_mount_options(ctx
, value
);
972 } else if(nis_error
== YPERR_KEY
) {
973 DEBUG(3, ("YP Key not found: while looking up \"%s\" in map \"%s\"\n",
974 user_name
, nis_map
));
975 DEBUG(3, ("using defaults for server and home directory\n"));
977 DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n",
978 yperr_string(nis_error
), user_name
, nis_map
));
982 DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name
, value
));
986 #endif /* WITH_NISPLUS_HOME */
989 /****************************************************************************
990 Check if a process exists. Does this work on all unixes?
991 ****************************************************************************/
993 bool process_exists(const struct server_id pid
)
995 if (procid_is_me(&pid
)) {
999 if (procid_is_local(&pid
)) {
1000 return (kill(pid
.pid
,0) == 0 || errno
!= ESRCH
);
1003 #ifdef CLUSTER_SUPPORT
1004 return ctdbd_process_exists(messaging_ctdbd_connection(),
1011 /*******************************************************************
1012 Convert a uid into a user name.
1013 ********************************************************************/
1015 const char *uidtoname(uid_t uid
)
1017 TALLOC_CTX
*ctx
= talloc_tos();
1019 struct passwd
*pass
= NULL
;
1021 pass
= getpwuid_alloc(ctx
,uid
);
1023 name
= talloc_strdup(ctx
,pass
->pw_name
);
1026 name
= talloc_asprintf(ctx
,
1033 /*******************************************************************
1034 Convert a gid into a group name.
1035 ********************************************************************/
1037 char *gidtoname(gid_t gid
)
1041 grp
= getgrgid(gid
);
1043 return talloc_strdup(talloc_tos(), grp
->gr_name
);
1046 return talloc_asprintf(talloc_tos(),
1052 /*******************************************************************
1053 Convert a user name into a uid.
1054 ********************************************************************/
1056 uid_t
nametouid(const char *name
)
1058 struct passwd
*pass
;
1062 pass
= Get_Pwnam_alloc(talloc_tos(), name
);
1069 u
= (uid_t
)strtol(name
, &p
, 0);
1070 if ((p
!= name
) && (*p
== '\0'))
1076 /*******************************************************************
1077 Convert a name to a gid_t if possible. Return -1 if not a group.
1078 ********************************************************************/
1080 gid_t
nametogid(const char *name
)
1086 g
= (gid_t
)strtol(name
, &p
, 0);
1087 if ((p
!= name
) && (*p
== '\0'))
1090 grp
= sys_getgrnam(name
);
1092 return(grp
->gr_gid
);
1096 /*******************************************************************
1097 Something really nasty happened - panic !
1098 ********************************************************************/
1100 void smb_panic(const char *const why
)
1108 if (global_clobber_region_function
) {
1109 DEBUG(0,("smb_panic: clobber_region() last called from [%s(%u)]\n",
1110 global_clobber_region_function
,
1111 global_clobber_region_line
));
1116 DEBUG(0,("PANIC (pid %llu): %s\n",
1117 (unsigned long long)sys_getpid(), why
));
1120 cmd
= lp_panic_action();
1122 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd
));
1123 result
= system(cmd
);
1126 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
1129 DEBUG(0, ("smb_panic(): action returned status %d\n",
1130 WEXITSTATUS(result
)));
1136 /*******************************************************************
1137 Print a backtrace of the stack to the debug log. This function
1138 DELIBERATELY LEAKS MEMORY. The expectation is that you should
1139 exit shortly after calling it.
1140 ********************************************************************/
1142 #ifdef HAVE_LIBUNWIND_H
1143 #include <libunwind.h>
1146 #ifdef HAVE_EXECINFO_H
1147 #include <execinfo.h>
1150 #ifdef HAVE_LIBEXC_H
1154 void log_stack_trace(void)
1156 #ifdef HAVE_LIBUNWIND
1157 /* Try to use libunwind before any other technique since on ia64
1158 * libunwind correctly walks the stack in more circumstances than
1161 unw_cursor_t cursor
;
1166 unw_word_t ip
, sp
, off
;
1168 procname
[sizeof(procname
) - 1] = '\0';
1170 if (unw_getcontext(&uc
) != 0) {
1171 goto libunwind_failed
;
1174 if (unw_init_local(&cursor
, &uc
) != 0) {
1175 goto libunwind_failed
;
1178 DEBUG(0, ("BACKTRACE:\n"));
1182 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
1183 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
1185 switch (unw_get_proc_name(&cursor
,
1186 procname
, sizeof(procname
) - 1, &off
) ) {
1190 /* Name truncated. */
1191 DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
1192 i
, procname
, (long long)off
,
1193 (long long)ip
, (long long) sp
));
1196 /* case -UNW_ENOINFO: */
1197 /* case -UNW_EUNSPEC: */
1198 /* No symbol name found. */
1199 DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
1200 i
, "<unknown symbol>",
1201 (long long)ip
, (long long) sp
));
1204 } while (unw_step(&cursor
) > 0);
1209 DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
1211 #elif HAVE_BACKTRACE_SYMBOLS
1212 void *backtrace_stack
[BACKTRACE_STACK_SIZE
];
1213 size_t backtrace_size
;
1214 char **backtrace_strings
;
1216 /* get the backtrace (stack frames) */
1217 backtrace_size
= backtrace(backtrace_stack
,BACKTRACE_STACK_SIZE
);
1218 backtrace_strings
= backtrace_symbols(backtrace_stack
, backtrace_size
);
1220 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
1221 (unsigned long)backtrace_size
));
1223 if (backtrace_strings
) {
1226 for (i
= 0; i
< backtrace_size
; i
++)
1227 DEBUGADD(0, (" #%u %s\n", i
, backtrace_strings
[i
]));
1229 /* Leak the backtrace_strings, rather than risk what free() might do */
1234 /* The IRIX libexc library provides an API for unwinding the stack. See
1235 * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
1236 * since we are about to abort anyway, it hardly matters.
1239 #define NAMESIZE 32 /* Arbitrary */
1241 __uint64_t addrs
[BACKTRACE_STACK_SIZE
];
1242 char * names
[BACKTRACE_STACK_SIZE
];
1243 char namebuf
[BACKTRACE_STACK_SIZE
* NAMESIZE
];
1250 ZERO_ARRAY(namebuf
);
1252 /* We need to be root so we can open our /proc entry to walk
1253 * our stack. It also helps when we want to dump core.
1257 for (i
= 0; i
< BACKTRACE_STACK_SIZE
; i
++) {
1258 names
[i
] = namebuf
+ (i
* NAMESIZE
);
1261 levels
= trace_back_stack(0, addrs
, names
,
1262 BACKTRACE_STACK_SIZE
, NAMESIZE
- 1);
1264 DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels
));
1265 for (i
= 0; i
< levels
; i
++) {
1266 DEBUGADD(0, (" #%d 0x%llx %s\n", i
, addrs
[i
], names
[i
]));
1271 DEBUG(0, ("unable to produce a stack trace on this platform\n"));
1275 /*******************************************************************
1276 A readdir wrapper which just returns the file name.
1277 ********************************************************************/
1279 const char *readdirname(SMB_STRUCT_DIR
*p
)
1281 SMB_STRUCT_DIRENT
*ptr
;
1287 ptr
= (SMB_STRUCT_DIRENT
*)sys_readdir(p
);
1291 dname
= ptr
->d_name
;
1298 #ifdef HAVE_BROKEN_READDIR_NAME
1299 /* using /usr/ucb/cc is BAD */
1303 return talloc_strdup(talloc_tos(), dname
);
1306 /*******************************************************************
1307 Utility function used to decide if the last component
1308 of a path matches a (possibly wildcarded) entry in a namelist.
1309 ********************************************************************/
1311 bool is_in_path(const char *name
, name_compare_entry
*namelist
, bool case_sensitive
)
1313 const char *last_component
;
1315 /* if we have no list it's obviously not in the path */
1316 if((namelist
== NULL
) || ((namelist
!= NULL
) && (namelist
[0].name
== NULL
))) {
1320 DEBUG(8, ("is_in_path: %s\n", name
));
1322 /* Get the last component of the unix name. */
1323 last_component
= strrchr_m(name
, '/');
1324 if (!last_component
) {
1325 last_component
= name
;
1327 last_component
++; /* Go past '/' */
1330 for(; namelist
->name
!= NULL
; namelist
++) {
1331 if(namelist
->is_wild
) {
1332 if (mask_match(last_component
, namelist
->name
, case_sensitive
)) {
1333 DEBUG(8,("is_in_path: mask match succeeded\n"));
1337 if((case_sensitive
&& (strcmp(last_component
, namelist
->name
) == 0))||
1338 (!case_sensitive
&& (StrCaseCmp(last_component
, namelist
->name
) == 0))) {
1339 DEBUG(8,("is_in_path: match succeeded\n"));
1344 DEBUG(8,("is_in_path: match not found\n"));
1348 /*******************************************************************
1349 Strip a '/' separated list into an array of
1350 name_compare_enties structures suitable for
1351 passing to is_in_path(). We do this for
1352 speed so we can pre-parse all the names in the list
1353 and don't do it for each call to is_in_path().
1354 namelist is modified here and is assumed to be
1355 a copy owned by the caller.
1356 We also check if the entry contains a wildcard to
1357 remove a potentially expensive call to mask_match
1359 ********************************************************************/
1361 void set_namearray(name_compare_entry
**ppname_array
, const char *namelist
)
1364 char *nameptr
= (char *)namelist
;
1365 int num_entries
= 0;
1368 (*ppname_array
) = NULL
;
1370 if((nameptr
== NULL
) || ((nameptr
!= NULL
) && (*nameptr
== '\0')))
1373 /* We need to make two passes over the string. The
1374 first to count the number of elements, the second
1379 if ( *nameptr
== '/' ) {
1380 /* cope with multiple (useless) /s) */
1384 /* anything left? */
1385 if ( *nameptr
== '\0' )
1388 /* find the next '/' or consume remaining */
1389 name_end
= strchr_m(nameptr
, '/');
1390 if (name_end
== NULL
)
1391 name_end
= (char *)nameptr
+ strlen(nameptr
);
1393 /* next segment please */
1394 nameptr
= name_end
+ 1;
1398 if(num_entries
== 0)
1401 if(( (*ppname_array
) = SMB_MALLOC_ARRAY(name_compare_entry
, num_entries
+ 1)) == NULL
) {
1402 DEBUG(0,("set_namearray: malloc fail\n"));
1406 /* Now copy out the names */
1407 nameptr
= (char *)namelist
;
1410 if ( *nameptr
== '/' ) {
1411 /* cope with multiple (useless) /s) */
1415 /* anything left? */
1416 if ( *nameptr
== '\0' )
1419 /* find the next '/' or consume remaining */
1420 name_end
= strchr_m(nameptr
, '/');
1424 name_end
= nameptr
+ strlen(nameptr
);
1426 (*ppname_array
)[i
].is_wild
= ms_has_wild(nameptr
);
1427 if(((*ppname_array
)[i
].name
= SMB_STRDUP(nameptr
)) == NULL
) {
1428 DEBUG(0,("set_namearray: malloc fail (1)\n"));
1432 /* next segment please */
1433 nameptr
= name_end
+ 1;
1437 (*ppname_array
)[i
].name
= NULL
;
1442 /****************************************************************************
1443 Routine to free a namearray.
1444 ****************************************************************************/
1446 void free_namearray(name_compare_entry
*name_array
)
1450 if(name_array
== NULL
)
1453 for(i
=0; name_array
[i
].name
!=NULL
; i
++)
1454 SAFE_FREE(name_array
[i
].name
);
1455 SAFE_FREE(name_array
);
1459 #define DBGC_CLASS DBGC_LOCKING
1461 /****************************************************************************
1462 Simple routine to query existing file locks. Cruft in NFS and 64->32 bit mapping
1463 is dealt with in posix.c
1464 Returns True if we have information regarding this lock region (and returns
1465 F_UNLCK in *ptype if the region is unlocked). False if the call failed.
1466 ****************************************************************************/
1468 bool fcntl_getlock(int fd
, SMB_OFF_T
*poffset
, SMB_OFF_T
*pcount
, int *ptype
, pid_t
*ppid
)
1470 SMB_STRUCT_FLOCK lock
;
1473 DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
1474 fd
,(double)*poffset
,(double)*pcount
,*ptype
));
1476 lock
.l_type
= *ptype
;
1477 lock
.l_whence
= SEEK_SET
;
1478 lock
.l_start
= *poffset
;
1479 lock
.l_len
= *pcount
;
1482 ret
= sys_fcntl_ptr(fd
,SMB_F_GETLK
,&lock
);
1486 DEBUG(3,("fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s)\n",
1487 (double)*poffset
,(double)*pcount
,*ptype
,strerror(errno
)));
1492 *ptype
= lock
.l_type
;
1493 *poffset
= lock
.l_start
;
1494 *pcount
= lock
.l_len
;
1497 DEBUG(3,("fcntl_getlock: fd %d is returned info %d pid %u\n",
1498 fd
, (int)lock
.l_type
, (unsigned int)lock
.l_pid
));
1503 #define DBGC_CLASS DBGC_ALL
1505 /*******************************************************************
1506 Is the name specified one of my netbios names.
1507 Returns true if it is equal, false otherwise.
1508 ********************************************************************/
1510 bool is_myname(const char *s
)
1515 for (n
=0; my_netbios_names(n
); n
++) {
1516 if (strequal(my_netbios_names(n
), s
)) {
1521 DEBUG(8, ("is_myname(\"%s\") returns %d\n", s
, ret
));
1525 /*******************************************************************
1526 Is the name specified our workgroup/domain.
1527 Returns true if it is equal, false otherwise.
1528 ********************************************************************/
1530 bool is_myworkgroup(const char *s
)
1534 if (strequal(s
, lp_workgroup())) {
1538 DEBUG(8, ("is_myworkgroup(\"%s\") returns %d\n", s
, ret
));
1542 /*******************************************************************
1543 we distinguish between 2K and XP by the "Native Lan Manager" string
1544 WinXP => "Windows 2002 5.1"
1545 WinXP 64bit => "Windows XP 5.2"
1546 Win2k => "Windows 2000 5.0"
1547 NT4 => "Windows NT 4.0"
1548 Win9x => "Windows 4.0"
1549 Windows 2003 doesn't set the native lan manager string but
1550 they do set the domain to "Windows 2003 5.2" (probably a bug).
1551 ********************************************************************/
1553 void ra_lanman_string( const char *native_lanman
)
1555 if ( strcmp( native_lanman
, "Windows 2002 5.1" ) == 0 )
1556 set_remote_arch( RA_WINXP
);
1557 else if ( strcmp( native_lanman
, "Windows XP 5.2" ) == 0 )
1558 set_remote_arch( RA_WINXP64
);
1559 else if ( strcmp( native_lanman
, "Windows Server 2003 5.2" ) == 0 )
1560 set_remote_arch( RA_WIN2K3
);
1563 static const char *remote_arch_str
;
1565 const char *get_remote_arch_str(void)
1567 if (!remote_arch_str
) {
1570 return remote_arch_str
;
1573 /*******************************************************************
1574 Set the horrid remote_arch string based on an enum.
1575 ********************************************************************/
1577 void set_remote_arch(enum remote_arch_types type
)
1582 remote_arch_str
= "WfWg";
1585 remote_arch_str
= "OS2";
1588 remote_arch_str
= "Win95";
1591 remote_arch_str
= "WinNT";
1594 remote_arch_str
= "Win2K";
1597 remote_arch_str
= "WinXP";
1600 remote_arch_str
= "WinXP64";
1603 remote_arch_str
= "Win2K3";
1606 remote_arch_str
= "Vista";
1609 remote_arch_str
= "Samba";
1612 remote_arch_str
= "CIFSFS";
1615 remote_arch_str
= "OSX";
1618 ra_type
= RA_UNKNOWN
;
1619 remote_arch_str
= "UNKNOWN";
1623 DEBUG(10,("set_remote_arch: Client arch is \'%s\'\n",
1627 /*******************************************************************
1628 Get the remote_arch type.
1629 ********************************************************************/
1631 enum remote_arch_types
get_remote_arch(void)
1636 const char *tab_depth(int level
, int depth
)
1638 if( CHECK_DEBUGLVL(level
) ) {
1639 dbgtext("%*s", depth
*4, "");
1644 /*****************************************************************************
1645 Provide a checksum on a string
1647 Input: s - the null-terminated character string for which the checksum
1650 Output: The checksum value calculated for s.
1651 *****************************************************************************/
1653 int str_checksum(const char *s
)
1655 TDB_DATA key
= string_tdb_data(s
);
1656 return tdb_jenkins_hash(&key
);
1659 /*****************************************************************
1660 Zero a memory area then free it. Used to catch bugs faster.
1661 *****************************************************************/
1663 void zero_free(void *p
, size_t size
)
1669 /*****************************************************************
1670 Set our open file limit to a requested max and return the limit.
1671 *****************************************************************/
1673 int set_maxfiles(int requested_max
)
1675 #if (defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE))
1677 int saved_current_limit
;
1679 if(getrlimit(RLIMIT_NOFILE
, &rlp
)) {
1680 DEBUG(0,("set_maxfiles: getrlimit (1) for RLIMIT_NOFILE failed with error %s\n",
1683 return requested_max
;
1687 * Set the fd limit to be real_max_open_files + MAX_OPEN_FUDGEFACTOR to
1688 * account for the extra fd we need
1689 * as well as the log files and standard
1690 * handles etc. Save the limit we want to set in case
1691 * we are running on an OS that doesn't support this limit (AIX)
1692 * which always returns RLIM_INFINITY for rlp.rlim_max.
1695 /* Try raising the hard (max) limit to the requested amount. */
1697 #if defined(RLIM_INFINITY)
1698 if (rlp
.rlim_max
!= RLIM_INFINITY
) {
1699 int orig_max
= rlp
.rlim_max
;
1701 if ( rlp
.rlim_max
< requested_max
)
1702 rlp
.rlim_max
= requested_max
;
1704 /* This failing is not an error - many systems (Linux) don't
1705 support our default request of 10,000 open files. JRA. */
1707 if(setrlimit(RLIMIT_NOFILE
, &rlp
)) {
1708 DEBUG(3,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d max files failed with error %s\n",
1709 (int)rlp
.rlim_max
, strerror(errno
) ));
1711 /* Set failed - restore original value from get. */
1712 rlp
.rlim_max
= orig_max
;
1717 /* Now try setting the soft (current) limit. */
1719 saved_current_limit
= rlp
.rlim_cur
= MIN(requested_max
,rlp
.rlim_max
);
1721 if(setrlimit(RLIMIT_NOFILE
, &rlp
)) {
1722 DEBUG(0,("set_maxfiles: setrlimit for RLIMIT_NOFILE for %d files failed with error %s\n",
1723 (int)rlp
.rlim_cur
, strerror(errno
) ));
1725 return saved_current_limit
;
1728 if(getrlimit(RLIMIT_NOFILE
, &rlp
)) {
1729 DEBUG(0,("set_maxfiles: getrlimit (2) for RLIMIT_NOFILE failed with error %s\n",
1732 return saved_current_limit
;
1735 #if defined(RLIM_INFINITY)
1736 if(rlp
.rlim_cur
== RLIM_INFINITY
)
1737 return saved_current_limit
;
1740 if((int)rlp
.rlim_cur
> saved_current_limit
)
1741 return saved_current_limit
;
1743 return rlp
.rlim_cur
;
1744 #else /* !defined(HAVE_GETRLIMIT) || !defined(RLIMIT_NOFILE) */
1746 * No way to know - just guess...
1748 return requested_max
;
1752 /*****************************************************************
1753 malloc that aborts with smb_panic on fail or zero size.
1754 *****************************************************************/
1756 void *smb_xmalloc_array(size_t size
, unsigned int count
)
1760 smb_panic("smb_xmalloc_array: called with zero size");
1762 if (count
>= MAX_ALLOC_SIZE
/size
) {
1763 smb_panic("smb_xmalloc_array: alloc size too large");
1765 if ((p
= SMB_MALLOC(size
*count
)) == NULL
) {
1766 DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
1767 (unsigned long)size
, (unsigned long)count
));
1768 smb_panic("smb_xmalloc_array: malloc failed");
1774 vasprintf that aborts on malloc fail
1777 int smb_xvasprintf(char **ptr
, const char *format
, va_list ap
)
1784 n
= vasprintf(ptr
, format
, ap2
);
1786 if (n
== -1 || ! *ptr
) {
1787 smb_panic("smb_xvasprintf: out of memory");
1792 /*****************************************************************
1793 Get local hostname and cache result.
1794 *****************************************************************/
1796 char *myhostname(void)
1800 ret
= get_myname(NULL
);
1806 * @brief Returns an absolute path to a file concatenating the provided
1807 * @a rootpath and @a basename
1809 * @param name Filename, relative to @a rootpath
1811 * @retval Pointer to a string containing the full path.
1814 static char *xx_path(const char *name
, const char *rootpath
)
1818 fname
= talloc_strdup(talloc_tos(), rootpath
);
1822 trim_string(fname
,"","/");
1824 if (!directory_exist(fname
)) {
1825 if (!mkdir(fname
,0755))
1826 DEBUG(1, ("Unable to create directory %s for file %s. "
1827 "Error was %s\n", fname
, name
, strerror(errno
)));
1830 return talloc_asprintf(talloc_tos(),
1837 * @brief Returns an absolute path to a file in the Samba lock directory.
1839 * @param name File to find, relative to LOCKDIR.
1841 * @retval Pointer to a talloc'ed string containing the full path.
1844 char *lock_path(const char *name
)
1846 return xx_path(name
, lp_lockdir());
1850 * @brief Returns an absolute path to a file in the Samba pid directory.
1852 * @param name File to find, relative to PIDDIR.
1854 * @retval Pointer to a talloc'ed string containing the full path.
1857 char *pid_path(const char *name
)
1859 return xx_path(name
, lp_piddir());
1863 * @brief Returns an absolute path to a file in the Samba lib directory.
1865 * @param name File to find, relative to LIBDIR.
1867 * @retval Pointer to a string containing the full path.
1870 char *lib_path(const char *name
)
1872 return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_LIBDIR(), name
);
1876 * @brief Returns an absolute path to a file in the Samba modules directory.
1878 * @param name File to find, relative to MODULESDIR.
1880 * @retval Pointer to a string containing the full path.
1883 char *modules_path(const char *name
)
1885 return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_MODULESDIR(), name
);
1889 * @brief Returns an absolute path to a file in the Samba data directory.
1891 * @param name File to find, relative to CODEPAGEDIR.
1893 * @retval Pointer to a talloc'ed string containing the full path.
1896 char *data_path(const char *name
)
1898 return talloc_asprintf(talloc_tos(), "%s/%s", get_dyn_CODEPAGEDIR(), name
);
1902 * @brief Returns an absolute path to a file in the Samba state directory.
1904 * @param name File to find, relative to STATEDIR.
1906 * @retval Pointer to a talloc'ed string containing the full path.
1909 char *state_path(const char *name
)
1911 return xx_path(name
, lp_statedir());
1915 * @brief Returns an absolute path to a file in the Samba cache directory.
1917 * @param name File to find, relative to CACHEDIR.
1919 * @retval Pointer to a talloc'ed string containing the full path.
1922 char *cache_path(const char *name
)
1924 return xx_path(name
, lp_cachedir());
1928 * @brief Returns the platform specific shared library extension.
1930 * @retval Pointer to a const char * containing the extension.
1933 const char *shlib_ext(void)
1935 return get_dyn_SHLIBEXT();
1938 /*******************************************************************
1939 Given a filename - get its directory name
1940 ********************************************************************/
1942 bool parent_dirname(TALLOC_CTX
*mem_ctx
, const char *dir
, char **parent
,
1948 p
= strrchr_m(dir
, '/'); /* Find final '/', if any */
1951 if (!(*parent
= talloc_strdup(mem_ctx
, "."))) {
1962 if (!(*parent
= (char *)TALLOC_MEMDUP(mem_ctx
, dir
, len
+1))) {
1965 (*parent
)[len
] = '\0';
1973 /*******************************************************************
1974 Determine if a pattern contains any Microsoft wildcard characters.
1975 *******************************************************************/
1977 bool ms_has_wild(const char *s
)
1981 if (lp_posix_pathnames()) {
1982 /* With posix pathnames no characters are wild. */
1986 while ((c
= *s
++)) {
1999 bool ms_has_wild_w(const smb_ucs2_t
*s
)
2002 if (!s
) return False
;
2003 while ((c
= *s
++)) {
2005 case UCS2_CHAR('*'):
2006 case UCS2_CHAR('?'):
2007 case UCS2_CHAR('<'):
2008 case UCS2_CHAR('>'):
2009 case UCS2_CHAR('"'):
2016 /*******************************************************************
2017 A wrapper that handles case sensitivity and the special handling
2019 *******************************************************************/
2021 bool mask_match(const char *string
, const char *pattern
, bool is_case_sensitive
)
2023 if (ISDOTDOT(string
))
2028 return ms_fnmatch(pattern
, string
, Protocol
<= PROTOCOL_LANMAN2
, is_case_sensitive
) == 0;
2031 /*******************************************************************
2032 A wrapper that handles case sensitivity and the special handling
2033 of the ".." name. Varient that is only called by old search code which requires
2034 pattern translation.
2035 *******************************************************************/
2037 bool mask_match_search(const char *string
, const char *pattern
, bool is_case_sensitive
)
2039 if (ISDOTDOT(string
))
2044 return ms_fnmatch(pattern
, string
, True
, is_case_sensitive
) == 0;
2047 /*******************************************************************
2048 A wrapper that handles a list of patters and calls mask_match()
2049 on each. Returns True if any of the patterns match.
2050 *******************************************************************/
2052 bool mask_match_list(const char *string
, char **list
, int listLen
, bool is_case_sensitive
)
2054 while (listLen
-- > 0) {
2055 if (mask_match(string
, *list
++, is_case_sensitive
))
2061 /*********************************************************
2062 Recursive routine that is called by unix_wild_match.
2063 *********************************************************/
2065 static bool unix_do_match(const char *regexp
, const char *str
)
2069 for( p
= regexp
; *p
&& *str
; ) {
2080 * Look for a character matching
2081 * the one after the '*'.
2085 return true; /* Automatic match */
2088 while(*str
&& (*p
!= *str
))
2092 * Patch from weidel@multichart.de. In the case of the regexp
2093 * '*XX*' we want to ensure there are at least 2 'X' characters
2094 * in the string after the '*' for a match to be made.
2101 * Eat all the characters that match, but count how many there were.
2104 while(*str
&& (*p
== *str
)) {
2110 * Now check that if the regexp had n identical characters that
2111 * matchcount had at least that many matches.
2114 while ( *(p
+1) && (*(p
+1) == *p
)) {
2119 if ( matchcount
<= 0 )
2123 str
--; /* We've eaten the match char after the '*' */
2125 if(unix_do_match(p
, str
))
2147 if (!*p
&& str
[0] == '.' && str
[1] == 0)
2150 if (!*str
&& *p
== '?') {
2156 if(!*str
&& (*p
== '*' && p
[1] == '\0'))
2162 /*******************************************************************
2163 Simple case insensitive interface to a UNIX wildcard matcher.
2164 Returns True if match, False if not.
2165 *******************************************************************/
2167 bool unix_wild_match(const char *pattern
, const char *string
)
2169 TALLOC_CTX
*ctx
= talloc_stackframe();
2175 p2
= talloc_strdup(ctx
,pattern
);
2176 s2
= talloc_strdup(ctx
,string
);
2184 /* Remove any *? and ** from the pattern as they are meaningless */
2185 for(p
= p2
; *p
; p
++) {
2186 while( *p
== '*' && (p
[1] == '?' ||p
[1] == '*')) {
2187 memmove(&p
[1], &p
[2], strlen(&p
[2])+1);
2191 if (strequal(p2
,"*")) {
2196 ret
= unix_do_match(p2
, s2
);
2201 /**********************************************************************
2202 Converts a name to a fully qualified domain name.
2203 Returns true if lookup succeeded, false if not (then fqdn is set to name)
2204 Note we deliberately use gethostbyname here, not getaddrinfo as we want
2205 to examine the h_aliases and I don't know how to do that with getaddrinfo.
2206 ***********************************************************************/
2208 bool name_to_fqdn(fstring fqdn
, const char *name
)
2211 struct hostent
*hp
= gethostbyname(name
);
2213 if (!hp
|| !hp
->h_name
|| !*hp
->h_name
) {
2214 DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name
));
2215 fstrcpy(fqdn
, name
);
2219 /* Find out if the fqdn is returned as an alias
2220 * to cope with /etc/hosts files where the first
2221 * name is not the fqdn but the short name */
2222 if (hp
->h_aliases
&& (! strchr_m(hp
->h_name
, '.'))) {
2224 for (i
= 0; hp
->h_aliases
[i
]; i
++) {
2225 if (strchr_m(hp
->h_aliases
[i
], '.')) {
2226 full
= hp
->h_aliases
[i
];
2231 if (full
&& (StrCaseCmp(full
, "localhost.localdomain") == 0)) {
2232 DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
2233 DEBUGADD(1, (" Specifing the machine hostname for address 127.0.0.1 may lead\n"));
2234 DEBUGADD(1, (" to Kerberos authentication problems as localhost.localdomain\n"));
2235 DEBUGADD(1, (" may end up being used instead of the real machine FQDN.\n"));
2242 DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name
, full
));
2243 fstrcpy(fqdn
, full
);
2247 /**********************************************************************
2248 Append a DATA_BLOB to a talloc'ed object
2249 ***********************************************************************/
2251 void *talloc_append_blob(TALLOC_CTX
*mem_ctx
, void *buf
, DATA_BLOB blob
)
2253 size_t old_size
= 0;
2256 if (blob
.length
== 0) {
2261 old_size
= talloc_get_size(buf
);
2264 result
= (char *)TALLOC_REALLOC(mem_ctx
, buf
, old_size
+ blob
.length
);
2265 if (result
== NULL
) {
2269 memcpy(result
+ old_size
, blob
.data
, blob
.length
);
2273 uint32
map_share_mode_to_deny_mode(uint32 share_access
, uint32 private_options
)
2275 switch (share_access
& ~FILE_SHARE_DELETE
) {
2276 case FILE_SHARE_NONE
:
2278 case FILE_SHARE_READ
:
2280 case FILE_SHARE_WRITE
:
2282 case FILE_SHARE_READ
|FILE_SHARE_WRITE
:
2285 if (private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
) {
2287 } else if (private_options
& NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
) {
2294 pid_t
procid_to_pid(const struct server_id
*proc
)
2299 static uint32 my_vnn
= NONCLUSTER_VNN
;
2301 void set_my_vnn(uint32 vnn
)
2303 DEBUG(10, ("vnn pid %d = %u\n", (int)sys_getpid(), (unsigned int)vnn
));
2307 uint32
get_my_vnn(void)
2312 static uint64_t my_unique_id
= 0;
2314 void set_my_unique_id(uint64_t unique_id
)
2316 my_unique_id
= unique_id
;
2319 struct server_id
pid_to_procid(pid_t pid
)
2321 struct server_id result
;
2323 result
.unique_id
= my_unique_id
;
2324 result
.vnn
= my_vnn
;
2328 struct server_id
procid_self(void)
2330 return pid_to_procid(sys_getpid());
2333 bool procid_equal(const struct server_id
*p1
, const struct server_id
*p2
)
2335 if (p1
->pid
!= p2
->pid
)
2337 if (p1
->vnn
!= p2
->vnn
)
2342 bool cluster_id_equal(const struct server_id
*id1
,
2343 const struct server_id
*id2
)
2345 return procid_equal(id1
, id2
);
2348 bool procid_is_me(const struct server_id
*pid
)
2350 if (pid
->pid
!= sys_getpid())
2352 if (pid
->vnn
!= my_vnn
)
2357 struct server_id
interpret_pid(const char *pid_string
)
2359 struct server_id result
;
2362 if (sscanf(pid_string
, "%u:%d", &vnn
, &pid
) == 2) {
2366 else if (sscanf(pid_string
, "%d", &pid
) == 1) {
2367 result
.vnn
= get_my_vnn();
2371 result
.vnn
= NONCLUSTER_VNN
;
2374 /* Assigning to result.pid may have overflowed
2375 Map negative pid to -1: i.e. error */
2376 if (result
.pid
< 0) {
2379 result
.unique_id
= 0;
2383 char *procid_str(TALLOC_CTX
*mem_ctx
, const struct server_id
*pid
)
2385 if (pid
->vnn
== NONCLUSTER_VNN
) {
2386 return talloc_asprintf(mem_ctx
,
2391 return talloc_asprintf(mem_ctx
,
2398 char *procid_str_static(const struct server_id
*pid
)
2400 return procid_str(talloc_tos(), pid
);
2403 bool procid_valid(const struct server_id
*pid
)
2405 return (pid
->pid
!= -1);
2408 bool procid_is_local(const struct server_id
*pid
)
2410 return pid
->vnn
== my_vnn
;
2413 /****************************************************************
2414 Check if offset/length fit into bufsize. Should probably be
2415 merged with is_offset_safe, but this would require a rewrite
2416 of lanman.c. Later :-)
2417 ****************************************************************/
2419 bool trans_oob(uint32_t bufsize
, uint32_t offset
, uint32_t length
)
2421 if ((offset
+ length
< offset
) || (offset
+ length
< length
)) {
2425 if ((offset
> bufsize
) || (offset
+ length
> bufsize
)) {
2432 /****************************************************************
2433 Check if an offset into a buffer is safe.
2434 If this returns True it's safe to indirect into the byte at
2436 ****************************************************************/
2438 bool is_offset_safe(const char *buf_base
, size_t buf_len
, char *ptr
, size_t off
)
2440 const char *end_base
= buf_base
+ buf_len
;
2441 char *end_ptr
= ptr
+ off
;
2443 if (!buf_base
|| !ptr
) {
2447 if (end_base
< buf_base
|| end_ptr
< ptr
) {
2448 return False
; /* wrap. */
2451 if (end_ptr
< end_base
) {
2457 /****************************************************************
2458 Return a safe pointer into a buffer, or NULL.
2459 ****************************************************************/
2461 char *get_safe_ptr(const char *buf_base
, size_t buf_len
, char *ptr
, size_t off
)
2463 return is_offset_safe(buf_base
, buf_len
, ptr
, off
) ?
2467 /****************************************************************
2468 Return a safe pointer into a string within a buffer, or NULL.
2469 ****************************************************************/
2471 char *get_safe_str_ptr(const char *buf_base
, size_t buf_len
, char *ptr
, size_t off
)
2473 if (!is_offset_safe(buf_base
, buf_len
, ptr
, off
)) {
2476 /* Check if a valid string exists at this offset. */
2477 if (skip_string(buf_base
,buf_len
, ptr
+ off
) == NULL
) {
2483 /****************************************************************
2484 Return an SVAL at a pointer, or failval if beyond the end.
2485 ****************************************************************/
2487 int get_safe_SVAL(const char *buf_base
, size_t buf_len
, char *ptr
, size_t off
, int failval
)
2490 * Note we use off+1 here, not off+2 as SVAL accesses ptr[0] and ptr[1],
2493 if (!is_offset_safe(buf_base
, buf_len
, ptr
, off
+1)) {
2496 return SVAL(ptr
,off
);
2499 /****************************************************************
2500 Return an IVAL at a pointer, or failval if beyond the end.
2501 ****************************************************************/
2503 int get_safe_IVAL(const char *buf_base
, size_t buf_len
, char *ptr
, size_t off
, int failval
)
2506 * Note we use off+3 here, not off+4 as IVAL accesses
2507 * ptr[0] ptr[1] ptr[2] ptr[3] NOT ptr[4].
2509 if (!is_offset_safe(buf_base
, buf_len
, ptr
, off
+3)) {
2512 return IVAL(ptr
,off
);
2515 /****************************************************************
2516 Split DOM\user into DOM and user. Do not mix with winbind variants of that
2517 call (they take care of winbind separator and other winbind specific settings).
2518 ****************************************************************/
2520 void split_domain_user(TALLOC_CTX
*mem_ctx
,
2521 const char *full_name
,
2525 const char *p
= NULL
;
2527 p
= strchr_m(full_name
, '\\');
2530 *domain
= talloc_strndup(mem_ctx
, full_name
,
2531 PTR_DIFF(p
, full_name
));
2532 *user
= talloc_strdup(mem_ctx
, p
+1);
2534 *domain
= talloc_strdup(mem_ctx
, "");
2535 *user
= talloc_strdup(mem_ctx
, full_name
);
2541 Disable these now we have checked all code paths
and ensured
2542 NULL returns on zero request
. JRA
.
2544 /****************************************************************
2545 talloc wrapper functions that guarentee a null pointer return
2547 ****************************************************************/
2549 #ifndef MAX_TALLOC_SIZE
2550 #define MAX_TALLOC_SIZE 0x10000000
2554 * talloc and zero memory.
2555 * - returns NULL if size is zero.
2558 void *_talloc_zero_zeronull(const void *ctx
, size_t size
, const char *name
)
2566 p
= talloc_named_const(ctx
, size
, name
);
2569 memset(p
, '\0', size
);
2576 * memdup with a talloc.
2577 * - returns NULL if size is zero.
2580 void *_talloc_memdup_zeronull(const void *t
, const void *p
, size_t size
, const char *name
)
2588 newp
= talloc_named_const(t
, size
, name
);
2590 memcpy(newp
, p
, size
);
2597 * alloc an array, checking for integer overflow in the array size.
2598 * - returns NULL if count or el_size are zero.
2601 void *_talloc_array_zeronull(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
2603 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
2607 if (el_size
== 0 || count
== 0) {
2611 return talloc_named_const(ctx
, el_size
* count
, name
);
2615 * alloc an zero array, checking for integer overflow in the array size
2616 * - returns NULL if count or el_size are zero.
2619 void *_talloc_zero_array_zeronull(const void *ctx
, size_t el_size
, unsigned count
, const char *name
)
2621 if (count
>= MAX_TALLOC_SIZE
/el_size
) {
2625 if (el_size
== 0 || count
== 0) {
2629 return _talloc_zero(ctx
, el_size
* count
, name
);
2633 * Talloc wrapper that returns NULL if size == 0.
2635 void *talloc_zeronull(const void *context
, size_t size
, const char *name
)
2640 return talloc_named_const(context
, size
, name
);
2644 /****************************************************************
2645 strip off leading '\\' from a hostname
2646 ****************************************************************/
2648 const char *strip_hostname(const char *s
)
2654 if (strlen_m(s
) < 3) {
2658 if (s
[0] == '\\') s
++;
2659 if (s
[0] == '\\') s
++;
2664 bool tevent_req_poll_ntstatus(struct tevent_req
*req
,
2665 struct tevent_context
*ev
,
2668 bool ret
= tevent_req_poll(req
, ev
);
2670 *status
= map_nt_error_from_unix(errno
);
2675 bool any_nt_status_not_ok(NTSTATUS err1
, NTSTATUS err2
, NTSTATUS
*result
)
2677 if (!NT_STATUS_IS_OK(err1
)) {
2681 if (!NT_STATUS_IS_OK(err2
)) {
2688 int timeval_to_msec(struct timeval t
)
2690 return t
.tv_sec
* 1000 + (t
.tv_usec
+999) / 1000;
2693 /*******************************************************************
2694 Check a given DOS pathname is valid for a share.
2695 ********************************************************************/
2697 char *valid_share_pathname(TALLOC_CTX
*ctx
, const char *dos_pathname
)
2701 if (!dos_pathname
) {
2705 ptr
= talloc_strdup(ctx
, dos_pathname
);
2709 /* Convert any '\' paths to '/' */
2711 ptr
= unix_clean_name(ctx
, ptr
);
2716 /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
2717 if (strlen(ptr
) > 2 && ptr
[1] == ':' && ptr
[0] != '/')
2720 /* Only absolute paths allowed. */
2727 /*******************************************************************
2728 Return True if the filename is one of the special executable types.
2729 ********************************************************************/
2731 bool is_executable(const char *fname
)
2733 if ((fname
= strrchr_m(fname
,'.'))) {
2734 if (strequal(fname
,".com") ||
2735 strequal(fname
,".dll") ||
2736 strequal(fname
,".exe") ||
2737 strequal(fname
,".sym")) {
2744 /****************************************************************************
2745 Open a file with a share mode - old openX method - map into NTCreate.
2746 ****************************************************************************/
2748 bool map_open_params_to_ntcreate(const char *smb_base_fname
,
2749 int deny_mode
, int open_func
,
2750 uint32
*paccess_mask
,
2751 uint32
*pshare_mode
,
2752 uint32
*pcreate_disposition
,
2753 uint32
*pcreate_options
,
2754 uint32_t *pprivate_flags
)
2758 uint32 create_disposition
;
2759 uint32 create_options
= FILE_NON_DIRECTORY_FILE
;
2760 uint32_t private_flags
= 0;
2762 DEBUG(10,("map_open_params_to_ntcreate: fname = %s, deny_mode = 0x%x, "
2763 "open_func = 0x%x\n",
2764 smb_base_fname
, (unsigned int)deny_mode
,
2765 (unsigned int)open_func
));
2767 /* Create the NT compatible access_mask. */
2768 switch (GET_OPENX_MODE(deny_mode
)) {
2769 case DOS_OPEN_EXEC
: /* Implies read-only - used to be FILE_READ_DATA */
2770 case DOS_OPEN_RDONLY
:
2771 access_mask
= FILE_GENERIC_READ
;
2773 case DOS_OPEN_WRONLY
:
2774 access_mask
= FILE_GENERIC_WRITE
;
2778 access_mask
= FILE_GENERIC_READ
|FILE_GENERIC_WRITE
;
2781 DEBUG(10,("map_open_params_to_ntcreate: bad open mode = 0x%x\n",
2782 (unsigned int)GET_OPENX_MODE(deny_mode
)));
2786 /* Create the NT compatible create_disposition. */
2787 switch (open_func
) {
2788 case OPENX_FILE_EXISTS_FAIL
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
2789 create_disposition
= FILE_CREATE
;
2792 case OPENX_FILE_EXISTS_OPEN
:
2793 create_disposition
= FILE_OPEN
;
2796 case OPENX_FILE_EXISTS_OPEN
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
2797 create_disposition
= FILE_OPEN_IF
;
2800 case OPENX_FILE_EXISTS_TRUNCATE
:
2801 create_disposition
= FILE_OVERWRITE
;
2804 case OPENX_FILE_EXISTS_TRUNCATE
|OPENX_FILE_CREATE_IF_NOT_EXIST
:
2805 create_disposition
= FILE_OVERWRITE_IF
;
2809 /* From samba4 - to be confirmed. */
2810 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_EXEC
) {
2811 create_disposition
= FILE_CREATE
;
2814 DEBUG(10,("map_open_params_to_ntcreate: bad "
2815 "open_func 0x%x\n", (unsigned int)open_func
));
2819 /* Create the NT compatible share modes. */
2820 switch (GET_DENY_MODE(deny_mode
)) {
2822 share_mode
= FILE_SHARE_NONE
;
2826 share_mode
= FILE_SHARE_READ
;
2830 share_mode
= FILE_SHARE_WRITE
;
2834 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
2838 private_flags
|= NTCREATEX_OPTIONS_PRIVATE_DENY_DOS
;
2839 if (is_executable(smb_base_fname
)) {
2840 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
2842 if (GET_OPENX_MODE(deny_mode
) == DOS_OPEN_RDONLY
) {
2843 share_mode
= FILE_SHARE_READ
;
2845 share_mode
= FILE_SHARE_NONE
;
2851 private_flags
|= NTCREATEX_OPTIONS_PRIVATE_DENY_FCB
;
2852 share_mode
= FILE_SHARE_NONE
;
2856 DEBUG(10,("map_open_params_to_ntcreate: bad deny_mode 0x%x\n",
2857 (unsigned int)GET_DENY_MODE(deny_mode
) ));
2861 DEBUG(10,("map_open_params_to_ntcreate: file %s, access_mask = 0x%x, "
2862 "share_mode = 0x%x, create_disposition = 0x%x, "
2863 "create_options = 0x%x private_flags = 0x%x\n",
2865 (unsigned int)access_mask
,
2866 (unsigned int)share_mode
,
2867 (unsigned int)create_disposition
,
2868 (unsigned int)create_options
,
2869 (unsigned int)private_flags
));
2872 *paccess_mask
= access_mask
;
2875 *pshare_mode
= share_mode
;
2877 if (pcreate_disposition
) {
2878 *pcreate_disposition
= create_disposition
;
2880 if (pcreate_options
) {
2881 *pcreate_options
= create_options
;
2883 if (pprivate_flags
) {
2884 *pprivate_flags
= private_flags
;