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