4 Unix SMB/CIFS implementation.
5 Machine customisation and include handling
6 Copyright (C) Andrew Tridgell 1994-1998
7 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #ifndef NO_CONFIG_H /* for some tests */
26 #include "smb_build.h"
32 #define DEFAULT_PRINTING PRINT_AIX
33 #define PRINTCAP_NAME "/etc/qconfig"
37 #define DEFAULT_PRINTING PRINT_HPUX
41 #define DEFAULT_PRINTING PRINT_QNX
45 /* on SUNOS4 termios.h conflicts with sys/ioctl.h */
49 #ifndef DEFAULT_PRINTING
50 #define DEFAULT_PRINTING PRINT_BSD
53 #define PRINTCAP_NAME "/etc/printcap"
57 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
58 * the parameter containing the format, and a2 the index of the first
60 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
62 #define PRINTF_ATTRIBUTE(a1, a2)
66 /** gcc attribute used on function parameters so that it does not emit
67 * warnings about them being unused. **/
68 # define UNUSED(param) param __attribute__ ((unused))
70 # define UNUSED(param) param
71 /** Feel free to add definitions for other compilers here. */
76 * <unistd.h> has to be included before any other to get
77 * large file support on Reliant UNIX. Yes, it's broken :-).
82 #endif /* RELIANTUNIX */
84 #include <sys/types.h>
86 #ifdef TIME_WITH_SYS_TIME
90 #ifdef HAVE_SYS_TIME_H
97 #ifdef HAVE_SYS_RESOURCE_H
98 #include <sys/resource.h>
108 #ifdef HAVE_SYS_PARAM_H
109 #include <sys/param.h>
116 #ifdef HAVE_SYS_SOCKET_H
117 #include <sys/socket.h>
120 #ifdef HAVE_UNIXSOCKET
124 #ifdef HAVE_SYS_SYSCALL_H
125 #include <sys/syscall.h>
134 #ifdef HAVE_STRINGS_H
149 #ifdef HAVE_SYS_FCNTL_H
150 #include <sys/fcntl.h>
154 #include <sys/stat.h>
160 #ifdef HAVE_SYS_IOCTL_H
161 #include <sys/ioctl.h>
164 #ifdef HAVE_SYS_FILIO_H
165 #include <sys/filio.h>
170 #ifdef HAVE_SYS_WAIT_H
171 #include <sys/wait.h>
179 #ifdef HAVE_SYS_PRIV_H
180 #include <sys/priv.h>
192 #ifdef HAVE_SYS_SELECT_H
193 #include <sys/select.h>
196 #ifdef HAVE_SYS_MODE_H
197 /* apparently AIX needs this for S_ISLNK */
199 #include <sys/mode.h>
215 #include <netinet/in.h>
216 #include <arpa/inet.h>
222 #ifdef HAVE_SYS_SYSLOG_H
223 #include <sys/syslog.h>
227 #include <sys/file.h>
229 #ifdef HAVE_NETINET_TCP_H
230 #include <netinet/tcp.h>
234 * The next three defines are needed to access the IPTOS_* options
238 #ifdef HAVE_NETINET_IN_SYSTM_H
239 #include <netinet/in_systm.h>
242 #ifdef HAVE_NETINET_IN_IP_H
243 #include <netinet/in_ip.h>
246 #ifdef HAVE_NETINET_IP_H
247 #include <netinet/ip.h>
250 #if defined(HAVE_TERMIOS_H)
251 /* POSIX terminal handling. */
253 #elif defined(HAVE_TERMIO_H)
254 /* Older SYSV terminal handling - don't use if we can avoid it. */
256 #elif defined(HAVE_SYS_TERMIO_H)
257 /* Older SYSV terminal handling - don't use if we can avoid it. */
258 #include <sys/termio.h>
263 # define NAMLEN(dirent) strlen((dirent)->d_name)
265 # define dirent direct
266 # define NAMLEN(dirent) (dirent)->d_namlen
268 # include <sys/ndir.h>
271 # include <sys/dir.h>
278 #ifdef HAVE_SYS_MMAN_H
279 #include <sys/mman.h>
287 #ifdef HAVE_SYS_MOUNT_H
288 #include <sys/mount.h>
291 #ifdef HAVE_SYS_VFS_H
295 #ifdef HAVE_SYS_ACL_H
299 #ifdef HAVE_SYS_FS_S5PARAM_H
300 #include <sys/fs/s5param.h>
303 #if defined (HAVE_SYS_FILSYS_H) && !defined (_CRAY)
304 #include <sys/filsys.h>
307 #ifdef HAVE_SYS_STATFS_H
308 # include <sys/statfs.h>
312 #include <sys/dustat.h>
315 #ifdef HAVE_SYS_STATVFS_H
316 #include <sys/statvfs.h>
323 #ifdef HAVE_GETPWANAM
324 #include <sys/label.h>
325 #include <sys/audit.h>
329 #ifdef HAVE_SYS_SECURITY_H
330 #include <sys/security.h>
332 #define PASSWORD_LENGTH 16
333 #endif /* HAVE_SYS_SECURITY_H */
339 #ifdef HAVE_STROPTS_H
343 #ifdef HAVE_SYS_CAPABILITY_H
345 #if defined(BROKEN_REDHAT_7_SYSTEM_HEADERS) && !defined(_I386_STATFS_H)
346 #define _I386_STATFS_H
347 #define BROKEN_REDHAT_7_STATFS_WORKAROUND
350 #include <sys/capability.h>
352 #ifdef BROKEN_REDHAT_7_STATFS_WORKAROUND
353 #undef _I386_STATFS_H
354 #undef BROKEN_REDHAT_7_STATFS_WORKAROUND
359 #if defined(HAVE_SYS_IPC_H)
361 #endif /* HAVE_SYS_IPC_H */
363 #if defined(HAVE_SYS_SHM_H)
365 #endif /* HAVE_SYS_SHM_H */
367 #ifdef HAVE_NATIVE_ICONV
386 #if HAVE_GSSAPI_GSSAPI_H
387 #include <gssapi/gssapi.h>
390 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
391 #include <gssapi/gssapi_generic.h>
398 /* we support ADS if we want it and have krb5 and ldap libs */
399 #if defined(WITH_ADS) && defined(HAVE_KRB5) && defined(HAVE_LDAP)
404 * Define VOLATILE if needed.
407 #if defined(HAVE_VOLATILE)
408 #define VOLATILE volatile
414 * Define additional missing types
416 #ifndef HAVE_SIG_ATOMIC_T_TYPE
417 typedef int sig_atomic_t;
420 #ifndef HAVE_SOCKLEN_T_TYPE
421 typedef int socklen_t;
426 Samba needs type definitions for
427 int8_t, int16_t, int32_t, int64_t
428 uint8_t, uint16_t, uint32_t and uint64_t.
430 Normally these are signed and unsigned 8, 16, 32 and 64 bit integers, but
431 they actually only need to be at least 8, 16, 32 and 64 bits
432 respectively. Thus if your word size is 8 bytes just defining them
433 as signed and unsigned int will work.
441 #define uint8 uint8_t
445 #define int16 int16_t
449 #define uint16 uint16_t
453 #define int32 int32_t
457 #define uint32 uint32_t
461 #define int64 int64_t
465 #define uint64 uint64_t
469 * Types for devices, inodes and offsets.
473 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_DEV64_T)
474 # define SMB_DEV_T dev64_t
476 # define SMB_DEV_T dev_t
481 * Setup the correctly sized inode type.
485 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_INO64_T)
486 # define SMB_INO_T ino64_t
488 # define SMB_INO_T ino_t
492 #ifndef LARGE_SMB_INO_T
493 # if (defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_INO64_T)) || (defined(SIZEOF_INO_T) && (SIZEOF_INO_T == 8))
494 # define LARGE_SMB_INO_T 1
498 #ifdef LARGE_SMB_INO_T
499 #define SINO_T(p, ofs, v) (SIVAL(p,ofs,(v)&0xFFFFFFFF), SIVAL(p,(ofs)+4,(v)>>32))
501 #define SINO_T(p, ofs, v) (SIVAL(p,ofs,v),SIVAL(p,(ofs)+4,0))
505 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T)
506 # define SMB_OFF_T off64_t
508 # define SMB_OFF_T off_t
513 * Set the define that tells us if we can do 64 bit
517 #ifndef LARGE_SMB_OFF_T
518 # if (defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T)) || (defined(SIZEOF_OFF_T) && (SIZEOF_OFF_T == 8))
519 # define LARGE_SMB_OFF_T 1
524 * Type for stat structure.
527 #ifndef SMB_STRUCT_STAT
528 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STAT64) && defined(HAVE_OFF64_T)
529 # define SMB_STRUCT_STAT struct stat64
531 # define SMB_STRUCT_STAT struct stat
536 * Type for dirent structure.
539 #ifndef SMB_STRUCT_DIRENT
540 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STRUCT_DIRENT64)
541 # define smb_dirent dirent64
543 # define smb_dirent dirent
548 * Defines for 64 bit fcntl locks.
551 #ifndef SMB_STRUCT_FLOCK
552 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STRUCT_FLOCK64) && defined(HAVE_OFF64_T)
553 # define SMB_STRUCT_FLOCK struct flock64
555 # define SMB_STRUCT_FLOCK struct flock
560 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STRUCT_FLOCK64) && defined(HAVE_OFF64_T)
561 # define SMB_F_SETLKW F_SETLKW64
563 # define SMB_F_SETLKW F_SETLKW
568 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STRUCT_FLOCK64) && defined(HAVE_OFF64_T)
569 # define SMB_F_SETLK F_SETLK64
571 # define SMB_F_SETLK F_SETLK
576 # if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_STRUCT_FLOCK64) && defined(HAVE_OFF64_T)
577 # define SMB_F_GETLK F_GETLK64
579 # define SMB_F_GETLK F_GETLK
584 #define MIN(a,b) ((a)<(b)?(a):(b))
588 #define MAX(a,b) ((a)>(b)?(a):(b))
591 #ifndef HAVE_STRERROR
592 extern char *sys_errlist[];
593 #define strerror(i) sys_errlist[i]
596 #ifndef HAVE_ERRNO_DECL
600 #ifdef HAVE_BROKEN_GETGROUPS
607 #define NGROUPS_MAX 32 /* Guess... */
610 /* Our own pstrings and fstrings */
613 /* Lists, trees, caching, database... */
615 #include "dlinklist.h"
616 #include "lib/ldb/include/ldb.h"
617 #include "lib/tdb/include/tdb.h"
618 #include "lib/tdb/include/spinlock.h"
619 #include "lib/tdb/include/tdbutil.h"
621 #include "nt_status.h"
622 #include "interfaces.h"
626 #include "messages.h"
628 #include "dynconfig.h"
634 #include "libcli/ldap/ldap.h"
635 #include "nameserv.h"
638 #include "byteorder.h"
643 #include "libcli/auth/ntlmssp.h"
644 #include "libcli/auth/credentials.h"
645 #include "libcli/auth/schannel.h"
646 #include "libcli/auth/kerberos.h"
647 #include "libcli/auth/gensec.h"
648 #include "libcli/auth/spnego.h"
650 #include "auth/auth.h"
660 #include "librpc/rpc/dcerpc.h"
662 #include "smbd/server.h"
663 #include "smbd/service.h"
664 #include "rpc_server/dcerpc_server.h"
667 #include "smb_server/smb_server.h"
668 #include "ntvfs/ntvfs.h"
669 #include "cli_context.h"
670 #include "registry.h"
672 #include "gtk/common/gtk-smb.h"
673 #include "gtk/common/select.h"
675 #include "libnet/libnet.h"
677 #define malloc_p(type) (type *)malloc(sizeof(type))
678 #define malloc_array_p(type, count) (type *)realloc_array(NULL, sizeof(type), count)
679 #define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count)
681 #ifndef HAVE_COMPARISON_FN_T
682 typedef int (*comparison_fn_t)(const void *, const void *);
685 /***** automatically generated prototypes *****/
686 #define _PRINTF_ATTRIBUTE(a1, a2) PRINTF_ATTRIBUTE(a1, a2)
688 #undef _PRINTF_ATTRIBUTE
689 #define _PRINTF_ATTRIBUTE(a1, a2)
691 /* String routines */
693 #include "safe_string.h"
696 #define QSORT_CAST (__compar_fn_t)
700 #define QSORT_CAST (int (*)(const void *, const void *))
704 #define SIGCLD SIGCHLD
711 #if defined(HAVE_PUTPRPWNAM) && defined(AUTH_CLEARTEXT_SEG_CHARS)
712 #define OSF1_ENH_SEC 1
715 #ifndef ALLOW_CHANGE_PASSWORD
716 #if (defined(HAVE_TERMIOS_H) && defined(HAVE_DUP2) && defined(HAVE_SETSID))
717 #define ALLOW_CHANGE_PASSWORD 1
721 /* what is the longest significant password available on your system?
722 Knowing this speeds up password searches a lot */
723 #ifndef PASSWORD_LENGTH
724 #define PASSWORD_LENGTH 8
727 #ifdef REPLACE_INET_NTOA
728 #define inet_ntoa rep_inet_ntoa
736 #define MAXPATHLEN 256
743 #ifndef INADDR_LOOPBACK
744 #define INADDR_LOOPBACK 0x7f000001
748 #define INADDR_NONE 0xffffffff
752 #define crypt ufc_crypt
756 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
759 #if defined(HAVE_CRYPT16) && defined(HAVE_GETAUTHUID)
760 #define ULTRIX_AUTH 1
764 char *strdup(const char *s);
768 void *memmove(void *dest,const void *src,int size);
771 #ifndef HAVE_INITGROUPS
772 int initgroups(char *name,gid_t id);
776 int rename(const char *zfrom, const char *zto);
780 time_t mktime(struct tm *t);
784 size_t strlcpy(char *d, const char *s, size_t bufsize);
788 size_t strlcat(char *d, const char *s, size_t bufsize);
791 #ifndef HAVE_FTRUNCATE
792 int ftruncate(int f,long l);
796 char *strndup(const char *s, size_t n);
800 size_t strnlen(const char *s, size_t n);
804 unsigned long strtoul(const char *nptr, char **endptr, int base);
808 int setenv(const char *name, const char *value, int overwrite);
811 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESUID_DECL))
813 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
815 #if (defined(USE_SETRESUID) && !defined(HAVE_SETRESGID_DECL))
816 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
818 #ifndef HAVE_VASPRINTF_DECL
819 int vasprintf(char **ptr, const char *format, va_list ap);
822 #if !defined(HAVE_BZERO) && defined(HAVE_MEMSET)
823 #define bzero(a,b) memset((a),'\0',(b))
826 #ifdef REPLACE_GETPASS
827 #define getpass(prompt) getsmbpass((prompt))
831 * Some older systems seem not to have MAXHOSTNAMELEN
834 #ifndef MAXHOSTNAMELEN
835 #define MAXHOSTNAMELEN 254
838 /* yuck, I'd like a better way of doing this */
839 #define DIRP_SIZE (256 + 32)
842 * glibc on linux doesn't seem to have MSG_WAITALL
843 * defined. I think the kernel has it though..
847 #define MSG_WAITALL 0
850 /* default socket options. Dave Miller thinks we should default to TCP_NODELAY
851 given the socket IO pattern that Samba uses */
853 #define DEFAULT_SOCKET_OPTIONS "TCP_NODELAY"
855 #define DEFAULT_SOCKET_OPTIONS ""
858 /* Load header file for dynamic linking stuff */
864 /* dmalloc -- free heap debugger (dmalloc.org). This should be near
865 * the *bottom* of include files so as not to conflict. */
866 #ifdef ENABLE_DMALLOC
867 # include <dmalloc.h>
871 /* Some POSIX definitions for those without */
874 #define S_IFDIR 0x4000
877 #define S_ISDIR(mode) ((mode & 0xF000) == S_IFDIR)
880 #define S_IRWXU 00700 /* read, write, execute: owner */
883 #define S_IRUSR 00400 /* read permission: owner */
886 #define S_IWUSR 00200 /* write permission: owner */
889 #define S_IXUSR 00100 /* execute permission: owner */
892 #define S_IRWXG 00070 /* read, write, execute: group */
895 #define S_IRGRP 00040 /* read permission: group */
898 #define S_IWGRP 00020 /* write permission: group */
901 #define S_IXGRP 00010 /* execute permission: group */
904 #define S_IRWXO 00007 /* read, write, execute: other */
907 #define S_IROTH 00004 /* read permission: other */
910 #define S_IWOTH 00002 /* write permission: other */
913 #define S_IXOTH 00001 /* execute permission: other */
916 /* For sys_adminlog(). */
918 #define LOG_EMERG 0 /* system is unusable */
922 #define LOG_ALERT 1 /* action must be taken immediately */
926 #define LOG_CRIT 2 /* critical conditions */
930 #define LOG_ERR 3 /* error conditions */
934 #define LOG_WARNING 4 /* warning conditions */
938 #define LOG_NOTICE 5 /* normal but significant condition */
942 #define LOG_INFO 6 /* informational */
946 #define LOG_DEBUG 7 /* debug-level messages */
949 /* NetBSD doesn't have these */
958 #if HAVE_KERNEL_SHARE_MODES
960 #define LOCK_MAND 32 /* This is a mandatory flock */
961 #define LOCK_READ 64 /* ... Which allows concurrent read operations */
962 #define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
963 #define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
967 extern int DEBUGLEVEL;
969 #define MAX_SEC_CTX_DEPTH 8 /* Maximum number of security contexts */
972 #ifdef GLIBC_HACK_FCNTL64
973 /* this is a gross hack. 64 bit locking is completely screwed up on
974 i386 Linux in glibc 2.1.95 (which ships with RedHat 7.0). This hack
975 "fixes" the problem with the current 2.4.0test kernels
977 #define fcntl fcntl64
985 /* Needed for sys_dlopen/sys_dlsym/sys_dlclose */
987 #define RTLD_GLOBAL 0
998 /* needed for some systems without iconv. Doesn't really matter
999 what error code we use */
1004 /* add varargs prototypes with printf checking */
1005 int fdprintf(int , const char *, ...) PRINTF_ATTRIBUTE(2,3);
1006 int d_printf(const char *, ...) PRINTF_ATTRIBUTE(1,2);
1007 int d_fprintf(FILE *f, const char *, ...) PRINTF_ATTRIBUTE(2,3);
1008 #ifndef HAVE_SNPRINTF_DECL
1009 int snprintf(char *,size_t ,const char *, ...) PRINTF_ATTRIBUTE(3,4);
1011 #ifndef HAVE_ASPRINTF_DECL
1012 int asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3);
1015 void sys_adminlog(int priority, const char *format_str, ...) PRINTF_ATTRIBUTE(2,3);
1017 int pstr_sprintf(pstring s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1018 int fstr_sprintf(fstring s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
1020 int d_vfprintf(FILE *f, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
1022 int smb_xvasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0);
1024 /* we used to use these fns, but now we have good replacements
1025 for snprintf and vsnprintf */
1026 #define slprintf snprintf
1027 #define vslprintf vsnprintf
1030 /* we need to use __va_copy() on some platforms */
1032 #define VA_COPY(dest, src) __va_copy(dest, src)
1034 #define VA_COPY(dest, src) (dest) = (src)
1038 time_t timegm(struct tm *tm);
1041 #if defined(VALGRIND)
1042 #define strlen(x) valgrind_strlen(x)
1046 * Veritas File System. Often in addition to native.
1049 #if defined(HAVE_SYS_FS_VX_QUOTA_H)
1053 #endif /* _INCLUDES_H */