r25055: Add file_id_string_tos
[Samba.git] / source / lib / system.c
blob9cef818fab213a6411c3e46d342e55405b5855d2
1 /*
2 Unix SMB/CIFS implementation.
3 Samba system utilities
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/>.
23 #include "includes.h"
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
27 #endif
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)
53 void *p = NULL;
54 int ret = posix_memalign( &p, align, size );
55 if ( ret == 0 )
56 return p;
58 return NULL;
59 #elif defined(HAVE_MEMALIGN)
60 return memalign( align, size );
61 #else
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();
68 #else
69 size_t pagesize = (size_t)-1;
70 #endif
71 if (pagesize == (size_t)-1) {
72 DEBUG(0,("memalign functionalaity not available on this platform!\n"));
73 return NULL;
75 if (size < pagesize) {
76 size = pagesize;
78 return SMB_MALLOC(size);
79 #endif
82 /*******************************************************************
83 A wrapper for usleep in case we don't have one.
84 ********************************************************************/
86 int sys_usleep(long usecs)
88 #ifndef HAVE_USLEEP
89 struct timeval tval;
90 #endif
93 * We need this braindamage as the glibc usleep
94 * is not SPEC1170 complient... grumble... JRA.
97 if(usecs < 0 || usecs > 1000000) {
98 errno = EINVAL;
99 return -1;
102 #if HAVE_USLEEP
103 usleep(usecs);
104 return 0;
105 #else /* HAVE_USLEEP */
107 * Fake it with select...
109 tval.tv_sec = 0;
110 tval.tv_usec = usecs/1000;
111 select(0,NULL,NULL,NULL,&tval);
112 return 0;
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)
122 ssize_t ret;
124 do {
125 ret = read(fd, buf, count);
126 } while (ret == -1 && errno == EINTR);
127 return ret;
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
134 ssize_t sys_write(int fd, const void *buf, size_t count)
136 ssize_t ret;
138 do {
139 ret = write(fd, buf, count);
140 } while (ret == -1 && errno == EINTR);
141 return ret;
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)
151 ssize_t ret;
153 do {
154 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
155 ret = pread64(fd, buf, count, off);
156 #else
157 ret = pread(fd, buf, count, off);
158 #endif
159 } while (ret == -1 && errno == EINTR);
160 return ret;
162 #endif
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)
171 ssize_t ret;
173 do {
174 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
175 ret = pwrite64(fd, buf, count, off);
176 #else
177 ret = pwrite(fd, buf, count, off);
178 #endif
179 } while (ret == -1 && errno == EINTR);
180 return ret;
182 #endif
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)
190 ssize_t ret;
192 do {
193 ret = send(s, msg, len, flags);
194 } while (ret == -1 && errno == EINTR);
195 return ret;
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)
204 ssize_t ret;
206 do {
207 ret = sendto(s, msg, len, flags, to, tolen);
208 } while (ret == -1 && errno == EINTR);
209 return ret;
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)
218 ssize_t ret;
220 do {
221 ret = recv(fd, buf, count, flags);
222 } while (ret == -1 && errno == EINTR);
223 return ret;
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)
232 ssize_t ret;
234 do {
235 ret = recvfrom(s, buf, len, flags, from, fromlen);
236 } while (ret == -1 && errno == EINTR);
237 return ret;
240 /*******************************************************************
241 A fcntl wrapper that will deal with EINTR.
242 ********************************************************************/
244 int sys_fcntl_ptr(int fd, int cmd, void *arg)
246 int ret;
248 do {
249 ret = fcntl(fd, cmd, arg);
250 } while (ret == -1 && errno == EINTR);
251 return ret;
254 /*******************************************************************
255 A fcntl wrapper that will deal with EINTR.
256 ********************************************************************/
258 int sys_fcntl_long(int fd, int cmd, long arg)
260 int ret;
262 do {
263 ret = fcntl(fd, cmd, arg);
264 } while (ret == -1 && errno == EINTR);
265 return ret;
268 /*******************************************************************
269 A stat() wrapper that will deal with 64 bit filesizes.
270 ********************************************************************/
272 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
274 int ret;
275 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
276 ret = stat64(fname, sbuf);
277 #else
278 ret = stat(fname, sbuf);
279 #endif
280 /* we always want directories to appear zero size */
281 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
282 return ret;
285 /*******************************************************************
286 An fstat() wrapper that will deal with 64 bit filesizes.
287 ********************************************************************/
289 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
291 int ret;
292 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
293 ret = fstat64(fd, sbuf);
294 #else
295 ret = fstat(fd, sbuf);
296 #endif
297 /* we always want directories to appear zero size */
298 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
299 return ret;
302 /*******************************************************************
303 An lstat() wrapper that will deal with 64 bit filesizes.
304 ********************************************************************/
306 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
308 int ret;
309 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
310 ret = lstat64(fname, sbuf);
311 #else
312 ret = lstat(fname, sbuf);
313 #endif
314 /* we always want directories to appear zero size */
315 if (ret == 0 && S_ISDIR(sbuf->st_mode)) sbuf->st_size = 0;
316 return ret;
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);
327 #else
328 return ftruncate(fd, offset);
329 #endif
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);
340 #else
341 return lseek(fd, offset, whence);
342 #endif
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);
355 #else
356 return fseek(fp, offset, whence);
357 #endif
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);
370 #else
371 return (SMB_OFF_T)ftell(fp);
372 #endif
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);
383 #else
385 * If creat64 isn't defined then ensure we call a potential open64.
386 * JRA.
388 return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
389 #endif
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);
400 #else
401 return open(path, oflag, mode);
402 #endif
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);
413 #else
414 return fopen(path, type);
415 #endif
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
426 int kernel_mode = 0;
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;
434 if (kernel_mode) {
435 flock(fd, kernel_mode);
437 #endif
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);
451 #else
452 return opendir(name);
453 #endif
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);
464 #else
465 return readdir(dirp);
466 #endif
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);
477 #else
478 seekdir(dirp, offset);
479 #endif
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);
490 #else
491 return (long)telldir(dirp);
492 #endif
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)
502 rewinddir64(dirp);
503 #else
504 rewinddir(dirp);
505 #endif
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);
516 #else
517 return closedir(dirp);
518 #endif
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);
530 #else
531 return mknod(path, mode, dev);
532 #endif
533 #else
534 /* No mknod system call. */
535 errno = ENOSYS;
536 return -1;
537 #endif
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);
548 #else
549 /* As realpath is not a system call we can't return ENOSYS. */
550 errno = EINVAL;
551 return NULL;
552 #endif
555 /*******************************************************************
556 The wait() calls vary between systems
557 ********************************************************************/
559 int sys_waitpid(pid_t pid,int *status,int options)
561 #ifdef HAVE_WAITPID
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)
574 char *wd;
575 #ifdef HAVE_GETCWD
576 wd = (char *)getcwd(s, sizeof (pstring));
577 #else
578 wd = (char *)getwd(s);
579 #endif
580 return wd;
583 /*******************************************************************
584 system wrapper for symlink
585 ********************************************************************/
587 int sys_symlink(const char *oldpath, const char *newpath)
589 #ifndef HAVE_SYMLINK
590 errno = ENOSYS;
591 return -1;
592 #else
593 return symlink(oldpath, newpath);
594 #endif
597 /*******************************************************************
598 system wrapper for readlink
599 ********************************************************************/
601 int sys_readlink(const char *path, char *buf, size_t bufsiz)
603 #ifndef HAVE_READLINK
604 errno = ENOSYS;
605 return -1;
606 #else
607 return readlink(path, buf, bufsiz);
608 #endif
611 /*******************************************************************
612 system wrapper for link
613 ********************************************************************/
615 int sys_link(const char *oldpath, const char *newpath)
617 #ifndef HAVE_LINK
618 errno = ENOSYS;
619 return -1;
620 #else
621 return link(oldpath, newpath);
622 #endif
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)
631 #ifndef HAVE_CHOWN
632 static int done;
633 if (!done) {
634 DEBUG(1,("WARNING: no chown!\n"));
635 done=1;
637 errno = ENOSYS;
638 return -1;
639 #else
640 return(chown(fname,uid,gid));
641 #endif
644 /*******************************************************************
645 Wrapper for lchown.
646 ********************************************************************/
648 int sys_lchown(const char *fname,uid_t uid,gid_t gid)
650 #ifndef HAVE_LCHOWN
651 static int done;
652 if (!done) {
653 DEBUG(1,("WARNING: no lchown!\n"));
654 done=1;
656 errno = ENOSYS;
657 return -1;
658 #else
659 return(lchown(fname,uid,gid));
660 #endif
663 /*******************************************************************
664 os/2 also doesn't have chroot
665 ********************************************************************/
666 int sys_chroot(const char *dname)
668 #ifndef HAVE_CHROOT
669 static int done;
670 if (!done) {
671 DEBUG(1,("WARNING: no chroot!\n"));
672 done=1;
674 errno = ENOSYS;
675 return -1;
676 #else
677 return(chroot(dname));
678 #endif
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
684 apparent reason.
685 ****************************************************************************/
687 struct hostent *sys_gethostbyname(const char *name)
689 #ifdef REDUCE_ROOT_DNS_LOOKUPS
690 char query[256], hostname[256];
691 char *domain;
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
700 original string.
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
710 original string.
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
732 #endif
734 #include <sys/capability.h>
736 #ifdef BROKEN_REDHAT_7_STATFS_WORKAROUND
737 #undef _I386_STATFS_H
738 #undef _PPC_STATFS_H
739 #undef BROKEN_REDHAT_7_STATFS_WORKAROUND
740 #endif
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,
754 BOOL enable)
756 cap_value_t cap_vals[2] = {0};
757 int num_cap_vals = 0;
759 cap_t cap;
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
767 * set to be kept.
769 if (!prctl(PR_GET_KEEPCAPS)) {
770 prctl(PR_SET_KEEPCAPS, 1);
772 #endif
774 cap = cap_get_proc();
775 if (cap == NULL) {
776 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
777 strerror(errno)));
778 return False;
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;
786 #endif
787 break;
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;
792 #elif CAP_MKNOD
793 /* Linux has CAP_MKNOD for DMAPI access. */
794 cap_vals[num_cap_vals++] = CAP_MKNOD;
795 #endif
796 break;
799 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
801 if (num_cap_vals == 0) {
802 cap_free(cap);
803 return True;
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",
816 strerror(errno)));
817 cap_free(cap);
818 return False;
821 cap_free(cap);
822 return True;
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)
854 return (long)rand();
855 #else
856 DEBUG(0,("Error - no random function available !\n"));
857 exit(1);
858 #endif
861 /**************************************************************************
862 Wrapper for srandom().
863 ****************************************************************************/
865 void sys_srandom(unsigned int seed)
867 #if defined(HAVE_SRANDOM)
868 srandom(seed);
869 #elif defined(HAVE_SRAND)
870 srand(seed);
871 #else
872 DEBUG(0,("Error - no srandom function available !\n"));
873 exit(1);
874 #endif
877 /**************************************************************************
878 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
879 ****************************************************************************/
881 int groups_max(void)
883 #if defined(SYSCONF_SC_NGROUPS_MAX)
884 int ret = sysconf(_SC_NGROUPS_MAX);
885 return (ret == -1) ? NGROUPS_MAX : ret;
886 #else
887 return NGROUPS_MAX;
888 #endif
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)
899 GID_T gid;
900 GID_T *group_list;
901 int i, ngroups;
903 if(setlen == 0) {
904 return getgroups(setlen, &gid);
908 * Broken case. We need to allocate a
909 * GID_T array of size setlen.
912 if(setlen < 0) {
913 errno = EINVAL;
914 return -1;
917 if (setlen == 0)
918 setlen = groups_max();
920 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
921 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
922 return -1;
925 if((ngroups = getgroups(setlen, group_list)) < 0) {
926 int saved_errno = errno;
927 SAFE_FREE(group_list);
928 errno = saved_errno;
929 return -1;
932 for(i = 0; i < ngroups; i++)
933 gidset[i] = (gid_t)group_list[i];
935 SAFE_FREE(group_list);
936 return ngroups;
939 static int sys_broken_setgroups(int setlen, gid_t *gidset)
941 GID_T *group_list;
942 int i ;
944 if (setlen == 0)
945 return 0 ;
947 if (setlen < 0 || setlen > groups_max()) {
948 errno = EINVAL;
949 return -1;
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"));
959 return -1;
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);
968 errno = saved_errno;
969 return -1;
972 SAFE_FREE(group_list);
973 return 0 ;
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
983 #endif
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;
994 int max;
995 int ret;
997 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
998 max = groups_max();
1000 /* No group list, just make sure we are setting the efective GID. */
1001 if (setlen == 0) {
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) {
1011 return -1;
1014 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1015 new_gidset[0] = primary_gid;
1016 setlen++;
1019 if (setlen > max) {
1020 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1021 setlen, max));
1022 setlen = max;
1025 #if defined(HAVE_BROKEN_GETGROUPS)
1026 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1027 #else
1028 ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1029 #endif
1031 if (new_gidset) {
1032 int errsav = errno;
1033 SAFE_FREE(new_gidset);
1034 errno = errsav;
1037 return ret;
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);
1050 #else
1051 return getgroups(setlen, gidset);
1052 #endif
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)
1062 errno = ENOSYS;
1063 return -1;
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);
1070 #else
1071 return setgroups(setlen, gidset);
1072 #endif
1075 /**************************************************************************
1076 Wrappers for setpwent(), getpwent() and endpwent()
1077 ****************************************************************************/
1079 void sys_setpwent(void)
1081 setpwent();
1084 struct passwd *sys_getpwent(void)
1086 return getpwent();
1089 void sys_endpwent(void)
1091 endpwent();
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)
1111 int i;
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)
1125 int i;
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)
1140 int i;
1141 uid_t uid;
1142 struct passwd new_pwd;
1144 for (i=0; i<10; i++) {
1145 generate_random_buffer((unsigned char *)&uid,
1146 sizeof(uid));
1147 if (getpwuid(uid) == NULL) {
1148 break;
1152 if (i==10) {
1153 /* Weird. No free uid found... */
1154 return;
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,
1166 &num_fake_pwd);
1168 DEBUG(10, ("Added fake user %s, have %d fake users\n",
1169 name, num_fake_pwd));
1172 #else
1174 struct passwd *sys_getpwnam(const char *name)
1176 return getpwnam(name);
1179 struct passwd *sys_getpwuid(uid_t uid)
1181 return getpwuid(uid);
1184 #endif
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
1201 resolution.
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)
1210 pstring fname;
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)
1220 pstring fname;
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)
1230 pstring fname;
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)
1240 pstring fname;
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)
1250 pstring fname;
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)
1260 pstring fname;
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);
1273 if(!dirval)
1274 return NULL;
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));
1282 return &retval;
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)
1291 pstring fname;
1292 char *p = sys_getwd(fname);
1294 if(!p)
1295 return NULL;
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)
1306 pstring fname;
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)
1316 pstring fname;
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;
1327 fstring name;
1328 struct passwd *pwret = sys_getpwnam(unicode_to_unix(name,wname,sizeof(name)));
1330 if(!pwret)
1331 return NULL;
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));
1341 return &retval;
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);
1353 if(!pwret)
1354 return NULL;
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));
1364 return &retval;
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)
1374 char *trunc_cmd;
1375 char *saveptr;
1376 char *ptr;
1377 int argcl;
1378 char **argl = NULL;
1379 int i;
1381 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1382 DEBUG(0, ("talloc failed\n"));
1383 goto nomem;
1386 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1387 TALLOC_FREE(trunc_cmd);
1388 errno = EINVAL;
1389 return NULL;
1393 * Count the args.
1396 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1397 argcl++;
1399 TALLOC_FREE(trunc_cmd);
1401 if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1402 goto nomem;
1406 * Now do the extraction.
1409 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1410 goto nomem;
1413 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1414 i = 0;
1416 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1417 goto nomem;
1420 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1422 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1423 goto nomem;
1427 argl[i++] = NULL;
1428 return argl;
1430 nomem:
1431 DEBUG(0, ("talloc failed\n"));
1432 TALLOC_FREE(trunc_cmd);
1433 TALLOC_FREE(argl);
1434 errno = ENOMEM;
1435 return NULL;
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. */
1450 mypid = (pid_t) -1;
1452 return forkret;
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)
1462 mypid = getpid();
1464 return mypid;
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
1475 int fd;
1476 pid_t child_pid;
1477 struct _popen_list *next;
1478 } popen_list;
1480 static popen_list *popen_chain;
1482 int sys_popen(const char *command)
1484 int parent_end, child_end;
1485 int pipe_fds[2];
1486 popen_list *entry = NULL;
1487 char **argl = NULL;
1489 if (pipe(pipe_fds) < 0)
1490 return -1;
1492 parent_end = pipe_fds[0];
1493 child_end = pipe_fds[1];
1495 if (!*command) {
1496 errno = EINVAL;
1497 goto err_exit;
1500 if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1501 goto err_exit;
1503 ZERO_STRUCTP(entry);
1506 * Extract the command and args into a NULL terminated array.
1509 if(!(argl = extract_args(NULL, command)))
1510 goto err_exit;
1512 entry->child_pid = sys_fork();
1514 if (entry->child_pid == -1) {
1515 goto err_exit;
1518 if (entry->child_pid == 0) {
1521 * Child !
1524 int child_std_end = STDOUT_FILENO;
1525 popen_list *p;
1527 close(parent_end);
1528 if (child_end != child_std_end) {
1529 dup2 (child_end, child_std_end);
1530 close (child_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)
1540 close(p->fd);
1542 execv(argl[0], argl);
1543 _exit (127);
1547 * Parent.
1550 close (child_end);
1551 TALLOC_FREE(argl);
1553 /* Link into popen_chain. */
1554 entry->next = popen_chain;
1555 popen_chain = entry;
1556 entry->fd = parent_end;
1558 return entry->fd;
1560 err_exit:
1562 SAFE_FREE(entry);
1563 SAFE_FREE(argl);
1564 close(pipe_fds[0]);
1565 close(pipe_fds[1]);
1566 return -1;
1569 /**************************************************************************
1570 Wrapper for pclose. Modified from the glibc sources.
1571 ****************************************************************************/
1573 int sys_pclose(int fd)
1575 int wstatus;
1576 popen_list **ptr = &popen_chain;
1577 popen_list *entry = NULL;
1578 pid_t wait_pid;
1579 int status = -1;
1581 /* Unlink from popen_chain. */
1582 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1583 if ((*ptr)->fd == fd) {
1584 entry = *ptr;
1585 *ptr = (*ptr)->next;
1586 status = 0;
1587 break;
1591 if (status < 0 || close(entry->fd) < 0)
1592 return -1;
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.
1600 do {
1601 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1602 } while (wait_pid == -1 && errno == EINTR);
1604 SAFE_FREE(entry);
1606 if (wait_pid == -1)
1607 return -1;
1608 return wstatus;
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);
1619 #else
1620 return NULL;
1621 #endif
1624 void *sys_dlsym(void *handle, const char *symbol)
1626 #if defined(HAVE_DLSYM)
1627 return dlsym(handle, symbol);
1628 #else
1629 return NULL;
1630 #endif
1633 int sys_dlclose (void *handle)
1635 #if defined(HAVE_DLCLOSE)
1636 return dlclose(handle);
1637 #else
1638 return 0;
1639 #endif
1642 const char *sys_dlerror(void)
1644 #if defined(HAVE_DLERROR)
1645 return dlerror();
1646 #else
1647 return NULL;
1648 #endif
1651 int sys_dup2(int oldfd, int newfd)
1653 #if defined(HAVE_DUP2)
1654 return dup2(oldfd, newfd);
1655 #else
1656 errno = ENOSYS;
1657 return -1;
1658 #endif
1661 /**************************************************************************
1662 Wrapper for Admin Logs.
1663 ****************************************************************************/
1665 void sys_adminlog(int priority, const char *format_str, ...)
1667 va_list ap;
1668 int ret;
1669 char *msgbuf = NULL;
1671 va_start( ap, format_str );
1672 ret = vasprintf( &msgbuf, format_str, ap );
1673 va_end( ap );
1675 if (ret == -1)
1676 return;
1678 #if defined(HAVE_SYSLOG)
1679 syslog( priority, "%s", msgbuf );
1680 #else
1681 DEBUG(0,("%s", msgbuf ));
1682 #endif
1683 SAFE_FREE(msgbuf);
1686 /******** Solaris EA helper function prototypes ********/
1687 #ifdef HAVE_ATTROPEN
1688 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1689 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1690 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1691 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1692 static int solaris_unlinkat(int attrdirfd, const char *name);
1693 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1694 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1695 #endif
1697 /**************************************************************************
1698 Wrappers for extented attribute calls. Based on the Linux package with
1699 support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1700 ****************************************************************************/
1702 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1704 #if defined(HAVE_GETXATTR)
1705 #ifndef XATTR_ADD_OPT
1706 return getxattr(path, name, value, size);
1707 #else
1708 int options = 0;
1709 return getxattr(path, name, value, size, 0, options);
1710 #endif
1711 #elif defined(HAVE_GETEA)
1712 return getea(path, name, value, size);
1713 #elif defined(HAVE_EXTATTR_GET_FILE)
1714 char *s;
1715 ssize_t retval;
1716 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1717 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1718 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1720 * The BSD implementation has a nasty habit of silently truncating
1721 * the returned value to the size of the buffer, so we have to check
1722 * that the buffer is large enough to fit the returned value.
1724 if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1725 if(retval > size) {
1726 errno = ERANGE;
1727 return -1;
1729 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1730 return retval;
1733 DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1734 return -1;
1735 #elif defined(HAVE_ATTR_GET)
1736 int retval, flags = 0;
1737 int valuelength = (int)size;
1738 char *attrname = strchr(name,'.') + 1;
1740 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1742 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1744 return retval ? retval : valuelength;
1745 #elif defined(HAVE_ATTROPEN)
1746 ssize_t ret = -1;
1747 int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1748 if (attrfd >= 0) {
1749 ret = solaris_read_xattr(attrfd, value, size);
1750 close(attrfd);
1752 return ret;
1753 #else
1754 errno = ENOSYS;
1755 return -1;
1756 #endif
1759 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1761 #if defined(HAVE_LGETXATTR)
1762 return lgetxattr(path, name, value, size);
1763 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1764 int options = XATTR_NOFOLLOW;
1765 return getxattr(path, name, value, size, 0, options);
1766 #elif defined(HAVE_LGETEA)
1767 return lgetea(path, name, value, size);
1768 #elif defined(HAVE_EXTATTR_GET_LINK)
1769 char *s;
1770 ssize_t retval;
1771 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1772 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1773 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1775 if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1776 if(retval > size) {
1777 errno = ERANGE;
1778 return -1;
1780 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1781 return retval;
1784 DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1785 return -1;
1786 #elif defined(HAVE_ATTR_GET)
1787 int retval, flags = ATTR_DONTFOLLOW;
1788 int valuelength = (int)size;
1789 char *attrname = strchr(name,'.') + 1;
1791 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1793 retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1795 return retval ? retval : valuelength;
1796 #elif defined(HAVE_ATTROPEN)
1797 ssize_t ret = -1;
1798 int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1799 if (attrfd >= 0) {
1800 ret = solaris_read_xattr(attrfd, value, size);
1801 close(attrfd);
1803 return ret;
1804 #else
1805 errno = ENOSYS;
1806 return -1;
1807 #endif
1810 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1812 #if defined(HAVE_FGETXATTR)
1813 #ifndef XATTR_ADD_OPT
1814 return fgetxattr(filedes, name, value, size);
1815 #else
1816 int options = 0;
1817 return fgetxattr(filedes, name, value, size, 0, options);
1818 #endif
1819 #elif defined(HAVE_FGETEA)
1820 return fgetea(filedes, name, value, size);
1821 #elif defined(HAVE_EXTATTR_GET_FD)
1822 char *s;
1823 ssize_t retval;
1824 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
1825 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1826 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1828 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1829 if(retval > size) {
1830 errno = ERANGE;
1831 return -1;
1833 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1834 return retval;
1837 DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1838 return -1;
1839 #elif defined(HAVE_ATTR_GETF)
1840 int retval, flags = 0;
1841 int valuelength = (int)size;
1842 char *attrname = strchr(name,'.') + 1;
1844 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1846 retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1848 return retval ? retval : valuelength;
1849 #elif defined(HAVE_ATTROPEN)
1850 ssize_t ret = -1;
1851 int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1852 if (attrfd >= 0) {
1853 ret = solaris_read_xattr(attrfd, value, size);
1854 close(attrfd);
1856 return ret;
1857 #else
1858 errno = ENOSYS;
1859 return -1;
1860 #endif
1863 #if defined(HAVE_EXTATTR_LIST_FILE)
1865 #define EXTATTR_PREFIX(s) (s), (sizeof((s))-1)
1867 static struct {
1868 int space;
1869 const char *name;
1870 size_t len;
1872 extattr[] = {
1873 { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1874 { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1877 typedef union {
1878 const char *path;
1879 int filedes;
1880 } extattr_arg;
1882 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1884 ssize_t list_size, total_size = 0;
1885 int i, t, len;
1886 char *buf;
1887 /* Iterate through extattr(2) namespaces */
1888 for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1889 switch(type) {
1890 #if defined(HAVE_EXTATTR_LIST_FILE)
1891 case 0:
1892 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1893 break;
1894 #endif
1895 #if defined(HAVE_EXTATTR_LIST_LINK)
1896 case 1:
1897 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1898 break;
1899 #endif
1900 #if defined(HAVE_EXTATTR_LIST_FD)
1901 case 2:
1902 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1903 break;
1904 #endif
1905 default:
1906 errno = ENOSYS;
1907 return -1;
1909 /* Some error happend. Errno should be set by the previous call */
1910 if(list_size < 0)
1911 return -1;
1912 /* No attributes */
1913 if(list_size == 0)
1914 continue;
1915 /* XXX: Call with an empty buffer may be used to calculate
1916 necessary buffer size. Unfortunately, we can't say, how
1917 many attributes were returned, so here is the potential
1918 problem with the emulation.
1920 if(list == NULL) {
1921 /* Take the worse case of one char attribute names -
1922 two bytes per name plus one more for sanity.
1924 total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1925 continue;
1927 /* Count necessary offset to fit namespace prefixes */
1928 len = 0;
1929 for(i = 0; i < list_size; i += list[i] + 1)
1930 len += extattr[t].len;
1932 total_size += list_size + len;
1933 /* Buffer is too small to fit the results */
1934 if(total_size > size) {
1935 errno = ERANGE;
1936 return -1;
1938 /* Shift results back, so we can prepend prefixes */
1939 buf = memmove(list + len, list, list_size);
1941 for(i = 0; i < list_size; i += len + 1) {
1942 len = buf[i];
1943 strncpy(list, extattr[t].name, extattr[t].len + 1);
1944 list += extattr[t].len;
1945 strncpy(list, buf + i + 1, len);
1946 list[len] = '\0';
1947 list += len + 1;
1949 size -= total_size;
1951 return total_size;
1954 #endif
1956 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1957 static char attr_buffer[ATTR_MAX_VALUELEN];
1959 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1961 int retval = 0, index;
1962 attrlist_cursor_t *cursor = 0;
1963 int total_size = 0;
1964 attrlist_t * al = (attrlist_t *)attr_buffer;
1965 attrlist_ent_t *ae;
1966 size_t ent_size, left = size;
1967 char *bp = list;
1969 while (True) {
1970 if (filedes)
1971 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1972 else
1973 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1974 if (retval) break;
1975 for (index = 0; index < al->al_count; index++) {
1976 ae = ATTR_ENTRY(attr_buffer, index);
1977 ent_size = strlen(ae->a_name) + sizeof("user.");
1978 if (left >= ent_size) {
1979 strncpy(bp, "user.", sizeof("user."));
1980 strncat(bp, ae->a_name, ent_size - sizeof("user."));
1981 bp += ent_size;
1982 left -= ent_size;
1983 } else if (size) {
1984 errno = ERANGE;
1985 retval = -1;
1986 break;
1988 total_size += ent_size;
1990 if (al->al_more == 0) break;
1992 if (retval == 0) {
1993 flags |= ATTR_ROOT;
1994 cursor = 0;
1995 while (True) {
1996 if (filedes)
1997 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1998 else
1999 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
2000 if (retval) break;
2001 for (index = 0; index < al->al_count; index++) {
2002 ae = ATTR_ENTRY(attr_buffer, index);
2003 ent_size = strlen(ae->a_name) + sizeof("system.");
2004 if (left >= ent_size) {
2005 strncpy(bp, "system.", sizeof("system."));
2006 strncat(bp, ae->a_name, ent_size - sizeof("system."));
2007 bp += ent_size;
2008 left -= ent_size;
2009 } else if (size) {
2010 errno = ERANGE;
2011 retval = -1;
2012 break;
2014 total_size += ent_size;
2016 if (al->al_more == 0) break;
2019 return (ssize_t)(retval ? retval : total_size);
2022 #endif
2024 ssize_t sys_listxattr (const char *path, char *list, size_t size)
2026 #if defined(HAVE_LISTXATTR)
2027 #ifndef XATTR_ADD_OPT
2028 return listxattr(path, list, size);
2029 #else
2030 int options = 0;
2031 return listxattr(path, list, size, options);
2032 #endif
2033 #elif defined(HAVE_LISTEA)
2034 return listea(path, list, size);
2035 #elif defined(HAVE_EXTATTR_LIST_FILE)
2036 extattr_arg arg;
2037 arg.path = path;
2038 return bsd_attr_list(0, arg, list, size);
2039 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
2040 return irix_attr_list(path, 0, list, size, 0);
2041 #elif defined(HAVE_ATTROPEN)
2042 ssize_t ret = -1;
2043 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
2044 if (attrdirfd >= 0) {
2045 ret = solaris_list_xattr(attrdirfd, list, size);
2046 close(attrdirfd);
2048 return ret;
2049 #else
2050 errno = ENOSYS;
2051 return -1;
2052 #endif
2055 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
2057 #if defined(HAVE_LLISTXATTR)
2058 return llistxattr(path, list, size);
2059 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
2060 int options = XATTR_NOFOLLOW;
2061 return listxattr(path, list, size, options);
2062 #elif defined(HAVE_LLISTEA)
2063 return llistea(path, list, size);
2064 #elif defined(HAVE_EXTATTR_LIST_LINK)
2065 extattr_arg arg;
2066 arg.path = path;
2067 return bsd_attr_list(1, arg, list, size);
2068 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
2069 return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
2070 #elif defined(HAVE_ATTROPEN)
2071 ssize_t ret = -1;
2072 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
2073 if (attrdirfd >= 0) {
2074 ret = solaris_list_xattr(attrdirfd, list, size);
2075 close(attrdirfd);
2077 return ret;
2078 #else
2079 errno = ENOSYS;
2080 return -1;
2081 #endif
2084 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
2086 #if defined(HAVE_FLISTXATTR)
2087 #ifndef XATTR_ADD_OPT
2088 return flistxattr(filedes, list, size);
2089 #else
2090 int options = 0;
2091 return flistxattr(filedes, list, size, options);
2092 #endif
2093 #elif defined(HAVE_FLISTEA)
2094 return flistea(filedes, list, size);
2095 #elif defined(HAVE_EXTATTR_LIST_FD)
2096 extattr_arg arg;
2097 arg.filedes = filedes;
2098 return bsd_attr_list(2, arg, list, size);
2099 #elif defined(HAVE_ATTR_LISTF)
2100 return irix_attr_list(NULL, filedes, list, size, 0);
2101 #elif defined(HAVE_ATTROPEN)
2102 ssize_t ret = -1;
2103 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2104 if (attrdirfd >= 0) {
2105 ret = solaris_list_xattr(attrdirfd, list, size);
2106 close(attrdirfd);
2108 return ret;
2109 #else
2110 errno = ENOSYS;
2111 return -1;
2112 #endif
2115 int sys_removexattr (const char *path, const char *name)
2117 #if defined(HAVE_REMOVEXATTR)
2118 #ifndef XATTR_ADD_OPT
2119 return removexattr(path, name);
2120 #else
2121 int options = 0;
2122 return removexattr(path, name, options);
2123 #endif
2124 #elif defined(HAVE_REMOVEEA)
2125 return removeea(path, name);
2126 #elif defined(HAVE_EXTATTR_DELETE_FILE)
2127 char *s;
2128 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2129 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2130 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2132 return extattr_delete_file(path, attrnamespace, attrname);
2133 #elif defined(HAVE_ATTR_REMOVE)
2134 int flags = 0;
2135 char *attrname = strchr(name,'.') + 1;
2137 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2139 return attr_remove(path, attrname, flags);
2140 #elif defined(HAVE_ATTROPEN)
2141 int ret = -1;
2142 int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
2143 if (attrdirfd >= 0) {
2144 ret = solaris_unlinkat(attrdirfd, name);
2145 close(attrdirfd);
2147 return ret;
2148 #else
2149 errno = ENOSYS;
2150 return -1;
2151 #endif
2154 int sys_lremovexattr (const char *path, const char *name)
2156 #if defined(HAVE_LREMOVEXATTR)
2157 return lremovexattr(path, name);
2158 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
2159 int options = XATTR_NOFOLLOW;
2160 return removexattr(path, name, options);
2161 #elif defined(HAVE_LREMOVEEA)
2162 return lremoveea(path, name);
2163 #elif defined(HAVE_EXTATTR_DELETE_LINK)
2164 char *s;
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 return extattr_delete_link(path, attrnamespace, attrname);
2170 #elif defined(HAVE_ATTR_REMOVE)
2171 int flags = ATTR_DONTFOLLOW;
2172 char *attrname = strchr(name,'.') + 1;
2174 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2176 return attr_remove(path, attrname, flags);
2177 #elif defined(HAVE_ATTROPEN)
2178 int ret = -1;
2179 int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
2180 if (attrdirfd >= 0) {
2181 ret = solaris_unlinkat(attrdirfd, name);
2182 close(attrdirfd);
2184 return ret;
2185 #else
2186 errno = ENOSYS;
2187 return -1;
2188 #endif
2191 int sys_fremovexattr (int filedes, const char *name)
2193 #if defined(HAVE_FREMOVEXATTR)
2194 #ifndef XATTR_ADD_OPT
2195 return fremovexattr(filedes, name);
2196 #else
2197 int options = 0;
2198 return fremovexattr(filedes, name, options);
2199 #endif
2200 #elif defined(HAVE_FREMOVEEA)
2201 return fremoveea(filedes, name);
2202 #elif defined(HAVE_EXTATTR_DELETE_FD)
2203 char *s;
2204 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2205 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2206 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2208 return extattr_delete_fd(filedes, attrnamespace, attrname);
2209 #elif defined(HAVE_ATTR_REMOVEF)
2210 int flags = 0;
2211 char *attrname = strchr(name,'.') + 1;
2213 if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2215 return attr_removef(filedes, attrname, flags);
2216 #elif defined(HAVE_ATTROPEN)
2217 int ret = -1;
2218 int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2219 if (attrdirfd >= 0) {
2220 ret = solaris_unlinkat(attrdirfd, name);
2221 close(attrdirfd);
2223 return ret;
2224 #else
2225 errno = ENOSYS;
2226 return -1;
2227 #endif
2230 #if !defined(HAVE_SETXATTR)
2231 #define XATTR_CREATE 0x1 /* set value, fail if attr already exists */
2232 #define XATTR_REPLACE 0x2 /* set value, fail if attr does not exist */
2233 #endif
2235 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2237 #if defined(HAVE_SETXATTR)
2238 #ifndef XATTR_ADD_OPT
2239 return setxattr(path, name, value, size, flags);
2240 #else
2241 int options = 0;
2242 return setxattr(path, name, value, size, 0, options);
2243 #endif
2244 #elif defined(HAVE_SETEA)
2245 return setea(path, name, value, size, flags);
2246 #elif defined(HAVE_EXTATTR_SET_FILE)
2247 char *s;
2248 int retval = 0;
2249 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2250 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2251 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2252 if (flags) {
2253 /* Check attribute existence */
2254 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
2255 if (retval < 0) {
2256 /* REPLACE attribute, that doesn't exist */
2257 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2258 errno = ENOATTR;
2259 return -1;
2261 /* Ignore other errors */
2263 else {
2264 /* CREATE attribute, that already exists */
2265 if (flags & XATTR_CREATE) {
2266 errno = EEXIST;
2267 return -1;
2271 retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2272 return (retval < 0) ? -1 : 0;
2273 #elif defined(HAVE_ATTR_SET)
2274 int myflags = 0;
2275 char *attrname = strchr(name,'.') + 1;
2277 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2278 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2279 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2281 return attr_set(path, attrname, (const char *)value, size, myflags);
2282 #elif defined(HAVE_ATTROPEN)
2283 int ret = -1;
2284 int myflags = O_RDWR;
2285 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2286 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2287 int attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2288 if (attrfd >= 0) {
2289 ret = solaris_write_xattr(attrfd, value, size);
2290 close(attrfd);
2292 return ret;
2293 #else
2294 errno = ENOSYS;
2295 return -1;
2296 #endif
2299 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2301 #if defined(HAVE_LSETXATTR)
2302 return lsetxattr(path, name, value, size, flags);
2303 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2304 int options = XATTR_NOFOLLOW;
2305 return setxattr(path, name, value, size, 0, options);
2306 #elif defined(LSETEA)
2307 return lsetea(path, name, value, size, flags);
2308 #elif defined(HAVE_EXTATTR_SET_LINK)
2309 char *s;
2310 int retval = 0;
2311 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2312 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2313 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2314 if (flags) {
2315 /* Check attribute existence */
2316 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2317 if (retval < 0) {
2318 /* REPLACE attribute, that doesn't exist */
2319 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2320 errno = ENOATTR;
2321 return -1;
2323 /* Ignore other errors */
2325 else {
2326 /* CREATE attribute, that already exists */
2327 if (flags & XATTR_CREATE) {
2328 errno = EEXIST;
2329 return -1;
2334 retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2335 return (retval < 0) ? -1 : 0;
2336 #elif defined(HAVE_ATTR_SET)
2337 int myflags = ATTR_DONTFOLLOW;
2338 char *attrname = strchr(name,'.') + 1;
2340 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2341 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2342 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2344 return attr_set(path, attrname, (const char *)value, size, myflags);
2345 #elif defined(HAVE_ATTROPEN)
2346 int ret = -1;
2347 int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2348 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2349 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2350 int attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2351 if (attrfd >= 0) {
2352 ret = solaris_write_xattr(attrfd, value, size);
2353 close(attrfd);
2355 return ret;
2356 #else
2357 errno = ENOSYS;
2358 return -1;
2359 #endif
2362 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2364 #if defined(HAVE_FSETXATTR)
2365 #ifndef XATTR_ADD_OPT
2366 return fsetxattr(filedes, name, value, size, flags);
2367 #else
2368 int options = 0;
2369 return fsetxattr(filedes, name, value, size, 0, options);
2370 #endif
2371 #elif defined(HAVE_FSETEA)
2372 return fsetea(filedes, name, value, size, flags);
2373 #elif defined(HAVE_EXTATTR_SET_FD)
2374 char *s;
2375 int retval = 0;
2376 int attrnamespace = (strncmp(name, "system", 6) == 0) ?
2377 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2378 const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2379 if (flags) {
2380 /* Check attribute existence */
2381 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2382 if (retval < 0) {
2383 /* REPLACE attribute, that doesn't exist */
2384 if (flags & XATTR_REPLACE && errno == ENOATTR) {
2385 errno = ENOATTR;
2386 return -1;
2388 /* Ignore other errors */
2390 else {
2391 /* CREATE attribute, that already exists */
2392 if (flags & XATTR_CREATE) {
2393 errno = EEXIST;
2394 return -1;
2398 retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2399 return (retval < 0) ? -1 : 0;
2400 #elif defined(HAVE_ATTR_SETF)
2401 int myflags = 0;
2402 char *attrname = strchr(name,'.') + 1;
2404 if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2405 if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2406 if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2408 return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2409 #elif defined(HAVE_ATTROPEN)
2410 int ret = -1;
2411 int myflags = O_RDWR | O_XATTR;
2412 if (flags & XATTR_CREATE) myflags |= O_EXCL;
2413 if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2414 int attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2415 if (attrfd >= 0) {
2416 ret = solaris_write_xattr(attrfd, value, size);
2417 close(attrfd);
2419 return ret;
2420 #else
2421 errno = ENOSYS;
2422 return -1;
2423 #endif
2426 /**************************************************************************
2427 helper functions for Solaris' EA support
2428 ****************************************************************************/
2429 #ifdef HAVE_ATTROPEN
2430 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2432 struct stat sbuf;
2434 if (fstat(attrfd, &sbuf) == -1) {
2435 errno = ENOATTR;
2436 return -1;
2439 /* This is to return the current size of the named extended attribute */
2440 if (size == 0) {
2441 return sbuf.st_size;
2444 /* check size and read xattr */
2445 if (sbuf.st_size > size) {
2446 errno = ERANGE;
2447 return -1;
2450 return read(attrfd, value, sbuf.st_size);
2453 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2455 ssize_t len = 0;
2456 int stop = 0;
2457 DIR *dirp;
2458 struct dirent *de;
2459 int newfd = dup(attrdirfd);
2460 /* CAUTION: The originating file descriptor should not be
2461 used again following the call to fdopendir().
2462 For that reason we dup() the file descriptor
2463 here to make things more clear. */
2464 dirp = fdopendir(newfd);
2466 while ((de = readdir(dirp))) {
2467 size_t listlen = strlen(de->d_name);
2468 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2469 /* we don't want "." and ".." here: */
2470 DEBUG(10,("skipped EA %s\n",de->d_name));
2471 continue;
2474 if (size == 0) {
2475 /* return the current size of the list of extended attribute names*/
2476 len += listlen + 1;
2477 } else {
2478 /* check size and copy entrieѕ + nul into list. */
2479 if ((len + listlen + 1) > size) {
2480 errno = ERANGE;
2481 len = -1;
2482 break;
2483 } else {
2484 safe_strcpy(list + len, de->d_name, listlen);
2485 pstrcpy(list + len, de->d_name);
2486 len += listlen;
2487 list[len] = '\0';
2488 ++len;
2493 if (closedir(dirp) == -1) {
2494 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2495 return -1;
2497 return len;
2500 static int solaris_unlinkat(int attrdirfd, const char *name)
2502 if (unlinkat(attrdirfd, name, 0) == -1) {
2503 if (errno == ENOENT) {
2504 errno = ENOATTR;
2506 return -1;
2508 return 0;
2511 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2513 int filedes = attropen(path, attrpath, oflag, mode);
2514 if (filedes == -1) {
2515 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2516 if (errno == EINVAL) {
2517 errno = ENOTSUP;
2518 } else {
2519 errno = ENOATTR;
2522 return filedes;
2525 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2527 int filedes = openat(fildes, path, oflag, mode);
2528 if (filedes == -1) {
2529 DEBUG(10,("openat FAILED: fd: %s, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2530 if (errno == EINVAL) {
2531 errno = ENOTSUP;
2532 } else {
2533 errno = ENOATTR;
2536 return filedes;
2539 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2541 if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2542 return 0;
2543 } else {
2544 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2545 return -1;
2548 #endif /*HAVE_ATTROPEN*/
2551 /****************************************************************************
2552 Return the major devicenumber for UNIX extensions.
2553 ****************************************************************************/
2555 uint32 unix_dev_major(SMB_DEV_T dev)
2557 #if defined(HAVE_DEVICE_MAJOR_FN)
2558 return (uint32)major(dev);
2559 #else
2560 return (uint32)(dev >> 8);
2561 #endif
2564 /****************************************************************************
2565 Return the minor devicenumber for UNIX extensions.
2566 ****************************************************************************/
2568 uint32 unix_dev_minor(SMB_DEV_T dev)
2570 #if defined(HAVE_DEVICE_MINOR_FN)
2571 return (uint32)minor(dev);
2572 #else
2573 return (uint32)(dev & 0xff);
2574 #endif
2577 #if defined(WITH_AIO)
2579 /*******************************************************************
2580 An aio_read wrapper that will deal with 64-bit sizes.
2581 ********************************************************************/
2583 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2585 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2586 return aio_read64(aiocb);
2587 #elif defined(HAVE_AIO_READ)
2588 return aio_read(aiocb);
2589 #else
2590 errno = ENOSYS;
2591 return -1;
2592 #endif
2595 /*******************************************************************
2596 An aio_write wrapper that will deal with 64-bit sizes.
2597 ********************************************************************/
2599 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2601 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2602 return aio_write64(aiocb);
2603 #elif defined(HAVE_AIO_WRITE)
2604 return aio_write(aiocb);
2605 #else
2606 errno = ENOSYS;
2607 return -1;
2608 #endif
2611 /*******************************************************************
2612 An aio_return wrapper that will deal with 64-bit sizes.
2613 ********************************************************************/
2615 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2617 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2618 return aio_return64(aiocb);
2619 #elif defined(HAVE_AIO_RETURN)
2620 return aio_return(aiocb);
2621 #else
2622 errno = ENOSYS;
2623 return -1;
2624 #endif
2627 /*******************************************************************
2628 An aio_cancel wrapper that will deal with 64-bit sizes.
2629 ********************************************************************/
2631 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2633 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2634 return aio_cancel64(fd, aiocb);
2635 #elif defined(HAVE_AIO_CANCEL)
2636 return aio_cancel(fd, aiocb);
2637 #else
2638 errno = ENOSYS;
2639 return -1;
2640 #endif
2643 /*******************************************************************
2644 An aio_error wrapper that will deal with 64-bit sizes.
2645 ********************************************************************/
2647 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2649 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2650 return aio_error64(aiocb);
2651 #elif defined(HAVE_AIO_ERROR)
2652 return aio_error(aiocb);
2653 #else
2654 errno = ENOSYS;
2655 return -1;
2656 #endif
2659 /*******************************************************************
2660 An aio_fsync wrapper that will deal with 64-bit sizes.
2661 ********************************************************************/
2663 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2665 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2666 return aio_fsync64(op, aiocb);
2667 #elif defined(HAVE_AIO_FSYNC)
2668 return aio_fsync(op, aiocb);
2669 #else
2670 errno = ENOSYS;
2671 return -1;
2672 #endif
2675 /*******************************************************************
2676 An aio_fsync wrapper that will deal with 64-bit sizes.
2677 ********************************************************************/
2679 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2681 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2682 return aio_suspend64(cblist, n, timeout);
2683 #elif defined(HAVE_AIO_FSYNC)
2684 return aio_suspend(cblist, n, timeout);
2685 #else
2686 errno = ENOSYS;
2687 return -1;
2688 #endif
2690 #else /* !WITH_AIO */
2692 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2694 errno = ENOSYS;
2695 return -1;
2698 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2700 errno = ENOSYS;
2701 return -1;
2704 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2706 errno = ENOSYS;
2707 return -1;
2710 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2712 errno = ENOSYS;
2713 return -1;
2716 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2718 errno = ENOSYS;
2719 return -1;
2722 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2724 errno = ENOSYS;
2725 return -1;
2728 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2730 errno = ENOSYS;
2731 return -1;
2733 #endif /* WITH_AIO */
2735 int sys_getpeereid( int s, uid_t *uid)
2737 #if defined(HAVE_PEERCRED)
2738 struct ucred cred;
2739 socklen_t cred_len = sizeof(struct ucred);
2740 int ret;
2742 ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2743 if (ret != 0) {
2744 return -1;
2747 if (cred_len != sizeof(struct ucred)) {
2748 errno = EINVAL;
2749 return -1;
2752 *uid = cred.uid;
2753 return 0;
2754 #else
2755 errno = ENOSYS;
2756 return -1;
2757 #endif