Remove "store create time" code, cause create time to be stored
[kai/samba.git] / source3 / lib / system.c
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
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
24
25 #ifdef HAVE_SYS_PRCTL_H
26 #include <sys/prctl.h>
27 #endif
28
29 /*
30    The idea is that this file will eventually have wrappers around all
31    important system calls in samba. The aims are:
32
33    - to enable easier porting by putting OS dependent stuff in here
34
35    - to allow for hooks into other "pseudo-filesystems"
36
37    - to allow easier integration of things like the japanese extensions
38
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.
42 */
43
44
45
46 /*******************************************************************
47  A wrapper for memalign
48 ********************************************************************/
49
50 void *sys_memalign( size_t align, size_t size )
51 {
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;
57
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;
74         }
75         if (size < pagesize) {
76                 size = pagesize;
77         }
78         return SMB_MALLOC(size);
79 #endif
80 }
81
82 /*******************************************************************
83  A wrapper for usleep in case we don't have one.
84 ********************************************************************/
85
86 int sys_usleep(long usecs)
87 {
88 #ifndef HAVE_USLEEP
89         struct timeval tval;
90 #endif
91
92         /*
93          * We need this braindamage as the glibc usleep
94          * is not SPEC1170 complient... grumble... JRA.
95          */
96
97         if(usecs < 0 || usecs > 999999) {
98                 errno = EINVAL;
99                 return -1;
100         }
101
102 #if HAVE_USLEEP
103         usleep(usecs);
104         return 0;
105 #else /* HAVE_USLEEP */
106         /*
107          * Fake it with select...
108          */
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 */
114 }
115
116 /*******************************************************************
117 A read wrapper that will deal with EINTR.
118 ********************************************************************/
119
120 ssize_t sys_read(int fd, void *buf, size_t count)
121 {
122         ssize_t ret;
123
124         do {
125                 ret = read(fd, buf, count);
126         } while (ret == -1 && errno == EINTR);
127         return ret;
128 }
129
130 /*******************************************************************
131 A write wrapper that will deal with EINTR.
132 ********************************************************************/
133
134 ssize_t sys_write(int fd, const void *buf, size_t count)
135 {
136         ssize_t ret;
137
138         do {
139                 ret = write(fd, buf, count);
140         } while (ret == -1 && errno == EINTR);
141         return ret;
142 }
143
144 /*******************************************************************
145 A writev wrapper that will deal with EINTR.
146 ********************************************************************/
147
148 ssize_t sys_writev(int fd, const struct iovec *iov, int iovcnt)
149 {
150         ssize_t ret;
151
152 #if 0
153         /* Try to confuse write_data_iov a bit */
154         if ((random() % 5) == 0) {
155                 return sys_write(fd, iov[0].iov_base, iov[0].iov_len);
156         }
157         if (iov[0].iov_len > 1) {
158                 return sys_write(fd, iov[0].iov_base,
159                                  (random() % (iov[0].iov_len-1)) + 1);
160         }
161 #endif
162
163         do {
164                 ret = writev(fd, iov, iovcnt);
165         } while (ret == -1 && errno == EINTR);
166         return ret;
167 }
168
169 /*******************************************************************
170 A pread wrapper that will deal with EINTR and 64-bit file offsets.
171 ********************************************************************/
172
173 #if defined(HAVE_PREAD) || defined(HAVE_PREAD64)
174 ssize_t sys_pread(int fd, void *buf, size_t count, SMB_OFF_T off)
175 {
176         ssize_t ret;
177
178         do {
179 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PREAD64)
180                 ret = pread64(fd, buf, count, off);
181 #else
182                 ret = pread(fd, buf, count, off);
183 #endif
184         } while (ret == -1 && errno == EINTR);
185         return ret;
186 }
187 #endif
188
189 /*******************************************************************
190 A write wrapper that will deal with EINTR and 64-bit file offsets.
191 ********************************************************************/
192
193 #if defined(HAVE_PWRITE) || defined(HAVE_PWRITE64)
194 ssize_t sys_pwrite(int fd, const void *buf, size_t count, SMB_OFF_T off)
195 {
196         ssize_t ret;
197
198         do {
199 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_PWRITE64)
200                 ret = pwrite64(fd, buf, count, off);
201 #else
202                 ret = pwrite(fd, buf, count, off);
203 #endif
204         } while (ret == -1 && errno == EINTR);
205         return ret;
206 }
207 #endif
208
209 /*******************************************************************
210 A send wrapper that will deal with EINTR.
211 ********************************************************************/
212
213 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
214 {
215         ssize_t ret;
216
217         do {
218                 ret = send(s, msg, len, flags);
219         } while (ret == -1 && errno == EINTR);
220         return ret;
221 }
222
223 /*******************************************************************
224 A sendto wrapper that will deal with EINTR.
225 ********************************************************************/
226
227 ssize_t sys_sendto(int s,  const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
228 {
229         ssize_t ret;
230
231         do {
232                 ret = sendto(s, msg, len, flags, to, tolen);
233         } while (ret == -1 && errno == EINTR);
234         return ret;
235 }
236
237 /*******************************************************************
238 A recv wrapper that will deal with EINTR.
239 ********************************************************************/
240
241 ssize_t sys_recv(int fd, void *buf, size_t count, int flags)
242 {
243         ssize_t ret;
244
245         do {
246                 ret = recv(fd, buf, count, flags);
247         } while (ret == -1 && errno == EINTR);
248         return ret;
249 }
250
251 /*******************************************************************
252 A recvfrom wrapper that will deal with EINTR.
253 ********************************************************************/
254
255 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
256 {
257         ssize_t ret;
258
259         do {
260                 ret = recvfrom(s, buf, len, flags, from, fromlen);
261         } while (ret == -1 && errno == EINTR);
262         return ret;
263 }
264
265 /*******************************************************************
266 A fcntl wrapper that will deal with EINTR.
267 ********************************************************************/
268
269 int sys_fcntl_ptr(int fd, int cmd, void *arg)
270 {
271         int ret;
272
273         do {
274                 ret = fcntl(fd, cmd, arg);
275         } while (ret == -1 && errno == EINTR);
276         return ret;
277 }
278
279 /*******************************************************************
280 A fcntl wrapper that will deal with EINTR.
281 ********************************************************************/
282
283 int sys_fcntl_long(int fd, int cmd, long arg)
284 {
285         int ret;
286
287         do {
288                 ret = fcntl(fd, cmd, arg);
289         } while (ret == -1 && errno == EINTR);
290         return ret;
291 }
292
293 /****************************************************************************
294  Get/Set all the possible time fields from a stat struct as a timespec.
295 ****************************************************************************/
296
297 static struct timespec get_atimespec(const struct stat *pst)
298 {
299 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
300         struct timespec ret;
301
302         /* Old system - no ns timestamp. */
303         ret.tv_sec = pst->st_atime;
304         ret.tv_nsec = 0;
305         return ret;
306 #else
307 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
308         return pst->st_atim;
309 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
310         struct timespec ret;
311         ret.tv_sec = pst->st_atime;
312         ret.tv_nsec = pst->st_atimensec;
313         return ret;
314 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
315         struct timespec ret;
316         ret.tv_sec = pst->st_atime;
317         ret.tv_nsec = pst->st_atime_n;
318         return ret;
319 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
320         struct timespec ret;
321         ret.tv_sec = pst->st_atime;
322         ret.tv_nsec = pst->st_uatime * 1000;
323         return ret;
324 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
325         return pst->st_atimespec;
326 #else
327 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
328 #endif
329 #endif
330 }
331
332 static struct timespec get_mtimespec(const struct stat *pst)
333 {
334 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
335         struct timespec ret;
336
337         /* Old system - no ns timestamp. */
338         ret.tv_sec = pst->st_mtime;
339         ret.tv_nsec = 0;
340         return ret;
341 #else
342 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
343         return pst->st_mtim;
344 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
345         struct timespec ret;
346         ret.tv_sec = pst->st_mtime;
347         ret.tv_nsec = pst->st_mtimensec;
348         return ret;
349 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
350         struct timespec ret;
351         ret.tv_sec = pst->st_mtime;
352         ret.tv_nsec = pst->st_mtime_n;
353         return ret;
354 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
355         struct timespec ret;
356         ret.tv_sec = pst->st_mtime;
357         ret.tv_nsec = pst->st_umtime * 1000;
358         return ret;
359 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
360         return pst->st_mtimespec;
361 #else
362 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
363 #endif
364 #endif
365 }
366
367 static struct timespec get_ctimespec(const struct stat *pst)
368 {
369 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
370         struct timespec ret;
371
372         /* Old system - no ns timestamp. */
373         ret.tv_sec = pst->st_ctime;
374         ret.tv_nsec = 0;
375         return ret;
376 #else
377 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
378         return pst->st_ctim;
379 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
380         struct timespec ret;
381         ret.tv_sec = pst->st_ctime;
382         ret.tv_nsec = pst->st_ctimensec;
383         return ret;
384 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
385         struct timespec ret;
386         ret.tv_sec = pst->st_ctime;
387         ret.tv_nsec = pst->st_ctime_n;
388         return ret;
389 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
390         struct timespec ret;
391         ret.tv_sec = pst->st_ctime;
392         ret.tv_nsec = pst->st_uctime * 1000;
393         return ret;
394 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
395         return pst->st_ctimespec;
396 #else
397 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
398 #endif
399 #endif
400 }
401
402 /****************************************************************************
403  Return the best approximation to a 'create time' under UNIX from a stat
404  structure.
405 ****************************************************************************/
406
407 static struct timespec calc_create_time_stat(const struct stat *st)
408 {
409         struct timespec ret, ret1;
410         struct timespec c_time = get_ctimespec(st);
411         struct timespec m_time = get_mtimespec(st);
412         struct timespec a_time = get_atimespec(st);
413
414         ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
415         ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
416
417         if(!null_timespec(ret1)) {
418                 return ret1;
419         }
420
421         /*
422          * One of ctime, mtime or atime was zero (probably atime).
423          * Just return MIN(ctime, mtime).
424          */
425         return ret;
426 }
427
428 /****************************************************************************
429  Return the best approximation to a 'create time' under UNIX from a stat_ex
430  structure.
431 ****************************************************************************/
432
433 static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
434 {
435         struct timespec ret, ret1;
436         struct timespec c_time = st->st_ex_ctime;
437         struct timespec m_time = st->st_ex_mtime;
438         struct timespec a_time = st->st_ex_atime;
439
440         ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
441         ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
442
443         if(!null_timespec(ret1)) {
444                 return ret1;
445         }
446
447         /*
448          * One of ctime, mtime or atime was zero (probably atime).
449          * Just return MIN(ctime, mtime).
450          */
451         return ret;
452 }
453
454 /****************************************************************************
455  Return the 'create time' from a stat struct if it exists (birthtime) or else
456  use the best approximation.
457 ****************************************************************************/
458
459 static void make_create_timespec(const struct stat *pst, struct stat_ex *dst)
460 {
461         if (S_ISDIR(pst->st_mode) && lp_fake_dir_create_times()) {
462                 dst->st_ex_btime.tv_sec = 315493200L;          /* 1/1/1980 */
463                 dst->st_ex_btime.tv_nsec = 0;
464         }
465
466         dst->st_ex_calculated_birthtime = false;
467
468 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
469         dst->st_ex_btime = pst->st_birthtimespec;
470 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
471         dst->st_ex_btime.tv_sec = pst->st_birthtime;
472         dst->st_ex_btime.tv_nsec = pst->st_birthtimenspec;
473 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
474         dst->st_ex_btime.tv_sec = pst->st_birthtime;
475         dst->st_ex_btime.tv_nsec = 0;
476 #else
477         dst->st_ex_btime = calc_create_time_stat(pst);
478         dst->st_ex_calculated_birthtime = true;
479 #endif
480
481         /* Deal with systems that don't initialize birthtime correctly.
482          * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
483          */
484         if (null_timespec(dst->st_ex_btime)) {
485                 dst->st_ex_btime = calc_create_time_stat(pst);
486                 dst->st_ex_calculated_birthtime = true;
487         }
488 }
489
490 /****************************************************************************
491  If we update a timestamp in a stat_ex struct we may have to recalculate
492  the birthtime. For now only implement this for write time, but we may
493  also need to do it for atime and ctime. JRA.
494 ****************************************************************************/
495
496 void update_stat_ex_mtime(struct stat_ex *dst,
497                                 struct timespec write_ts)
498 {
499         dst->st_ex_mtime = write_ts;
500
501         /* We may have to recalculate btime. */
502         if (dst->st_ex_calculated_birthtime) {
503                 dst->st_ex_btime = calc_create_time_stat_ex(dst);
504         }
505 }
506
507 void update_stat_ex_create_time(struct stat_ex *dst,
508                                 struct timespec create_time)
509 {
510         dst->st_ex_btime = create_time;
511         dst->st_ex_calculated_birthtime = false;
512 }
513
514 static void init_stat_ex_from_stat (struct stat_ex *dst,
515                                     const struct stat *src)
516 {
517         dst->st_ex_dev = src->st_dev;
518         dst->st_ex_ino = src->st_ino;
519         dst->st_ex_mode = src->st_mode;
520         dst->st_ex_nlink = src->st_nlink;
521         dst->st_ex_uid = src->st_uid;
522         dst->st_ex_gid = src->st_gid;
523         dst->st_ex_rdev = src->st_rdev;
524         dst->st_ex_size = src->st_size;
525         dst->st_ex_atime = get_atimespec(src);
526         dst->st_ex_mtime = get_mtimespec(src);
527         dst->st_ex_ctime = get_ctimespec(src);
528         make_create_timespec(src, dst);
529         dst->st_ex_blksize = src->st_blksize;
530         dst->st_ex_blocks = src->st_blocks;
531
532 #ifdef HAVE_STAT_ST_FLAGS
533         dst->st_ex_flags = src->st_flags;
534 #else
535         dst->st_ex_flags = 0;
536 #endif
537 }
538
539 /*******************************************************************
540 A stat() wrapper that will deal with 64 bit filesizes.
541 ********************************************************************/
542
543 int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
544 {
545         int ret;
546 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
547         ret = stat64(fname, sbuf);
548 #else
549         struct stat statbuf;
550         ret = stat(fname, &statbuf);
551 #endif
552         if (ret == 0) {
553                 /* we always want directories to appear zero size */
554                 if (S_ISDIR(statbuf.st_mode)) {
555                         statbuf.st_size = 0;
556                 }
557                 init_stat_ex_from_stat(sbuf, &statbuf);
558         }
559         return ret;
560 }
561
562 /*******************************************************************
563  An fstat() wrapper that will deal with 64 bit filesizes.
564 ********************************************************************/
565
566 int sys_fstat(int fd,SMB_STRUCT_STAT *sbuf)
567 {
568         int ret;
569 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FSTAT64)
570         ret = fstat64(fd, sbuf);
571 #else
572         struct stat statbuf;
573         ret = fstat(fd, &statbuf);
574 #endif
575         if (ret == 0) {
576                 /* we always want directories to appear zero size */
577                 if (S_ISDIR(statbuf.st_mode)) {
578                         statbuf.st_size = 0;
579                 }
580                 init_stat_ex_from_stat(sbuf, &statbuf);
581         }
582         return ret;
583 }
584
585 /*******************************************************************
586  An lstat() wrapper that will deal with 64 bit filesizes.
587 ********************************************************************/
588
589 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
590 {
591         int ret;
592 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSTAT64)
593         ret = lstat64(fname, sbuf);
594 #else
595         struct stat statbuf;
596         ret = lstat(fname, &statbuf);
597 #endif
598         if (ret == 0) {
599                 /* we always want directories to appear zero size */
600                 if (S_ISDIR(statbuf.st_mode)) {
601                         statbuf.st_size = 0;
602                 }
603                 init_stat_ex_from_stat(sbuf, &statbuf);
604         }
605         return ret;
606 }
607
608 /*******************************************************************
609  An ftruncate() wrapper that will deal with 64 bit filesizes.
610 ********************************************************************/
611
612 int sys_ftruncate(int fd, SMB_OFF_T offset)
613 {
614 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_FTRUNCATE64)
615         return ftruncate64(fd, offset);
616 #else
617         return ftruncate(fd, offset);
618 #endif
619 }
620
621 /*******************************************************************
622  An lseek() wrapper that will deal with 64 bit filesizes.
623 ********************************************************************/
624
625 SMB_OFF_T sys_lseek(int fd, SMB_OFF_T offset, int whence)
626 {
627 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_LSEEK64)
628         return lseek64(fd, offset, whence);
629 #else
630         return lseek(fd, offset, whence);
631 #endif
632 }
633
634 /*******************************************************************
635  An fseek() wrapper that will deal with 64 bit filesizes.
636 ********************************************************************/
637
638 int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
639 {
640 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEK64)
641         return fseek64(fp, offset, whence);
642 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
643         return fseeko64(fp, offset, whence);
644 #else
645         return fseek(fp, offset, whence);
646 #endif
647 }
648
649 /*******************************************************************
650  An ftell() wrapper that will deal with 64 bit filesizes.
651 ********************************************************************/
652
653 SMB_OFF_T sys_ftell(FILE *fp)
654 {
655 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELL64)
656         return (SMB_OFF_T)ftell64(fp);
657 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FTELLO64)
658         return (SMB_OFF_T)ftello64(fp);
659 #else
660         return (SMB_OFF_T)ftell(fp);
661 #endif
662 }
663
664 /*******************************************************************
665  A creat() wrapper that will deal with 64 bit filesizes.
666 ********************************************************************/
667
668 int sys_creat(const char *path, mode_t mode)
669 {
670 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CREAT64)
671         return creat64(path, mode);
672 #else
673         /*
674          * If creat64 isn't defined then ensure we call a potential open64.
675          * JRA.
676          */
677         return sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
678 #endif
679 }
680
681 /*******************************************************************
682  An open() wrapper that will deal with 64 bit filesizes.
683 ********************************************************************/
684
685 int sys_open(const char *path, int oflag, mode_t mode)
686 {
687 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPEN64)
688         return open64(path, oflag, mode);
689 #else
690         return open(path, oflag, mode);
691 #endif
692 }
693
694 /*******************************************************************
695  An fopen() wrapper that will deal with 64 bit filesizes.
696 ********************************************************************/
697
698 FILE *sys_fopen(const char *path, const char *type)
699 {
700 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_FOPEN64)
701         return fopen64(path, type);
702 #else
703         return fopen(path, type);
704 #endif
705 }
706
707
708 /*******************************************************************
709  A flock() wrapper that will perform the kernel flock.
710 ********************************************************************/
711
712 void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
713 {
714 #if HAVE_KERNEL_SHARE_MODES
715         int kernel_mode = 0;
716         if (share_mode == FILE_SHARE_WRITE) {
717                 kernel_mode = LOCK_MAND|LOCK_WRITE;
718         } else if (share_mode == FILE_SHARE_READ) {
719                 kernel_mode = LOCK_MAND|LOCK_READ;
720         } else if (share_mode == FILE_SHARE_NONE) {
721                 kernel_mode = LOCK_MAND;
722         }
723         if (kernel_mode) {
724                 flock(fd, kernel_mode);
725         }
726 #endif
727         ;
728 }
729
730
731
732 /*******************************************************************
733  An opendir wrapper that will deal with 64 bit filesizes.
734 ********************************************************************/
735
736 SMB_STRUCT_DIR *sys_opendir(const char *name)
737 {
738 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OPENDIR64)
739         return opendir64(name);
740 #else
741         return opendir(name);
742 #endif
743 }
744
745 /*******************************************************************
746  A readdir wrapper that will deal with 64 bit filesizes.
747 ********************************************************************/
748
749 SMB_STRUCT_DIRENT *sys_readdir(SMB_STRUCT_DIR *dirp)
750 {
751 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_READDIR64)
752         return readdir64(dirp);
753 #else
754         return readdir(dirp);
755 #endif
756 }
757
758 /*******************************************************************
759  A seekdir wrapper that will deal with 64 bit filesizes.
760 ********************************************************************/
761
762 void sys_seekdir(SMB_STRUCT_DIR *dirp, long offset)
763 {
764 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_SEEKDIR64)
765         seekdir64(dirp, offset);
766 #else
767         seekdir(dirp, offset);
768 #endif
769 }
770
771 /*******************************************************************
772  A telldir wrapper that will deal with 64 bit filesizes.
773 ********************************************************************/
774
775 long sys_telldir(SMB_STRUCT_DIR *dirp)
776 {
777 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_TELLDIR64)
778         return (long)telldir64(dirp);
779 #else
780         return (long)telldir(dirp);
781 #endif
782 }
783
784 /*******************************************************************
785  A rewinddir wrapper that will deal with 64 bit filesizes.
786 ********************************************************************/
787
788 void sys_rewinddir(SMB_STRUCT_DIR *dirp)
789 {
790 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_REWINDDIR64)
791         rewinddir64(dirp);
792 #else
793         rewinddir(dirp);
794 #endif
795 }
796
797 /*******************************************************************
798  A close wrapper that will deal with 64 bit filesizes.
799 ********************************************************************/
800
801 int sys_closedir(SMB_STRUCT_DIR *dirp)
802 {
803 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_CLOSEDIR64)
804         return closedir64(dirp);
805 #else
806         return closedir(dirp);
807 #endif
808 }
809
810 /*******************************************************************
811  An mknod() wrapper that will deal with 64 bit filesizes.
812 ********************************************************************/
813
814 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
815 {
816 #if defined(HAVE_MKNOD) || defined(HAVE_MKNOD64)
817 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_MKNOD64) && defined(HAVE_DEV64_T)
818         return mknod64(path, mode, dev);
819 #else
820         return mknod(path, mode, dev);
821 #endif
822 #else
823         /* No mknod system call. */
824         errno = ENOSYS;
825         return -1;
826 #endif
827 }
828
829 /*******************************************************************
830 The wait() calls vary between systems
831 ********************************************************************/
832
833 int sys_waitpid(pid_t pid,int *status,int options)
834 {
835 #ifdef HAVE_WAITPID
836         return waitpid(pid,status,options);
837 #else /* HAVE_WAITPID */
838         return wait4(pid, status, options, NULL);
839 #endif /* HAVE_WAITPID */
840 }
841
842 /*******************************************************************
843  System wrapper for getwd
844 ********************************************************************/
845
846 char *sys_getwd(char *s)
847 {
848         char *wd;
849 #ifdef HAVE_GETCWD
850         wd = (char *)getcwd(s, PATH_MAX);
851 #else
852         wd = (char *)getwd(s);
853 #endif
854         return wd;
855 }
856
857 #if defined(HAVE_POSIX_CAPABILITIES)
858
859 /**************************************************************************
860  Try and abstract process capabilities (for systems that have them).
861 ****************************************************************************/
862
863 /* Set the POSIX capabilities needed for the given purpose into the effective
864  * capability set of the current process. Make sure they are always removed
865  * from the inheritable set, because there is no circumstance in which our
866  * children should inherit our elevated privileges.
867  */
868 static bool set_process_capability(enum smbd_capability capability,
869                                    bool enable)
870 {
871         cap_value_t cap_vals[2] = {0};
872         int num_cap_vals = 0;
873
874         cap_t cap;
875
876 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
877         /* On Linux, make sure that any capabilities we grab are sticky
878          * across UID changes. We expect that this would allow us to keep both
879          * the effective and permitted capability sets, but as of circa 2.6.16,
880          * only the permitted set is kept. It is a bug (which we work around)
881          * that the effective set is lost, but we still require the effective
882          * set to be kept.
883          */
884         if (!prctl(PR_GET_KEEPCAPS)) {
885                 prctl(PR_SET_KEEPCAPS, 1);
886         }
887 #endif
888
889         cap = cap_get_proc();
890         if (cap == NULL) {
891                 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
892                         strerror(errno)));
893                 return False;
894         }
895
896         switch (capability) {
897                 case KERNEL_OPLOCK_CAPABILITY:
898 #ifdef CAP_NETWORK_MGT
899                         /* IRIX has CAP_NETWORK_MGT for oplocks. */
900                         cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
901 #endif
902                         break;
903                 case DMAPI_ACCESS_CAPABILITY:
904 #ifdef CAP_DEVICE_MGT
905                         /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
906                         cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
907 #elif CAP_MKNOD
908                         /* Linux has CAP_MKNOD for DMAPI access. */
909                         cap_vals[num_cap_vals++] = CAP_MKNOD;
910 #endif
911                         break;
912                 case LEASE_CAPABILITY:
913 #ifdef CAP_LEASE
914                         cap_vals[num_cap_vals++] = CAP_LEASE;
915 #endif
916                         break;
917         }
918
919         SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
920
921         if (num_cap_vals == 0) {
922                 cap_free(cap);
923                 return True;
924         }
925
926         cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
927                 enable ? CAP_SET : CAP_CLEAR);
928
929         /* We never want to pass capabilities down to our children, so make
930          * sure they are not inherited.
931          */
932         cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
933
934         if (cap_set_proc(cap) == -1) {
935                 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
936                         strerror(errno)));
937                 cap_free(cap);
938                 return False;
939         }
940
941         cap_free(cap);
942         return True;
943 }
944
945 #endif /* HAVE_POSIX_CAPABILITIES */
946
947 /****************************************************************************
948  Gain the oplock capability from the kernel if possible.
949 ****************************************************************************/
950
951 void set_effective_capability(enum smbd_capability capability)
952 {
953 #if defined(HAVE_POSIX_CAPABILITIES)
954         set_process_capability(capability, True);
955 #endif /* HAVE_POSIX_CAPABILITIES */
956 }
957
958 void drop_effective_capability(enum smbd_capability capability)
959 {
960 #if defined(HAVE_POSIX_CAPABILITIES)
961         set_process_capability(capability, False);
962 #endif /* HAVE_POSIX_CAPABILITIES */
963 }
964
965 /**************************************************************************
966  Wrapper for random().
967 ****************************************************************************/
968
969 long sys_random(void)
970 {
971 #if defined(HAVE_RANDOM)
972         return (long)random();
973 #elif defined(HAVE_RAND)
974         return (long)rand();
975 #else
976         DEBUG(0,("Error - no random function available !\n"));
977         exit(1);
978 #endif
979 }
980
981 /**************************************************************************
982  Wrapper for srandom().
983 ****************************************************************************/
984
985 void sys_srandom(unsigned int seed)
986 {
987 #if defined(HAVE_SRANDOM)
988         srandom(seed);
989 #elif defined(HAVE_SRAND)
990         srand(seed);
991 #else
992         DEBUG(0,("Error - no srandom function available !\n"));
993         exit(1);
994 #endif
995 }
996
997 /**************************************************************************
998  Returns equivalent to NGROUPS_MAX - using sysconf if needed.
999 ****************************************************************************/
1000
1001 int groups_max(void)
1002 {
1003 #if defined(SYSCONF_SC_NGROUPS_MAX)
1004         int ret = sysconf(_SC_NGROUPS_MAX);
1005         return (ret == -1) ? NGROUPS_MAX : ret;
1006 #else
1007         return NGROUPS_MAX;
1008 #endif
1009 }
1010
1011 /**************************************************************************
1012  Wrap setgroups and getgroups for systems that declare getgroups() as
1013  returning an array of gid_t, but actuall return an array of int.
1014 ****************************************************************************/
1015
1016 #if defined(HAVE_BROKEN_GETGROUPS)
1017 static int sys_broken_getgroups(int setlen, gid_t *gidset)
1018 {
1019         GID_T gid;
1020         GID_T *group_list;
1021         int i, ngroups;
1022
1023         if(setlen == 0) {
1024                 return getgroups(setlen, &gid);
1025         }
1026
1027         /*
1028          * Broken case. We need to allocate a
1029          * GID_T array of size setlen.
1030          */
1031
1032         if(setlen < 0) {
1033                 errno = EINVAL; 
1034                 return -1;
1035         } 
1036
1037         if (setlen == 0)
1038                 setlen = groups_max();
1039
1040         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
1041                 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
1042                 return -1;
1043         }
1044
1045         if((ngroups = getgroups(setlen, group_list)) < 0) {
1046                 int saved_errno = errno;
1047                 SAFE_FREE(group_list);
1048                 errno = saved_errno;
1049                 return -1;
1050         }
1051
1052         for(i = 0; i < ngroups; i++)
1053                 gidset[i] = (gid_t)group_list[i];
1054
1055         SAFE_FREE(group_list);
1056         return ngroups;
1057 }
1058
1059 static int sys_broken_setgroups(int setlen, gid_t *gidset)
1060 {
1061         GID_T *group_list;
1062         int i ; 
1063
1064         if (setlen == 0)
1065                 return 0 ;
1066
1067         if (setlen < 0 || setlen > groups_max()) {
1068                 errno = EINVAL; 
1069                 return -1;   
1070         }
1071
1072         /*
1073          * Broken case. We need to allocate a
1074          * GID_T array of size setlen.
1075          */
1076
1077         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
1078                 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
1079                 return -1;    
1080         }
1081
1082         for(i = 0; i < setlen; i++) 
1083                 group_list[i] = (GID_T) gidset[i]; 
1084
1085         if(setgroups(setlen, group_list) != 0) {
1086                 int saved_errno = errno;
1087                 SAFE_FREE(group_list);
1088                 errno = saved_errno;
1089                 return -1;
1090         }
1091
1092         SAFE_FREE(group_list);
1093         return 0 ;
1094 }
1095
1096 #endif /* HAVE_BROKEN_GETGROUPS */
1097
1098 /* This is a list of systems that require the first GID passed to setgroups(2)
1099  * to be the effective GID. If your system is one of these, add it here.
1100  */
1101 #if defined (FREEBSD) || defined (DARWINOS)
1102 #define USE_BSD_SETGROUPS
1103 #endif
1104
1105 #if defined(USE_BSD_SETGROUPS)
1106 /* Depending on the particular BSD implementation, the first GID that is
1107  * passed to setgroups(2) will either be ignored or will set the credential's
1108  * effective GID. In either case, the right thing to do is to guarantee that
1109  * gidset[0] is the effective GID.
1110  */
1111 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
1112 {
1113         gid_t *new_gidset = NULL;
1114         int max;
1115         int ret;
1116
1117         /* setgroups(2) will fail with EINVAL if we pass too many groups. */
1118         max = groups_max();
1119
1120         /* No group list, just make sure we are setting the efective GID. */
1121         if (setlen == 0) {
1122                 return setgroups(1, &primary_gid);
1123         }
1124
1125         /* If the primary gid is not the first array element, grow the array
1126          * and insert it at the front.
1127          */
1128         if (gidset[0] != primary_gid) {
1129                 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
1130                 if (new_gidset == NULL) {
1131                         return -1;
1132                 }
1133
1134                 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
1135                 new_gidset[0] = primary_gid;
1136                 setlen++;
1137         }
1138
1139         if (setlen > max) {
1140                 DEBUG(3, ("forced to truncate group list from %d to %d\n",
1141                         setlen, max));
1142                 setlen = max;
1143         }
1144
1145 #if defined(HAVE_BROKEN_GETGROUPS)
1146         ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
1147 #else
1148         ret = setgroups(setlen, new_gidset ? new_gidset : gidset);
1149 #endif
1150
1151         if (new_gidset) {
1152                 int errsav = errno;
1153                 SAFE_FREE(new_gidset);
1154                 errno = errsav;
1155         }
1156
1157         return ret;
1158 }
1159
1160 #endif /* USE_BSD_SETGROUPS */
1161
1162 /**************************************************************************
1163  Wrapper for getgroups. Deals with broken (int) case.
1164 ****************************************************************************/
1165
1166 int sys_getgroups(int setlen, gid_t *gidset)
1167 {
1168 #if defined(HAVE_BROKEN_GETGROUPS)
1169         return sys_broken_getgroups(setlen, gidset);
1170 #else
1171         return getgroups(setlen, gidset);
1172 #endif
1173 }
1174
1175 /**************************************************************************
1176  Wrapper for setgroups. Deals with broken (int) case and BSD case.
1177 ****************************************************************************/
1178
1179 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
1180 {
1181 #if !defined(HAVE_SETGROUPS)
1182         errno = ENOSYS;
1183         return -1;
1184 #endif /* HAVE_SETGROUPS */
1185
1186 #if defined(USE_BSD_SETGROUPS)
1187         return sys_bsd_setgroups(primary_gid, setlen, gidset);
1188 #elif defined(HAVE_BROKEN_GETGROUPS)
1189         return sys_broken_setgroups(setlen, gidset);
1190 #else
1191         return setgroups(setlen, gidset);
1192 #endif
1193 }
1194
1195 /**************************************************************************
1196  Wrappers for setpwent(), getpwent() and endpwent()
1197 ****************************************************************************/
1198
1199 void sys_setpwent(void)
1200 {
1201         setpwent();
1202 }
1203
1204 struct passwd *sys_getpwent(void)
1205 {
1206         return getpwent();
1207 }
1208
1209 void sys_endpwent(void)
1210 {
1211         endpwent();
1212 }
1213
1214 /**************************************************************************
1215  Wrappers for getpwnam(), getpwuid(), getgrnam(), getgrgid()
1216 ****************************************************************************/
1217
1218
1219 struct passwd *sys_getpwnam(const char *name)
1220 {
1221         return getpwnam(name);
1222 }
1223
1224 struct passwd *sys_getpwuid(uid_t uid)
1225 {
1226         return getpwuid(uid);
1227 }
1228
1229 struct group *sys_getgrnam(const char *name)
1230 {
1231         return getgrnam(name);
1232 }
1233
1234 struct group *sys_getgrgid(gid_t gid)
1235 {
1236         return getgrgid(gid);
1237 }
1238
1239 /**************************************************************************
1240  Extract a command into an arg list.
1241 ****************************************************************************/
1242
1243 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
1244 {
1245         char *trunc_cmd;
1246         char *saveptr;
1247         char *ptr;
1248         int argcl;
1249         char **argl = NULL;
1250         int i;
1251
1252         if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1253                 DEBUG(0, ("talloc failed\n"));
1254                 goto nomem;
1255         }
1256
1257         if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
1258                 TALLOC_FREE(trunc_cmd);
1259                 errno = EINVAL;
1260                 return NULL;
1261         }
1262
1263         /*
1264          * Count the args.
1265          */
1266
1267         for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1268                 argcl++;
1269
1270         TALLOC_FREE(trunc_cmd);
1271
1272         if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
1273                 goto nomem;
1274         }
1275
1276         /*
1277          * Now do the extraction.
1278          */
1279
1280         if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1281                 goto nomem;
1282         }
1283
1284         ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1285         i = 0;
1286
1287         if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1288                 goto nomem;
1289         }
1290
1291         while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1292
1293                 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1294                         goto nomem;
1295                 }
1296         }
1297
1298         argl[i++] = NULL;
1299         TALLOC_FREE(trunc_cmd);
1300         return argl;
1301
1302  nomem:
1303         DEBUG(0, ("talloc failed\n"));
1304         TALLOC_FREE(trunc_cmd);
1305         TALLOC_FREE(argl);
1306         errno = ENOMEM;
1307         return NULL;
1308 }
1309
1310 /**************************************************************************
1311  Wrapper for popen. Safer as it doesn't search a path.
1312  Modified from the glibc sources.
1313  modified by tridge to return a file descriptor. We must kick our FILE* habit
1314 ****************************************************************************/
1315
1316 typedef struct _popen_list
1317 {
1318         int fd;
1319         pid_t child_pid;
1320         struct _popen_list *next;
1321 } popen_list;
1322
1323 static popen_list *popen_chain;
1324
1325 int sys_popen(const char *command)
1326 {
1327         int parent_end, child_end;
1328         int pipe_fds[2];
1329         popen_list *entry = NULL;
1330         char **argl = NULL;
1331
1332         if (pipe(pipe_fds) < 0)
1333                 return -1;
1334
1335         parent_end = pipe_fds[0];
1336         child_end = pipe_fds[1];
1337
1338         if (!*command) {
1339                 errno = EINVAL;
1340                 goto err_exit;
1341         }
1342
1343         if((entry = SMB_MALLOC_P(popen_list)) == NULL)
1344                 goto err_exit;
1345
1346         ZERO_STRUCTP(entry);
1347
1348         /*
1349          * Extract the command and args into a NULL terminated array.
1350          */
1351
1352         if(!(argl = extract_args(NULL, command)))
1353                 goto err_exit;
1354
1355         entry->child_pid = sys_fork();
1356
1357         if (entry->child_pid == -1) {
1358                 goto err_exit;
1359         }
1360
1361         if (entry->child_pid == 0) {
1362
1363                 /*
1364                  * Child !
1365                  */
1366
1367                 int child_std_end = STDOUT_FILENO;
1368                 popen_list *p;
1369
1370                 close(parent_end);
1371                 if (child_end != child_std_end) {
1372                         dup2 (child_end, child_std_end);
1373                         close (child_end);
1374                 }
1375
1376                 /*
1377                  * POSIX.2:  "popen() shall ensure that any streams from previous
1378                  * popen() calls that remain open in the parent process are closed
1379                  * in the new child process."
1380                  */
1381
1382                 for (p = popen_chain; p; p = p->next)
1383                         close(p->fd);
1384
1385                 execv(argl[0], argl);
1386                 _exit (127);
1387         }
1388
1389         /*
1390          * Parent.
1391          */
1392
1393         close (child_end);
1394         TALLOC_FREE(argl);
1395
1396         /* Link into popen_chain. */
1397         entry->next = popen_chain;
1398         popen_chain = entry;
1399         entry->fd = parent_end;
1400
1401         return entry->fd;
1402
1403 err_exit:
1404
1405         SAFE_FREE(entry);
1406         SAFE_FREE(argl);
1407         close(pipe_fds[0]);
1408         close(pipe_fds[1]);
1409         return -1;
1410 }
1411
1412 /**************************************************************************
1413  Wrapper for pclose. Modified from the glibc sources.
1414 ****************************************************************************/
1415
1416 int sys_pclose(int fd)
1417 {
1418         int wstatus;
1419         popen_list **ptr = &popen_chain;
1420         popen_list *entry = NULL;
1421         pid_t wait_pid;
1422         int status = -1;
1423
1424         /* Unlink from popen_chain. */
1425         for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1426                 if ((*ptr)->fd == fd) {
1427                         entry = *ptr;
1428                         *ptr = (*ptr)->next;
1429                         status = 0;
1430                         break;
1431                 }
1432         }
1433
1434         if (status < 0 || close(entry->fd) < 0)
1435                 return -1;
1436
1437         /*
1438          * As Samba is catching and eating child process
1439          * exits we don't really care about the child exit
1440          * code, a -1 with errno = ECHILD will do fine for us.
1441          */
1442
1443         do {
1444                 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1445         } while (wait_pid == -1 && errno == EINTR);
1446
1447         SAFE_FREE(entry);
1448
1449         if (wait_pid == -1)
1450                 return -1;
1451         return wstatus;
1452 }
1453
1454 /**************************************************************************
1455  Wrapper for Admin Logs.
1456 ****************************************************************************/
1457
1458  void sys_adminlog(int priority, const char *format_str, ...) 
1459 {
1460         va_list ap;
1461         int ret;
1462         char *msgbuf = NULL;
1463
1464         va_start( ap, format_str );
1465         ret = vasprintf( &msgbuf, format_str, ap );
1466         va_end( ap );
1467
1468         if (ret == -1)
1469                 return;
1470
1471 #if defined(HAVE_SYSLOG)
1472         syslog( priority, "%s", msgbuf );
1473 #else
1474         DEBUG(0,("%s", msgbuf ));
1475 #endif
1476         SAFE_FREE(msgbuf);
1477 }
1478
1479 /******** Solaris EA helper function prototypes ********/
1480 #ifdef HAVE_ATTROPEN
1481 #define SOLARIS_ATTRMODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
1482 static int solaris_write_xattr(int attrfd, const char *value, size_t size);
1483 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size);
1484 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size);
1485 static int solaris_unlinkat(int attrdirfd, const char *name);
1486 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode);
1487 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode);
1488 #endif
1489
1490 /**************************************************************************
1491  Wrappers for extented attribute calls. Based on the Linux package with
1492  support for IRIX and (Net|Free)BSD also. Expand as other systems have them.
1493 ****************************************************************************/
1494
1495 ssize_t sys_getxattr (const char *path, const char *name, void *value, size_t size)
1496 {
1497 #if defined(HAVE_GETXATTR)
1498 #ifndef XATTR_ADD_OPT
1499         return getxattr(path, name, value, size);
1500 #else
1501         int options = 0;
1502         return getxattr(path, name, value, size, 0, options);
1503 #endif
1504 #elif defined(HAVE_GETEA)
1505         return getea(path, name, value, size);
1506 #elif defined(HAVE_EXTATTR_GET_FILE)
1507         char *s;
1508         ssize_t retval;
1509         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1510                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1511         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1512         /*
1513          * The BSD implementation has a nasty habit of silently truncating
1514          * the returned value to the size of the buffer, so we have to check
1515          * that the buffer is large enough to fit the returned value.
1516          */
1517         if((retval=extattr_get_file(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1518                 if(retval > size) {
1519                         errno = ERANGE;
1520                         return -1;
1521                 }
1522                 if((retval=extattr_get_file(path, attrnamespace, attrname, value, size)) >= 0)
1523                         return retval;
1524         }
1525
1526         DEBUG(10,("sys_getxattr: extattr_get_file() failed with: %s\n", strerror(errno)));
1527         return -1;
1528 #elif defined(HAVE_ATTR_GET)
1529         int retval, flags = 0;
1530         int valuelength = (int)size;
1531         char *attrname = strchr(name,'.') + 1;
1532
1533         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1534
1535         retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1536
1537         return retval ? retval : valuelength;
1538 #elif defined(HAVE_ATTROPEN)
1539         ssize_t ret = -1;
1540         int attrfd = solaris_attropen(path, name, O_RDONLY, 0);
1541         if (attrfd >= 0) {
1542                 ret = solaris_read_xattr(attrfd, value, size);
1543                 close(attrfd);
1544         }
1545         return ret;
1546 #else
1547         errno = ENOSYS;
1548         return -1;
1549 #endif
1550 }
1551
1552 ssize_t sys_lgetxattr (const char *path, const char *name, void *value, size_t size)
1553 {
1554 #if defined(HAVE_LGETXATTR)
1555         return lgetxattr(path, name, value, size);
1556 #elif defined(HAVE_GETXATTR) && defined(XATTR_ADD_OPT)
1557         int options = XATTR_NOFOLLOW;
1558         return getxattr(path, name, value, size, 0, options);
1559 #elif defined(HAVE_LGETEA)
1560         return lgetea(path, name, value, size);
1561 #elif defined(HAVE_EXTATTR_GET_LINK)
1562         char *s;
1563         ssize_t retval;
1564         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1565                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1566         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1567
1568         if((retval=extattr_get_link(path, attrnamespace, attrname, NULL, 0)) >= 0) {
1569                 if(retval > size) {
1570                         errno = ERANGE;
1571                         return -1;
1572                 }
1573                 if((retval=extattr_get_link(path, attrnamespace, attrname, value, size)) >= 0)
1574                         return retval;
1575         }
1576
1577         DEBUG(10,("sys_lgetxattr: extattr_get_link() failed with: %s\n", strerror(errno)));
1578         return -1;
1579 #elif defined(HAVE_ATTR_GET)
1580         int retval, flags = ATTR_DONTFOLLOW;
1581         int valuelength = (int)size;
1582         char *attrname = strchr(name,'.') + 1;
1583
1584         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1585
1586         retval = attr_get(path, attrname, (char *)value, &valuelength, flags);
1587
1588         return retval ? retval : valuelength;
1589 #elif defined(HAVE_ATTROPEN)
1590         ssize_t ret = -1;
1591         int attrfd = solaris_attropen(path, name, O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1592         if (attrfd >= 0) {
1593                 ret = solaris_read_xattr(attrfd, value, size);
1594                 close(attrfd);
1595         }
1596         return ret;
1597 #else
1598         errno = ENOSYS;
1599         return -1;
1600 #endif
1601 }
1602
1603 ssize_t sys_fgetxattr (int filedes, const char *name, void *value, size_t size)
1604 {
1605 #if defined(HAVE_FGETXATTR)
1606 #ifndef XATTR_ADD_OPT
1607         return fgetxattr(filedes, name, value, size);
1608 #else
1609         int options = 0;
1610         return fgetxattr(filedes, name, value, size, 0, options);
1611 #endif
1612 #elif defined(HAVE_FGETEA)
1613         return fgetea(filedes, name, value, size);
1614 #elif defined(HAVE_EXTATTR_GET_FD)
1615         char *s;
1616         ssize_t retval;
1617         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1618                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1619         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1620
1621         if((retval=extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0)) >= 0) {
1622                 if(retval > size) {
1623                         errno = ERANGE;
1624                         return -1;
1625                 }
1626                 if((retval=extattr_get_fd(filedes, attrnamespace, attrname, value, size)) >= 0)
1627                         return retval;
1628         }
1629
1630         DEBUG(10,("sys_fgetxattr: extattr_get_fd() failed with: %s\n", strerror(errno)));
1631         return -1;
1632 #elif defined(HAVE_ATTR_GETF)
1633         int retval, flags = 0;
1634         int valuelength = (int)size;
1635         char *attrname = strchr(name,'.') + 1;
1636
1637         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1638
1639         retval = attr_getf(filedes, attrname, (char *)value, &valuelength, flags);
1640
1641         return retval ? retval : valuelength;
1642 #elif defined(HAVE_ATTROPEN)
1643         ssize_t ret = -1;
1644         int attrfd = solaris_openat(filedes, name, O_RDONLY|O_XATTR, 0);
1645         if (attrfd >= 0) {
1646                 ret = solaris_read_xattr(attrfd, value, size);
1647                 close(attrfd);
1648         }
1649         return ret;
1650 #else
1651         errno = ENOSYS;
1652         return -1;
1653 #endif
1654 }
1655
1656 #if defined(HAVE_EXTATTR_LIST_FILE)
1657
1658 #define EXTATTR_PREFIX(s)       (s), (sizeof((s))-1)
1659
1660 static struct {
1661         int space;
1662         const char *name;
1663         size_t len;
1664
1665 extattr[] = {
1666         { EXTATTR_NAMESPACE_SYSTEM, EXTATTR_PREFIX("system.") },
1667         { EXTATTR_NAMESPACE_USER, EXTATTR_PREFIX("user.") },
1668 };
1669
1670 typedef union {
1671         const char *path;
1672         int filedes;
1673 } extattr_arg;
1674
1675 static ssize_t bsd_attr_list (int type, extattr_arg arg, char *list, size_t size)
1676 {
1677         ssize_t list_size, total_size = 0;
1678         int i, t, len;
1679         char *buf;
1680         /* Iterate through extattr(2) namespaces */
1681         for(t = 0; t < (sizeof(extattr)/sizeof(extattr[0])); t++) {
1682                 switch(type) {
1683 #if defined(HAVE_EXTATTR_LIST_FILE)
1684                         case 0:
1685                                 list_size = extattr_list_file(arg.path, extattr[t].space, list, size);
1686                                 break;
1687 #endif
1688 #if defined(HAVE_EXTATTR_LIST_LINK)
1689                         case 1:
1690                                 list_size = extattr_list_link(arg.path, extattr[t].space, list, size);
1691                                 break;
1692 #endif
1693 #if defined(HAVE_EXTATTR_LIST_FD)
1694                         case 2:
1695                                 list_size = extattr_list_fd(arg.filedes, extattr[t].space, list, size);
1696                                 break;
1697 #endif
1698                         default:
1699                                 errno = ENOSYS;
1700                                 return -1;
1701                 }
1702                 /* Some error happend. Errno should be set by the previous call */
1703                 if(list_size < 0)
1704                         return -1;
1705                 /* No attributes */
1706                 if(list_size == 0)
1707                         continue;
1708                 /* XXX: Call with an empty buffer may be used to calculate
1709                    necessary buffer size. Unfortunately, we can't say, how
1710                    many attributes were returned, so here is the potential
1711                    problem with the emulation.
1712                 */
1713                 if(list == NULL) {
1714                         /* Take the worse case of one char attribute names - 
1715                            two bytes per name plus one more for sanity.
1716                         */
1717                         total_size += list_size + (list_size/2 + 1)*extattr[t].len;
1718                         continue;
1719                 }
1720                 /* Count necessary offset to fit namespace prefixes */
1721                 len = 0;
1722                 for(i = 0; i < list_size; i += list[i] + 1)
1723                         len += extattr[t].len;
1724
1725                 total_size += list_size + len;
1726                 /* Buffer is too small to fit the results */
1727                 if(total_size > size) {
1728                         errno = ERANGE;
1729                         return -1;
1730                 }
1731                 /* Shift results back, so we can prepend prefixes */
1732                 buf = memmove(list + len, list, list_size);
1733
1734                 for(i = 0; i < list_size; i += len + 1) {
1735                         len = buf[i];
1736                         strncpy(list, extattr[t].name, extattr[t].len + 1);
1737                         list += extattr[t].len;
1738                         strncpy(list, buf + i + 1, len);
1739                         list[len] = '\0';
1740                         list += len + 1;
1741                 }
1742                 size -= total_size;
1743         }
1744         return total_size;
1745 }
1746
1747 #endif
1748
1749 #if defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1750 static char attr_buffer[ATTR_MAX_VALUELEN];
1751
1752 static ssize_t irix_attr_list(const char *path, int filedes, char *list, size_t size, int flags)
1753 {
1754         int retval = 0, index;
1755         attrlist_cursor_t *cursor = 0;
1756         int total_size = 0;
1757         attrlist_t * al = (attrlist_t *)attr_buffer;
1758         attrlist_ent_t *ae;
1759         size_t ent_size, left = size;
1760         char *bp = list;
1761
1762         while (True) {
1763             if (filedes)
1764                 retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1765             else
1766                 retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1767             if (retval) break;
1768             for (index = 0; index < al->al_count; index++) {
1769                 ae = ATTR_ENTRY(attr_buffer, index);
1770                 ent_size = strlen(ae->a_name) + sizeof("user.");
1771                 if (left >= ent_size) {
1772                     strncpy(bp, "user.", sizeof("user."));
1773                     strncat(bp, ae->a_name, ent_size - sizeof("user."));
1774                     bp += ent_size;
1775                     left -= ent_size;
1776                 } else if (size) {
1777                     errno = ERANGE;
1778                     retval = -1;
1779                     break;
1780                 }
1781                 total_size += ent_size;
1782             }
1783             if (al->al_more == 0) break;
1784         }
1785         if (retval == 0) {
1786             flags |= ATTR_ROOT;
1787             cursor = 0;
1788             while (True) {
1789                 if (filedes)
1790                     retval = attr_listf(filedes, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1791                 else
1792                     retval = attr_list(path, attr_buffer, ATTR_MAX_VALUELEN, flags, cursor);
1793                 if (retval) break;
1794                 for (index = 0; index < al->al_count; index++) {
1795                     ae = ATTR_ENTRY(attr_buffer, index);
1796                     ent_size = strlen(ae->a_name) + sizeof("system.");
1797                     if (left >= ent_size) {
1798                         strncpy(bp, "system.", sizeof("system."));
1799                         strncat(bp, ae->a_name, ent_size - sizeof("system."));
1800                         bp += ent_size;
1801                         left -= ent_size;
1802                     } else if (size) {
1803                         errno = ERANGE;
1804                         retval = -1;
1805                         break;
1806                     }
1807                     total_size += ent_size;
1808                 }
1809                 if (al->al_more == 0) break;
1810             }
1811         }
1812         return (ssize_t)(retval ? retval : total_size);
1813 }
1814
1815 #endif
1816
1817 ssize_t sys_listxattr (const char *path, char *list, size_t size)
1818 {
1819 #if defined(HAVE_LISTXATTR)
1820 #ifndef XATTR_ADD_OPT
1821         return listxattr(path, list, size);
1822 #else
1823         int options = 0;
1824         return listxattr(path, list, size, options);
1825 #endif
1826 #elif defined(HAVE_LISTEA)
1827         return listea(path, list, size);
1828 #elif defined(HAVE_EXTATTR_LIST_FILE)
1829         extattr_arg arg;
1830         arg.path = path;
1831         return bsd_attr_list(0, arg, list, size);
1832 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1833         return irix_attr_list(path, 0, list, size, 0);
1834 #elif defined(HAVE_ATTROPEN)
1835         ssize_t ret = -1;
1836         int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1837         if (attrdirfd >= 0) {
1838                 ret = solaris_list_xattr(attrdirfd, list, size);
1839                 close(attrdirfd);
1840         }
1841         return ret;
1842 #else
1843         errno = ENOSYS;
1844         return -1;
1845 #endif
1846 }
1847
1848 ssize_t sys_llistxattr (const char *path, char *list, size_t size)
1849 {
1850 #if defined(HAVE_LLISTXATTR)
1851         return llistxattr(path, list, size);
1852 #elif defined(HAVE_LISTXATTR) && defined(XATTR_ADD_OPT)
1853         int options = XATTR_NOFOLLOW;
1854         return listxattr(path, list, size, options);
1855 #elif defined(HAVE_LLISTEA)
1856         return llistea(path, list, size);
1857 #elif defined(HAVE_EXTATTR_LIST_LINK)
1858         extattr_arg arg;
1859         arg.path = path;
1860         return bsd_attr_list(1, arg, list, size);
1861 #elif defined(HAVE_ATTR_LIST) && defined(HAVE_SYS_ATTRIBUTES_H)
1862         return irix_attr_list(path, 0, list, size, ATTR_DONTFOLLOW);
1863 #elif defined(HAVE_ATTROPEN)
1864         ssize_t ret = -1;
1865         int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1866         if (attrdirfd >= 0) {
1867                 ret = solaris_list_xattr(attrdirfd, list, size);
1868                 close(attrdirfd);
1869         }
1870         return ret;
1871 #else
1872         errno = ENOSYS;
1873         return -1;
1874 #endif
1875 }
1876
1877 ssize_t sys_flistxattr (int filedes, char *list, size_t size)
1878 {
1879 #if defined(HAVE_FLISTXATTR)
1880 #ifndef XATTR_ADD_OPT
1881         return flistxattr(filedes, list, size);
1882 #else
1883         int options = 0;
1884         return flistxattr(filedes, list, size, options);
1885 #endif
1886 #elif defined(HAVE_FLISTEA)
1887         return flistea(filedes, list, size);
1888 #elif defined(HAVE_EXTATTR_LIST_FD)
1889         extattr_arg arg;
1890         arg.filedes = filedes;
1891         return bsd_attr_list(2, arg, list, size);
1892 #elif defined(HAVE_ATTR_LISTF)
1893         return irix_attr_list(NULL, filedes, list, size, 0);
1894 #elif defined(HAVE_ATTROPEN)
1895         ssize_t ret = -1;
1896         int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
1897         if (attrdirfd >= 0) {
1898                 ret = solaris_list_xattr(attrdirfd, list, size);
1899                 close(attrdirfd);
1900         }
1901         return ret;
1902 #else
1903         errno = ENOSYS;
1904         return -1;
1905 #endif
1906 }
1907
1908 int sys_removexattr (const char *path, const char *name)
1909 {
1910 #if defined(HAVE_REMOVEXATTR)
1911 #ifndef XATTR_ADD_OPT
1912         return removexattr(path, name);
1913 #else
1914         int options = 0;
1915         return removexattr(path, name, options);
1916 #endif
1917 #elif defined(HAVE_REMOVEEA)
1918         return removeea(path, name);
1919 #elif defined(HAVE_EXTATTR_DELETE_FILE)
1920         char *s;
1921         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1922                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1923         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1924
1925         return extattr_delete_file(path, attrnamespace, attrname);
1926 #elif defined(HAVE_ATTR_REMOVE)
1927         int flags = 0;
1928         char *attrname = strchr(name,'.') + 1;
1929
1930         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1931
1932         return attr_remove(path, attrname, flags);
1933 #elif defined(HAVE_ATTROPEN)
1934         int ret = -1;
1935         int attrdirfd = solaris_attropen(path, ".", O_RDONLY, 0);
1936         if (attrdirfd >= 0) {
1937                 ret = solaris_unlinkat(attrdirfd, name);
1938                 close(attrdirfd);
1939         }
1940         return ret;
1941 #else
1942         errno = ENOSYS;
1943         return -1;
1944 #endif
1945 }
1946
1947 int sys_lremovexattr (const char *path, const char *name)
1948 {
1949 #if defined(HAVE_LREMOVEXATTR)
1950         return lremovexattr(path, name);
1951 #elif defined(HAVE_REMOVEXATTR) && defined(XATTR_ADD_OPT)
1952         int options = XATTR_NOFOLLOW;
1953         return removexattr(path, name, options);
1954 #elif defined(HAVE_LREMOVEEA)
1955         return lremoveea(path, name);
1956 #elif defined(HAVE_EXTATTR_DELETE_LINK)
1957         char *s;
1958         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1959                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1960         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
1961
1962         return extattr_delete_link(path, attrnamespace, attrname);
1963 #elif defined(HAVE_ATTR_REMOVE)
1964         int flags = ATTR_DONTFOLLOW;
1965         char *attrname = strchr(name,'.') + 1;
1966
1967         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
1968
1969         return attr_remove(path, attrname, flags);
1970 #elif defined(HAVE_ATTROPEN)
1971         int ret = -1;
1972         int attrdirfd = solaris_attropen(path, ".", O_RDONLY|AT_SYMLINK_NOFOLLOW, 0);
1973         if (attrdirfd >= 0) {
1974                 ret = solaris_unlinkat(attrdirfd, name);
1975                 close(attrdirfd);
1976         }
1977         return ret;
1978 #else
1979         errno = ENOSYS;
1980         return -1;
1981 #endif
1982 }
1983
1984 int sys_fremovexattr (int filedes, const char *name)
1985 {
1986 #if defined(HAVE_FREMOVEXATTR)
1987 #ifndef XATTR_ADD_OPT
1988         return fremovexattr(filedes, name);
1989 #else
1990         int options = 0;
1991         return fremovexattr(filedes, name, options);
1992 #endif
1993 #elif defined(HAVE_FREMOVEEA)
1994         return fremoveea(filedes, name);
1995 #elif defined(HAVE_EXTATTR_DELETE_FD)
1996         char *s;
1997         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
1998                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
1999         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2000
2001         return extattr_delete_fd(filedes, attrnamespace, attrname);
2002 #elif defined(HAVE_ATTR_REMOVEF)
2003         int flags = 0;
2004         char *attrname = strchr(name,'.') + 1;
2005
2006         if (strncmp(name, "system", 6) == 0) flags |= ATTR_ROOT;
2007
2008         return attr_removef(filedes, attrname, flags);
2009 #elif defined(HAVE_ATTROPEN)
2010         int ret = -1;
2011         int attrdirfd = solaris_openat(filedes, ".", O_RDONLY|O_XATTR, 0);
2012         if (attrdirfd >= 0) {
2013                 ret = solaris_unlinkat(attrdirfd, name);
2014                 close(attrdirfd);
2015         }
2016         return ret;
2017 #else
2018         errno = ENOSYS;
2019         return -1;
2020 #endif
2021 }
2022
2023 int sys_setxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2024 {
2025 #if defined(HAVE_SETXATTR)
2026 #ifndef XATTR_ADD_OPT
2027         return setxattr(path, name, value, size, flags);
2028 #else
2029         int options = 0;
2030         return setxattr(path, name, value, size, 0, options);
2031 #endif
2032 #elif defined(HAVE_SETEA)
2033         return setea(path, name, value, size, flags);
2034 #elif defined(HAVE_EXTATTR_SET_FILE)
2035         char *s;
2036         int retval = 0;
2037         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
2038                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2039         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2040         if (flags) {
2041                 /* Check attribute existence */
2042                 retval = extattr_get_file(path, attrnamespace, attrname, NULL, 0);
2043                 if (retval < 0) {
2044                         /* REPLACE attribute, that doesn't exist */
2045                         if (flags & XATTR_REPLACE && errno == ENOATTR) {
2046                                 errno = ENOATTR;
2047                                 return -1;
2048                         }
2049                         /* Ignore other errors */
2050                 }
2051                 else {
2052                         /* CREATE attribute, that already exists */
2053                         if (flags & XATTR_CREATE) {
2054                                 errno = EEXIST;
2055                                 return -1;
2056                         }
2057                 }
2058         }
2059         retval = extattr_set_file(path, attrnamespace, attrname, value, size);
2060         return (retval < 0) ? -1 : 0;
2061 #elif defined(HAVE_ATTR_SET)
2062         int myflags = 0;
2063         char *attrname = strchr(name,'.') + 1;
2064
2065         if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2066         if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2067         if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2068
2069         return attr_set(path, attrname, (const char *)value, size, myflags);
2070 #elif defined(HAVE_ATTROPEN)
2071         int ret = -1;
2072         int myflags = O_RDWR;
2073         int attrfd;
2074         if (flags & XATTR_CREATE) myflags |= O_EXCL;
2075         if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2076         attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2077         if (attrfd >= 0) {
2078                 ret = solaris_write_xattr(attrfd, value, size);
2079                 close(attrfd);
2080         }
2081         return ret;
2082 #else
2083         errno = ENOSYS;
2084         return -1;
2085 #endif
2086 }
2087
2088 int sys_lsetxattr (const char *path, const char *name, const void *value, size_t size, int flags)
2089 {
2090 #if defined(HAVE_LSETXATTR)
2091         return lsetxattr(path, name, value, size, flags);
2092 #elif defined(HAVE_SETXATTR) && defined(XATTR_ADD_OPT)
2093         int options = XATTR_NOFOLLOW;
2094         return setxattr(path, name, value, size, 0, options);
2095 #elif defined(LSETEA)
2096         return lsetea(path, name, value, size, flags);
2097 #elif defined(HAVE_EXTATTR_SET_LINK)
2098         char *s;
2099         int retval = 0;
2100         int attrnamespace = (strncmp(name, "system", 6) == 0) ? 
2101                 EXTATTR_NAMESPACE_SYSTEM : EXTATTR_NAMESPACE_USER;
2102         const char *attrname = ((s=strchr_m(name, '.')) == NULL) ? name : s + 1;
2103         if (flags) {
2104                 /* Check attribute existence */
2105                 retval = extattr_get_link(path, attrnamespace, attrname, NULL, 0);
2106                 if (retval < 0) {
2107                         /* REPLACE attribute, that doesn't exist */
2108                         if (flags & XATTR_REPLACE && errno == ENOATTR) {
2109                                 errno = ENOATTR;
2110                                 return -1;
2111                         }
2112                         /* Ignore other errors */
2113                 }
2114                 else {
2115                         /* CREATE attribute, that already exists */
2116                         if (flags & XATTR_CREATE) {
2117                                 errno = EEXIST;
2118                                 return -1;
2119                         }
2120                 }
2121         }
2122
2123         retval = extattr_set_link(path, attrnamespace, attrname, value, size);
2124         return (retval < 0) ? -1 : 0;
2125 #elif defined(HAVE_ATTR_SET)
2126         int myflags = ATTR_DONTFOLLOW;
2127         char *attrname = strchr(name,'.') + 1;
2128
2129         if (strncmp(name, "system", 6) == 0) myflags |= ATTR_ROOT;
2130         if (flags & XATTR_CREATE) myflags |= ATTR_CREATE;
2131         if (flags & XATTR_REPLACE) myflags |= ATTR_REPLACE;
2132
2133         return attr_set(path, attrname, (const char *)value, size, myflags);
2134 #elif defined(HAVE_ATTROPEN)
2135         int ret = -1;
2136         int myflags = O_RDWR | AT_SYMLINK_NOFOLLOW;
2137         int attrfd;
2138         if (flags & XATTR_CREATE) myflags |= O_EXCL;
2139         if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2140         attrfd = solaris_attropen(path, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2141         if (attrfd >= 0) {
2142                 ret = solaris_write_xattr(attrfd, value, size);
2143                 close(attrfd);
2144         }
2145         return ret;
2146 #else
2147         errno = ENOSYS;
2148         return -1;
2149 #endif
2150 }
2151
2152 int sys_fsetxattr (int filedes, const char *name, const void *value, size_t size, int flags)
2153 {
2154 #if defined(HAVE_FSETXATTR)
2155 #ifndef XATTR_ADD_OPT
2156         return fsetxattr(filedes, name, value, size, flags);
2157 #else
2158         int options = 0;
2159         return fsetxattr(filedes, name, value, size, 0, options);
2160 #endif
2161 #elif defined(HAVE_FSETEA)
2162         return fsetea(filedes, name, value, size, flags);
2163 #elif defined(HAVE_EXTATTR_SET_FD)
2164         char *s;
2165         int retval = 0;
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;
2169         if (flags) {
2170                 /* Check attribute existence */
2171                 retval = extattr_get_fd(filedes, attrnamespace, attrname, NULL, 0);
2172                 if (retval < 0) {
2173                         /* REPLACE attribute, that doesn't exist */
2174                         if (flags & XATTR_REPLACE && errno == ENOATTR) {
2175                                 errno = ENOATTR;
2176                                 return -1;
2177                         }
2178                         /* Ignore other errors */
2179                 }
2180                 else {
2181                         /* CREATE attribute, that already exists */
2182                         if (flags & XATTR_CREATE) {
2183                                 errno = EEXIST;
2184                                 return -1;
2185                         }
2186                 }
2187         }
2188         retval = extattr_set_fd(filedes, attrnamespace, attrname, value, size);
2189         return (retval < 0) ? -1 : 0;
2190 #elif defined(HAVE_ATTR_SETF)
2191         int myflags = 0;
2192         char *attrname = strchr(name,'.') + 1;
2193
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;
2197
2198         return attr_setf(filedes, attrname, (const char *)value, size, myflags);
2199 #elif defined(HAVE_ATTROPEN)
2200         int ret = -1;
2201         int myflags = O_RDWR | O_XATTR;
2202         int attrfd;
2203         if (flags & XATTR_CREATE) myflags |= O_EXCL;
2204         if (!(flags & XATTR_REPLACE)) myflags |= O_CREAT;
2205         attrfd = solaris_openat(filedes, name, myflags, (mode_t) SOLARIS_ATTRMODE);
2206         if (attrfd >= 0) {
2207                 ret = solaris_write_xattr(attrfd, value, size);
2208                 close(attrfd);
2209         }
2210         return ret;
2211 #else
2212         errno = ENOSYS;
2213         return -1;
2214 #endif
2215 }
2216
2217 /**************************************************************************
2218  helper functions for Solaris' EA support
2219 ****************************************************************************/
2220 #ifdef HAVE_ATTROPEN
2221 static ssize_t solaris_read_xattr(int attrfd, void *value, size_t size)
2222 {
2223         struct stat sbuf;
2224
2225         if (fstat(attrfd, &sbuf) == -1) {
2226                 errno = ENOATTR;
2227                 return -1;
2228         }
2229
2230         /* This is to return the current size of the named extended attribute */
2231         if (size == 0) {
2232                 return sbuf.st_size;
2233         }
2234
2235         /* check size and read xattr */
2236         if (sbuf.st_size > size) {
2237                 errno = ERANGE;
2238                 return -1;
2239         }
2240
2241         return read(attrfd, value, sbuf.st_size);
2242 }
2243
2244 static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
2245 {
2246         ssize_t len = 0;
2247         DIR *dirp;
2248         struct dirent *de;
2249         int newfd = dup(attrdirfd);
2250         /* CAUTION: The originating file descriptor should not be
2251                     used again following the call to fdopendir().
2252                     For that reason we dup() the file descriptor
2253                     here to make things more clear. */
2254         dirp = fdopendir(newfd);
2255
2256         while ((de = readdir(dirp))) {
2257                 size_t listlen = strlen(de->d_name);
2258                 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
2259                         /* we don't want "." and ".." here: */
2260                         DEBUG(10,("skipped EA %s\n",de->d_name));
2261                         continue;
2262                 }
2263
2264                 if (size == 0) {
2265                         /* return the current size of the list of extended attribute names*/
2266                         len += listlen + 1;
2267                 } else {
2268                         /* check size and copy entrieÑ• + nul into list. */
2269                         if ((len + listlen + 1) > size) {
2270                                 errno = ERANGE;
2271                                 len = -1;
2272                                 break;
2273                         } else {
2274                                 safe_strcpy(list + len, de->d_name, listlen);
2275                                 len += listlen;
2276                                 list[len] = '\0';
2277                                 ++len;
2278                         }
2279                 }
2280         }
2281
2282         if (closedir(dirp) == -1) {
2283                 DEBUG(0,("closedir dirp failed: %s\n",strerror(errno)));
2284                 return -1;
2285         }
2286         return len;
2287 }
2288
2289 static int solaris_unlinkat(int attrdirfd, const char *name)
2290 {
2291         if (unlinkat(attrdirfd, name, 0) == -1) {
2292                 if (errno == ENOENT) {
2293                         errno = ENOATTR;
2294                 }
2295                 return -1;
2296         }
2297         return 0;
2298 }
2299
2300 static int solaris_attropen(const char *path, const char *attrpath, int oflag, mode_t mode)
2301 {
2302         int filedes = attropen(path, attrpath, oflag, mode);
2303         if (filedes == -1) {
2304                 DEBUG(10,("attropen FAILED: path: %s, name: %s, errno: %s\n",path,attrpath,strerror(errno)));
2305                 if (errno == EINVAL) {
2306                         errno = ENOTSUP;
2307                 } else {
2308                         errno = ENOATTR;
2309                 }
2310         }
2311         return filedes;
2312 }
2313
2314 static int solaris_openat(int fildes, const char *path, int oflag, mode_t mode)
2315 {
2316         int filedes = openat(fildes, path, oflag, mode);
2317         if (filedes == -1) {
2318                 DEBUG(10,("openat FAILED: fd: %d, path: %s, errno: %s\n",filedes,path,strerror(errno)));
2319                 if (errno == EINVAL) {
2320                         errno = ENOTSUP;
2321                 } else {
2322                         errno = ENOATTR;
2323                 }
2324         }
2325         return filedes;
2326 }
2327
2328 static int solaris_write_xattr(int attrfd, const char *value, size_t size)
2329 {
2330         if ((ftruncate(attrfd, 0) == 0) && (write(attrfd, value, size) == size)) {
2331                 return 0;
2332         } else {
2333                 DEBUG(10,("solaris_write_xattr FAILED!\n"));
2334                 return -1;
2335         }
2336 }
2337 #endif /*HAVE_ATTROPEN*/
2338
2339
2340 /****************************************************************************
2341  Return the major devicenumber for UNIX extensions.
2342 ****************************************************************************/
2343
2344 uint32 unix_dev_major(SMB_DEV_T dev)
2345 {
2346 #if defined(HAVE_DEVICE_MAJOR_FN)
2347         return (uint32)major(dev);
2348 #else
2349         return (uint32)(dev >> 8);
2350 #endif
2351 }
2352
2353 /****************************************************************************
2354  Return the minor devicenumber for UNIX extensions.
2355 ****************************************************************************/
2356
2357 uint32 unix_dev_minor(SMB_DEV_T dev)
2358 {
2359 #if defined(HAVE_DEVICE_MINOR_FN)
2360         return (uint32)minor(dev);
2361 #else
2362         return (uint32)(dev & 0xff);
2363 #endif
2364 }
2365
2366 #if defined(WITH_AIO)
2367
2368 /*******************************************************************
2369  An aio_read wrapper that will deal with 64-bit sizes.
2370 ********************************************************************/
2371
2372 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2373 {
2374 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_READ64)
2375         return aio_read64(aiocb);
2376 #elif defined(HAVE_AIO_READ)
2377         return aio_read(aiocb);
2378 #else
2379         errno = ENOSYS;
2380         return -1;
2381 #endif
2382 }
2383
2384 /*******************************************************************
2385  An aio_write wrapper that will deal with 64-bit sizes.
2386 ********************************************************************/
2387
2388 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2389 {
2390 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_WRITE64)
2391         return aio_write64(aiocb);
2392 #elif defined(HAVE_AIO_WRITE)
2393         return aio_write(aiocb);
2394 #else
2395         errno = ENOSYS;
2396         return -1;
2397 #endif
2398 }
2399
2400 /*******************************************************************
2401  An aio_return wrapper that will deal with 64-bit sizes.
2402 ********************************************************************/
2403
2404 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2405 {
2406 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_RETURN64)
2407         return aio_return64(aiocb);
2408 #elif defined(HAVE_AIO_RETURN)
2409         return aio_return(aiocb);
2410 #else
2411         errno = ENOSYS;
2412         return -1;
2413 #endif
2414 }
2415
2416 /*******************************************************************
2417  An aio_cancel wrapper that will deal with 64-bit sizes.
2418 ********************************************************************/
2419
2420 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2421 {
2422 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_CANCEL64)
2423         return aio_cancel64(fd, aiocb);
2424 #elif defined(HAVE_AIO_CANCEL)
2425         return aio_cancel(fd, aiocb);
2426 #else
2427         errno = ENOSYS;
2428         return -1;
2429 #endif
2430 }
2431
2432 /*******************************************************************
2433  An aio_error wrapper that will deal with 64-bit sizes.
2434 ********************************************************************/
2435
2436 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2437 {
2438 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_ERROR64)
2439         return aio_error64(aiocb);
2440 #elif defined(HAVE_AIO_ERROR)
2441         return aio_error(aiocb);
2442 #else
2443         errno = ENOSYS;
2444         return -1;
2445 #endif
2446 }
2447
2448 /*******************************************************************
2449  An aio_fsync wrapper that will deal with 64-bit sizes.
2450 ********************************************************************/
2451
2452 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2453 {
2454 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_FSYNC64)
2455         return aio_fsync64(op, aiocb);
2456 #elif defined(HAVE_AIO_FSYNC)
2457         return aio_fsync(op, aiocb);
2458 #else
2459         errno = ENOSYS;
2460         return -1;
2461 #endif
2462 }
2463
2464 /*******************************************************************
2465  An aio_fsync wrapper that will deal with 64-bit sizes.
2466 ********************************************************************/
2467
2468 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2469 {
2470 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_AIOCB64) && defined(HAVE_AIO_SUSPEND64)
2471         return aio_suspend64(cblist, n, timeout);
2472 #elif defined(HAVE_AIO_FSYNC)
2473         return aio_suspend(cblist, n, timeout);
2474 #else
2475         errno = ENOSYS;
2476         return -1;
2477 #endif
2478 }
2479 #else /* !WITH_AIO */
2480
2481 int sys_aio_read(SMB_STRUCT_AIOCB *aiocb)
2482 {
2483         errno = ENOSYS;
2484         return -1;
2485 }
2486
2487 int sys_aio_write(SMB_STRUCT_AIOCB *aiocb)
2488 {
2489         errno = ENOSYS;
2490         return -1;
2491 }
2492
2493 ssize_t sys_aio_return(SMB_STRUCT_AIOCB *aiocb)
2494 {
2495         errno = ENOSYS;
2496         return -1;
2497 }
2498
2499 int sys_aio_cancel(int fd, SMB_STRUCT_AIOCB *aiocb)
2500 {
2501         errno = ENOSYS;
2502         return -1;
2503 }
2504
2505 int sys_aio_error(const SMB_STRUCT_AIOCB *aiocb)
2506 {
2507         errno = ENOSYS;
2508         return -1;
2509 }
2510
2511 int sys_aio_fsync(int op, SMB_STRUCT_AIOCB *aiocb)
2512 {
2513         errno = ENOSYS;
2514         return -1;
2515 }
2516
2517 int sys_aio_suspend(const SMB_STRUCT_AIOCB * const cblist[], int n, const struct timespec *timeout)
2518 {
2519         errno = ENOSYS;
2520         return -1;
2521 }
2522 #endif /* WITH_AIO */
2523
2524 int sys_getpeereid( int s, uid_t *uid)
2525 {
2526 #if defined(HAVE_PEERCRED)
2527         struct ucred cred;
2528         socklen_t cred_len = sizeof(struct ucred);
2529         int ret;
2530
2531         ret = getsockopt(s, SOL_SOCKET, SO_PEERCRED, (void *)&cred, &cred_len);
2532         if (ret != 0) {
2533                 return -1;
2534         }
2535
2536         if (cred_len != sizeof(struct ucred)) {
2537                 errno = EINVAL;
2538                 return -1;
2539         }
2540
2541         *uid = cred.uid;
2542         return 0;
2543 #else
2544         errno = ENOSYS;
2545         return -1;
2546 #endif
2547 }
2548
2549 int sys_getnameinfo(const struct sockaddr *psa,
2550                         socklen_t salen,
2551                         char *host,
2552                         size_t hostlen,
2553                         char *service,
2554                         size_t servlen,
2555                         int flags)
2556 {
2557         /*
2558          * For Solaris we must make sure salen is the
2559          * correct length for the incoming sa_family.
2560          */
2561
2562         if (salen == sizeof(struct sockaddr_storage)) {
2563                 salen = sizeof(struct sockaddr_in);
2564 #if defined(HAVE_IPV6)
2565                 if (psa->sa_family == AF_INET6) {
2566                         salen = sizeof(struct sockaddr_in6);
2567                 }
2568 #endif
2569         }
2570         return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
2571 }
2572
2573 int sys_connect(int fd, const struct sockaddr * addr)
2574 {
2575         socklen_t salen = -1;
2576
2577         if (addr->sa_family == AF_INET) {
2578             salen = sizeof(struct sockaddr_in);
2579         } else if (addr->sa_family == AF_UNIX) {
2580             salen = sizeof(struct sockaddr_un);
2581         }
2582 #if defined(HAVE_IPV6)
2583         else if (addr->sa_family == AF_INET6) {
2584             salen = sizeof(struct sockaddr_in6);
2585         }
2586 #endif
2587
2588         return connect(fd, addr, salen);
2589 }