2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2005
6 Copyright (C) Timur Bakeyev 2005
7 Copyright (C) Bjoern Jacke 2006-2007
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 3 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, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
30 The idea is that this file will eventually have wrappers around all
31 important system calls in samba. The aims are:
33 - to enable easier porting by putting OS dependent stuff in here
35 - to allow for hooks into other "pseudo-filesystems"
37 - to allow easier integration of things like the japanese extensions
39 - to support the philosophy of Samba to expose the features of
40 the OS within the SMB model. In general whatever file/printer/variable
41 expansions/etc make sense to the OS should be acceptable to Samba.
46 /*******************************************************************
47 A wrapper for memalign
48 ********************************************************************/
50 void *sys_memalign( size_t align
, size_t size
)
52 #if defined(HAVE_POSIX_MEMALIGN)
54 int ret
= posix_memalign( &p
, align
, size
);
59 #elif defined(HAVE_MEMALIGN)
60 return memalign( align
, size
);
62 /* On *BSD systems memaligns doesn't exist, but memory will
63 * be aligned on allocations of > pagesize. */
64 #if defined(SYSCONF_SC_PAGESIZE)
65 size_t pagesize
= (size_t)sysconf(_SC_PAGESIZE
);
66 #elif defined(HAVE_GETPAGESIZE)
67 size_t pagesize
= (size_t)getpagesize();
69 size_t pagesize
= (size_t)-1;
71 if (pagesize
== (size_t)-1) {
72 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
75 if (size
< pagesize
) {
78 return SMB_MALLOC(size
);
82 /*******************************************************************
83 A wrapper for usleep in case we don't have one.
84 ********************************************************************/
86 int sys_usleep(long usecs
)
93 * We need this braindamage as the glibc usleep
94 * is not SPEC1170 complient... grumble... JRA.
97 if(usecs
< 0 || usecs
> 1000000) {
105 #else /* HAVE_USLEEP */
107 * Fake it with select...
110 tval
.tv_usec
= usecs
/1000;
111 select(0,NULL
,NULL
,NULL
,&tval
);
113 #endif /* HAVE_USLEEP */
116 /*******************************************************************
117 A read wrapper that will deal with EINTR.
118 ********************************************************************/
120 ssize_t
sys_read(int fd
, void *buf
, size_t count
)
125 ret
= read(fd
, buf
, count
);
126 } while (ret
== -1 && errno
== EINTR
);
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
134 ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
139 ret
= write(fd
, buf
, count
);
140 } while (ret
== -1 && errno
== EINTR
);
144 /*******************************************************************
145 A writev wrapper that will deal with EINTR.
146 ********************************************************************/
148 ssize_t
sys_writev(int fd
, const struct iovec
*iov
, int iovcnt
)
153 ret
= writev(fd
, iov
, iovcnt
);
154 } while (ret
== -1 && errno
== EINTR
);
158 /*******************************************************************
159 A pread wrapper that will deal with EINTR and 64-bit file offsets.
160 ********************************************************************/
162 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
163 ssize_t
sys_pread(int fd
, void *buf
, size_t count
, SMB_OFF_T off
)
168 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
169 ret
= pread64(fd
, buf
, count
, off
);
171 ret
= pread(fd
, buf
, count
, off
);
173 } while (ret
== -1 && errno
== EINTR
);
178 /*******************************************************************
179 A write wrapper that will deal with EINTR and 64-bit file offsets.
180 ********************************************************************/
182 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
183 ssize_t
sys_pwrite(int fd
, const void *buf
, size_t count
, SMB_OFF_T off
)
188 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
189 ret
= pwrite64(fd
, buf
, count
, off
);
191 ret
= pwrite(fd
, buf
, count
, off
);
193 } while (ret
== -1 && errno
== EINTR
);
198 /*******************************************************************
199 A send wrapper that will deal with EINTR.
200 ********************************************************************/
202 ssize_t
sys_send(int s
, const void *msg
, size_t len
, int flags
)
207 ret
= send(s
, msg
, len
, flags
);
208 } while (ret
== -1 && errno
== EINTR
);
212 /*******************************************************************
213 A sendto wrapper that will deal with EINTR.
214 ********************************************************************/
216 ssize_t
sys_sendto(int s
, const void *msg
, size_t len
, int flags
, const struct sockaddr
*to
, socklen_t tolen
)
221 ret
= sendto(s
, msg
, len
, flags
, to
, tolen
);
222 } while (ret
== -1 && errno
== EINTR
);
226 /*******************************************************************
227 A recv wrapper that will deal with EINTR.
228 ********************************************************************/
230 ssize_t
sys_recv(int fd
, void *buf
, size_t count
, int flags
)
235 ret
= recv(fd
, buf
, count
, flags
);
236 } while (ret
== -1 && errno
== EINTR
);
240 /*******************************************************************
241 A recvfrom wrapper that will deal with EINTR.
242 ********************************************************************/
244 ssize_t
sys_recvfrom(int s
, void *buf
, size_t len
, int flags
, struct sockaddr
*from
, socklen_t
*fromlen
)
249 ret
= recvfrom(s
, buf
, len
, flags
, from
, fromlen
);
250 } while (ret
== -1 && errno
== EINTR
);
254 /*******************************************************************
255 A fcntl wrapper that will deal with EINTR.
256 ********************************************************************/
258 int sys_fcntl_ptr(int fd
, int cmd
, void *arg
)
263 ret
= fcntl(fd
, cmd
, arg
);
264 } while (ret
== -1 && errno
== EINTR
);
268 /*******************************************************************
269 A fcntl wrapper that will deal with EINTR.
270 ********************************************************************/
272 int sys_fcntl_long(int fd
, int cmd
, long arg
)
277 ret
= fcntl(fd
, cmd
, arg
);
278 } while (ret
== -1 && errno
== EINTR
);
282 /*******************************************************************
283 A stat() wrapper that will deal with 64 bit filesizes.
284 ********************************************************************/
286 int sys_stat(const char *fname
,SMB_STRUCT_STAT
*sbuf
)
289 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
290 ret
= stat64(fname
, sbuf
);
292 ret
= stat(fname
, sbuf
);
294 /* we always want directories to appear zero size */
295 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
299 /*******************************************************************
300 An fstat() wrapper that will deal with 64 bit filesizes.
301 ********************************************************************/
303 int sys_fstat(int fd
,SMB_STRUCT_STAT
*sbuf
)
306 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
307 ret
= fstat64(fd
, sbuf
);
309 ret
= fstat(fd
, sbuf
);
311 /* we always want directories to appear zero size */
312 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
316 /*******************************************************************
317 An lstat() wrapper that will deal with 64 bit filesizes.
318 ********************************************************************/
320 int sys_lstat(const char *fname
,SMB_STRUCT_STAT
*sbuf
)
323 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
324 ret
= lstat64(fname
, sbuf
);
326 ret
= lstat(fname
, sbuf
);
328 /* we always want directories to appear zero size */
329 if (ret
== 0 && S_ISDIR(sbuf
->st_mode
)) sbuf
->st_size
= 0;
333 /*******************************************************************
334 An ftruncate() wrapper that will deal with 64 bit filesizes.
335 ********************************************************************/
337 int sys_ftruncate(int fd
, SMB_OFF_T offset
)
339 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
340 return ftruncate64(fd
, offset
);
342 return ftruncate(fd
, offset
);
346 /*******************************************************************
347 An lseek() wrapper that will deal with 64 bit filesizes.
348 ********************************************************************/
350 SMB_OFF_T
sys_lseek(int fd
, SMB_OFF_T offset
, int whence
)
352 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
353 return lseek64(fd
, offset
, whence
);
355 return lseek(fd
, offset
, whence
);
359 /*******************************************************************
360 An fseek() wrapper that will deal with 64 bit filesizes.
361 ********************************************************************/
363 int sys_fseek(FILE *fp
, SMB_OFF_T offset
, int whence
)
365 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
366 return fseek64(fp
, offset
, whence
);
367 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
368 return fseeko64(fp
, offset
, whence
);
370 return fseek(fp
, offset
, whence
);
374 /*******************************************************************
375 An ftell() wrapper that will deal with 64 bit filesizes.
376 ********************************************************************/
378 SMB_OFF_T
sys_ftell(FILE *fp
)
380 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
381 return (SMB_OFF_T
)ftell64(fp
);
382 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
383 return (SMB_OFF_T
)ftello64(fp
);
385 return (SMB_OFF_T
)ftell(fp
);
389 /*******************************************************************
390 A creat() wrapper that will deal with 64 bit filesizes.
391 ********************************************************************/
393 int sys_creat(const char *path
, mode_t mode
)
395 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
396 return creat64(path
, mode
);
399 * If creat64 isn't defined then ensure we call a potential open64.
402 return sys_open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
406 /*******************************************************************
407 An open() wrapper that will deal with 64 bit filesizes.
408 ********************************************************************/
410 int sys_open(const char *path
, int oflag
, mode_t mode
)
412 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
413 return open64(path
, oflag
, mode
);
415 return open(path
, oflag
, mode
);
419 /*******************************************************************
420 An fopen() wrapper that will deal with 64 bit filesizes.
421 ********************************************************************/
423 FILE *sys_fopen(const char *path
, const char *type
)
425 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
426 return fopen64(path
, type
);
428 return fopen(path
, type
);
433 /*******************************************************************
434 A flock() wrapper that will perform the kernel flock.
435 ********************************************************************/
437 void kernel_flock(int fd
, uint32 share_mode
)
439 #if HAVE_KERNEL_SHARE_MODES
441 if (share_mode
== FILE_SHARE_WRITE
) {
442 kernel_mode
= LOCK_MAND
|LOCK_WRITE
;
443 } else if (share_mode
== FILE_SHARE_READ
) {
444 kernel_mode
= LOCK_MAND
|LOCK_READ
;
445 } else if (share_mode
== FILE_SHARE_NONE
) {
446 kernel_mode
= LOCK_MAND
;
449 flock(fd
, kernel_mode
);
457 /*******************************************************************
458 An opendir wrapper that will deal with 64 bit filesizes.
459 ********************************************************************/
461 SMB_STRUCT_DIR
*sys_opendir(const char *name
)
463 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
464 return opendir64(name
);
466 return opendir(name
);
470 /*******************************************************************
471 A readdir wrapper that will deal with 64 bit filesizes.
472 ********************************************************************/
474 SMB_STRUCT_DIRENT
*sys_readdir(SMB_STRUCT_DIR
*dirp
)
476 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
477 return readdir64(dirp
);
479 return readdir(dirp
);
483 /*******************************************************************
484 A seekdir wrapper that will deal with 64 bit filesizes.
485 ********************************************************************/
487 void sys_seekdir(SMB_STRUCT_DIR
*dirp
, long offset
)
489 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
490 seekdir64(dirp
, offset
);
492 seekdir(dirp
, offset
);
496 /*******************************************************************
497 A telldir wrapper that will deal with 64 bit filesizes.
498 ********************************************************************/
500 long sys_telldir(SMB_STRUCT_DIR
*dirp
)
502 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
503 return (long)telldir64(dirp
);
505 return (long)telldir(dirp
);
509 /*******************************************************************
510 A rewinddir wrapper that will deal with 64 bit filesizes.
511 ********************************************************************/
513 void sys_rewinddir(SMB_STRUCT_DIR
*dirp
)
515 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
522 /*******************************************************************
523 A close wrapper that will deal with 64 bit filesizes.
524 ********************************************************************/
526 int sys_closedir(SMB_STRUCT_DIR
*dirp
)
528 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
529 return closedir64(dirp
);
531 return closedir(dirp
);
535 /*******************************************************************
536 An mknod() wrapper that will deal with 64 bit filesizes.
537 ********************************************************************/
539 int sys_mknod(const char *path
, mode_t mode
, SMB_DEV_T dev
)
541 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
542 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
543 return mknod64(path
, mode
, dev
);
545 return mknod(path
, mode
, dev
);
548 /* No mknod system call. */
554 /*******************************************************************
555 Wrapper for realpath.
556 ********************************************************************/
558 char *sys_realpath(const char *path
, char *resolved_path
)
560 #if defined(HAVE_REALPATH)
561 return realpath(path
, resolved_path
);
563 /* As realpath is not a system call we can't return ENOSYS. */
569 /*******************************************************************
570 The wait() calls vary between systems
571 ********************************************************************/
573 int sys_waitpid(pid_t pid
,int *status
,int options
)
576 return waitpid(pid
,status
,options
);
577 #else /* HAVE_WAITPID */
578 return wait4(pid
, status
, options
, NULL
);
579 #endif /* HAVE_WAITPID */
582 /*******************************************************************
583 System wrapper for getwd
584 ********************************************************************/
586 char *sys_getwd(char *s
)
590 wd
= (char *)getcwd(s
, PATH_MAX
);
592 wd
= (char *)getwd(s
);
597 /*******************************************************************
598 system wrapper for symlink
599 ********************************************************************/
601 int sys_symlink(const char *oldpath
, const char *newpath
)
607 return symlink(oldpath
, newpath
);
611 /*******************************************************************
612 system wrapper for readlink
613 ********************************************************************/
615 int sys_readlink(const char *path
, char *buf
, size_t bufsiz
)
617 #ifndef HAVE_READLINK
621 return readlink(path
, buf
, bufsiz
);
625 /*******************************************************************
626 system wrapper for link
627 ********************************************************************/
629 int sys_link(const char *oldpath
, const char *newpath
)
635 return link(oldpath
, newpath
);
639 /*******************************************************************
640 chown isn't used much but OS/2 doesn't have it
641 ********************************************************************/
643 int sys_chown(const char *fname
,uid_t uid
,gid_t gid
)
648 DEBUG(1,("WARNING: no chown!\n"));
654 return(chown(fname
,uid
,gid
));
658 /*******************************************************************
660 ********************************************************************/
662 int sys_lchown(const char *fname
,uid_t uid
,gid_t gid
)
667 DEBUG(1,("WARNING: no lchown!\n"));
673 return(lchown(fname
,uid
,gid
));
677 /*******************************************************************
678 os/2 also doesn't have chroot
679 ********************************************************************/
680 int sys_chroot(const char *dname
)
685 DEBUG(1,("WARNING: no chroot!\n"));
691 return(chroot(dname
));
695 #if defined(HAVE_POSIX_CAPABILITIES)
697 /**************************************************************************
698 Try and abstract process capabilities (for systems that have them).
699 ****************************************************************************/
701 /* Set the POSIX capabilities needed for the given purpose into the effective
702 * capability set of the current process. Make sure they are always removed
703 * from the inheritable set, because there is no circumstance in which our
704 * children should inherit our elevated privileges.
706 static bool set_process_capability(enum smbd_capability capability
,
709 cap_value_t cap_vals
[2] = {0};
710 int num_cap_vals
= 0;
714 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
715 /* On Linux, make sure that any capabilities we grab are sticky
716 * across UID changes. We expect that this would allow us to keep both
717 * the effective and permitted capability sets, but as of circa 2.6.16,
718 * only the permitted set is kept. It is a bug (which we work around)
719 * that the effective set is lost, but we still require the effective
722 if (!prctl(PR_GET_KEEPCAPS
)) {
723 prctl(PR_SET_KEEPCAPS
, 1);
727 cap
= cap_get_proc();
729 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
734 switch (capability
) {
735 case KERNEL_OPLOCK_CAPABILITY
:
736 #ifdef CAP_NETWORK_MGT
737 /* IRIX has CAP_NETWORK_MGT for oplocks. */
738 cap_vals
[num_cap_vals
++] = CAP_NETWORK_MGT
;
741 case DMAPI_ACCESS_CAPABILITY
:
742 #ifdef CAP_DEVICE_MGT
743 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
744 cap_vals
[num_cap_vals
++] = CAP_DEVICE_MGT
;
746 /* Linux has CAP_MKNOD for DMAPI access. */
747 cap_vals
[num_cap_vals
++] = CAP_MKNOD
;
750 case LEASE_CAPABILITY
:
752 cap_vals
[num_cap_vals
++] = CAP_LEASE
;
757 SMB_ASSERT(num_cap_vals
<= ARRAY_SIZE(cap_vals
));
759 if (num_cap_vals
== 0) {
764 cap_set_flag(cap
, CAP_EFFECTIVE
, num_cap_vals
, cap_vals
,
765 enable
? CAP_SET
: CAP_CLEAR
);
767 /* We never want to pass capabilities down to our children, so make
768 * sure they are not inherited.
770 cap_set_flag(cap
, CAP_INHERITABLE
, num_cap_vals
, cap_vals
, CAP_CLEAR
);
772 if (cap_set_proc(cap
) == -1) {
773 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
783 #endif /* HAVE_POSIX_CAPABILITIES */
785 /****************************************************************************
786 Gain the oplock capability from the kernel if possible.
787 ****************************************************************************/
789 void set_effective_capability(enum smbd_capability capability
)
791 #if defined(HAVE_POSIX_CAPABILITIES)
792 set_process_capability(capability
, True
);
793 #endif /* HAVE_POSIX_CAPABILITIES */
796 void drop_effective_capability(enum smbd_capability capability
)
798 #if defined(HAVE_POSIX_CAPABILITIES)
799 set_process_capability(capability
, False
);
800 #endif /* HAVE_POSIX_CAPABILITIES */
803 /**************************************************************************
804 Wrapper for random().
805 ****************************************************************************/
807 long sys_random(void)
809 #if defined(HAVE_RANDOM)
810 return (long)random();
811 #elif defined(HAVE_RAND)
814 DEBUG(0,("Error - no random function available !\n"));
819 /**************************************************************************
820 Wrapper for srandom().
821 ****************************************************************************/
823 void sys_srandom(unsigned int seed
)
825 #if defined(HAVE_SRANDOM)
827 #elif defined(HAVE_SRAND)
830 DEBUG(0,("Error - no srandom function available !\n"));
835 /**************************************************************************
836 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
837 ****************************************************************************/
841 #if defined(SYSCONF_SC_NGROUPS_MAX)
842 int ret
= sysconf(_SC_NGROUPS_MAX
);
843 return (ret
== -1) ? NGROUPS_MAX
: ret
;
849 /**************************************************************************
850 Wrap setgroups and getgroups for systems that declare getgroups() as
851 returning an array of gid_t, but actuall return an array of int.
852 ****************************************************************************/
854 #if defined(HAVE_BROKEN_GETGROUPS)
855 static int sys_broken_getgroups(int setlen
, gid_t
*gidset
)
862 return getgroups(setlen
, &gid
);
866 * Broken case. We need to allocate a
867 * GID_T array of size setlen.
876 setlen
= groups_max();
878 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
879 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
883 if((ngroups
= getgroups(setlen
, group_list
)) < 0) {
884 int saved_errno
= errno
;
885 SAFE_FREE(group_list
);
890 for(i
= 0; i
< ngroups
; i
++)
891 gidset
[i
] = (gid_t
)group_list
[i
];
893 SAFE_FREE(group_list
);
897 static int sys_broken_setgroups(int setlen
, gid_t
*gidset
)
905 if (setlen
< 0 || setlen
> groups_max()) {
911 * Broken case. We need to allocate a
912 * GID_T array of size setlen.
915 if((group_list
= SMB_MALLOC_ARRAY(GID_T
, setlen
)) == NULL
) {
916 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
920 for(i
= 0; i
< setlen
; i
++)
921 group_list
[i
] = (GID_T
) gidset
[i
];
923 if(setgroups(setlen
, group_list
) != 0) {
924 int saved_errno
= errno
;
925 SAFE_FREE(group_list
);
930 SAFE_FREE(group_list
);
934 #endif /* HAVE_BROKEN_GETGROUPS */
936 /* This is a list of systems that require the first GID passed to setgroups(2)
937 * to be the effective GID. If your system is one of these, add it here.
939 #if defined (FREEBSD) || defined (DARWINOS)
940 #define USE_BSD_SETGROUPS
943 #if defined(USE_BSD_SETGROUPS)
944 /* Depending on the particular BSD implementation, the first GID that is
945 * passed to setgroups(2) will either be ignored or will set the credential's
946 * effective GID. In either case, the right thing to do is to guarantee that
947 * gidset[0] is the effective GID.
949 static int sys_bsd_setgroups(gid_t primary_gid
, int setlen
, const gid_t
*gidset
)
951 gid_t
*new_gidset
= NULL
;
955 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
958 /* No group list, just make sure we are setting the efective GID. */
960 return setgroups(1, &primary_gid
);
963 /* If the primary gid is not the first array element, grow the array
964 * and insert it at the front.
966 if (gidset
[0] != primary_gid
) {
967 new_gidset
= SMB_MALLOC_ARRAY(gid_t
, setlen
+ 1);
968 if (new_gidset
== NULL
) {
972 memcpy(new_gidset
+ 1, gidset
, (setlen
* sizeof(gid_t
)));
973 new_gidset
[0] = primary_gid
;
978 DEBUG(3, ("forced to truncate group list from %d to %d\n",
983 #if defined(HAVE_BROKEN_GETGROUPS)
984 ret
= sys_broken_setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
986 ret
= setgroups(setlen
, new_gidset
? new_gidset
: gidset
);
991 SAFE_FREE(new_gidset
);
998 #endif /* USE_BSD_SETGROUPS */
1000 /**************************************************************************
1001 Wrapper for getgroups. Deals with broken (int) case.
1002 ****************************************************************************/
1004 int sys_getgroups(int setlen
, gid_t
*gidset
)
1006 #if defined(HAVE_BROKEN_GETGROUPS)
1007 return sys_broken_getgroups(setlen
, gidset
);
1009 return getgroups(setlen
, gidset
);
1013 /**************************************************************************
1014 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1015 ****************************************************************************/
1017 int sys_setgroups(gid_t
UNUSED(primary_gid
), int setlen
, gid_t
*gidset
)
1019 #if !defined(HAVE_SETGROUPS)
1022 #endif /* HAVE_SETGROUPS */
1024 #if defined(USE_BSD_SETGROUPS)
1025 return sys_bsd_setgroups(primary_gid
, setlen
, gidset
);
1026 #elif defined(HAVE_BROKEN_GETGROUPS)
1027 return sys_broken_setgroups(setlen
, gidset
);
1029 return setgroups(setlen
, gidset
);
1033 /**************************************************************************
1034 Wrappers for setpwent(), getpwent() and endpwent()
1035 ****************************************************************************/
1037 void sys_setpwent(void)
1042 struct passwd
*sys_getpwent(void)
1047 void sys_endpwent(void)
1052 /**************************************************************************
1053 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1054 ****************************************************************************/
1057 struct passwd
*sys_getpwnam(const char *name
)
1059 return getpwnam(name
);
1062 struct passwd
*sys_getpwuid(uid_t uid
)
1064 return getpwuid(uid
);
1067 struct group
*sys_getgrnam(const char *name
)
1069 return getgrnam(name
);
1072 struct group
*sys_getgrgid(gid_t gid
)
1074 return getgrgid(gid
);
1077 /**************************************************************************
1078 Extract a command into an arg list.
1079 ****************************************************************************/
1081 static char **extract_args(TALLOC_CTX
*mem_ctx
, const char *command
)
1090 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1091 DEBUG(0, ("talloc failed\n"));
1095 if(!(ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
))) {
1096 TALLOC_FREE(trunc_cmd
);
1105 for( argcl
= 1; ptr
; ptr
= strtok_r(NULL
, " \t", &saveptr
))
1108 TALLOC_FREE(trunc_cmd
);
1110 if (!(argl
= TALLOC_ARRAY(mem_ctx
, char *, argcl
+ 1))) {
1115 * Now do the extraction.
1118 if (!(trunc_cmd
= talloc_strdup(mem_ctx
, command
))) {
1122 ptr
= strtok_r(trunc_cmd
, " \t", &saveptr
);
1125 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1129 while((ptr
= strtok_r(NULL
, " \t", &saveptr
)) != NULL
) {
1131 if (!(argl
[i
++] = talloc_strdup(argl
, ptr
))) {
1140 DEBUG(0, ("talloc failed\n"));
1141 TALLOC_FREE(trunc_cmd
);
1147 /**************************************************************************
1148 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1149 a sys_getpid() that only does a system call *once*.
1150 ****************************************************************************/
1152 static pid_t mypid
= (pid_t
)-1;
1154 pid_t
sys_fork(void)
1156 pid_t forkret
= fork();
1158 if (forkret
== (pid_t
)0) /* Child - reset mypid so sys_getpid does a system call. */
1164 /**************************************************************************
1165 Wrapper for getpid. Ensures we only do a system call *once*.
1166 ****************************************************************************/
1168 pid_t
sys_getpid(void)
1170 if (mypid
== (pid_t
)-1)
1176 /**************************************************************************
1177 Wrapper for popen. Safer as it doesn't search a path.
1178 Modified from the glibc sources.
1179 modified by tridge to return a file descriptor. We must kick our FILE* habit
1180 ****************************************************************************/
1182 typedef struct _popen_list
1186 struct _popen_list
*next
;
1189 static popen_list
*popen_chain
;
1191 int sys_popen(const char *command
)
1193 int parent_end
, child_end
;
1195 popen_list
*entry
= NULL
;
1198 if (pipe(pipe_fds
) < 0)
1201 parent_end
= pipe_fds
[0];
1202 child_end
= pipe_fds
[1];
1209 if((entry
= SMB_MALLOC_P(popen_list
)) == NULL
)
1212 ZERO_STRUCTP(entry
);
1215 * Extract the command and args into a NULL terminated array.
1218 if(!(argl
= extract_args(NULL
, command
)))
1221 entry
->child_pid
= sys_fork();
1223 if (entry
->child_pid
== -1) {
1227 if (entry
->child_pid
== 0) {
1233 int child_std_end
= STDOUT_FILENO
;
1237 if (child_end
!= child_std_end
) {
1238 dup2 (child_end
, child_std_end
);
1243 * POSIX.2: "popen() shall ensure that any streams from previous
1244 * popen() calls that remain open in the parent process are closed
1245 * in the new child process."
1248 for (p
= popen_chain
; p
; p
= p
->next
)
1251 execv(argl
[0], argl
);
1262 /* Link into popen_chain. */
1263 entry
->next
= popen_chain
;
1264 popen_chain
= entry
;
1265 entry
->fd
= parent_end
;
1278 /**************************************************************************
1279 Wrapper for pclose. Modified from the glibc sources.
1280 ****************************************************************************/
1282 int sys_pclose(int fd
)
1285 popen_list
**ptr
= &popen_chain
;
1286 popen_list
*entry
= NULL
;
1290 /* Unlink from popen_chain. */
1291 for ( ; *ptr
!= NULL
; ptr
= &(*ptr
)->next
) {
1292 if ((*ptr
)->fd
== fd
) {
1294 *ptr
= (*ptr
)->next
;
1300 if (status
< 0 || close(entry
->fd
) < 0)
1304 * As Samba is catching and eating child process
1305 * exits we don't really care about the child exit
1306 * code, a -1 with errno = ECHILD will do fine for us.
1310 wait_pid
= sys_waitpid (entry
->child_pid
, &wstatus
, 0);
1311 } while (wait_pid
== -1 && errno
== EINTR
);
1320 /**************************************************************************
1321 Wrappers for dlopen, dlsym, dlclose.
1322 ****************************************************************************/
1324 void *sys_dlopen(const char *name
, int flags
)
1326 #if defined(HAVE_DLOPEN)
1327 return dlopen(name
, flags
);
1333 void *sys_dlsym(void *handle
, const char *symbol
)
1335 #if defined(HAVE_DLSYM)
1336 return dlsym(handle
, symbol
);
1342 int sys_dlclose (void *handle
)
1344 #if defined(HAVE_DLCLOSE)
1345 return dlclose(handle
);
1351 const char *sys_dlerror(void)
1353 #if defined(HAVE_DLERROR)
1360 int sys_dup2(int oldfd
, int newfd
)
1362 #if defined(HAVE_DUP2)
1363 return dup2(oldfd
, newfd
);
1370 /**************************************************************************
1371 Wrapper for Admin Logs.
1372 ****************************************************************************/
1374 void sys_adminlog(int priority
, const char *format_str
, ...)
1378 char *msgbuf
= NULL
;
1380 va_start( ap
, format_str
);
1381 ret
= vasprintf( &msgbuf
, format_str
, ap
);
1387 #if defined(HAVE_SYSLOG)
1388 syslog( priority
, "%s", msgbuf
);
1390 DEBUG(0,("%s", msgbuf
));
1395 /******** Solaris EA helper function prototypes ********/
1396 #ifdef HAVE_ATTROPEN
1397 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1398 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
);
1399 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
);
1400 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
);
1401 static int solaris_unlinkat(int attrdirfd
, const char *name
);
1402 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
);
1403 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
);
1406 /**************************************************************************
1407 Wrappers for extented attribute calls. Based on the Linux package with
1408 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1409 ****************************************************************************/
1411 ssize_t
sys_getxattr (const char *path
, const char *name
, void *value
, size_t size
)
1413 #if defined(HAVE_GETXATTR)
1414 #ifndef XATTR_ADD_OPT
1415 return getxattr(path
, name
, value
, size
);
1418 return getxattr(path
, name
, value
, size
, 0, options
);
1420 #elif defined(HAVE_GETEA)
1421 return getea(path
, name
, value
, size
);
1422 #elif defined(HAVE_EXTATTR_GET_FILE)
1425 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1426 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1427 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1429 * The BSD implementation has a nasty habit of silently truncating
1430 * the returned value to the size of the buffer, so we have to check
1431 * that the buffer is large enough to fit the returned value.
1433 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1438 if((retval
=extattr_get_file(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1442 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno
)));
1444 #elif defined(HAVE_ATTR_GET)
1445 int retval
, flags
= 0;
1446 int valuelength
= (int)size
;
1447 char *attrname
= strchr(name
,'.') + 1;
1449 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1451 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1453 return retval
? retval
: valuelength
;
1454 #elif defined(HAVE_ATTROPEN)
1456 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
, 0);
1458 ret
= solaris_read_xattr(attrfd
, value
, size
);
1468 ssize_t
sys_lgetxattr (const char *path
, const char *name
, void *value
, size_t size
)
1470 #if defined(HAVE_LGETXATTR)
1471 return lgetxattr(path
, name
, value
, size
);
1472 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1473 int options
= XATTR_NOFOLLOW
;
1474 return getxattr(path
, name
, value
, size
, 0, options
);
1475 #elif defined(HAVE_LGETEA)
1476 return lgetea(path
, name
, value
, size
);
1477 #elif defined(HAVE_EXTATTR_GET_LINK)
1480 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1481 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1482 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1484 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1489 if((retval
=extattr_get_link(path
, attrnamespace
, attrname
, value
, size
)) >= 0)
1493 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno
)));
1495 #elif defined(HAVE_ATTR_GET)
1496 int retval
, flags
= ATTR_DONTFOLLOW
;
1497 int valuelength
= (int)size
;
1498 char *attrname
= strchr(name
,'.') + 1;
1500 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1502 retval
= attr_get(path
, attrname
, (char *)value
, &valuelength
, flags
);
1504 return retval
? retval
: valuelength
;
1505 #elif defined(HAVE_ATTROPEN)
1507 int attrfd
= solaris_attropen(path
, name
, O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1509 ret
= solaris_read_xattr(attrfd
, value
, size
);
1519 ssize_t
sys_fgetxattr (int filedes
, const char *name
, void *value
, size_t size
)
1521 #if defined(HAVE_FGETXATTR)
1522 #ifndef XATTR_ADD_OPT
1523 return fgetxattr(filedes
, name
, value
, size
);
1526 return fgetxattr(filedes
, name
, value
, size
, 0, options
);
1528 #elif defined(HAVE_FGETEA)
1529 return fgetea(filedes
, name
, value
, size
);
1530 #elif defined(HAVE_EXTATTR_GET_FD)
1533 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1534 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1535 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1537 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0)) >= 0) {
1542 if((retval
=extattr_get_fd(filedes
, attrnamespace
, attrname
, value
, size
)) >= 0)
1546 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno
)));
1548 #elif defined(HAVE_ATTR_GETF)
1549 int retval
, flags
= 0;
1550 int valuelength
= (int)size
;
1551 char *attrname
= strchr(name
,'.') + 1;
1553 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1555 retval
= attr_getf(filedes
, attrname
, (char *)value
, &valuelength
, flags
);
1557 return retval
? retval
: valuelength
;
1558 #elif defined(HAVE_ATTROPEN)
1560 int attrfd
= solaris_openat(filedes
, name
, O_RDONLY
|O_XATTR
, 0);
1562 ret
= solaris_read_xattr(attrfd
, value
, size
);
1572 #if defined(HAVE_EXTATTR_LIST_FILE)
1574 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1582 { EXTATTR_NAMESPACE_SYSTEM
, EXTATTR_PREFIX("system.") },
1583 { EXTATTR_NAMESPACE_USER
, EXTATTR_PREFIX("user.") },
1591 static ssize_t
bsd_attr_list (int type
, extattr_arg arg
, char *list
, size_t size
)
1593 ssize_t list_size
, total_size
= 0;
1596 /* Iterate through extattr(2) namespaces */
1597 for(t
= 0; t
< (sizeof(extattr
)/sizeof(extattr
[0])); t
++) {
1599 #if defined(HAVE_EXTATTR_LIST_FILE)
1601 list_size
= extattr_list_file(arg
.path
, extattr
[t
].space
, list
, size
);
1604 #if defined(HAVE_EXTATTR_LIST_LINK)
1606 list_size
= extattr_list_link(arg
.path
, extattr
[t
].space
, list
, size
);
1609 #if defined(HAVE_EXTATTR_LIST_FD)
1611 list_size
= extattr_list_fd(arg
.filedes
, extattr
[t
].space
, list
, size
);
1618 /* Some error happend. Errno should be set by the previous call */
1624 /* XXX: Call with an empty buffer may be used to calculate
1625 necessary buffer size. Unfortunately, we can't say, how
1626 many attributes were returned, so here is the potential
1627 problem with the emulation.
1630 /* Take the worse case of one char attribute names -
1631 two bytes per name plus one more for sanity.
1633 total_size
+= list_size
+ (list_size
/2 + 1)*extattr
[t
].len
;
1636 /* Count necessary offset to fit namespace prefixes */
1638 for(i
= 0; i
< list_size
; i
+= list
[i
] + 1)
1639 len
+= extattr
[t
].len
;
1641 total_size
+= list_size
+ len
;
1642 /* Buffer is too small to fit the results */
1643 if(total_size
> size
) {
1647 /* Shift results back, so we can prepend prefixes */
1648 buf
= memmove(list
+ len
, list
, list_size
);
1650 for(i
= 0; i
< list_size
; i
+= len
+ 1) {
1652 strncpy(list
, extattr
[t
].name
, extattr
[t
].len
+ 1);
1653 list
+= extattr
[t
].len
;
1654 strncpy(list
, buf
+ i
+ 1, len
);
1665 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1666 static char attr_buffer
[ATTR_MAX_VALUELEN
];
1668 static ssize_t
irix_attr_list(const char *path
, int filedes
, char *list
, size_t size
, int flags
)
1670 int retval
= 0, index
;
1671 attrlist_cursor_t
*cursor
= 0;
1673 attrlist_t
* al
= (attrlist_t
*)attr_buffer
;
1675 size_t ent_size
, left
= size
;
1680 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1682 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1684 for (index
= 0; index
< al
->al_count
; index
++) {
1685 ae
= ATTR_ENTRY(attr_buffer
, index
);
1686 ent_size
= strlen(ae
->a_name
) + sizeof("user.");
1687 if (left
>= ent_size
) {
1688 strncpy(bp
, "user.", sizeof("user."));
1689 strncat(bp
, ae
->a_name
, ent_size
- sizeof("user."));
1697 total_size
+= ent_size
;
1699 if (al
->al_more
== 0) break;
1706 retval
= attr_listf(filedes
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1708 retval
= attr_list(path
, attr_buffer
, ATTR_MAX_VALUELEN
, flags
, cursor
);
1710 for (index
= 0; index
< al
->al_count
; index
++) {
1711 ae
= ATTR_ENTRY(attr_buffer
, index
);
1712 ent_size
= strlen(ae
->a_name
) + sizeof("system.");
1713 if (left
>= ent_size
) {
1714 strncpy(bp
, "system.", sizeof("system."));
1715 strncat(bp
, ae
->a_name
, ent_size
- sizeof("system."));
1723 total_size
+= ent_size
;
1725 if (al
->al_more
== 0) break;
1728 return (ssize_t
)(retval
? retval
: total_size
);
1733 ssize_t
sys_listxattr (const char *path
, char *list
, size_t size
)
1735 #if defined(HAVE_LISTXATTR)
1736 #ifndef XATTR_ADD_OPT
1737 return listxattr(path
, list
, size
);
1740 return listxattr(path
, list
, size
, options
);
1742 #elif defined(HAVE_LISTEA)
1743 return listea(path
, list
, size
);
1744 #elif defined(HAVE_EXTATTR_LIST_FILE)
1747 return bsd_attr_list(0, arg
, list
, size
);
1748 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1749 return irix_attr_list(path
, 0, list
, size
, 0);
1750 #elif defined(HAVE_ATTROPEN)
1752 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1753 if (attrdirfd
>= 0) {
1754 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1764 ssize_t
sys_llistxattr (const char *path
, char *list
, size_t size
)
1766 #if defined(HAVE_LLISTXATTR)
1767 return llistxattr(path
, list
, size
);
1768 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1769 int options
= XATTR_NOFOLLOW
;
1770 return listxattr(path
, list
, size
, options
);
1771 #elif defined(HAVE_LLISTEA)
1772 return llistea(path
, list
, size
);
1773 #elif defined(HAVE_EXTATTR_LIST_LINK)
1776 return bsd_attr_list(1, arg
, list
, size
);
1777 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1778 return irix_attr_list(path
, 0, list
, size
, ATTR_DONTFOLLOW
);
1779 #elif defined(HAVE_ATTROPEN)
1781 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1782 if (attrdirfd
>= 0) {
1783 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1793 ssize_t
sys_flistxattr (int filedes
, char *list
, size_t size
)
1795 #if defined(HAVE_FLISTXATTR)
1796 #ifndef XATTR_ADD_OPT
1797 return flistxattr(filedes
, list
, size
);
1800 return flistxattr(filedes
, list
, size
, options
);
1802 #elif defined(HAVE_FLISTEA)
1803 return flistea(filedes
, list
, size
);
1804 #elif defined(HAVE_EXTATTR_LIST_FD)
1806 arg
.filedes
= filedes
;
1807 return bsd_attr_list(2, arg
, list
, size
);
1808 #elif defined(HAVE_ATTR_LISTF)
1809 return irix_attr_list(NULL
, filedes
, list
, size
, 0);
1810 #elif defined(HAVE_ATTROPEN)
1812 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
1813 if (attrdirfd
>= 0) {
1814 ret
= solaris_list_xattr(attrdirfd
, list
, size
);
1824 int sys_removexattr (const char *path
, const char *name
)
1826 #if defined(HAVE_REMOVEXATTR)
1827 #ifndef XATTR_ADD_OPT
1828 return removexattr(path
, name
);
1831 return removexattr(path
, name
, options
);
1833 #elif defined(HAVE_REMOVEEA)
1834 return removeea(path
, name
);
1835 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1837 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1838 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1839 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1841 return extattr_delete_file(path
, attrnamespace
, attrname
);
1842 #elif defined(HAVE_ATTR_REMOVE)
1844 char *attrname
= strchr(name
,'.') + 1;
1846 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1848 return attr_remove(path
, attrname
, flags
);
1849 #elif defined(HAVE_ATTROPEN)
1851 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
, 0);
1852 if (attrdirfd
>= 0) {
1853 ret
= solaris_unlinkat(attrdirfd
, name
);
1863 int sys_lremovexattr (const char *path
, const char *name
)
1865 #if defined(HAVE_LREMOVEXATTR)
1866 return lremovexattr(path
, name
);
1867 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1868 int options
= XATTR_NOFOLLOW
;
1869 return removexattr(path
, name
, options
);
1870 #elif defined(HAVE_LREMOVEEA)
1871 return lremoveea(path
, name
);
1872 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1874 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1875 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1876 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1878 return extattr_delete_link(path
, attrnamespace
, attrname
);
1879 #elif defined(HAVE_ATTR_REMOVE)
1880 int flags
= ATTR_DONTFOLLOW
;
1881 char *attrname
= strchr(name
,'.') + 1;
1883 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1885 return attr_remove(path
, attrname
, flags
);
1886 #elif defined(HAVE_ATTROPEN)
1888 int attrdirfd
= solaris_attropen(path
, ".", O_RDONLY
|AT_SYMLINK_NOFOLLOW
, 0);
1889 if (attrdirfd
>= 0) {
1890 ret
= solaris_unlinkat(attrdirfd
, name
);
1900 int sys_fremovexattr (int filedes
, const char *name
)
1902 #if defined(HAVE_FREMOVEXATTR)
1903 #ifndef XATTR_ADD_OPT
1904 return fremovexattr(filedes
, name
);
1907 return fremovexattr(filedes
, name
, options
);
1909 #elif defined(HAVE_FREMOVEEA)
1910 return fremoveea(filedes
, name
);
1911 #elif defined(HAVE_EXTATTR_DELETE_FD)
1913 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1914 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1915 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1917 return extattr_delete_fd(filedes
, attrnamespace
, attrname
);
1918 #elif defined(HAVE_ATTR_REMOVEF)
1920 char *attrname
= strchr(name
,'.') + 1;
1922 if (strncmp(name
, "system", 6) == 0) flags
|= ATTR_ROOT
;
1924 return attr_removef(filedes
, attrname
, flags
);
1925 #elif defined(HAVE_ATTROPEN)
1927 int attrdirfd
= solaris_openat(filedes
, ".", O_RDONLY
|O_XATTR
, 0);
1928 if (attrdirfd
>= 0) {
1929 ret
= solaris_unlinkat(attrdirfd
, name
);
1939 int sys_setxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
1941 #if defined(HAVE_SETXATTR)
1942 #ifndef XATTR_ADD_OPT
1943 return setxattr(path
, name
, value
, size
, flags
);
1946 return setxattr(path
, name
, value
, size
, 0, options
);
1948 #elif defined(HAVE_SETEA)
1949 return setea(path
, name
, value
, size
, flags
);
1950 #elif defined(HAVE_EXTATTR_SET_FILE)
1953 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
1954 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
1955 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
1957 /* Check attribute existence */
1958 retval
= extattr_get_file(path
, attrnamespace
, attrname
, NULL
, 0);
1960 /* REPLACE attribute, that doesn't exist */
1961 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
1965 /* Ignore other errors */
1968 /* CREATE attribute, that already exists */
1969 if (flags
& XATTR_CREATE
) {
1975 retval
= extattr_set_file(path
, attrnamespace
, attrname
, value
, size
);
1976 return (retval
< 0) ? -1 : 0;
1977 #elif defined(HAVE_ATTR_SET)
1979 char *attrname
= strchr(name
,'.') + 1;
1981 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
1982 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
1983 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
1985 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
1986 #elif defined(HAVE_ATTROPEN)
1988 int myflags
= O_RDWR
;
1990 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
1991 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
1992 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
1994 ret
= solaris_write_xattr(attrfd
, value
, size
);
2004 int sys_lsetxattr (const char *path
, const char *name
, const void *value
, size_t size
, int flags
)
2006 #if defined(HAVE_LSETXATTR)
2007 return lsetxattr(path
, name
, value
, size
, flags
);
2008 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2009 int options
= XATTR_NOFOLLOW
;
2010 return setxattr(path
, name
, value
, size
, 0, options
);
2011 #elif defined(LSETEA)
2012 return lsetea(path
, name
, value
, size
, flags
);
2013 #elif defined(HAVE_EXTATTR_SET_LINK)
2016 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2017 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2018 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2020 /* Check attribute existence */
2021 retval
= extattr_get_link(path
, attrnamespace
, attrname
, NULL
, 0);
2023 /* REPLACE attribute, that doesn't exist */
2024 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2028 /* Ignore other errors */
2031 /* CREATE attribute, that already exists */
2032 if (flags
& XATTR_CREATE
) {
2039 retval
= extattr_set_link(path
, attrnamespace
, attrname
, value
, size
);
2040 return (retval
< 0) ? -1 : 0;
2041 #elif defined(HAVE_ATTR_SET)
2042 int myflags
= ATTR_DONTFOLLOW
;
2043 char *attrname
= strchr(name
,'.') + 1;
2045 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2046 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2047 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2049 return attr_set(path
, attrname
, (const char *)value
, size
, myflags
);
2050 #elif defined(HAVE_ATTROPEN)
2052 int myflags
= O_RDWR
| AT_SYMLINK_NOFOLLOW
;
2054 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2055 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2056 attrfd
= solaris_attropen(path
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2058 ret
= solaris_write_xattr(attrfd
, value
, size
);
2068 int sys_fsetxattr (int filedes
, const char *name
, const void *value
, size_t size
, int flags
)
2070 #if defined(HAVE_FSETXATTR)
2071 #ifndef XATTR_ADD_OPT
2072 return fsetxattr(filedes
, name
, value
, size
, flags
);
2075 return fsetxattr(filedes
, name
, value
, size
, 0, options
);
2077 #elif defined(HAVE_FSETEA)
2078 return fsetea(filedes
, name
, value
, size
, flags
);
2079 #elif defined(HAVE_EXTATTR_SET_FD)
2082 int attrnamespace
= (strncmp(name
, "system", 6) == 0) ?
2083 EXTATTR_NAMESPACE_SYSTEM
: EXTATTR_NAMESPACE_USER
;
2084 const char *attrname
= ((s
=strchr_m(name
, '.')) == NULL
) ? name
: s
+ 1;
2086 /* Check attribute existence */
2087 retval
= extattr_get_fd(filedes
, attrnamespace
, attrname
, NULL
, 0);
2089 /* REPLACE attribute, that doesn't exist */
2090 if (flags
& XATTR_REPLACE
&& errno
== ENOATTR
) {
2094 /* Ignore other errors */
2097 /* CREATE attribute, that already exists */
2098 if (flags
& XATTR_CREATE
) {
2104 retval
= extattr_set_fd(filedes
, attrnamespace
, attrname
, value
, size
);
2105 return (retval
< 0) ? -1 : 0;
2106 #elif defined(HAVE_ATTR_SETF)
2108 char *attrname
= strchr(name
,'.') + 1;
2110 if (strncmp(name
, "system", 6) == 0) myflags
|= ATTR_ROOT
;
2111 if (flags
& XATTR_CREATE
) myflags
|= ATTR_CREATE
;
2112 if (flags
& XATTR_REPLACE
) myflags
|= ATTR_REPLACE
;
2114 return attr_setf(filedes
, attrname
, (const char *)value
, size
, myflags
);
2115 #elif defined(HAVE_ATTROPEN)
2117 int myflags
= O_RDWR
| O_XATTR
;
2119 if (flags
& XATTR_CREATE
) myflags
|= O_EXCL
;
2120 if (!(flags
& XATTR_REPLACE
)) myflags
|= O_CREAT
;
2121 attrfd
= solaris_openat(filedes
, name
, myflags
, (mode_t
) SOLARIS_ATTRMODE
);
2123 ret
= solaris_write_xattr(attrfd
, value
, size
);
2133 /**************************************************************************
2134 helper functions for Solaris' EA support
2135 ****************************************************************************/
2136 #ifdef HAVE_ATTROPEN
2137 static ssize_t
solaris_read_xattr(int attrfd
, void *value
, size_t size
)
2141 if (fstat(attrfd
, &sbuf
) == -1) {
2146 /* This is to return the current size of the named extended attribute */
2148 return sbuf
.st_size
;
2151 /* check size and read xattr */
2152 if (sbuf
.st_size
> size
) {
2157 return read(attrfd
, value
, sbuf
.st_size
);
2160 static ssize_t
solaris_list_xattr(int attrdirfd
, char *list
, size_t size
)
2166 int newfd
= dup(attrdirfd
);
2167 /* CAUTION: The originating file descriptor should not be
2168 used again following the call to fdopendir().
2169 For that reason we dup() the file descriptor
2170 here to make things more clear. */
2171 dirp
= fdopendir(newfd
);
2173 while ((de
= readdir(dirp
))) {
2174 size_t listlen
= strlen(de
->d_name
);
2175 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, "..")) {
2176 /* we don't want "." and ".." here: */
2177 DEBUG(10,("skipped EA %s\n",de
->d_name
));
2182 /* return the current size of the list of extended attribute names*/
2185 /* check size and copy entrieѕ + nul into list. */
2186 if ((len
+ listlen
+ 1) > size
) {
2191 safe_strcpy(list
+ len
, de
->d_name
, listlen
);
2199 if (closedir(dirp
) == -1) {
2200 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno
)));
2206 static int solaris_unlinkat(int attrdirfd
, const char *name
)
2208 if (unlinkat(attrdirfd
, name
, 0) == -1) {
2209 if (errno
== ENOENT
) {
2217 static int solaris_attropen(const char *path
, const char *attrpath
, int oflag
, mode_t mode
)
2219 int filedes
= attropen(path
, attrpath
, oflag
, mode
);
2220 if (filedes
== -1) {
2221 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path
,attrpath
,strerror(errno
)));
2222 if (errno
== EINVAL
) {
2231 static int solaris_openat(int fildes
, const char *path
, int oflag
, mode_t mode
)
2233 int filedes
= openat(fildes
, path
, oflag
, mode
);
2234 if (filedes
== -1) {
2235 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes
,path
,strerror(errno
)));
2236 if (errno
== EINVAL
) {
2245 static int solaris_write_xattr(int attrfd
, const char *value
, size_t size
)
2247 if ((ftruncate(attrfd
, 0) == 0) && (write(attrfd
, value
, size
) == size
)) {
2250 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2254 #endif /*HAVE_ATTROPEN*/
2257 /****************************************************************************
2258 Return the major devicenumber for UNIX extensions.
2259 ****************************************************************************/
2261 uint32
unix_dev_major(SMB_DEV_T dev
)
2263 #if defined(HAVE_DEVICE_MAJOR_FN)
2264 return (uint32
)major(dev
);
2266 return (uint32
)(dev
>> 8);
2270 /****************************************************************************
2271 Return the minor devicenumber for UNIX extensions.
2272 ****************************************************************************/
2274 uint32
unix_dev_minor(SMB_DEV_T dev
)
2276 #if defined(HAVE_DEVICE_MINOR_FN)
2277 return (uint32
)minor(dev
);
2279 return (uint32
)(dev
& 0xff);
2283 #if defined(WITH_AIO)
2285 /*******************************************************************
2286 An aio_read wrapper that will deal with 64-bit sizes.
2287 ********************************************************************/
2289 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2291 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2292 return aio_read64(aiocb
);
2293 #elif defined(HAVE_AIO_READ)
2294 return aio_read(aiocb
);
2301 /*******************************************************************
2302 An aio_write wrapper that will deal with 64-bit sizes.
2303 ********************************************************************/
2305 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2307 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2308 return aio_write64(aiocb
);
2309 #elif defined(HAVE_AIO_WRITE)
2310 return aio_write(aiocb
);
2317 /*******************************************************************
2318 An aio_return wrapper that will deal with 64-bit sizes.
2319 ********************************************************************/
2321 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2323 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2324 return aio_return64(aiocb
);
2325 #elif defined(HAVE_AIO_RETURN)
2326 return aio_return(aiocb
);
2333 /*******************************************************************
2334 An aio_cancel wrapper that will deal with 64-bit sizes.
2335 ********************************************************************/
2337 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2339 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2340 return aio_cancel64(fd
, aiocb
);
2341 #elif defined(HAVE_AIO_CANCEL)
2342 return aio_cancel(fd
, aiocb
);
2349 /*******************************************************************
2350 An aio_error wrapper that will deal with 64-bit sizes.
2351 ********************************************************************/
2353 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2355 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2356 return aio_error64(aiocb
);
2357 #elif defined(HAVE_AIO_ERROR)
2358 return aio_error(aiocb
);
2365 /*******************************************************************
2366 An aio_fsync wrapper that will deal with 64-bit sizes.
2367 ********************************************************************/
2369 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2371 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2372 return aio_fsync64(op
, aiocb
);
2373 #elif defined(HAVE_AIO_FSYNC)
2374 return aio_fsync(op
, aiocb
);
2381 /*******************************************************************
2382 An aio_fsync wrapper that will deal with 64-bit sizes.
2383 ********************************************************************/
2385 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2387 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2388 return aio_suspend64(cblist
, n
, timeout
);
2389 #elif defined(HAVE_AIO_FSYNC)
2390 return aio_suspend(cblist
, n
, timeout
);
2396 #else /* !WITH_AIO */
2398 int sys_aio_read(SMB_STRUCT_AIOCB
*aiocb
)
2404 int sys_aio_write(SMB_STRUCT_AIOCB
*aiocb
)
2410 ssize_t
sys_aio_return(SMB_STRUCT_AIOCB
*aiocb
)
2416 int sys_aio_cancel(int fd
, SMB_STRUCT_AIOCB
*aiocb
)
2422 int sys_aio_error(const SMB_STRUCT_AIOCB
*aiocb
)
2428 int sys_aio_fsync(int op
, SMB_STRUCT_AIOCB
*aiocb
)
2434 int sys_aio_suspend(const SMB_STRUCT_AIOCB
* const cblist
[], int n
, const struct timespec
*timeout
)
2439 #endif /* WITH_AIO */
2441 int sys_getpeereid( int s
, uid_t
*uid
)
2443 #if defined(HAVE_PEERCRED)
2445 socklen_t cred_len
= sizeof(struct ucred
);
2448 ret
= getsockopt(s
, SOL_SOCKET
, SO_PEERCRED
, (void *)&cred
, &cred_len
);
2453 if (cred_len
!= sizeof(struct ucred
)) {
2466 int sys_getnameinfo(const struct sockaddr
*psa
,
2475 * For Solaris we must make sure salen is the
2476 * correct length for the incoming sa_family.
2479 if (salen
== sizeof(struct sockaddr_storage
)) {
2480 salen
= sizeof(struct sockaddr_in
);
2481 #if defined(HAVE_IPV6)
2482 if (psa
->sa_family
== AF_INET6
) {
2483 salen
= sizeof(struct sockaddr_in6
);
2487 return getnameinfo(psa
, salen
, host
, hostlen
, service
, servlen
, flags
);
2490 int sys_connect(int fd
, const struct sockaddr
* addr
)
2492 socklen_t salen
= -1;
2494 if (addr
->sa_family
== AF_INET
) {
2495 salen
= sizeof(struct sockaddr_in
);
2496 } else if (addr
->sa_family
== AF_UNIX
) {
2497 salen
= sizeof(struct sockaddr_un
);
2499 #if defined(HAVE_IPV6)
2500 else if (addr
->sa_family
== AF_INET6
) {
2501 salen
= sizeof(struct sockaddr_in6
);
2505 return connect(fd
, addr
, salen
);