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 pread wrapper that will deal with EINTR and 64-bit file offsets.
146 ********************************************************************/
148 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
149 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
154 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
155 ret = pread64(fd, buf, count, off);
157 ret = pread(fd, buf, count, off);
159 } while (ret == -1 && errno == EINTR);
164 /*******************************************************************
165 A write wrapper that will deal with EINTR and 64-bit file offsets.
166 ********************************************************************/
168 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
169 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
174 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
175 ret = pwrite64(fd, buf, count, off);
177 ret = pwrite(fd, buf, count, off);
179 } while (ret == -1 && errno == EINTR);
184 /*******************************************************************
185 A send wrapper that will deal with EINTR.
186 ********************************************************************/
188 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
193 ret = send(s, msg, len, flags);
194 } while (ret == -1 && errno == EINTR);
198 /*******************************************************************
199 A sendto wrapper that will deal with EINTR.
200 ********************************************************************/
202 ssize_t sys_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
207 ret = sendto(s, msg, len, flags, to, tolen);
208 } while (ret == -1 && errno == EINTR);
212 /*******************************************************************
213 A recv wrapper that will deal with EINTR.
214 ********************************************************************/
216 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
221 ret = recv(fd, buf, count, flags);
222 } while (ret == -1 && errno == EINTR);
226 /*******************************************************************
227 A recvfrom wrapper that will deal with EINTR.
228 ********************************************************************/
230 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
235 ret = recvfrom(s, buf, len, flags, from, fromlen);
236 } while (ret == -1 && errno == EINTR);
240 /*******************************************************************
241 A fcntl wrapper that will deal with EINTR.
242 ********************************************************************/
244 int sys_fcntl_ptr(int fd, int cmd, void *arg)
249 ret = fcntl(fd, cmd, arg);
250 } while (ret == -1 && errno == EINTR);
254 /*******************************************************************
255 A fcntl wrapper that will deal with EINTR.
256 ********************************************************************/
258 int sys_fcntl_long(int fd, int cmd, long arg)
263 ret = fcntl(fd, cmd, arg);
264 } while (ret == -1 && errno == EINTR);
268 /*******************************************************************
269 A stat() wrapper that will deal with 64 bit filesizes.
270 ********************************************************************/
272 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
275 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
276 ret = stat64(fname, sbuf);
278 ret = stat(fname, sbuf);
280 /* we always want directories to appear zero size */
281 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
285 /*******************************************************************
286 An fstat() wrapper that will deal with 64 bit filesizes.
287 ********************************************************************/
289 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
292 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
293 ret = fstat64(fd, sbuf);
295 ret = fstat(fd, sbuf);
297 /* we always want directories to appear zero size */
298 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
302 /*******************************************************************
303 An lstat() wrapper that will deal with 64 bit filesizes.
304 ********************************************************************/
306 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
309 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
310 ret = lstat64(fname, sbuf);
312 ret = lstat(fname, sbuf);
314 /* we always want directories to appear zero size */
315 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
319 /*******************************************************************
320 An ftruncate() wrapper that will deal with 64 bit filesizes.
321 ********************************************************************/
323 int sys_ftruncate(int fd, SMB_OFF_T offset)
325 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
326 return ftruncate64(fd, offset);
328 return ftruncate(fd, offset);
332 /*******************************************************************
333 An lseek() wrapper that will deal with 64 bit filesizes.
334 ********************************************************************/
336 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
338 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
339 return lseek64(fd, offset, whence);
341 return lseek(fd, offset, whence);
345 /*******************************************************************
346 An fseek() wrapper that will deal with 64 bit filesizes.
347 ********************************************************************/
349 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
351 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
352 return fseek64(fp, offset, whence);
353 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
354 return fseeko64(fp, offset, whence);
356 return fseek(fp, offset, whence);
360 /*******************************************************************
361 An ftell() wrapper that will deal with 64 bit filesizes.
362 ********************************************************************/
364 SMB_OFF_T sys_ftell(FILE *fp)
366 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
367 return (SMB_OFF_T)ftell64(fp);
368 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
369 return (SMB_OFF_T)ftello64(fp);
371 return (SMB_OFF_T)ftell(fp);
375 /*******************************************************************
376 A creat() wrapper that will deal with 64 bit filesizes.
377 ********************************************************************/
379 int sys_creat(const char *path, mode_t mode)
381 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
382 return creat64(path, mode);
385 * If creat64 isn't defined then ensure we call a potential open64.
388 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
392 /*******************************************************************
393 An open() wrapper that will deal with 64 bit filesizes.
394 ********************************************************************/
396 int sys_open(const char *path, int oflag, mode_t mode)
398 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
399 return open64(path, oflag, mode);
401 return open(path, oflag, mode);
405 /*******************************************************************
406 An fopen() wrapper that will deal with 64 bit filesizes.
407 ********************************************************************/
409 FILE *sys_fopen(const char *path, const char *type)
411 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
412 return fopen64(path, type);
414 return fopen(path, type);
419 /*******************************************************************
420 A flock() wrapper that will perform the kernel flock.
421 ********************************************************************/
423 void kernel_flock(int fd, uint32 share_mode)
425 #if HAVE_KERNEL_SHARE_MODES
427 if (share_mode == FILE_SHARE_WRITE) {
428 kernel_mode = LOCK_MAND|LOCK_WRITE;
429 } else if (share_mode == FILE_SHARE_READ) {
430 kernel_mode = LOCK_MAND|LOCK_READ;
431 } else if (share_mode == FILE_SHARE_NONE) {
432 kernel_mode = LOCK_MAND;
435 flock(fd, kernel_mode);
443 /*******************************************************************
444 An opendir wrapper that will deal with 64 bit filesizes.
445 ********************************************************************/
447 SMB_STRUCT_DIR *sys_opendir(const char *name)
449 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
450 return opendir64(name);
452 return opendir(name);
456 /*******************************************************************
457 A readdir wrapper that will deal with 64 bit filesizes.
458 ********************************************************************/
460 SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
462 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
463 return readdir64(dirp);
465 return readdir(dirp);
469 /*******************************************************************
470 A seekdir wrapper that will deal with 64 bit filesizes.
471 ********************************************************************/
473 void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
475 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
476 seekdir64(dirp, offset);
478 seekdir(dirp, offset);
482 /*******************************************************************
483 A telldir wrapper that will deal with 64 bit filesizes.
484 ********************************************************************/
486 long sys_telldir(SMB_STRUCT_DIR *dirp)
488 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
489 return (long)telldir64(dirp);
491 return (long)telldir(dirp);
495 /*******************************************************************
496 A rewinddir wrapper that will deal with 64 bit filesizes.
497 ********************************************************************/
499 void sys_rewinddir(SMB_STRUCT_DIR *dirp)
501 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
508 /*******************************************************************
509 A close wrapper that will deal with 64 bit filesizes.
510 ********************************************************************/
512 int sys_closedir(SMB_STRUCT_DIR *dirp)
514 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
515 return closedir64(dirp);
517 return closedir(dirp);
521 /*******************************************************************
522 An mknod() wrapper that will deal with 64 bit filesizes.
523 ********************************************************************/
525 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
527 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
528 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
529 return mknod64(path, mode, dev);
531 return mknod(path, mode, dev);
534 /* No mknod system call. */
540 /*******************************************************************
541 Wrapper for realpath.
542 ********************************************************************/
544 char *sys_realpath(const char *path, char *resolved_path)
546 #if defined(HAVE_REALPATH)
547 return realpath(path, resolved_path);
549 /* As realpath is not a system call we can't return ENOSYS. */
555 /*******************************************************************
556 The wait() calls vary between systems
557 ********************************************************************/
559 int sys_waitpid(pid_t pid,int *status,int options)
562 return waitpid(pid,status,options);
563 #else /* HAVE_WAITPID */
564 return wait4(pid, status, options, NULL);
565 #endif /* HAVE_WAITPID */
568 /*******************************************************************
569 System wrapper for getwd
570 ********************************************************************/
572 char *sys_getwd(char *s)
577 wd = (char *)getcwd(s, PATH_MAX);
579 wd = (char *)getcwd(s, sizeof (pstring));
582 wd = (char *)getwd(s);
587 /*******************************************************************
588 system wrapper for symlink
589 ********************************************************************/
591 int sys_symlink(const char *oldpath, const char *newpath)
597 return symlink(oldpath, newpath);
601 /*******************************************************************
602 system wrapper for readlink
603 ********************************************************************/
605 int sys_readlink(const char *path, char *buf, size_t bufsiz)
607 #ifndef HAVE_READLINK
611 return readlink(path, buf, bufsiz);
615 /*******************************************************************
616 system wrapper for link
617 ********************************************************************/
619 int sys_link(const char *oldpath, const char *newpath)
625 return link(oldpath, newpath);
629 /*******************************************************************
630 chown isn't used much but OS/2 doesn't have it
631 ********************************************************************/
633 int sys_chown(const char *fname,uid_t uid,gid_t gid)
638 DEBUG(1,("WARNING: no chown!\n"));
644 return(chown(fname,uid,gid));
648 /*******************************************************************
650 ********************************************************************/
652 int sys_lchown(const char *fname,uid_t uid,gid_t gid)
657 DEBUG(1,("WARNING: no lchown!\n"));
663 return(lchown(fname,uid,gid));
667 /*******************************************************************
668 os/2 also doesn't have chroot
669 ********************************************************************/
670 int sys_chroot(const char *dname)
675 DEBUG(1,("WARNING: no chroot!\n"));
681 return(chroot(dname));
685 #if defined(HAVE_POSIX_CAPABILITIES)
687 #ifdef HAVE_SYS_CAPABILITY_H
689 #if defined(BROKEN_REDHAT_7_SYSTEM_HEADERS) && !defined(_I386_STATFS_H) && !defined(_PPC_STATFS_H)
690 #define _I386_STATFS_H
691 #define _PPC_STATFS_H
692 #define BROKEN_REDHAT_7_STATFS_WORKAROUND
695 #include <sys/capability.h>
697 #ifdef BROKEN_REDHAT_7_STATFS_WORKAROUND
698 #undef _I386_STATFS_H
700 #undef BROKEN_REDHAT_7_STATFS_WORKAROUND
703 #endif /* HAVE_SYS_CAPABILITY_H */
705 /**************************************************************************
706 Try and abstract process capabilities (for systems that have them).
707 ****************************************************************************/
709 /* Set the POSIX capabilities needed for the given purpose into the effective
710 * capability set of the current process. Make sure they are always removed
711 * from the inheritable set, because there is no circumstance in which our
712 * children should inherit our elevated privileges.
714 static bool set_process_capability(enum smbd_capability capability,
717 cap_value_t cap_vals[2] = {0};
718 int num_cap_vals = 0;
722 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
723 /* On Linux, make sure that any capabilities we grab are sticky
724 * across UID changes. We expect that this would allow us to keep both
725 * the effective and permitted capability sets, but as of circa 2.6.16,
726 * only the permitted set is kept. It is a bug (which we work around)
727 * that the effective set is lost, but we still require the effective
730 if (!prctl(PR_GET_KEEPCAPS)) {
731 prctl(PR_SET_KEEPCAPS, 1);
735 cap = cap_get_proc();
737 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
742 switch (capability) {
743 case KERNEL_OPLOCK_CAPABILITY:
744 #ifdef CAP_NETWORK_MGT
745 /* IRIX has CAP_NETWORK_MGT for oplocks. */
746 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
749 case DMAPI_ACCESS_CAPABILITY:
750 #ifdef CAP_DEVICE_MGT
751 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
752 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
754 /* Linux has CAP_MKNOD for DMAPI access. */
755 cap_vals[num_cap_vals++] = CAP_MKNOD;
760 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
762 if (num_cap_vals == 0) {
767 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
768 enable ? CAP_SET : CAP_CLEAR);
770 /* We never want to pass capabilities down to our children, so make
771 * sure they are not inherited.
773 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
775 if (cap_set_proc(cap) == -1) {
776 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
786 #endif /* HAVE_POSIX_CAPABILITIES */
788 /****************************************************************************
789 Gain the oplock capability from the kernel if possible.
790 ****************************************************************************/
792 void set_effective_capability(enum smbd_capability capability)
794 #if defined(HAVE_POSIX_CAPABILITIES)
795 set_process_capability(capability, True);
796 #endif /* HAVE_POSIX_CAPABILITIES */
799 void drop_effective_capability(enum smbd_capability capability)
801 #if defined(HAVE_POSIX_CAPABILITIES)
802 set_process_capability(capability, False);
803 #endif /* HAVE_POSIX_CAPABILITIES */
806 /**************************************************************************
807 Wrapper for random().
808 ****************************************************************************/
810 long sys_random(void)
812 #if defined(HAVE_RANDOM)
813 return (long)random();
814 #elif defined(HAVE_RAND)
817 DEBUG(0,("Error - no random function available !\n"));
822 /**************************************************************************
823 Wrapper for srandom().
824 ****************************************************************************/
826 void sys_srandom(unsigned int seed)
828 #if defined(HAVE_SRANDOM)
830 #elif defined(HAVE_SRAND)
833 DEBUG(0,("Error - no srandom function available !\n"));
838 /**************************************************************************
839 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
840 ****************************************************************************/
844 #if defined(SYSCONF_SC_NGROUPS_MAX)
845 int ret = sysconf(_SC_NGROUPS_MAX);
846 return (ret == -1) ? NGROUPS_MAX : ret;
852 /**************************************************************************
853 Wrap setgroups and getgroups for systems that declare getgroups() as
854 returning an array of gid_t, but actuall return an array of int.
855 ****************************************************************************/
857 #if defined(HAVE_BROKEN_GETGROUPS)
858 static int sys_broken_getgroups(int setlen, gid_t *gidset)
865 return getgroups(setlen, &gid);
869 * Broken case. We need to allocate a
870 * GID_T array of size setlen.
879 setlen = groups_max();
881 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
882 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
886 if((ngroups = getgroups(setlen, group_list)) < 0) {
887 int saved_errno = errno;
888 SAFE_FREE(group_list);
893 for(i = 0; i < ngroups; i++)
894 gidset[i] = (gid_t)group_list[i];
896 SAFE_FREE(group_list);
900 static int sys_broken_setgroups(int setlen, gid_t *gidset)
908 if (setlen < 0 || setlen > groups_max()) {
914 * Broken case. We need to allocate a
915 * GID_T array of size setlen.
918 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
919 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
923 for(i = 0; i < setlen; i++)
924 group_list[i] = (GID_T) gidset[i];
926 if(setgroups(setlen, group_list) != 0) {
927 int saved_errno = errno;
928 SAFE_FREE(group_list);
933 SAFE_FREE(group_list);
937 #endif /* HAVE_BROKEN_GETGROUPS */
939 /* This is a list of systems that require the first GID passed to setgroups(2)
940 * to be the effective GID. If your system is one of these, add it here.
942 #if defined (FREEBSD) || defined (DARWINOS)
943 #define USE_BSD_SETGROUPS
946 #if defined(USE_BSD_SETGROUPS)
947 /* Depending on the particular BSD implementation, the first GID that is
948 * passed to setgroups(2) will either be ignored or will set the credential's
949 * effective GID. In either case, the right thing to do is to guarantee that
950 * gidset[0] is the effective GID.
952 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
954 gid_t *new_gidset = NULL;
958 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
961 /* No group list, just make sure we are setting the efective GID. */
963 return setgroups(1, &primary_gid);
966 /* If the primary gid is not the first array element, grow the array
967 * and insert it at the front.
969 if (gidset[0] != primary_gid) {
970 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
971 if (new_gidset == NULL) {
975 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
976 new_gidset[0] = primary_gid;
981 DEBUG(3, ("forced to truncate group list from %d to %d\n",
986 #if defined(HAVE_BROKEN_GETGROUPS)
987 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
989 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
994 SAFE_FREE(new_gidset);
1001 #endif /* USE_BSD_SETGROUPS */
1003 /**************************************************************************
1004 Wrapper for getgroups. Deals with broken (int) case.
1005 ****************************************************************************/
1007 int sys_getgroups(int setlen, gid_t *gidset)
1009 #if defined(HAVE_BROKEN_GETGROUPS)
1010 return sys_broken_getgroups(setlen, gidset);
1012 return getgroups(setlen, gidset);
1016 /**************************************************************************
1017 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1018 ****************************************************************************/
1020 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1022 #if !defined(HAVE_SETGROUPS)
1025 #endif /* HAVE_SETGROUPS */
1027 #if defined(USE_BSD_SETGROUPS)
1028 return sys_bsd_setgroups(primary_gid, setlen, gidset);
1029 #elif defined(HAVE_BROKEN_GETGROUPS)
1030 return sys_broken_setgroups(setlen, gidset);
1032 return setgroups(setlen, gidset);
1036 /**************************************************************************
1037 Wrappers for setpwent(), getpwent() and endpwent()
1038 ****************************************************************************/
1040 void sys_setpwent(void)
1045 struct passwd *sys_getpwent(void)
1050 void sys_endpwent(void)
1055 /**************************************************************************
1056 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1057 ****************************************************************************/
1059 #ifdef ENABLE_BUILD_FARM_HACKS
1062 * In the build farm we want to be able to join machines to the domain. As we
1063 * don't have root access, we need to bypass direct access to /etc/passwd
1064 * after a user has been created via samr. Fake those users.
1067 static struct passwd *fake_pwd;
1068 static int num_fake_pwd;
1070 struct passwd *sys_getpwnam(const char *name)
1074 for (i=0; i<num_fake_pwd; i++) {
1075 if (strcmp(fake_pwd[i].pw_name, name) == 0) {
1076 DEBUG(10, ("Returning fake user %s\n", name));
1077 return &fake_pwd[i];
1081 return getpwnam(name);
1084 struct passwd *sys_getpwuid(uid_t uid)
1088 for (i=0; i<num_fake_pwd; i++) {
1089 if (fake_pwd[i].pw_uid == uid) {
1090 DEBUG(10, ("Returning fake user %s\n",
1091 fake_pwd[i].pw_name));
1092 return &fake_pwd[i];
1096 return getpwuid(uid);
1099 void faked_create_user(const char *name)
1103 struct passwd new_pwd;
1105 for (i=0; i<10; i++) {
1106 generate_random_buffer((unsigned char *)&uid,
1108 if (getpwuid(uid) == NULL) {
1114 /* Weird. No free uid found... */
1118 new_pwd.pw_name = SMB_STRDUP(name);
1119 new_pwd.pw_passwd = SMB_STRDUP("x");
1120 new_pwd.pw_uid = uid;
1121 new_pwd.pw_gid = 100;
1122 new_pwd.pw_gecos = SMB_STRDUP("faked user");
1123 new_pwd.pw_dir = SMB_STRDUP("/nodir");
1124 new_pwd.pw_shell = SMB_STRDUP("/bin/false");
1126 ADD_TO_ARRAY(NULL, struct passwd, new_pwd, &fake_pwd,
1129 DEBUG(10, ("Added fake user %s, have %d fake users\n",
1130 name, num_fake_pwd));
1135 struct passwd *sys_getpwnam(const char *name)
1137 return getpwnam(name);
1140 struct passwd *sys_getpwuid(uid_t uid)
1142 return getpwuid(uid);
1147 struct group *sys_getgrnam(const char *name)
1149 return getgrnam(name);
1152 struct group *sys_getgrgid(gid_t gid)
1154 return getgrgid(gid);
1157 /**************************************************************************
1158 Extract a command into an arg list.
1159 ****************************************************************************/
1161 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1170 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1171 DEBUG(0, ("talloc failed\n"));
1175 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1176 TALLOC_FREE(trunc_cmd);
1185 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1188 TALLOC_FREE(trunc_cmd);
1190 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1195 * Now do the extraction.
1198 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1202 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1205 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1209 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1211 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1220 DEBUG(0, ("talloc failed\n"));
1221 TALLOC_FREE(trunc_cmd);
1227 /**************************************************************************
1228 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1229 a sys_getpid() that only does a system call *once*.
1230 ****************************************************************************/
1232 static pid_t mypid = (pid_t)-1;
1234 pid_t sys_fork(void)
1236 pid_t forkret = fork();
1238 if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */
1244 /**************************************************************************
1245 Wrapper for getpid. Ensures we only do a system call *once*.
1246 ****************************************************************************/
1248 pid_t sys_getpid(void)
1250 if (mypid == (pid_t)-1)
1256 /**************************************************************************
1257 Wrapper for popen. Safer as it doesn't search a path.
1258 Modified from the glibc sources.
1259 modified by tridge to return a file descriptor. We must kick our FILE* habit
1260 ****************************************************************************/
1262 typedef struct _popen_list
1266 struct _popen_list *next;
1269 static popen_list *popen_chain;
1271 int sys_popen(const char *command)
1273 int parent_end, child_end;
1275 popen_list *entry = NULL;
1278 if (pipe(pipe_fds) < 0)
1281 parent_end = pipe_fds[0];
1282 child_end = pipe_fds[1];
1289 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1292 ZERO_STRUCTP(entry);
1295 * Extract the command and args into a NULL terminated array.
1298 if(!(argl = extract_args(NULL, command)))
1301 entry->child_pid = sys_fork();
1303 if (entry->child_pid == -1) {
1307 if (entry->child_pid == 0) {
1313 int child_std_end = STDOUT_FILENO;
1317 if (child_end != child_std_end) {
1318 dup2 (child_end, child_std_end);
1323 * POSIX.2: "popen() shall ensure that any streams from previous
1324 * popen() calls that remain open in the parent process are closed
1325 * in the new child process."
1328 for (p = popen_chain; p; p = p->next)
1331 execv(argl[0], argl);
1342 /* Link into popen_chain. */
1343 entry->next = popen_chain;
1344 popen_chain = entry;
1345 entry->fd = parent_end;
1358 /**************************************************************************
1359 Wrapper for pclose. Modified from the glibc sources.
1360 ****************************************************************************/
1362 int sys_pclose(int fd)
1365 popen_list **ptr = &popen_chain;
1366 popen_list *entry = NULL;
1370 /* Unlink from popen_chain. */
1371 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1372 if ((*ptr)->fd == fd) {
1374 *ptr = (*ptr)->next;
1380 if (status < 0 || close(entry->fd) < 0)
1384 * As Samba is catching and eating child process
1385 * exits we don't really care about the child exit
1386 * code, a -1 with errno = ECHILD will do fine for us.
1390 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1391 } while (wait_pid == -1 && errno == EINTR);
1400 /**************************************************************************
1401 Wrappers for dlopen, dlsym, dlclose.
1402 ****************************************************************************/
1404 void *sys_dlopen(const char *name, int flags)
1406 #if defined(HAVE_DLOPEN)
1407 return dlopen(name, flags);
1413 void *sys_dlsym(void *handle, const char *symbol)
1415 #if defined(HAVE_DLSYM)
1416 return dlsym(handle, symbol);
1422 int sys_dlclose (void *handle)
1424 #if defined(HAVE_DLCLOSE)
1425 return dlclose(handle);
1431 const char *sys_dlerror(void)
1433 #if defined(HAVE_DLERROR)
1440 int sys_dup2(int oldfd, int newfd)
1442 #if defined(HAVE_DUP2)
1443 return dup2(oldfd, newfd);
1450 /**************************************************************************
1451 Wrapper for Admin Logs.
1452 ****************************************************************************/
1454 void sys_adminlog(int priority, const char *format_str, ...)
1458 char *msgbuf = NULL;
1460 va_start( ap, format_str );
1461 ret = vasprintf( &msgbuf, format_str, ap );
1467 #if defined(HAVE_SYSLOG)
1468 syslog( priority, "%s", msgbuf );
1470 DEBUG(0,("%s", msgbuf ));
1475 /******** Solaris EA helper function prototypes ********/
1476 #ifdef HAVE_ATTROPEN
1477 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1478 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1479 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1480 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1481 static int solaris_unlinkat(int attrdirfd, const char *name);
1482 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1483 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1486 /**************************************************************************
1487 Wrappers for extented attribute calls. Based on the Linux package with
1488 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1489 ****************************************************************************/
1491 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1493 #if defined(HAVE_GETXATTR)
1494 #ifndef XATTR_ADD_OPT
1495 return getxattr(path, name, value, size);
1498 return getxattr(path, name, value, size, 0, options);
1500 #elif defined(HAVE_GETEA)
1501 return getea(path, name, value, size);
1502 #elif defined(HAVE_EXTATTR_GET_FILE)
1505 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1506 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1507 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1509 * The BSD implementation has a nasty habit of silently truncating
1510 * the returned value to the size of the buffer, so we have to check
1511 * that the buffer is large enough to fit the returned value.
1513 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1518 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1522 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1524 #elif defined(HAVE_ATTR_GET)
1525 int retval, flags = 0;
1526 int valuelength = (int)size;
1527 char *attrname = strchr(name,'.') + 1;
1529 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1531 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1533 return retval ? retval : valuelength;
1534 #elif defined(HAVE_ATTROPEN)
1536 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1538 ret = solaris_read_xattr(attrfd, value, size);
1548 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1550 #if defined(HAVE_LGETXATTR)
1551 return lgetxattr(path, name, value, size);
1552 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1553 int options = XATTR_NOFOLLOW;
1554 return getxattr(path, name, value, size, 0, options);
1555 #elif defined(HAVE_LGETEA)
1556 return lgetea(path, name, value, size);
1557 #elif defined(HAVE_EXTATTR_GET_LINK)
1560 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1561 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1562 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1564 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1569 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1573 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1575 #elif defined(HAVE_ATTR_GET)
1576 int retval, flags = ATTR_DONTFOLLOW;
1577 int valuelength = (int)size;
1578 char *attrname = strchr(name,'.') + 1;
1580 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1582 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1584 return retval ? retval : valuelength;
1585 #elif defined(HAVE_ATTROPEN)
1587 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1589 ret = solaris_read_xattr(attrfd, value, size);
1599 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1601 #if defined(HAVE_FGETXATTR)
1602 #ifndef XATTR_ADD_OPT
1603 return fgetxattr(filedes, name, value, size);
1606 return fgetxattr(filedes, name, value, size, 0, options);
1608 #elif defined(HAVE_FGETEA)
1609 return fgetea(filedes, name, value, size);
1610 #elif defined(HAVE_EXTATTR_GET_FD)
1613 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1614 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1615 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1617 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1622 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1626 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1628 #elif defined(HAVE_ATTR_GETF)
1629 int retval, flags = 0;
1630 int valuelength = (int)size;
1631 char *attrname = strchr(name,'.') + 1;
1633 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1635 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1637 return retval ? retval : valuelength;
1638 #elif defined(HAVE_ATTROPEN)
1640 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1642 ret = solaris_read_xattr(attrfd, value, size);
1652 #if defined(HAVE_EXTATTR_LIST_FILE)
1654 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1662 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1663 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1671 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1673 ssize_t list_size, total_size = 0;
1676 /* Iterate through extattr(2) namespaces */
1677 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1679 #if defined(HAVE_EXTATTR_LIST_FILE)
1681 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1684 #if defined(HAVE_EXTATTR_LIST_LINK)
1686 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1689 #if defined(HAVE_EXTATTR_LIST_FD)
1691 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1698 /* Some error happend. Errno should be set by the previous call */
1704 /* XXX: Call with an empty buffer may be used to calculate
1705 necessary buffer size. Unfortunately, we can't say, how
1706 many attributes were returned, so here is the potential
1707 problem with the emulation.
1710 /* Take the worse case of one char attribute names -
1711 two bytes per name plus one more for sanity.
1713 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1716 /* Count necessary offset to fit namespace prefixes */
1718 for(i = 0; i < list_size; i += list[i] + 1)
1719 len += extattr[t].len;
1721 total_size += list_size + len;
1722 /* Buffer is too small to fit the results */
1723 if(total_size > size) {
1727 /* Shift results back, so we can prepend prefixes */
1728 buf = memmove(list + len, list, list_size);
1730 for(i = 0; i < list_size; i += len + 1) {
1732 strncpy(list, extattr[t].name, extattr[t].len + 1);
1733 list += extattr[t].len;
1734 strncpy(list, buf + i + 1, len);
1745 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1746 static char attr_buffer[ATTR_MAX_VALUELEN];
1748 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1750 int retval = 0, index;
1751 attrlist_cursor_t *cursor = 0;
1753 attrlist_t * al = (attrlist_t *)attr_buffer;
1755 size_t ent_size, left = size;
1760 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1762 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1764 for (index = 0; index < al->al_count; index++) {
1765 ae = ATTR_ENTRY(attr_buffer, index);
1766 ent_size = strlen(ae->a_name) + sizeof("user.");
1767 if (left >= ent_size) {
1768 strncpy(bp, "user.", sizeof("user."));
1769 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1777 total_size += ent_size;
1779 if (al->al_more == 0) break;
1786 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1788 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1790 for (index = 0; index < al->al_count; index++) {
1791 ae = ATTR_ENTRY(attr_buffer, index);
1792 ent_size = strlen(ae->a_name) + sizeof("system.");
1793 if (left >= ent_size) {
1794 strncpy(bp, "system.", sizeof("system."));
1795 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1803 total_size += ent_size;
1805 if (al->al_more == 0) break;
1808 return (ssize_t)(retval ? retval : total_size);
1813 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1815 #if defined(HAVE_LISTXATTR)
1816 #ifndef XATTR_ADD_OPT
1817 return listxattr(path, list, size);
1820 return listxattr(path, list, size, options);
1822 #elif defined(HAVE_LISTEA)
1823 return listea(path, list, size);
1824 #elif defined(HAVE_EXTATTR_LIST_FILE)
1827 return bsd_attr_list(0, arg, list, size);
1828 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1829 return irix_attr_list(path, 0, list, size, 0);
1830 #elif defined(HAVE_ATTROPEN)
1832 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1833 if (attrdirfd >= 0) {
1834 ret = solaris_list_xattr(attrdirfd, list, size);
1844 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1846 #if defined(HAVE_LLISTXATTR)
1847 return llistxattr(path, list, size);
1848 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1849 int options = XATTR_NOFOLLOW;
1850 return listxattr(path, list, size, options);
1851 #elif defined(HAVE_LLISTEA)
1852 return llistea(path, list, size);
1853 #elif defined(HAVE_EXTATTR_LIST_LINK)
1856 return bsd_attr_list(1, arg, list, size);
1857 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1858 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1859 #elif defined(HAVE_ATTROPEN)
1861 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1862 if (attrdirfd >= 0) {
1863 ret = solaris_list_xattr(attrdirfd, list, size);
1873 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1875 #if defined(HAVE_FLISTXATTR)
1876 #ifndef XATTR_ADD_OPT
1877 return flistxattr(filedes, list, size);
1880 return flistxattr(filedes, list, size, options);
1882 #elif defined(HAVE_FLISTEA)
1883 return flistea(filedes, list, size);
1884 #elif defined(HAVE_EXTATTR_LIST_FD)
1886 arg.filedes = filedes;
1887 return bsd_attr_list(2, arg, list, size);
1888 #elif defined(HAVE_ATTR_LISTF)
1889 return irix_attr_list(NULL, filedes, list, size, 0);
1890 #elif defined(HAVE_ATTROPEN)
1892 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1893 if (attrdirfd >= 0) {
1894 ret = solaris_list_xattr(attrdirfd, list, size);
1904 int sys_removexattr (const char *path, const char *name)
1906 #if defined(HAVE_REMOVEXATTR)
1907 #ifndef XATTR_ADD_OPT
1908 return removexattr(path, name);
1911 return removexattr(path, name, options);
1913 #elif defined(HAVE_REMOVEEA)
1914 return removeea(path, name);
1915 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1917 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1918 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1919 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1921 return extattr_delete_file(path, attrnamespace, attrname);
1922 #elif defined(HAVE_ATTR_REMOVE)
1924 char *attrname = strchr(name,'.') + 1;
1926 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1928 return attr_remove(path, attrname, flags);
1929 #elif defined(HAVE_ATTROPEN)
1931 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1932 if (attrdirfd >= 0) {
1933 ret = solaris_unlinkat(attrdirfd, name);
1943 int sys_lremovexattr (const char *path, const char *name)
1945 #if defined(HAVE_LREMOVEXATTR)
1946 return lremovexattr(path, name);
1947 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1948 int options = XATTR_NOFOLLOW;
1949 return removexattr(path, name, options);
1950 #elif defined(HAVE_LREMOVEEA)
1951 return lremoveea(path, name);
1952 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1954 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1955 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1956 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1958 return extattr_delete_link(path, attrnamespace, attrname);
1959 #elif defined(HAVE_ATTR_REMOVE)
1960 int flags = ATTR_DONTFOLLOW;
1961 char *attrname = strchr(name,'.') + 1;
1963 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1965 return attr_remove(path, attrname, flags);
1966 #elif defined(HAVE_ATTROPEN)
1968 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1969 if (attrdirfd >= 0) {
1970 ret = solaris_unlinkat(attrdirfd, name);
1980 int sys_fremovexattr (int filedes, const char *name)
1982 #if defined(HAVE_FREMOVEXATTR)
1983 #ifndef XATTR_ADD_OPT
1984 return fremovexattr(filedes, name);
1987 return fremovexattr(filedes, name, options);
1989 #elif defined(HAVE_FREMOVEEA)
1990 return fremoveea(filedes, name);
1991 #elif defined(HAVE_EXTATTR_DELETE_FD)
1993 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1994 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1995 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1997 return extattr_delete_fd(filedes, attrnamespace, attrname);
1998 #elif defined(HAVE_ATTR_REMOVEF)
2000 char *attrname = strchr(name,'.') + 1;
2002 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2004 return attr_removef(filedes, attrname, flags);
2005 #elif defined(HAVE_ATTROPEN)
2007 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2008 if (attrdirfd >= 0) {
2009 ret = solaris_unlinkat(attrdirfd, name);
2019 #if !defined(HAVE_SETXATTR)
2020 #define XATTR_CREATE 0x1 /* set value, fail if attr already exists */
2021 #define XATTR_REPLACE 0x2 /* set value, fail if attr does not exist */
2024 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2026 #if defined(HAVE_SETXATTR)
2027 #ifndef XATTR_ADD_OPT
2028 return setxattr(path, name, value, size, flags);
2031 return setxattr(path, name, value, size, 0, options);
2033 #elif defined(HAVE_SETEA)
2034 return setea(path, name, value, size, flags);
2035 #elif defined(HAVE_EXTATTR_SET_FILE)
2038 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2039 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2040 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2042 /* Check attribute existence */
2043 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
2045 /* REPLACE attribute, that doesn't exist */
2046 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2050 /* Ignore other errors */
2053 /* CREATE attribute, that already exists */
2054 if (flags & XATTR_CREATE) {
2060 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2061 return (retval < 0) ? -1 : 0;
2062 #elif defined(HAVE_ATTR_SET)
2064 char *attrname = strchr(name,'.') + 1;
2066 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2067 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2068 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2070 return attr_set(path, attrname, (const char *)value, size, myflags);
2071 #elif defined(HAVE_ATTROPEN)
2073 int myflags = O_RDWR;
2074 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2075 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2076 int attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2078 ret = solaris_write_xattr(attrfd, value, size);
2088 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2090 #if defined(HAVE_LSETXATTR)
2091 return lsetxattr(path, name, value, size, flags);
2092 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2093 int options = XATTR_NOFOLLOW;
2094 return setxattr(path, name, value, size, 0, options);
2095 #elif defined(LSETEA)
2096 return lsetea(path, name, value, size, flags);
2097 #elif defined(HAVE_EXTATTR_SET_LINK)
2100 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2101 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2102 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2104 /* Check attribute existence */
2105 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2107 /* REPLACE attribute, that doesn't exist */
2108 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2112 /* Ignore other errors */
2115 /* CREATE attribute, that already exists */
2116 if (flags & XATTR_CREATE) {
2123 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2124 return (retval < 0) ? -1 : 0;
2125 #elif defined(HAVE_ATTR_SET)
2126 int myflags = ATTR_DONTFOLLOW;
2127 char *attrname = strchr(name,'.') + 1;
2129 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2130 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2131 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2133 return attr_set(path, attrname, (const char *)value, size, myflags);
2134 #elif defined(HAVE_ATTROPEN)
2136 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2137 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2138 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2139 int attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2141 ret = solaris_write_xattr(attrfd, value, size);
2151 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2153 #if defined(HAVE_FSETXATTR)
2154 #ifndef XATTR_ADD_OPT
2155 return fsetxattr(filedes, name, value, size, flags);
2158 return fsetxattr(filedes, name, value, size, 0, options);
2160 #elif defined(HAVE_FSETEA)
2161 return fsetea(filedes, name, value, size, flags);
2162 #elif defined(HAVE_EXTATTR_SET_FD)
2165 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2166 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2167 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2169 /* Check attribute existence */
2170 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2172 /* REPLACE attribute, that doesn't exist */
2173 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2177 /* Ignore other errors */
2180 /* CREATE attribute, that already exists */
2181 if (flags & XATTR_CREATE) {
2187 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2188 return (retval < 0) ? -1 : 0;
2189 #elif defined(HAVE_ATTR_SETF)
2191 char *attrname = strchr(name,'.') + 1;
2193 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2194 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2195 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2197 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2198 #elif defined(HAVE_ATTROPEN)
2200 int myflags = O_RDWR | O_XATTR;
2201 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2202 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2203 int attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2205 ret = solaris_write_xattr(attrfd, value, size);
2215 /**************************************************************************
2216 helper functions for Solaris' EA support
2217 ****************************************************************************/
2218 #ifdef HAVE_ATTROPEN
2219 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2223 if (fstat(attrfd, &sbuf) == -1) {
2228 /* This is to return the current size of the named extended attribute */
2230 return sbuf.st_size;
2233 /* check size and read xattr */
2234 if (sbuf.st_size > size) {
2239 return read(attrfd, value, sbuf.st_size);
2242 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2248 int newfd = dup(attrdirfd);
2249 /* CAUTION: The originating file descriptor should not be
2250 used again following the call to fdopendir().
2251 For that reason we dup() the file descriptor
2252 here to make things more clear. */
2253 dirp = fdopendir(newfd);
2255 while ((de = readdir(dirp))) {
2256 size_t listlen = strlen(de->d_name);
2257 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2258 /* we don't want "." and ".." here: */
2259 DEBUG(10,("skipped EA %s\n",de->d_name));
2264 /* return the current size of the list of extended attribute names*/
2267 /* check size and copy entrieѕ + nul into list. */
2268 if ((len + listlen + 1) > size) {
2273 safe_strcpy(list + len, de->d_name, listlen);
2274 pstrcpy(list + len, de->d_name);
2282 if (closedir(dirp) == -1) {
2283 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2289 static int solaris_unlinkat(int attrdirfd, const char *name)
2291 if (unlinkat(attrdirfd, name, 0) == -1) {
2292 if (errno == ENOENT) {
2300 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2302 int filedes = attropen(path, attrpath, oflag, mode);
2303 if (filedes == -1) {
2304 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2305 if (errno == EINVAL) {
2314 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2316 int filedes = openat(fildes, path, oflag, mode);
2317 if (filedes == -1) {
2318 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2319 if (errno == EINVAL) {
2328 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2330 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2333 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2337 #endif /*HAVE_ATTROPEN*/
2340 /****************************************************************************
2341 Return the major devicenumber for UNIX extensions.
2342 ****************************************************************************/
2344 uint32 unix_dev_major(SMB_DEV_T dev)
2346 #if defined(HAVE_DEVICE_MAJOR_FN)
2347 return (uint32)major(dev);
2349 return (uint32)(dev >> 8);
2353 /****************************************************************************
2354 Return the minor devicenumber for UNIX extensions.
2355 ****************************************************************************/
2357 uint32 unix_dev_minor(SMB_DEV_T dev)
2359 #if defined(HAVE_DEVICE_MINOR_FN)
2360 return (uint32)minor(dev);
2362 return (uint32)(dev & 0xff);
2366 #if defined(WITH_AIO)
2368 /*******************************************************************
2369 An aio_read wrapper that will deal with 64-bit sizes.
2370 ********************************************************************/
2372 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2374 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2375 return aio_read64(aiocb);
2376 #elif defined(HAVE_AIO_READ)
2377 return aio_read(aiocb);
2384 /*******************************************************************
2385 An aio_write wrapper that will deal with 64-bit sizes.
2386 ********************************************************************/
2388 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2390 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2391 return aio_write64(aiocb);
2392 #elif defined(HAVE_AIO_WRITE)
2393 return aio_write(aiocb);
2400 /*******************************************************************
2401 An aio_return wrapper that will deal with 64-bit sizes.
2402 ********************************************************************/
2404 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2406 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2407 return aio_return64(aiocb);
2408 #elif defined(HAVE_AIO_RETURN)
2409 return aio_return(aiocb);
2416 /*******************************************************************
2417 An aio_cancel wrapper that will deal with 64-bit sizes.
2418 ********************************************************************/
2420 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2422 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2423 return aio_cancel64(fd, aiocb);
2424 #elif defined(HAVE_AIO_CANCEL)
2425 return aio_cancel(fd, aiocb);
2432 /*******************************************************************
2433 An aio_error wrapper that will deal with 64-bit sizes.
2434 ********************************************************************/
2436 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2438 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2439 return aio_error64(aiocb);
2440 #elif defined(HAVE_AIO_ERROR)
2441 return aio_error(aiocb);
2448 /*******************************************************************
2449 An aio_fsync wrapper that will deal with 64-bit sizes.
2450 ********************************************************************/
2452 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2454 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2455 return aio_fsync64(op, aiocb);
2456 #elif defined(HAVE_AIO_FSYNC)
2457 return aio_fsync(op, aiocb);
2464 /*******************************************************************
2465 An aio_fsync wrapper that will deal with 64-bit sizes.
2466 ********************************************************************/
2468 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2470 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2471 return aio_suspend64(cblist, n, timeout);
2472 #elif defined(HAVE_AIO_FSYNC)
2473 return aio_suspend(cblist, n, timeout);
2479 #else /* !WITH_AIO */
2481 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2487 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2493 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2499 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2505 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2511 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2517 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2522 #endif /* WITH_AIO */
2524 int sys_getpeereid( int s, uid_t *uid)
2526 #if defined(HAVE_PEERCRED)
2528 socklen_t cred_len = sizeof(struct ucred);
2531 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2536 if (cred_len != sizeof(struct ucred)) {