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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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)
576 wd = (char *)getcwd(s, sizeof (pstring));
578 wd = (char *)getwd(s);
583 /*******************************************************************
584 system wrapper for symlink
585 ********************************************************************/
587 int sys_symlink(const char *oldpath, const char *newpath)
593 return symlink(oldpath, newpath);
597 /*******************************************************************
598 system wrapper for readlink
599 ********************************************************************/
601 int sys_readlink(const char *path, char *buf, size_t bufsiz)
603 #ifndef HAVE_READLINK
607 return readlink(path, buf, bufsiz);
611 /*******************************************************************
612 system wrapper for link
613 ********************************************************************/
615 int sys_link(const char *oldpath, const char *newpath)
621 return link(oldpath, newpath);
625 /*******************************************************************
626 chown isn't used much but OS/2 doesn't have it
627 ********************************************************************/
629 int sys_chown(const char *fname,uid_t uid,gid_t gid)
634 DEBUG(1,("WARNING: no chown!\n"));
640 return(chown(fname,uid,gid));
644 /*******************************************************************
646 ********************************************************************/
648 int sys_lchown(const char *fname,uid_t uid,gid_t gid)
653 DEBUG(1,("WARNING: no lchown!\n"));
659 return(lchown(fname,uid,gid));
663 /*******************************************************************
664 os/2 also doesn't have chroot
665 ********************************************************************/
666 int sys_chroot(const char *dname)
671 DEBUG(1,("WARNING: no chroot!\n"));
677 return(chroot(dname));
681 /**************************************************************************
682 A wrapper for gethostbyname() that tries avoids looking up hostnames
683 in the root domain, which can cause dial-on-demand links to come up for no
685 ****************************************************************************/
687 struct hostent *sys_gethostbyname(const char *name)
689 #ifdef REDUCE_ROOT_DNS_LOOKUPS
690 char query[256], hostname[256];
693 /* Does this name have any dots in it? If so, make no change */
695 if (strchr_m(name, '.'))
696 return(gethostbyname(name));
698 /* Get my hostname, which should have domain name
699 attached. If not, just do the gethostname on the
703 gethostname(hostname, sizeof(hostname) - 1);
704 hostname[sizeof(hostname) - 1] = 0;
705 if ((domain = strchr_m(hostname, '.')) == NULL)
706 return(gethostbyname(name));
708 /* Attach domain name to query and do modified query.
709 If names too large, just do gethostname on the
713 if((strlen(name) + strlen(domain)) >= sizeof(query))
714 return(gethostbyname(name));
716 slprintf(query, sizeof(query)-1, "%s%s", name, domain);
717 return(gethostbyname(query));
718 #else /* REDUCE_ROOT_DNS_LOOKUPS */
719 return(gethostbyname(name));
720 #endif /* REDUCE_ROOT_DNS_LOOKUPS */
724 #if defined(HAVE_POSIX_CAPABILITIES)
726 #ifdef HAVE_SYS_CAPABILITY_H
728 #if defined(BROKEN_REDHAT_7_SYSTEM_HEADERS) && !defined(_I386_STATFS_H) && !defined(_PPC_STATFS_H)
729 #define _I386_STATFS_H
730 #define _PPC_STATFS_H
731 #define BROKEN_REDHAT_7_STATFS_WORKAROUND
734 #include <sys/capability.h>
736 #ifdef BROKEN_REDHAT_7_STATFS_WORKAROUND
737 #undef _I386_STATFS_H
739 #undef BROKEN_REDHAT_7_STATFS_WORKAROUND
742 #endif /* HAVE_SYS_CAPABILITY_H */
744 /**************************************************************************
745 Try and abstract process capabilities (for systems that have them).
746 ****************************************************************************/
748 /* Set the POSIX capabilities needed for the given purpose into the effective
749 * capability set of the current process. Make sure they are always removed
750 * from the inheritable set, because there is no circumstance in which our
751 * children should inherit our elevated privileges.
753 static BOOL set_process_capability(enum smbd_capability capability,
756 cap_value_t cap_vals[2] = {0};
757 int num_cap_vals = 0;
761 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
762 /* On Linux, make sure that any capabilities we grab are sticky
763 * across UID changes. We expect that this would allow us to keep both
764 * the effective and permitted capability sets, but as of circa 2.6.16,
765 * only the permitted set is kept. It is a bug (which we work around)
766 * that the effective set is lost, but we still require the effective
769 if (!prctl(PR_GET_KEEPCAPS)) {
770 prctl(PR_SET_KEEPCAPS, 1);
774 cap = cap_get_proc();
776 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
781 switch (capability) {
782 case KERNEL_OPLOCK_CAPABILITY:
783 #ifdef CAP_NETWORK_MGT
784 /* IRIX has CAP_NETWORK_MGT for oplocks. */
785 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
788 case DMAPI_ACCESS_CAPABILITY:
789 #ifdef CAP_DEVICE_MGT
790 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
791 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
793 /* Linux has CAP_MKNOD for DMAPI access. */
794 cap_vals[num_cap_vals++] = CAP_MKNOD;
799 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
801 if (num_cap_vals == 0) {
806 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
807 enable ? CAP_SET : CAP_CLEAR);
809 /* We never want to pass capabilities down to our children, so make
810 * sure they are not inherited.
812 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
814 if (cap_set_proc(cap) == -1) {
815 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
825 #endif /* HAVE_POSIX_CAPABILITIES */
827 /****************************************************************************
828 Gain the oplock capability from the kernel if possible.
829 ****************************************************************************/
831 void set_effective_capability(enum smbd_capability capability)
833 #if defined(HAVE_POSIX_CAPABILITIES)
834 set_process_capability(capability, True);
835 #endif /* HAVE_POSIX_CAPABILITIES */
838 void drop_effective_capability(enum smbd_capability capability)
840 #if defined(HAVE_POSIX_CAPABILITIES)
841 set_process_capability(capability, False);
842 #endif /* HAVE_POSIX_CAPABILITIES */
845 /**************************************************************************
846 Wrapper for random().
847 ****************************************************************************/
849 long sys_random(void)
851 #if defined(HAVE_RANDOM)
852 return (long)random();
853 #elif defined(HAVE_RAND)
856 DEBUG(0,("Error - no random function available !\n"));
861 /**************************************************************************
862 Wrapper for srandom().
863 ****************************************************************************/
865 void sys_srandom(unsigned int seed)
867 #if defined(HAVE_SRANDOM)
869 #elif defined(HAVE_SRAND)
872 DEBUG(0,("Error - no srandom function available !\n"));
877 /**************************************************************************
878 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
879 ****************************************************************************/
883 #if defined(SYSCONF_SC_NGROUPS_MAX)
884 int ret = sysconf(_SC_NGROUPS_MAX);
885 return (ret == -1) ? NGROUPS_MAX : ret;
891 /**************************************************************************
892 Wrap setgroups and getgroups for systems that declare getgroups() as
893 returning an array of gid_t, but actuall return an array of int.
894 ****************************************************************************/
896 #if defined(HAVE_BROKEN_GETGROUPS)
897 static int sys_broken_getgroups(int setlen, gid_t *gidset)
904 return getgroups(setlen, &gid);
908 * Broken case. We need to allocate a
909 * GID_T array of size setlen.
918 setlen = groups_max();
920 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
921 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
925 if((ngroups = getgroups(setlen, group_list)) < 0) {
926 int saved_errno = errno;
927 SAFE_FREE(group_list);
932 for(i = 0; i < ngroups; i++)
933 gidset[i] = (gid_t)group_list[i];
935 SAFE_FREE(group_list);
939 static int sys_broken_setgroups(int setlen, gid_t *gidset)
947 if (setlen < 0 || setlen > groups_max()) {
953 * Broken case. We need to allocate a
954 * GID_T array of size setlen.
957 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
958 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
962 for(i = 0; i < setlen; i++)
963 group_list[i] = (GID_T) gidset[i];
965 if(setgroups(setlen, group_list) != 0) {
966 int saved_errno = errno;
967 SAFE_FREE(group_list);
972 SAFE_FREE(group_list);
976 #endif /* HAVE_BROKEN_GETGROUPS */
978 /* This is a list of systems that require the first GID passed to setgroups(2)
979 * to be the effective GID. If your system is one of these, add it here.
981 #if defined (FREEBSD) || defined (DARWINOS)
982 #define USE_BSD_SETGROUPS
985 #if defined(USE_BSD_SETGROUPS)
986 /* Depending on the particular BSD implementation, the first GID that is
987 * passed to setgroups(2) will either be ignored or will set the credential's
988 * effective GID. In either case, the right thing to do is to guarantee that
989 * gidset[0] is the effective GID.
991 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
993 gid_t *new_gidset = NULL;
997 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1000 /* No group list, just make sure we are setting the efective GID. */
1002 return setgroups(1, &primary_gid);
1005 /* If the primary gid is not the first array element, grow the array
1006 * and insert it at the front.
1008 if (gidset[0] != primary_gid) {
1009 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
1010 if (new_gidset == NULL) {
1014 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1015 new_gidset[0] = primary_gid;
1020 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1025 #if defined(HAVE_BROKEN_GETGROUPS)
1026 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1028 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1033 SAFE_FREE(new_gidset);
1040 #endif /* USE_BSD_SETGROUPS */
1042 /**************************************************************************
1043 Wrapper for getgroups. Deals with broken (int) case.
1044 ****************************************************************************/
1046 int sys_getgroups(int setlen, gid_t *gidset)
1048 #if defined(HAVE_BROKEN_GETGROUPS)
1049 return sys_broken_getgroups(setlen, gidset);
1051 return getgroups(setlen, gidset);
1055 /**************************************************************************
1056 Wrapper for setgroups. Deals with broken (int) case and BSD case.
1057 ****************************************************************************/
1059 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1061 #if !defined(HAVE_SETGROUPS)
1064 #endif /* HAVE_SETGROUPS */
1066 #if defined(USE_BSD_SETGROUPS)
1067 return sys_bsd_setgroups(primary_gid, setlen, gidset);
1068 #elif defined(HAVE_BROKEN_GETGROUPS)
1069 return sys_broken_setgroups(setlen, gidset);
1071 return setgroups(setlen, gidset);
1075 /**************************************************************************
1076 Wrappers for setpwent(), getpwent() and endpwent()
1077 ****************************************************************************/
1079 void sys_setpwent(void)
1084 struct passwd *sys_getpwent(void)
1089 void sys_endpwent(void)
1094 /**************************************************************************
1095 Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1096 ****************************************************************************/
1098 #ifdef ENABLE_BUILD_FARM_HACKS
1101 * In the build farm we want to be able to join machines to the domain. As we
1102 * don't have root access, we need to bypass direct access to /etc/passwd
1103 * after a user has been created via samr. Fake those users.
1106 static struct passwd *fake_pwd;
1107 static int num_fake_pwd;
1109 struct passwd *sys_getpwnam(const char *name)
1113 for (i=0; i<num_fake_pwd; i++) {
1114 if (strcmp(fake_pwd[i].pw_name, name) == 0) {
1115 DEBUG(10, ("Returning fake user %s\n", name));
1116 return &fake_pwd[i];
1120 return getpwnam(name);
1123 struct passwd *sys_getpwuid(uid_t uid)
1127 for (i=0; i<num_fake_pwd; i++) {
1128 if (fake_pwd[i].pw_uid == uid) {
1129 DEBUG(10, ("Returning fake user %s\n",
1130 fake_pwd[i].pw_name));
1131 return &fake_pwd[i];
1135 return getpwuid(uid);
1138 void faked_create_user(const char *name)
1142 struct passwd new_pwd;
1144 for (i=0; i<10; i++) {
1145 generate_random_buffer((unsigned char *)&uid,
1147 if (getpwuid(uid) == NULL) {
1153 /* Weird. No free uid found... */
1157 new_pwd.pw_name = SMB_STRDUP(name);
1158 new_pwd.pw_passwd = SMB_STRDUP("x");
1159 new_pwd.pw_uid = uid;
1160 new_pwd.pw_gid = 100;
1161 new_pwd.pw_gecos = SMB_STRDUP("faked user");
1162 new_pwd.pw_dir = SMB_STRDUP("/nodir");
1163 new_pwd.pw_shell = SMB_STRDUP("/bin/false");
1165 ADD_TO_ARRAY(NULL, struct passwd, new_pwd, &fake_pwd,
1168 DEBUG(10, ("Added fake user %s, have %d fake users\n",
1169 name, num_fake_pwd));
1174 struct passwd *sys_getpwnam(const char *name)
1176 return getpwnam(name);
1179 struct passwd *sys_getpwuid(uid_t uid)
1181 return getpwuid(uid);
1186 struct group *sys_getgrnam(const char *name)
1188 return getgrnam(name);
1191 struct group *sys_getgrgid(gid_t gid)
1193 return getgrgid(gid);
1196 #if 0 /* NOT CURRENTLY USED - JRA */
1197 /**************************************************************************
1198 The following are the UNICODE versions of *all* system interface functions
1199 called within Samba. Ok, ok, the exceptions are the gethostbyXX calls,
1200 which currently are left as ascii as they are not used other than in name
1202 ****************************************************************************/
1204 /**************************************************************************
1205 Wide stat. Just narrow and call sys_xxx.
1206 ****************************************************************************/
1208 int wsys_stat(const smb_ucs2_t *wfname,SMB_STRUCT_STAT *sbuf)
1211 return sys_stat(unicode_to_unix(fname,wfname,sizeof(fname)), sbuf);
1214 /**************************************************************************
1215 Wide lstat. Just narrow and call sys_xxx.
1216 ****************************************************************************/
1218 int wsys_lstat(const smb_ucs2_t *wfname,SMB_STRUCT_STAT *sbuf)
1221 return sys_lstat(unicode_to_unix(fname,wfname,sizeof(fname)), sbuf);
1224 /**************************************************************************
1225 Wide creat. Just narrow and call sys_xxx.
1226 ****************************************************************************/
1228 int wsys_creat(const smb_ucs2_t *wfname, mode_t mode)
1231 return sys_creat(unicode_to_unix(fname,wfname,sizeof(fname)), mode);
1234 /**************************************************************************
1235 Wide open. Just narrow and call sys_xxx.
1236 ****************************************************************************/
1238 int wsys_open(const smb_ucs2_t *wfname, int oflag, mode_t mode)
1241 return sys_open(unicode_to_unix(fname,wfname,sizeof(fname)), oflag, mode);
1244 /**************************************************************************
1245 Wide fopen. Just narrow and call sys_xxx.
1246 ****************************************************************************/
1248 FILE *wsys_fopen(const smb_ucs2_t *wfname, const char *type)
1251 return sys_fopen(unicode_to_unix(fname,wfname,sizeof(fname)), type);
1254 /**************************************************************************
1255 Wide opendir. Just narrow and call sys_xxx.
1256 ****************************************************************************/
1258 SMB_STRUCT_DIR *wsys_opendir(const smb_ucs2_t *wfname)
1261 return opendir(unicode_to_unix(fname,wfname,sizeof(fname)));
1264 /**************************************************************************
1265 Wide readdir. Return a structure pointer containing a wide filename.
1266 ****************************************************************************/
1268 SMB_STRUCT_WDIRENT *wsys_readdir(SMB_STRUCT_DIR *dirp)
1270 static SMB_STRUCT_WDIRENT retval;
1271 SMB_STRUCT_DIRENT *dirval = sys_readdir(dirp);
1277 * The only POSIX defined member of this struct is d_name.
1280 unix_to_unicode(retval.d_name,dirval->d_name,sizeof(retval.d_name));
1285 /**************************************************************************
1286 Wide getwd. Call sys_xxx and widen. Assumes s points to a wpstring.
1287 ****************************************************************************/
1289 smb_ucs2_t *wsys_getwd(smb_ucs2_t *s)
1292 char *p = sys_getwd(fname);
1297 return unix_to_unicode(s, p, sizeof(wpstring));
1300 /**************************************************************************
1301 Wide chown. Just narrow and call sys_xxx.
1302 ****************************************************************************/
1304 int wsys_chown(const smb_ucs2_t *wfname, uid_t uid, gid_t gid)
1307 return chown(unicode_to_unix(fname,wfname,sizeof(fname)), uid, gid);
1310 /**************************************************************************
1311 Wide chroot. Just narrow and call sys_xxx.
1312 ****************************************************************************/
1314 int wsys_chroot(const smb_ucs2_t *wfname)
1317 return chroot(unicode_to_unix(fname,wfname,sizeof(fname)));
1320 /**************************************************************************
1321 Wide getpwnam. Return a structure pointer containing wide names.
1322 ****************************************************************************/
1324 SMB_STRUCT_WPASSWD *wsys_getpwnam(const smb_ucs2_t *wname)
1326 static SMB_STRUCT_WPASSWD retval;
1328 struct passwd *pwret = sys_getpwnam(unicode_to_unix(name,wname,sizeof(name)));
1333 unix_to_unicode(retval.pw_name, pwret->pw_name, sizeof(retval.pw_name));
1334 retval.pw_passwd = pwret->pw_passwd;
1335 retval.pw_uid = pwret->pw_uid;
1336 retval.pw_gid = pwret->pw_gid;
1337 unix_to_unicode(retval.pw_gecos, pwret->pw_gecos, sizeof(retval.pw_gecos));
1338 unix_to_unicode(retval.pw_dir, pwret->pw_dir, sizeof(retval.pw_dir));
1339 unix_to_unicode(retval.pw_shell, pwret->pw_shell, sizeof(retval.pw_shell));
1344 /**************************************************************************
1345 Wide getpwuid. Return a structure pointer containing wide names.
1346 ****************************************************************************/
1348 SMB_STRUCT_WPASSWD *wsys_getpwuid(uid_t uid)
1350 static SMB_STRUCT_WPASSWD retval;
1351 struct passwd *pwret = sys_getpwuid(uid);
1356 unix_to_unicode(retval.pw_name, pwret->pw_name, sizeof(retval.pw_name));
1357 retval.pw_passwd = pwret->pw_passwd;
1358 retval.pw_uid = pwret->pw_uid;
1359 retval.pw_gid = pwret->pw_gid;
1360 unix_to_unicode(retval.pw_gecos, pwret->pw_gecos, sizeof(retval.pw_gecos));
1361 unix_to_unicode(retval.pw_dir, pwret->pw_dir, sizeof(retval.pw_dir));
1362 unix_to_unicode(retval.pw_shell, pwret->pw_shell, sizeof(retval.pw_shell));
1366 #endif /* NOT CURRENTLY USED - JRA */
1368 /**************************************************************************
1369 Extract a command into an arg list.
1370 ****************************************************************************/
1372 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1381 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1382 DEBUG(0, ("talloc failed\n"));
1386 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1387 TALLOC_FREE(trunc_cmd);
1396 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1399 TALLOC_FREE(trunc_cmd);
1401 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1406 * Now do the extraction.
1409 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1413 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1416 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1420 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1422 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1431 DEBUG(0, ("talloc failed\n"));
1432 TALLOC_FREE(trunc_cmd);
1438 /**************************************************************************
1439 Wrapper for fork. Ensures that mypid is reset. Used so we can write
1440 a sys_getpid() that only does a system call *once*.
1441 ****************************************************************************/
1443 static pid_t mypid = (pid_t)-1;
1445 pid_t sys_fork(void)
1447 pid_t forkret = fork();
1449 if (forkret == (pid_t)0) /* Child - reset mypid so sys_getpid does a system call. */
1455 /**************************************************************************
1456 Wrapper for getpid. Ensures we only do a system call *once*.
1457 ****************************************************************************/
1459 pid_t sys_getpid(void)
1461 if (mypid == (pid_t)-1)
1467 /**************************************************************************
1468 Wrapper for popen. Safer as it doesn't search a path.
1469 Modified from the glibc sources.
1470 modified by tridge to return a file descriptor. We must kick our FILE* habit
1471 ****************************************************************************/
1473 typedef struct _popen_list
1477 struct _popen_list *next;
1480 static popen_list *popen_chain;
1482 int sys_popen(const char *command)
1484 int parent_end, child_end;
1486 popen_list *entry = NULL;
1489 if (pipe(pipe_fds) < 0)
1492 parent_end = pipe_fds[0];
1493 child_end = pipe_fds[1];
1500 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1503 ZERO_STRUCTP(entry);
1506 * Extract the command and args into a NULL terminated array.
1509 if(!(argl = extract_args(NULL, command)))
1512 entry->child_pid = sys_fork();
1514 if (entry->child_pid == -1) {
1518 if (entry->child_pid == 0) {
1524 int child_std_end = STDOUT_FILENO;
1528 if (child_end != child_std_end) {
1529 dup2 (child_end, child_std_end);
1534 * POSIX.2: "popen() shall ensure that any streams from previous
1535 * popen() calls that remain open in the parent process are closed
1536 * in the new child process."
1539 for (p = popen_chain; p; p = p->next)
1542 execv(argl[0], argl);
1553 /* Link into popen_chain. */
1554 entry->next = popen_chain;
1555 popen_chain = entry;
1556 entry->fd = parent_end;
1569 /**************************************************************************
1570 Wrapper for pclose. Modified from the glibc sources.
1571 ****************************************************************************/
1573 int sys_pclose(int fd)
1576 popen_list **ptr = &popen_chain;
1577 popen_list *entry = NULL;
1581 /* Unlink from popen_chain. */
1582 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1583 if ((*ptr)->fd == fd) {
1585 *ptr = (*ptr)->next;
1591 if (status < 0 || close(entry->fd) < 0)
1595 * As Samba is catching and eating child process
1596 * exits we don't really care about the child exit
1597 * code, a -1 with errno = ECHILD will do fine for us.
1601 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1602 } while (wait_pid == -1 && errno == EINTR);
1611 /**************************************************************************
1612 Wrappers for dlopen, dlsym, dlclose.
1613 ****************************************************************************/
1615 void *sys_dlopen(const char *name, int flags)
1617 #if defined(HAVE_DLOPEN)
1618 return dlopen(name, flags);
1624 void *sys_dlsym(void *handle, const char *symbol)
1626 #if defined(HAVE_DLSYM)
1627 return dlsym(handle, symbol);
1633 int sys_dlclose (void *handle)
1635 #if defined(HAVE_DLCLOSE)
1636 return dlclose(handle);
1642 const char *sys_dlerror(void)
1644 #if defined(HAVE_DLERROR)
1651 int sys_dup2(int oldfd, int newfd)
1653 #if defined(HAVE_DUP2)
1654 return dup2(oldfd, newfd);
1661 /**************************************************************************
1662 Wrapper for Admin Logs.
1663 ****************************************************************************/
1665 void sys_adminlog(int priority, const char *format_str, ...)
1669 char *msgbuf = NULL;
1671 va_start( ap, format_str );
1672 ret = vasprintf( &msgbuf, format_str, ap );
1678 #if defined(HAVE_SYSLOG)
1679 syslog( priority, "%s", msgbuf );
1681 DEBUG(0,("%s", msgbuf ));
1686 /**************************************************************************
1687 Wrappers for extented attribute calls. Based on the Linux package with
1688 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1689 ****************************************************************************/
1691 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1693 #if defined(HAVE_GETXATTR)
1694 #ifndef XATTR_ADD_OPT
1695 return getxattr(path, name, value, size);
1698 return getxattr(path, name, value, size, 0, options);
1700 #elif defined(HAVE_GETEA)
1701 return getea(path, name, value, size);
1702 #elif defined(HAVE_EXTATTR_GET_FILE)
1705 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1706 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1707 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1709 * The BSD implementation has a nasty habit of silently truncating
1710 * the returned value to the size of the buffer, so we have to check
1711 * that the buffer is large enough to fit the returned value.
1713 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1718 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1722 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1724 #elif defined(HAVE_ATTR_GET)
1725 int retval, flags = 0;
1726 int valuelength = (int)size;
1727 char *attrname = strchr(name,'.') + 1;
1729 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1731 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1733 return retval ? retval : valuelength;
1740 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1742 #if defined(HAVE_LGETXATTR)
1743 return lgetxattr(path, name, value, size);
1744 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1745 int options = XATTR_NOFOLLOW;
1746 return getxattr(path, name, value, size, 0, options);
1747 #elif defined(HAVE_LGETEA)
1748 return lgetea(path, name, value, size);
1749 #elif defined(HAVE_EXTATTR_GET_LINK)
1752 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1753 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1754 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1756 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1761 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1765 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1767 #elif defined(HAVE_ATTR_GET)
1768 int retval, flags = ATTR_DONTFOLLOW;
1769 int valuelength = (int)size;
1770 char *attrname = strchr(name,'.') + 1;
1772 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1774 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1776 return retval ? retval : valuelength;
1783 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1785 #if defined(HAVE_FGETXATTR)
1786 #ifndef XATTR_ADD_OPT
1787 return fgetxattr(filedes, name, value, size);
1790 return fgetxattr(filedes, name, value, size, 0, options);
1792 #elif defined(HAVE_FGETEA)
1793 return fgetea(filedes, name, value, size);
1794 #elif defined(HAVE_EXTATTR_GET_FD)
1797 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1798 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1799 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1801 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1806 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1810 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1812 #elif defined(HAVE_ATTR_GETF)
1813 int retval, flags = 0;
1814 int valuelength = (int)size;
1815 char *attrname = strchr(name,'.') + 1;
1817 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1819 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1821 return retval ? retval : valuelength;
1828 #if defined(HAVE_EXTATTR_LIST_FILE)
1830 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1838 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1839 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1847 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1849 ssize_t list_size, total_size = 0;
1852 /* Iterate through extattr(2) namespaces */
1853 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1855 #if defined(HAVE_EXTATTR_LIST_FILE)
1857 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1860 #if defined(HAVE_EXTATTR_LIST_LINK)
1862 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1865 #if defined(HAVE_EXTATTR_LIST_FD)
1867 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1874 /* Some error happend. Errno should be set by the previous call */
1880 /* XXX: Call with an empty buffer may be used to calculate
1881 necessary buffer size. Unfortunately, we can't say, how
1882 many attributes were returned, so here is the potential
1883 problem with the emulation.
1886 /* Take the worse case of one char attribute names -
1887 two bytes per name plus one more for sanity.
1889 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1892 /* Count necessary offset to fit namespace prefixes */
1894 for(i = 0; i < list_size; i += list[i] + 1)
1895 len += extattr[t].len;
1897 total_size += list_size + len;
1898 /* Buffer is too small to fit the results */
1899 if(total_size > size) {
1903 /* Shift results back, so we can prepend prefixes */
1904 buf = memmove(list + len, list, list_size);
1906 for(i = 0; i < list_size; i += len + 1) {
1908 strncpy(list, extattr[t].name, extattr[t].len + 1);
1909 list += extattr[t].len;
1910 strncpy(list, buf + i + 1, len);
1921 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1922 static char attr_buffer[ATTR_MAX_VALUELEN];
1924 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1926 int retval = 0, index;
1927 attrlist_cursor_t *cursor = 0;
1929 attrlist_t * al = (attrlist_t *)attr_buffer;
1931 size_t ent_size, left = size;
1936 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1938 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1940 for (index = 0; index < al->al_count; index++) {
1941 ae = ATTR_ENTRY(attr_buffer, index);
1942 ent_size = strlen(ae->a_name) + sizeof("user.");
1943 if (left >= ent_size) {
1944 strncpy(bp, "user.", sizeof("user."));
1945 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1953 total_size += ent_size;
1955 if (al->al_more == 0) break;
1962 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1964 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1966 for (index = 0; index < al->al_count; index++) {
1967 ae = ATTR_ENTRY(attr_buffer, index);
1968 ent_size = strlen(ae->a_name) + sizeof("system.");
1969 if (left >= ent_size) {
1970 strncpy(bp, "system.", sizeof("system."));
1971 strncat(bp, ae->a_name, ent_size - sizeof("system."));
1979 total_size += ent_size;
1981 if (al->al_more == 0) break;
1984 return (ssize_t)(retval ? retval : total_size);
1989 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1991 #if defined(HAVE_LISTXATTR)
1992 #ifndef XATTR_ADD_OPT
1993 return listxattr(path, list, size);
1996 return listxattr(path, list, size, options);
1998 #elif defined(HAVE_LISTEA)
1999 return listea(path, list, size);
2000 #elif defined(HAVE_EXTATTR_LIST_FILE)
2003 return bsd_attr_list(0, arg, list, size);
2004 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
2005 return irix_attr_list(path, 0, list, size, 0);
2012 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
2014 #if defined(HAVE_LLISTXATTR)
2015 return llistxattr(path, list, size);
2016 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
2017 int options = XATTR_NOFOLLOW;
2018 return listxattr(path, list, size, options);
2019 #elif defined(HAVE_LLISTEA)
2020 return llistea(path, list, size);
2021 #elif defined(HAVE_EXTATTR_LIST_LINK)
2024 return bsd_attr_list(1, arg, list, size);
2025 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
2026 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
2033 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
2035 #if defined(HAVE_FLISTXATTR)
2036 #ifndef XATTR_ADD_OPT
2037 return flistxattr(filedes, list, size);
2040 return flistxattr(filedes, list, size, options);
2042 #elif defined(HAVE_FLISTEA)
2043 return flistea(filedes, list, size);
2044 #elif defined(HAVE_EXTATTR_LIST_FD)
2046 arg.filedes = filedes;
2047 return bsd_attr_list(2, arg, list, size);
2048 #elif defined(HAVE_ATTR_LISTF)
2049 return irix_attr_list(NULL, filedes, list, size, 0);
2056 int sys_removexattr (const char *path, const char *name)
2058 #if defined(HAVE_REMOVEXATTR)
2059 #ifndef XATTR_ADD_OPT
2060 return removexattr(path, name);
2063 return removexattr(path, name, options);
2065 #elif defined(HAVE_REMOVEEA)
2066 return removeea(path, name);
2067 #elif defined(HAVE_EXTATTR_DELETE_FILE)
2069 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2070 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2071 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2073 return extattr_delete_file(path, attrnamespace, attrname);
2074 #elif defined(HAVE_ATTR_REMOVE)
2076 char *attrname = strchr(name,'.') + 1;
2078 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2080 return attr_remove(path, attrname, flags);
2087 int sys_lremovexattr (const char *path, const char *name)
2089 #if defined(HAVE_LREMOVEXATTR)
2090 return lremovexattr(path, name);
2091 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
2092 int options = XATTR_NOFOLLOW;
2093 return removexattr(path, name, options);
2094 #elif defined(HAVE_LREMOVEEA)
2095 return lremoveea(path, name);
2096 #elif defined(HAVE_EXTATTR_DELETE_LINK)
2098 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2099 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2100 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2102 return extattr_delete_link(path, attrnamespace, attrname);
2103 #elif defined(HAVE_ATTR_REMOVE)
2104 int flags = ATTR_DONTFOLLOW;
2105 char *attrname = strchr(name,'.') + 1;
2107 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2109 return attr_remove(path, attrname, flags);
2116 int sys_fremovexattr (int filedes, const char *name)
2118 #if defined(HAVE_FREMOVEXATTR)
2119 #ifndef XATTR_ADD_OPT
2120 return fremovexattr(filedes, name);
2123 return fremovexattr(filedes, name, options);
2125 #elif defined(HAVE_FREMOVEEA)
2126 return fremoveea(filedes, name);
2127 #elif defined(HAVE_EXTATTR_DELETE_FD)
2129 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2130 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2131 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2133 return extattr_delete_fd(filedes, attrnamespace, attrname);
2134 #elif defined(HAVE_ATTR_REMOVEF)
2136 char *attrname = strchr(name,'.') + 1;
2138 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2140 return attr_removef(filedes, attrname, flags);
2147 #if !defined(HAVE_SETXATTR)
2148 #define XATTR_CREATE 0x1 /* set value, fail if attr already exists */
2149 #define XATTR_REPLACE 0x2 /* set value, fail if attr does not exist */
2152 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2154 #if defined(HAVE_SETXATTR)
2155 #ifndef XATTR_ADD_OPT
2156 return setxattr(path, name, value, size, flags);
2159 return setxattr(path, name, value, size, 0, options);
2161 #elif defined(HAVE_SETEA)
2162 return setea(path, name, value, size, flags);
2163 #elif defined(HAVE_EXTATTR_SET_FILE)
2166 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2167 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2168 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2170 /* Check attribute existence */
2171 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
2173 /* REPLACE attribute, that doesn't exist */
2174 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2178 /* Ignore other errors */
2181 /* CREATE attribute, that already exists */
2182 if (flags & XATTR_CREATE) {
2188 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2189 return (retval < 0) ? -1 : 0;
2190 #elif defined(HAVE_ATTR_SET)
2192 char *attrname = strchr(name,'.') + 1;
2194 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2195 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2196 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2198 return attr_set(path, attrname, (const char *)value, size, myflags);
2205 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2207 #if defined(HAVE_LSETXATTR)
2208 return lsetxattr(path, name, value, size, flags);
2209 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2210 int options = XATTR_NOFOLLOW;
2211 return setxattr(path, name, value, size, 0, options);
2212 #elif defined(LSETEA)
2213 return lsetea(path, name, value, size, flags);
2214 #elif defined(HAVE_EXTATTR_SET_LINK)
2217 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2218 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2219 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2221 /* Check attribute existence */
2222 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2224 /* REPLACE attribute, that doesn't exist */
2225 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2229 /* Ignore other errors */
2232 /* CREATE attribute, that already exists */
2233 if (flags & XATTR_CREATE) {
2240 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2241 return (retval < 0) ? -1 : 0;
2242 #elif defined(HAVE_ATTR_SET)
2243 int myflags = ATTR_DONTFOLLOW;
2244 char *attrname = strchr(name,'.') + 1;
2246 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2247 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2248 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2250 return attr_set(path, attrname, (const char *)value, size, myflags);
2257 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2259 #if defined(HAVE_FSETXATTR)
2260 #ifndef XATTR_ADD_OPT
2261 return fsetxattr(filedes, name, value, size, flags);
2264 return fsetxattr(filedes, name, value, size, 0, options);
2266 #elif defined(HAVE_FSETEA)
2267 return fsetea(filedes, name, value, size, flags);
2268 #elif defined(HAVE_EXTATTR_SET_FD)
2271 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2272 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2273 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2275 /* Check attribute existence */
2276 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2278 /* REPLACE attribute, that doesn't exist */
2279 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2283 /* Ignore other errors */
2286 /* CREATE attribute, that already exists */
2287 if (flags & XATTR_CREATE) {
2293 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2294 return (retval < 0) ? -1 : 0;
2295 #elif defined(HAVE_ATTR_SETF)
2297 char *attrname = strchr(name,'.') + 1;
2299 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2300 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2301 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2303 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2310 /****************************************************************************
2311 Return the major devicenumber for UNIX extensions.
2312 ****************************************************************************/
2314 uint32 unix_dev_major(SMB_DEV_T dev)
2316 #if defined(HAVE_DEVICE_MAJOR_FN)
2317 return (uint32)major(dev);
2319 return (uint32)(dev >> 8);
2323 /****************************************************************************
2324 Return the minor devicenumber for UNIX extensions.
2325 ****************************************************************************/
2327 uint32 unix_dev_minor(SMB_DEV_T dev)
2329 #if defined(HAVE_DEVICE_MINOR_FN)
2330 return (uint32)minor(dev);
2332 return (uint32)(dev & 0xff);
2336 #if defined(WITH_AIO)
2338 /*******************************************************************
2339 An aio_read wrapper that will deal with 64-bit sizes.
2340 ********************************************************************/
2342 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2344 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2345 return aio_read64(aiocb);
2346 #elif defined(HAVE_AIO_READ)
2347 return aio_read(aiocb);
2354 /*******************************************************************
2355 An aio_write wrapper that will deal with 64-bit sizes.
2356 ********************************************************************/
2358 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2360 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2361 return aio_write64(aiocb);
2362 #elif defined(HAVE_AIO_WRITE)
2363 return aio_write(aiocb);
2370 /*******************************************************************
2371 An aio_return wrapper that will deal with 64-bit sizes.
2372 ********************************************************************/
2374 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2376 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2377 return aio_return64(aiocb);
2378 #elif defined(HAVE_AIO_RETURN)
2379 return aio_return(aiocb);
2386 /*******************************************************************
2387 An aio_cancel wrapper that will deal with 64-bit sizes.
2388 ********************************************************************/
2390 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2392 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2393 return aio_cancel64(fd, aiocb);
2394 #elif defined(HAVE_AIO_CANCEL)
2395 return aio_cancel(fd, aiocb);
2402 /*******************************************************************
2403 An aio_error wrapper that will deal with 64-bit sizes.
2404 ********************************************************************/
2406 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2408 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2409 return aio_error64(aiocb);
2410 #elif defined(HAVE_AIO_ERROR)
2411 return aio_error(aiocb);
2418 /*******************************************************************
2419 An aio_fsync wrapper that will deal with 64-bit sizes.
2420 ********************************************************************/
2422 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2424 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2425 return aio_fsync64(op, aiocb);
2426 #elif defined(HAVE_AIO_FSYNC)
2427 return aio_fsync(op, aiocb);
2434 /*******************************************************************
2435 An aio_fsync wrapper that will deal with 64-bit sizes.
2436 ********************************************************************/
2438 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2440 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2441 return aio_suspend64(cblist, n, timeout);
2442 #elif defined(HAVE_AIO_FSYNC)
2443 return aio_suspend(cblist, n, timeout);
2449 #else /* !WITH_AIO */
2451 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2457 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2463 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2469 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2475 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2481 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2487 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2492 #endif /* WITH_AIO */
2494 int sys_getpeereid( int s, uid_t *uid)
2496 #if defined(HAVE_PEERCRED)
2498 socklen_t cred_len = sizeof(struct ucred);
2501 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2506 if (cred_len != sizeof(struct ucred)) {