Revert "smbd: add an effective connection_struct->user_ev_ctx that holds the event...
[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_calculated_birthtime = false;
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_calculated_birthtime = true;
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_calculated_birthtime = true;
315         }
316 }
317
318 /****************************************************************************
319  If we update a timestamp in a stat_ex struct we may have to recalculate
320  the birthtime. For now only implement this for write time, but we may
321  also need to do it for atime and ctime. JRA.
322 ****************************************************************************/
323
324 void update_stat_ex_mtime(struct stat_ex *dst,
325                                 struct timespec write_ts)
326 {
327         dst->st_ex_mtime = write_ts;
328
329         /* We may have to recalculate btime. */
330         if (dst->st_ex_calculated_birthtime) {
331                 dst->st_ex_btime = calc_create_time_stat_ex(dst);
332         }
333 }
334
335 void update_stat_ex_create_time(struct stat_ex *dst,
336                                 struct timespec create_time)
337 {
338         dst->st_ex_btime = create_time;
339         dst->st_ex_calculated_birthtime = false;
340 }
341
342 void init_stat_ex_from_stat (struct stat_ex *dst,
343                             const struct stat *src,
344                             bool fake_dir_create_times)
345 {
346         dst->st_ex_dev = src->st_dev;
347         dst->st_ex_ino = src->st_ino;
348         dst->st_ex_mode = src->st_mode;
349         dst->st_ex_nlink = src->st_nlink;
350         dst->st_ex_uid = src->st_uid;
351         dst->st_ex_gid = src->st_gid;
352         dst->st_ex_rdev = src->st_rdev;
353         dst->st_ex_size = src->st_size;
354         dst->st_ex_atime = get_atimespec(src);
355         dst->st_ex_mtime = get_mtimespec(src);
356         dst->st_ex_ctime = get_ctimespec(src);
357         make_create_timespec(src, dst, fake_dir_create_times);
358 #ifdef HAVE_STAT_ST_BLKSIZE
359         dst->st_ex_blksize = src->st_blksize;
360 #else
361         dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
362 #endif
363
364 #ifdef HAVE_STAT_ST_BLOCKS
365         dst->st_ex_blocks = src->st_blocks;
366 #else
367         dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
368 #endif
369
370 #ifdef HAVE_STAT_ST_FLAGS
371         dst->st_ex_flags = src->st_flags;
372 #else
373         dst->st_ex_flags = 0;
374 #endif
375 }
376
377 /*******************************************************************
378 A stat() wrapper.
379 ********************************************************************/
380
381 int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
382              bool fake_dir_create_times)
383 {
384         int ret;
385         struct stat statbuf;
386         ret = stat(fname, &statbuf);
387         if (ret == 0) {
388                 /* we always want directories to appear zero size */
389                 if (S_ISDIR(statbuf.st_mode)) {
390                         statbuf.st_size = 0;
391                 }
392                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
393         }
394         return ret;
395 }
396
397 /*******************************************************************
398  An fstat() wrapper.
399 ********************************************************************/
400
401 int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
402 {
403         int ret;
404         struct stat statbuf;
405         ret = fstat(fd, &statbuf);
406         if (ret == 0) {
407                 /* we always want directories to appear zero size */
408                 if (S_ISDIR(statbuf.st_mode)) {
409                         statbuf.st_size = 0;
410                 }
411                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
412         }
413         return ret;
414 }
415
416 /*******************************************************************
417  An lstat() wrapper.
418 ********************************************************************/
419
420 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
421               bool fake_dir_create_times)
422 {
423         int ret;
424         struct stat statbuf;
425         ret = lstat(fname, &statbuf);
426         if (ret == 0) {
427                 /* we always want directories to appear zero size */
428                 if (S_ISDIR(statbuf.st_mode)) {
429                         statbuf.st_size = 0;
430                 }
431                 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
432         }
433         return ret;
434 }
435
436 /*******************************************************************
437  An posix_fallocate() wrapper.
438 ********************************************************************/
439 int sys_posix_fallocate(int fd, off_t offset, off_t len)
440 {
441 #if defined(HAVE_POSIX_FALLOCATE)
442         return posix_fallocate(fd, offset, len);
443 #elif defined(F_RESVSP64)
444         /* this handles XFS on IRIX */
445         struct flock64 fl;
446         off_t new_len = offset + len;
447         int ret;
448         struct stat64 sbuf;
449
450         /* unlikely to get a too large file on a 64bit system but ... */
451         if (new_len < 0)
452                 return EFBIG;
453
454         fl.l_whence = SEEK_SET;
455         fl.l_start = offset;
456         fl.l_len = len;
457
458         ret=fcntl(fd, F_RESVSP64, &fl);
459
460         if (ret != 0)
461                 return errno;
462
463         /* Make sure the file gets enlarged after we allocated space: */
464         fstat64(fd, &sbuf);
465         if (new_len > sbuf.st_size)
466                 ftruncate64(fd, new_len);
467         return 0;
468 #else
469         return ENOSYS;
470 #endif
471 }
472
473 /*******************************************************************
474  An fallocate() function that matches the semantics of the Linux one.
475 ********************************************************************/
476
477 #ifdef HAVE_LINUX_FALLOC_H
478 #include <linux/falloc.h>
479 #endif
480
481 int sys_fallocate(int fd, uint32_t mode, off_t offset, off_t len)
482 {
483 #if defined(HAVE_LINUX_FALLOCATE)
484         int lmode = 0;
485
486         if (mode & VFS_FALLOCATE_FL_KEEP_SIZE) {
487                 lmode |= FALLOC_FL_KEEP_SIZE;
488                 mode &= ~VFS_FALLOCATE_FL_KEEP_SIZE;
489         }
490
491 #if defined(HAVE_FALLOC_FL_PUNCH_HOLE)
492         if (mode & VFS_FALLOCATE_FL_PUNCH_HOLE) {
493                 lmode |= FALLOC_FL_PUNCH_HOLE;
494                 mode &= ~VFS_FALLOCATE_FL_PUNCH_HOLE;
495         }
496 #endif  /* HAVE_FALLOC_FL_PUNCH_HOLE */
497
498         if (mode != 0) {
499                 DEBUG(2, ("unmapped fallocate flags: %lx\n",
500                       (unsigned long)mode));
501                 errno = EINVAL;
502                 return -1;
503         }
504         return fallocate(fd, lmode, offset, len);
505 #else   /* HAVE_LINUX_FALLOCATE */
506         /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
507         errno = ENOSYS;
508         return -1;
509 #endif  /* HAVE_LINUX_FALLOCATE */
510 }
511
512 #ifdef HAVE_KERNEL_SHARE_MODES
513 #ifndef LOCK_MAND
514 #define LOCK_MAND       32      /* This is a mandatory flock */
515 #define LOCK_READ       64      /* ... Which allows concurrent read operations */
516 #define LOCK_WRITE      128     /* ... Which allows concurrent write operations */
517 #define LOCK_RW         192     /* ... Which allows concurrent read & write ops */
518 #endif
519 #endif
520
521 /*******************************************************************
522  A flock() wrapper that will perform the kernel flock.
523 ********************************************************************/
524
525 void kernel_flock(int fd, uint32_t share_mode, uint32_t access_mask)
526 {
527 #ifdef HAVE_KERNEL_SHARE_MODES
528         int kernel_mode = 0;
529         if (share_mode == FILE_SHARE_WRITE) {
530                 kernel_mode = LOCK_MAND|LOCK_WRITE;
531         } else if (share_mode == FILE_SHARE_READ) {
532                 kernel_mode = LOCK_MAND|LOCK_READ;
533         } else if (share_mode == FILE_SHARE_NONE) {
534                 kernel_mode = LOCK_MAND;
535         }
536         if (kernel_mode) {
537                 flock(fd, kernel_mode);
538         }
539 #endif
540         ;
541 }
542
543
544
545 /*******************************************************************
546  An fdopendir wrapper.
547 ********************************************************************/
548
549 DIR *sys_fdopendir(int fd)
550 {
551 #if defined(HAVE_FDOPENDIR)
552         return fdopendir(fd);
553 #else
554         errno = ENOSYS;
555         return NULL;
556 #endif
557 }
558
559 /*******************************************************************
560  An mknod() wrapper.
561 ********************************************************************/
562
563 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
564 {
565 #if defined(HAVE_MKNOD)
566         return mknod(path, mode, dev);
567 #else
568         /* No mknod system call. */
569         errno = ENOSYS;
570         return -1;
571 #endif
572 }
573
574 /*******************************************************************
575  System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
576  on error (malloc fail usually).
577 ********************************************************************/
578
579 char *sys_getwd(void)
580 {
581 #ifdef GETCWD_TAKES_NULL
582         return getcwd(NULL, 0);
583 #elif defined(HAVE_GETCWD)
584         char *wd = NULL, *s = NULL;
585         size_t allocated = PATH_MAX;
586
587         while (1) {
588                 s = SMB_REALLOC_ARRAY(s, char, allocated);
589                 if (s == NULL) {
590                         return NULL;
591                 }
592                 wd = getcwd(s, allocated);
593                 if (wd) {
594                         break;
595                 }
596                 if (errno != ERANGE) {
597                         int saved_errno = errno;
598                         SAFE_FREE(s);
599                         errno = saved_errno;
600                         break;
601                 }
602                 allocated *= 2;
603                 if (allocated < PATH_MAX) {
604                         SAFE_FREE(s);
605                         break;
606                 }
607         }
608         return wd;
609 #else
610         char *wd = NULL;
611         char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
612         if (s == NULL) {
613                 return NULL;
614         }
615         wd = getwd(s);
616         if (wd == NULL) {
617                 int saved_errno = errno;
618                 SAFE_FREE(s);
619                 errno = saved_errno;
620         }
621         return wd;
622 #endif
623 }
624
625 #if defined(HAVE_POSIX_CAPABILITIES)
626
627 /**************************************************************************
628  Try and abstract process capabilities (for systems that have them).
629 ****************************************************************************/
630
631 /* Set the POSIX capabilities needed for the given purpose into the effective
632  * capability set of the current process. Make sure they are always removed
633  * from the inheritable set, because there is no circumstance in which our
634  * children should inherit our elevated privileges.
635  */
636 static bool set_process_capability(enum smbd_capability capability,
637                                    bool enable)
638 {
639         cap_value_t cap_vals[2] = {0};
640         int num_cap_vals = 0;
641
642         cap_t cap;
643
644 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
645         /* On Linux, make sure that any capabilities we grab are sticky
646          * across UID changes. We expect that this would allow us to keep both
647          * the effective and permitted capability sets, but as of circa 2.6.16,
648          * only the permitted set is kept. It is a bug (which we work around)
649          * that the effective set is lost, but we still require the effective
650          * set to be kept.
651          */
652         if (!prctl(PR_GET_KEEPCAPS)) {
653                 prctl(PR_SET_KEEPCAPS, 1);
654         }
655 #endif
656
657         cap = cap_get_proc();
658         if (cap == NULL) {
659                 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
660                         strerror(errno)));
661                 return False;
662         }
663
664         switch (capability) {
665                 case KERNEL_OPLOCK_CAPABILITY:
666 #ifdef CAP_NETWORK_MGT
667                         /* IRIX has CAP_NETWORK_MGT for oplocks. */
668                         cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
669 #endif
670                         break;
671                 case DMAPI_ACCESS_CAPABILITY:
672 #ifdef CAP_DEVICE_MGT
673                         /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
674                         cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
675 #elif CAP_MKNOD
676                         /* Linux has CAP_MKNOD for DMAPI access. */
677                         cap_vals[num_cap_vals++] = CAP_MKNOD;
678 #endif
679                         break;
680                 case LEASE_CAPABILITY:
681 #ifdef CAP_LEASE
682                         cap_vals[num_cap_vals++] = CAP_LEASE;
683 #endif
684                         break;
685                 case DAC_OVERRIDE_CAPABILITY:
686 #ifdef CAP_DAC_OVERRIDE
687                         cap_vals[num_cap_vals++] = CAP_DAC_OVERRIDE;
688 #endif
689         }
690
691         SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
692
693         if (num_cap_vals == 0) {
694                 cap_free(cap);
695                 return True;
696         }
697
698         cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
699                 enable ? CAP_SET : CAP_CLEAR);
700
701         /* We never want to pass capabilities down to our children, so make
702          * sure they are not inherited.
703          */
704         cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
705
706         if (cap_set_proc(cap) == -1) {
707                 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
708                         strerror(errno)));
709                 cap_free(cap);
710                 return False;
711         }
712
713         cap_free(cap);
714         return True;
715 }
716
717 #endif /* HAVE_POSIX_CAPABILITIES */
718
719 /****************************************************************************
720  Gain the oplock capability from the kernel if possible.
721 ****************************************************************************/
722
723 void set_effective_capability(enum smbd_capability capability)
724 {
725 #if defined(HAVE_POSIX_CAPABILITIES)
726         set_process_capability(capability, True);
727 #endif /* HAVE_POSIX_CAPABILITIES */
728 }
729
730 void drop_effective_capability(enum smbd_capability capability)
731 {
732 #if defined(HAVE_POSIX_CAPABILITIES)
733         set_process_capability(capability, False);
734 #endif /* HAVE_POSIX_CAPABILITIES */
735 }
736
737 /**************************************************************************
738  Wrapper for random().
739 ****************************************************************************/
740
741 long sys_random(void)
742 {
743 #if defined(HAVE_RANDOM)
744         return (long)random();
745 #elif defined(HAVE_RAND)
746         return (long)rand();
747 #else
748         DEBUG(0,("Error - no random function available !\n"));
749         exit(1);
750 #endif
751 }
752
753 /**************************************************************************
754  Wrapper for srandom().
755 ****************************************************************************/
756
757 void sys_srandom(unsigned int seed)
758 {
759 #if defined(HAVE_SRANDOM)
760         srandom(seed);
761 #elif defined(HAVE_SRAND)
762         srand(seed);
763 #else
764         DEBUG(0,("Error - no srandom function available !\n"));
765         exit(1);
766 #endif
767 }
768
769 #ifndef NGROUPS_MAX
770 #define NGROUPS_MAX 32 /* Guess... */
771 #endif
772
773 /**************************************************************************
774  Returns equivalent to NGROUPS_MAX - using sysconf if needed.
775 ****************************************************************************/
776
777 int groups_max(void)
778 {
779 #if defined(SYSCONF_SC_NGROUPS_MAX)
780         int ret = sysconf(_SC_NGROUPS_MAX);
781         return (ret == -1) ? NGROUPS_MAX : ret;
782 #else
783         return NGROUPS_MAX;
784 #endif
785 }
786
787 /**************************************************************************
788  Wrap setgroups and getgroups for systems that declare getgroups() as
789  returning an array of gid_t, but actuall return an array of int.
790 ****************************************************************************/
791
792 #if defined(HAVE_BROKEN_GETGROUPS)
793
794 #ifdef HAVE_BROKEN_GETGROUPS
795 #define GID_T int
796 #else
797 #define GID_T gid_t
798 #endif
799
800 static int sys_broken_getgroups(int setlen, gid_t *gidset)
801 {
802         GID_T *group_list;
803         int i, ngroups;
804
805         if(setlen == 0) {
806                 return getgroups(0, NULL);
807         }
808
809         /*
810          * Broken case. We need to allocate a
811          * GID_T array of size setlen.
812          */
813
814         if(setlen < 0) {
815                 errno = EINVAL; 
816                 return -1;
817         } 
818
819         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
820                 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
821                 return -1;
822         }
823
824         if((ngroups = getgroups(setlen, group_list)) < 0) {
825                 int saved_errno = errno;
826                 SAFE_FREE(group_list);
827                 errno = saved_errno;
828                 return -1;
829         }
830
831         /*
832          * We're safe here as if ngroups > setlen then
833          * getgroups *must* return EINVAL.
834          * pubs.opengroup.org/onlinepubs/009695399/functions/getgroups.html
835          */
836
837         for(i = 0; i < ngroups; i++)
838                 gidset[i] = (gid_t)group_list[i];
839
840         SAFE_FREE(group_list);
841         return ngroups;
842 }
843
844 static int sys_broken_setgroups(int setlen, gid_t *gidset)
845 {
846         GID_T *group_list;
847         int i ; 
848
849         if (setlen == 0)
850                 return 0 ;
851
852         if (setlen < 0 || setlen > groups_max()) {
853                 errno = EINVAL; 
854                 return -1;   
855         }
856
857         /*
858          * Broken case. We need to allocate a
859          * GID_T array of size setlen.
860          */
861
862         if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
863                 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
864                 return -1;    
865         }
866
867         for(i = 0; i < setlen; i++) 
868                 group_list[i] = (GID_T) gidset[i]; 
869
870         if(samba_setgroups(setlen, group_list) != 0) {
871                 int saved_errno = errno;
872                 SAFE_FREE(group_list);
873                 errno = saved_errno;
874                 return -1;
875         }
876
877         SAFE_FREE(group_list);
878         return 0 ;
879 }
880
881 #endif /* HAVE_BROKEN_GETGROUPS */
882
883 /* This is a list of systems that require the first GID passed to setgroups(2)
884  * to be the effective GID. If your system is one of these, add it here.
885  */
886 #if defined (FREEBSD) || defined (DARWINOS)
887 #define USE_BSD_SETGROUPS
888 #endif
889
890 #if defined(USE_BSD_SETGROUPS)
891 /* Depending on the particular BSD implementation, the first GID that is
892  * passed to setgroups(2) will either be ignored or will set the credential's
893  * effective GID. In either case, the right thing to do is to guarantee that
894  * gidset[0] is the effective GID.
895  */
896 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
897 {
898         gid_t *new_gidset = NULL;
899         int max;
900         int ret;
901
902         /* setgroups(2) will fail with EINVAL if we pass too many groups. */
903         max = groups_max();
904
905         /* No group list, just make sure we are setting the efective GID. */
906         if (setlen == 0) {
907                 return samba_setgroups(1, &primary_gid);
908         }
909
910         /* If the primary gid is not the first array element, grow the array
911          * and insert it at the front.
912          */
913         if (gidset[0] != primary_gid) {
914                 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
915                 if (new_gidset == NULL) {
916                         return -1;
917                 }
918
919                 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
920                 new_gidset[0] = primary_gid;
921                 setlen++;
922         }
923
924         if (setlen > max) {
925                 DEBUG(3, ("forced to truncate group list from %d to %d\n",
926                         setlen, max));
927                 setlen = max;
928         }
929
930 #if defined(HAVE_BROKEN_GETGROUPS)
931         ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
932 #else
933         ret = samba_setgroups(setlen, new_gidset ? new_gidset : gidset);
934 #endif
935
936         if (new_gidset) {
937                 int errsav = errno;
938                 SAFE_FREE(new_gidset);
939                 errno = errsav;
940         }
941
942         return ret;
943 }
944
945 #endif /* USE_BSD_SETGROUPS */
946
947 /**************************************************************************
948  Wrapper for getgroups. Deals with broken (int) case.
949 ****************************************************************************/
950
951 int sys_getgroups(int setlen, gid_t *gidset)
952 {
953 #if defined(HAVE_BROKEN_GETGROUPS)
954         return sys_broken_getgroups(setlen, gidset);
955 #else
956         return getgroups(setlen, gidset);
957 #endif
958 }
959
960 /**************************************************************************
961  Wrapper for setgroups. Deals with broken (int) case and BSD case.
962 ****************************************************************************/
963
964 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
965 {
966 #if !defined(HAVE_SETGROUPS)
967         errno = ENOSYS;
968         return -1;
969 #endif /* HAVE_SETGROUPS */
970
971 #if defined(USE_BSD_SETGROUPS)
972         return sys_bsd_setgroups(primary_gid, setlen, gidset);
973 #elif defined(HAVE_BROKEN_GETGROUPS)
974         return sys_broken_setgroups(setlen, gidset);
975 #else
976         return samba_setgroups(setlen, gidset);
977 #endif
978 }
979
980 /****************************************************************************
981  Return the major devicenumber for UNIX extensions.
982 ****************************************************************************/
983
984 uint32_t unix_dev_major(SMB_DEV_T dev)
985 {
986 #if defined(HAVE_DEVICE_MAJOR_FN)
987         return (uint32_t)major(dev);
988 #else
989         return (uint32_t)(dev >> 8);
990 #endif
991 }
992
993 /****************************************************************************
994  Return the minor devicenumber for UNIX extensions.
995 ****************************************************************************/
996
997 uint32_t unix_dev_minor(SMB_DEV_T dev)
998 {
999 #if defined(HAVE_DEVICE_MINOR_FN)
1000         return (uint32_t)minor(dev);
1001 #else
1002         return (uint32_t)(dev & 0xff);
1003 #endif
1004 }
1005
1006 /**************************************************************************
1007  Wrapper for realpath.
1008 ****************************************************************************/
1009
1010 char *sys_realpath(const char *path)
1011 {
1012         char *result;
1013
1014 #ifdef REALPATH_TAKES_NULL
1015         result = realpath(path, NULL);
1016 #else
1017         result = SMB_MALLOC_ARRAY(char, PATH_MAX + 1);
1018         if (result) {
1019                 char *resolved_path = realpath(path, result);
1020                 if (!resolved_path) {
1021                         SAFE_FREE(result);
1022                 } else {
1023                         /* SMB_ASSERT(result == resolved_path) ? */
1024                         result = resolved_path;
1025                 }
1026         }
1027 #endif
1028         return result;
1029 }
1030
1031 #if 0
1032 /*******************************************************************
1033  Return the number of CPUs.
1034 ********************************************************************/
1035
1036 int sys_get_number_of_cores(void)
1037 {
1038         int ret = -1;
1039
1040 #if defined(HAVE_SYSCONF)
1041 #if defined(_SC_NPROCESSORS_ONLN)
1042         ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
1043 #endif
1044 #if defined(_SC_NPROCESSORS_CONF)
1045         if (ret < 1) {
1046                 ret = (int)sysconf(_SC_NPROCESSORS_CONF);
1047         }
1048 #endif
1049 #elif defined(HAVE_SYSCTL) && defined(CTL_HW)
1050         int name[2];
1051         unsigned int len = sizeof(ret);
1052
1053         name[0] = CTL_HW;
1054 #if defined(HW_AVAILCPU)
1055         name[1] = HW_AVAILCPU;
1056
1057         if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
1058                 ret = -1;
1059         }
1060 #endif
1061 #if defined(HW_NCPU)
1062         if(ret < 1) {
1063                 name[0] = CTL_HW;
1064                 name[1] = HW_NCPU;
1065                 if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
1066                         ret = -1;
1067                 }
1068         }
1069 #endif
1070 #endif
1071         if (ret < 1) {
1072                 ret = 1;
1073         }
1074         return ret;
1075 }
1076 #endif