s3: add st_ex_itime to struct stat_ex
[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 #include "system/syslog.h"
25 #include "system/capability.h"
26 #include "system/passwd.h"
27 #include "system/filesys.h"
28 #include "../lib/util/setid.h"
29
30 #ifdef HAVE_SYS_SYSCTL_H
31 #include <sys/sysctl.h>
32 #endif
33
34 #ifdef HAVE_SYS_PRCTL_H
35 #include <sys/prctl.h>
36 #endif
37
38 /*
39    The idea is that this file will eventually have wrappers around all
40    important system calls in samba. The aims are:
41
42    - to enable easier porting by putting OS dependent stuff in here
43
44    - to allow for hooks into other "pseudo-filesystems"
45
46    - to allow easier integration of things like the japanese extensions
47
48    - to support the philosophy of Samba to expose the features of
49      the OS within the SMB model. In general whatever file/printer/variable
50      expansions/etc make sense to the OS should be acceptable to Samba.
51 */
52
53 /*******************************************************************
54 A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
55 ********************************************************************/
56
57 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
58 {
59         ssize_t ret;
60
61         do {
62                 ret = send(s, msg, len, flags);
63         } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
64
65         return ret;
66 }
67
68 /*******************************************************************
69 A recvfrom wrapper that will deal with EINTR.
70 NB. As used with non-blocking sockets, return on EAGAIN/EWOULDBLOCK
71 ********************************************************************/
72
73 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
74 {
75         ssize_t ret;
76
77         do {
78                 ret = recvfrom(s, buf, len, flags, from, fromlen);
79         } while (ret == -1 && (errno == EINTR));
80         return ret;
81 }
82
83 /*******************************************************************
84 A fcntl wrapper that will deal with EINTR.
85 ********************************************************************/
86
87 int sys_fcntl_ptr(int fd, int cmd, void *arg)
88 {
89         int ret;
90
91         do {
92                 ret = fcntl(fd, cmd, arg);
93         } while (ret == -1 && errno == EINTR);
94         return ret;
95 }
96
97 /*******************************************************************
98 A fcntl wrapper that will deal with EINTR.
99 ********************************************************************/
100
101 int sys_fcntl_long(int fd, int cmd, long arg)
102 {
103         int ret;
104
105         do {
106                 ret = fcntl(fd, cmd, arg);
107         } while (ret == -1 && errno == EINTR);
108         return ret;
109 }
110
111 /****************************************************************************
112  Get/Set all the possible time fields from a stat struct as a timespec.
113 ****************************************************************************/
114
115 static struct timespec get_atimespec(const struct stat *pst)
116 {
117 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
118         struct timespec ret;
119
120         /* Old system - no ns timestamp. */
121         ret.tv_sec = pst->st_atime;
122         ret.tv_nsec = 0;
123         return ret;
124 #else
125 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
126         struct timespec ret;
127         ret.tv_sec = pst->st_atim.tv_sec;
128         ret.tv_nsec = pst->st_atim.tv_nsec;
129         return ret;
130 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
131         struct timespec ret;
132         ret.tv_sec = pst->st_atime;
133         ret.tv_nsec = pst->st_atimensec;
134         return ret;
135 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
136         struct timespec ret;
137         ret.tv_sec = pst->st_atime;
138         ret.tv_nsec = pst->st_atime_n;
139         return ret;
140 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
141         struct timespec ret;
142         ret.tv_sec = pst->st_atime;
143         ret.tv_nsec = pst->st_uatime * 1000;
144         return ret;
145 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
146         return pst->st_atimespec;
147 #else
148 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
149 #endif
150 #endif
151 }
152
153 static struct timespec get_mtimespec(const struct stat *pst)
154 {
155 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
156         struct timespec ret;
157
158         /* Old system - no ns timestamp. */
159         ret.tv_sec = pst->st_mtime;
160         ret.tv_nsec = 0;
161         return ret;
162 #else
163 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
164         struct timespec ret;
165         ret.tv_sec = pst->st_mtim.tv_sec;
166         ret.tv_nsec = pst->st_mtim.tv_nsec;
167         return ret;
168 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
169         struct timespec ret;
170         ret.tv_sec = pst->st_mtime;
171         ret.tv_nsec = pst->st_mtimensec;
172         return ret;
173 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
174         struct timespec ret;
175         ret.tv_sec = pst->st_mtime;
176         ret.tv_nsec = pst->st_mtime_n;
177         return ret;
178 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
179         struct timespec ret;
180         ret.tv_sec = pst->st_mtime;
181         ret.tv_nsec = pst->st_umtime * 1000;
182         return ret;
183 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
184         return pst->st_mtimespec;
185 #else
186 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
187 #endif
188 #endif
189 }
190
191 static struct timespec get_ctimespec(const struct stat *pst)
192 {
193 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
194         struct timespec ret;
195
196         /* Old system - no ns timestamp. */
197         ret.tv_sec = pst->st_ctime;
198         ret.tv_nsec = 0;
199         return ret;
200 #else
201 #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC)
202         struct timespec ret;
203         ret.tv_sec = pst->st_ctim.tv_sec;
204         ret.tv_nsec = pst->st_ctim.tv_nsec;
205         return ret;
206 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
207         struct timespec ret;
208         ret.tv_sec = pst->st_ctime;
209         ret.tv_nsec = pst->st_ctimensec;
210         return ret;
211 #elif defined(HAVE_STRUCT_STAT_ST_MTIME_N)
212         struct timespec ret;
213         ret.tv_sec = pst->st_ctime;
214         ret.tv_nsec = pst->st_ctime_n;
215         return ret;
216 #elif defined(HAVE_STRUCT_STAT_ST_UMTIME)
217         struct timespec ret;
218         ret.tv_sec = pst->st_ctime;
219         ret.tv_nsec = pst->st_uctime * 1000;
220         return ret;
221 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC)
222         return pst->st_ctimespec;
223 #else
224 #error  CONFIGURE_ERROR_IN_DETECTING_TIMESPEC_IN_STAT
225 #endif
226 #endif
227 }
228
229 /****************************************************************************
230  Return the best approximation to a 'create time' under UNIX from a stat
231  structure.
232 ****************************************************************************/
233
234 static struct timespec calc_create_time_stat(const struct stat *st)
235 {
236         struct timespec ret, ret1;
237         struct timespec c_time = get_ctimespec(st);
238         struct timespec m_time = get_mtimespec(st);
239         struct timespec a_time = get_atimespec(st);
240
241         ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
242         ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
243
244         if(!null_timespec(ret1)) {
245                 return ret1;
246         }
247
248         /*
249          * One of ctime, mtime or atime was zero (probably atime).
250          * Just return MIN(ctime, mtime).
251          */
252         return ret;
253 }
254
255 /****************************************************************************
256  Return the best approximation to a 'create time' under UNIX from a stat_ex
257  structure.
258 ****************************************************************************/
259
260 static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
261 {
262         struct timespec ret, ret1;
263         struct timespec c_time = st->st_ex_ctime;
264         struct timespec m_time = st->st_ex_mtime;
265         struct timespec a_time = st->st_ex_atime;
266
267         ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
268         ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
269
270         if(!null_timespec(ret1)) {
271                 return ret1;
272         }
273
274         /*
275          * One of ctime, mtime or atime was zero (probably atime).
276          * Just return MIN(ctime, mtime).
277          */
278         return ret;
279 }
280
281 /****************************************************************************
282  Return the 'create time' from a stat struct if it exists (birthtime) or else
283  use the best approximation.
284 ****************************************************************************/
285
286 static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
287                                  bool fake_dir_create_times)
288 {
289         if (S_ISDIR(pst->st_mode) && fake_dir_create_times) {
290                 dst->st_ex_btime.tv_sec = 315493200L;          /* 1/1/1980 */
291                 dst->st_ex_btime.tv_nsec = 0;
292         }
293
294         dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_BTIME;
295
296 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC)
297         dst->st_ex_btime = pst->st_birthtimespec;
298 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC)
299         dst->st_ex_btime.tv_sec = pst->st_birthtime;
300         dst->st_ex_btime.tv_nsec = pst->st_birthtimenspec;
301 #elif defined(HAVE_STRUCT_STAT_ST_BIRTHTIME)
302         dst->st_ex_btime.tv_sec = pst->st_birthtime;
303         dst->st_ex_btime.tv_nsec = 0;
304 #else
305         dst->st_ex_btime = calc_create_time_stat(pst);
306         dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_BTIME;
307 #endif
308
309         /* Deal with systems that don't initialize birthtime correctly.
310          * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
311          */
312         if (null_timespec(dst->st_ex_btime)) {
313                 dst->st_ex_btime = calc_create_time_stat(pst);
314                 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_BTIME;
315         }
316
317         dst->st_ex_itime = dst->st_ex_btime;
318         dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_ITIME;
319 }
320
321 /****************************************************************************
322  If we update a timestamp in a stat_ex struct we may have to recalculate
323  the birthtime. For now only implement this for write time, but we may
324  also need to do it for atime and ctime. JRA.
325 ****************************************************************************/
326
327 void update_stat_ex_mtime(struct stat_ex *dst,
328                                 struct timespec write_ts)
329 {
330         dst->st_ex_mtime = write_ts;
331
332         /* We may have to recalculate btime. */
333         if (dst->st_ex_iflags & ST_EX_IFLAG_CALCULATED_BTIME) {
334                 dst->st_ex_btime = calc_create_time_stat_ex(dst);
335         }
336 }
337
338 void update_stat_ex_create_time(struct stat_ex *dst,
339                                 struct timespec create_time)
340 {
341         dst->st_ex_btime = create_time;
342         dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_BTIME;
343 }
344
345 void init_stat_ex_from_stat (struct stat_ex *dst,
346                             const struct stat *src,
347                             bool fake_dir_create_times)
348 {
349         dst->st_ex_dev = src->st_dev;
350         dst->st_ex_ino = src->st_ino;
351         dst->st_ex_mode = src->st_mode;
352         dst->st_ex_nlink = src->st_nlink;
353         dst->st_ex_uid = src->st_uid;
354         dst->st_ex_gid = src->st_gid;
355         dst->st_ex_rdev = src->st_rdev;
356         dst->st_ex_size = src->st_size;
357         dst->st_ex_atime = get_atimespec(src);
358         dst->st_ex_mtime = get_mtimespec(src);
359         dst->st_ex_ctime = get_ctimespec(src);
360         dst->st_ex_iflags = 0;
361         make_create_timespec(src, dst, fake_dir_create_times);
362 #ifdef HAVE_STAT_ST_BLKSIZE
363         dst->st_ex_blksize = src->st_blksize;
364 #else
365         dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
366 #endif
367
368 #ifdef HAVE_STAT_ST_BLOCKS
369         dst->st_ex_blocks = src->st_blocks;
370 #else
371         dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
372 #endif
373
374 #ifdef HAVE_STAT_ST_FLAGS
375         dst->st_ex_flags = src->st_flags;
376 #else
377         dst->st_ex_flags = 0;
378 #endif
379 }
380
381 /*******************************************************************
382 A stat() wrapper.
383 ********************************************************************/
384
385 int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
386              bool fake_dir_create_times)
387 {
388         int ret;
389         struct stat statbuf;
390         ret = stat(fname, &statbuf);
391         if (ret == 0) {
392                 /* we always want directories to appear zero size */
393                 if (S_ISDIR(statbuf.st_mode)) {
394                         statbuf.st_size = 0;
395                 }
396                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
397         }
398         return ret;
399 }
400
401 /*******************************************************************
402  An fstat() wrapper.
403 ********************************************************************/
404
405 int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
406 {
407         int ret;
408         struct stat statbuf;
409         ret = fstat(fd, &statbuf);
410         if (ret == 0) {
411                 /* we always want directories to appear zero size */
412                 if (S_ISDIR(statbuf.st_mode)) {
413                         statbuf.st_size = 0;
414                 }
415                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
416         }
417         return ret;
418 }
419
420 /*******************************************************************
421  An lstat() wrapper.
422 ********************************************************************/
423
424 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
425               bool fake_dir_create_times)
426 {
427         int ret;
428         struct stat statbuf;
429         ret = lstat(fname, &statbuf);
430         if (ret == 0) {
431                 /* we always want directories to appear zero size */
432                 if (S_ISDIR(statbuf.st_mode)) {
433                         statbuf.st_size = 0;
434                 }
435                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
436         }
437         return ret;
438 }
439
440 /*******************************************************************
441  An posix_fallocate() wrapper.
442 ********************************************************************/
443 int sys_posix_fallocate(int fd, off_t offset, off_t len)
444 {
445 #if defined(HAVE_POSIX_FALLOCATE)
446         return posix_fallocate(fd, offset, len);
447 #elif defined(F_RESVSP64)
448         /* this handles XFS on IRIX */
449         struct flock64 fl;
450         off_t new_len = offset + len;
451         int ret;
452         struct stat64 sbuf;
453
454         /* unlikely to get a too large file on a 64bit system but ... */
455         if (new_len < 0)
456                 return EFBIG;
457
458         fl.l_whence = SEEK_SET;
459         fl.l_start = offset;
460         fl.l_len = len;
461
462         ret=fcntl(fd, F_RESVSP64, &fl);
463
464         if (ret != 0)
465                 return errno;
466
467         /* Make sure the file gets enlarged after we allocated space: */
468         fstat64(fd, &sbuf);
469         if (new_len > sbuf.st_size)
470                 ftruncate64(fd, new_len);
471         return 0;
472 #else
473         return ENOSYS;
474 #endif
475 }
476
477 /*******************************************************************
478  An fallocate() function that matches the semantics of the Linux one.
479 ********************************************************************/
480
481 #ifdef HAVE_LINUX_FALLOC_H
482 #include <linux/falloc.h>
483 #endif
484
485 int sys_fallocate(int fd, uint32_t mode, off_t offset, off_t len)
486 {
487 #if defined(HAVE_LINUX_FALLOCATE)
488         int lmode = 0;
489
490         if (mode & VFS_FALLOCATE_FL_KEEP_SIZE) {
491                 lmode |= FALLOC_FL_KEEP_SIZE;
492                 mode &= ~VFS_FALLOCATE_FL_KEEP_SIZE;
493         }
494
495 #if defined(HAVE_FALLOC_FL_PUNCH_HOLE)
496         if (mode & VFS_FALLOCATE_FL_PUNCH_HOLE) {
497                 lmode |= FALLOC_FL_PUNCH_HOLE;
498                 mode &= ~VFS_FALLOCATE_FL_PUNCH_HOLE;
499         }
500 #endif  /* HAVE_FALLOC_FL_PUNCH_HOLE */
501
502         if (mode != 0) {
503                 DEBUG(2, ("unmapped fallocate flags: %lx\n",
504                       (unsigned long)mode));
505                 errno = EINVAL;
506                 return -1;
507         }
508         return fallocate(fd, lmode, offset, len);
509 #else   /* HAVE_LINUX_FALLOCATE */
510         /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
511         errno = ENOSYS;
512         return -1;
513 #endif  /* HAVE_LINUX_FALLOCATE */
514 }
515
516 #ifdef HAVE_KERNEL_SHARE_MODES
517 #ifndef LOCK_MAND
518 #define LOCK_MAND       32      /* This is a mandatory flock */
519 #define LOCK_READ       64      /* ... Which allows concurrent read operations */
520 #define LOCK_WRITE      128     /* ... Which allows concurrent write operations */
521 #define LOCK_RW         192     /* ... Which allows concurrent read & write ops */
522 #endif
523 #endif
524
525 /*******************************************************************
526  A flock() wrapper that will perform the kernel flock.
527 ********************************************************************/
528
529 void kernel_flock(int fd, uint32_t share_mode, uint32_t access_mask)
530 {
531 #ifdef HAVE_KERNEL_SHARE_MODES
532         int kernel_mode = 0;
533         if (share_mode == FILE_SHARE_WRITE) {
534                 kernel_mode = LOCK_MAND|LOCK_WRITE;
535         } else if (share_mode == FILE_SHARE_READ) {
536                 kernel_mode = LOCK_MAND|LOCK_READ;
537         } else if (share_mode == FILE_SHARE_NONE) {
538                 kernel_mode = LOCK_MAND;
539         }
540         if (kernel_mode) {
541                 flock(fd, kernel_mode);
542         }
543 #endif
544         ;
545 }
546
547
548
549 /*******************************************************************
550  An fdopendir wrapper.
551 ********************************************************************/
552
553 DIR *sys_fdopendir(int fd)
554 {
555 #if defined(HAVE_FDOPENDIR)
556         return fdopendir(fd);
557 #else
558         errno = ENOSYS;
559         return NULL;
560 #endif
561 }
562
563 /*******************************************************************
564  An mknod() wrapper.
565 ********************************************************************/
566
567 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
568 {
569 #if defined(HAVE_MKNOD)
570         return mknod(path, mode, dev);
571 #else
572         /* No mknod system call. */
573         errno = ENOSYS;
574         return -1;
575 #endif
576 }
577
578 /*******************************************************************
579  System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
580  on error (malloc fail usually).
581 ********************************************************************/
582
583 char *sys_getwd(void)
584 {
585 #ifdef GETCWD_TAKES_NULL
586         return getcwd(NULL, 0);
587 #elif defined(HAVE_GETCWD)
588         char *wd = NULL, *s = NULL;
589         size_t allocated = PATH_MAX;
590
591         while (1) {
592                 s = SMB_REALLOC_ARRAY(s, char, allocated);
593                 if (s == NULL) {
594                         return NULL;
595                 }
596                 wd = getcwd(s, allocated);
597                 if (wd) {
598                         break;
599                 }
600                 if (errno != ERANGE) {
601                         int saved_errno = errno;
602                         SAFE_FREE(s);
603                         errno = saved_errno;
604                         break;
605                 }
606                 allocated *= 2;
607                 if (allocated < PATH_MAX) {
608                         SAFE_FREE(s);
609                         break;
610                 }
611         }
612         return wd;
613 #else
614         char *wd = NULL;
615         char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
616         if (s == NULL) {
617                 return NULL;
618         }
619         wd = getwd(s);
620         if (wd == NULL) {
621                 int saved_errno = errno;
622                 SAFE_FREE(s);
623                 errno = saved_errno;
624         }
625         return wd;
626 #endif
627 }
628
629 #if defined(HAVE_POSIX_CAPABILITIES)
630
631 /**************************************************************************
632  Try and abstract process capabilities (for systems that have them).
633 ****************************************************************************/
634
635 /* Set the POSIX capabilities needed for the given purpose into the effective
636  * capability set of the current process. Make sure they are always removed
637  * from the inheritable set, because there is no circumstance in which our
638  * children should inherit our elevated privileges.
639  */
640 static bool set_process_capability(enum smbd_capability capability,
641                                    bool enable)
642 {
643         cap_value_t cap_vals[2] = {0};
644         int num_cap_vals = 0;
645
646         cap_t cap;
647
648 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
649         /* On Linux, make sure that any capabilities we grab are sticky
650          * across UID changes. We expect that this would allow us to keep both
651          * the effective and permitted capability sets, but as of circa 2.6.16,
652          * only the permitted set is kept. It is a bug (which we work around)
653          * that the effective set is lost, but we still require the effective
654          * set to be kept.
655          */
656         if (!prctl(PR_GET_KEEPCAPS)) {
657                 prctl(PR_SET_KEEPCAPS, 1);
658         }
659 #endif
660
661         cap = cap_get_proc();
662         if (cap == NULL) {
663                 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
664                         strerror(errno)));
665                 return False;
666         }
667
668         switch (capability) {
669                 case KERNEL_OPLOCK_CAPABILITY:
670 #ifdef CAP_NETWORK_MGT
671                         /* IRIX has CAP_NETWORK_MGT for oplocks. */
672                         cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
673 #endif
674                         break;
675                 case DMAPI_ACCESS_CAPABILITY:
676 #ifdef CAP_DEVICE_MGT
677                         /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
678                         cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
679 #elif CAP_MKNOD
680                         /* Linux has CAP_MKNOD for DMAPI access. */
681                         cap_vals[num_cap_vals++] = CAP_MKNOD;
682 #endif
683                         break;
684                 case LEASE_CAPABILITY:
685 #ifdef CAP_LEASE
686                         cap_vals[num_cap_vals++] = CAP_LEASE;
687 #endif
688                         break;
689                 case DAC_OVERRIDE_CAPABILITY:
690 #ifdef CAP_DAC_OVERRIDE
691                         cap_vals[num_cap_vals++] = CAP_DAC_OVERRIDE;
692 #endif
693         }
694
695         SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
696
697         if (num_cap_vals == 0) {
698                 cap_free(cap);
699                 return True;
700         }
701
702         cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
703                 enable ? CAP_SET : CAP_CLEAR);
704
705         /* We never want to pass capabilities down to our children, so make
706          * sure they are not inherited.
707          */
708         cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
709
710         if (cap_set_proc(cap) == -1) {
711                 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
712                         strerror(errno)));
713                 cap_free(cap);
714                 return False;
715         }
716
717         cap_free(cap);
718         return True;
719 }
720
721 #endif /* HAVE_POSIX_CAPABILITIES */
722
723 /****************************************************************************
724  Gain the oplock capability from the kernel if possible.
725 ****************************************************************************/
726
727 void set_effective_capability(enum smbd_capability capability)
728 {
729 #if defined(HAVE_POSIX_CAPABILITIES)
730         set_process_capability(capability, True);
731 #endif /* HAVE_POSIX_CAPABILITIES */
732 }
733
734 void drop_effective_capability(enum smbd_capability capability)
735 {
736 #if defined(HAVE_POSIX_CAPABILITIES)
737         set_process_capability(capability, False);
738 #endif /* HAVE_POSIX_CAPABILITIES */
739 }
740
741 /**************************************************************************
742  Wrapper for random().
743 ****************************************************************************/
744
745 long sys_random(void)
746 {
747 #if defined(HAVE_RANDOM)
748         return (long)random();
749 #elif defined(HAVE_RAND)
750         return (long)rand();
751 #else
752         DEBUG(0,("Error - no random function available !\n"));
753         exit(1);
754 #endif
755 }
756
757 /**************************************************************************
758  Wrapper for srandom().
759 ****************************************************************************/
760
761 void sys_srandom(unsigned int seed)
762 {
763 #if defined(HAVE_SRANDOM)
764         srandom(seed);
765 #elif defined(HAVE_SRAND)
766         srand(seed);
767 #else
768         DEBUG(0,("Error - no srandom function available !\n"));
769         exit(1);
770 #endif
771 }
772
773 #ifndef NGROUPS_MAX
774 #define NGROUPS_MAX 32 /* Guess... */
775 #endif
776
777 /**************************************************************************
778  Returns equivalent to NGROUPS_MAX - using sysconf if needed.
779 ****************************************************************************/
780
781 int groups_max(void)
782 {
783 #if defined(SYSCONF_SC_NGROUPS_MAX)
784         int ret = sysconf(_SC_NGROUPS_MAX);
785         return (ret == -1) ? NGROUPS_MAX : ret;
786 #else
787         return NGROUPS_MAX;
788 #endif
789 }
790
791 /**************************************************************************
792  Wrap setgroups and getgroups for systems that declare getgroups() as
793  returning an array of gid_t, but actuall return an array of int.
794 ****************************************************************************/
795
796 #if defined(HAVE_BROKEN_GETGROUPS)
797
798 #ifdef HAVE_BROKEN_GETGROUPS
799 #define GID_T int
800 #else
801 #define GID_T gid_t
802 #endif
803
804 static int sys_broken_getgroups(int setlen, gid_t *gidset)
805 {
806         GID_T *group_list;
807         int i, ngroups;
808
809         if(setlen == 0) {
810                 return getgroups(0, NULL);
811         }
812
813         /*
814          * Broken case. We need to allocate a
815          * GID_T array of size setlen.
816          */
817
818         if(setlen < 0) {
819                 errno = EINVAL; 
820                 return -1;
821         } 
822
823         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
824                 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
825                 return -1;
826         }
827
828         if((ngroups = getgroups(setlen, group_list)) < 0) {
829                 int saved_errno = errno;
830                 SAFE_FREE(group_list);
831                 errno = saved_errno;
832                 return -1;
833         }
834
835         /*
836          * We're safe here as if ngroups > setlen then
837          * getgroups *must* return EINVAL.
838          * pubs.opengroup.org/onlinepubs/009695399/functions/getgroups.html
839          */
840
841         for(i = 0; i < ngroups; i++)
842                 gidset[i] = (gid_t)group_list[i];
843
844         SAFE_FREE(group_list);
845         return ngroups;
846 }
847
848 static int sys_broken_setgroups(int setlen, gid_t *gidset)
849 {
850         GID_T *group_list;
851         int i ; 
852
853         if (setlen == 0)
854                 return 0 ;
855
856         if (setlen < 0 || setlen > groups_max()) {
857                 errno = EINVAL; 
858                 return -1;   
859         }
860
861         /*
862          * Broken case. We need to allocate a
863          * GID_T array of size setlen.
864          */
865
866         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
867                 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
868                 return -1;    
869         }
870
871         for(i = 0; i < setlen; i++) 
872                 group_list[i] = (GID_T) gidset[i]; 
873
874         if(samba_setgroups(setlen, group_list) != 0) {
875                 int saved_errno = errno;
876                 SAFE_FREE(group_list);
877                 errno = saved_errno;
878                 return -1;
879         }
880
881         SAFE_FREE(group_list);
882         return 0 ;
883 }
884
885 #endif /* HAVE_BROKEN_GETGROUPS */
886
887 /* This is a list of systems that require the first GID passed to setgroups(2)
888  * to be the effective GID. If your system is one of these, add it here.
889  */
890 #if defined (FREEBSD) || defined (DARWINOS)
891 #define USE_BSD_SETGROUPS
892 #endif
893
894 #if defined(USE_BSD_SETGROUPS)
895 /* Depending on the particular BSD implementation, the first GID that is
896  * passed to setgroups(2) will either be ignored or will set the credential's
897  * effective GID. In either case, the right thing to do is to guarantee that
898  * gidset[0] is the effective GID.
899  */
900 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
901 {
902         gid_t *new_gidset = NULL;
903         int max;
904         int ret;
905
906         /* setgroups(2) will fail with EINVAL if we pass too many groups. */
907         max = groups_max();
908
909         /* No group list, just make sure we are setting the efective GID. */
910         if (setlen == 0) {
911                 return samba_setgroups(1, &primary_gid);
912         }
913
914         /* If the primary gid is not the first array element, grow the array
915          * and insert it at the front.
916          */
917         if (gidset[0] != primary_gid) {
918                 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
919                 if (new_gidset == NULL) {
920                         return -1;
921                 }
922
923                 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
924                 new_gidset[0] = primary_gid;
925                 setlen++;
926         }
927
928         if (setlen > max) {
929                 DEBUG(3, ("forced to truncate group list from %d to %d\n",
930                         setlen, max));
931                 setlen = max;
932         }
933
934 #if defined(HAVE_BROKEN_GETGROUPS)
935         ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
936 #else
937         ret = samba_setgroups(setlen, new_gidset ? new_gidset : gidset);
938 #endif
939
940         if (new_gidset) {
941                 int errsav = errno;
942                 SAFE_FREE(new_gidset);
943                 errno = errsav;
944         }
945
946         return ret;
947 }
948
949 #endif /* USE_BSD_SETGROUPS */
950
951 /**************************************************************************
952  Wrapper for getgroups. Deals with broken (int) case.
953 ****************************************************************************/
954
955 int sys_getgroups(int setlen, gid_t *gidset)
956 {
957 #if defined(HAVE_BROKEN_GETGROUPS)
958         return sys_broken_getgroups(setlen, gidset);
959 #else
960         return getgroups(setlen, gidset);
961 #endif
962 }
963
964 /**************************************************************************
965  Wrapper for setgroups. Deals with broken (int) case and BSD case.
966 ****************************************************************************/
967
968 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
969 {
970 #if !defined(HAVE_SETGROUPS)
971         errno = ENOSYS;
972         return -1;
973 #endif /* HAVE_SETGROUPS */
974
975 #if defined(USE_BSD_SETGROUPS)
976         return sys_bsd_setgroups(primary_gid, setlen, gidset);
977 #elif defined(HAVE_BROKEN_GETGROUPS)
978         return sys_broken_setgroups(setlen, gidset);
979 #else
980         return samba_setgroups(setlen, gidset);
981 #endif
982 }
983
984 /****************************************************************************
985  Return the major devicenumber for UNIX extensions.
986 ****************************************************************************/
987
988 uint32_t unix_dev_major(SMB_DEV_T dev)
989 {
990 #if defined(HAVE_DEVICE_MAJOR_FN)
991         return (uint32_t)major(dev);
992 #else
993         return (uint32_t)(dev >> 8);
994 #endif
995 }
996
997 /****************************************************************************
998  Return the minor devicenumber for UNIX extensions.
999 ****************************************************************************/
1000
1001 uint32_t unix_dev_minor(SMB_DEV_T dev)
1002 {
1003 #if defined(HAVE_DEVICE_MINOR_FN)
1004         return (uint32_t)minor(dev);
1005 #else
1006         return (uint32_t)(dev & 0xff);
1007 #endif
1008 }
1009
1010 /**************************************************************************
1011  Wrapper for realpath.
1012 ****************************************************************************/
1013
1014 char *sys_realpath(const char *path)
1015 {
1016         char *result;
1017
1018 #ifdef REALPATH_TAKES_NULL
1019         result = realpath(path, NULL);
1020 #else
1021         result = SMB_MALLOC_ARRAY(char, PATH_MAX + 1);
1022         if (result) {
1023                 char *resolved_path = realpath(path, result);
1024                 if (!resolved_path) {
1025                         SAFE_FREE(result);
1026                 } else {
1027                         /* SMB_ASSERT(result == resolved_path) ? */
1028                         result = resolved_path;
1029                 }
1030         }
1031 #endif
1032         return result;
1033 }
1034
1035 #if 0
1036 /*******************************************************************
1037  Return the number of CPUs.
1038 ********************************************************************/
1039
1040 int sys_get_number_of_cores(void)
1041 {
1042         int ret = -1;
1043
1044 #if defined(HAVE_SYSCONF)
1045 #if defined(_SC_NPROCESSORS_ONLN)
1046         ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
1047 #endif
1048 #if defined(_SC_NPROCESSORS_CONF)
1049         if (ret < 1) {
1050                 ret = (int)sysconf(_SC_NPROCESSORS_CONF);
1051         }
1052 #endif
1053 #elif defined(HAVE_SYSCTL) && defined(CTL_HW)
1054         int name[2];
1055         unsigned int len = sizeof(ret);
1056
1057         name[0] = CTL_HW;
1058 #if defined(HW_AVAILCPU)
1059         name[1] = HW_AVAILCPU;
1060
1061         if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
1062                 ret = -1;
1063         }
1064 #endif
1065 #if defined(HW_NCPU)
1066         if(ret < 1) {
1067                 name[0] = CTL_HW;
1068                 name[1] = HW_NCPU;
1069                 if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
1070                         ret = -1;
1071                 }
1072         }
1073 #endif
1074 #endif
1075         if (ret < 1) {
1076                 ret = 1;
1077         }
1078         return ret;
1079 }
1080 #endif