s4:torture: Use samba_gnutls_arcfour_confounded_md5() in test_ChangePasswordRandomBytes
[Samba.git] / source3 / lib / system.c
bloba67388e436a8193785875751885b8458c20b85f7
1 /*
2 Unix SMB/CIFS implementation.
3 Samba system utilities
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1998-2005
6 Copyright (C) Timur Bakeyev 2005
7 Copyright (C) Bjoern Jacke 2006-2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
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"
30 #ifdef HAVE_SYS_SYSCTL_H
31 #include <sys/sysctl.h>
32 #endif
34 #ifdef HAVE_SYS_PRCTL_H
35 #include <sys/prctl.h>
36 #endif
39 The idea is that this file will eventually have wrappers around all
40 important system calls in samba. The aims are:
42 - to enable easier porting by putting OS dependent stuff in here
44 - to allow for hooks into other "pseudo-filesystems"
46 - to allow easier integration of things like the japanese extensions
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.
53 /*******************************************************************
54 A send wrapper that will deal with EINTR or EAGAIN or EWOULDBLOCK.
55 ********************************************************************/
57 ssize_t sys_send(int s, const void *msg, size_t len, int flags)
59 ssize_t ret;
61 do {
62 ret = send(s, msg, len, flags);
63 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
65 return ret;
68 /*******************************************************************
69 A recvfrom wrapper that will deal with EINTR.
70 NB. As used with non-blocking sockets, return on EAGAIN/EWOULDBLOCK
71 ********************************************************************/
73 ssize_t sys_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
75 ssize_t ret;
77 do {
78 ret = recvfrom(s, buf, len, flags, from, fromlen);
79 } while (ret == -1 && (errno == EINTR));
80 return ret;
83 /*******************************************************************
84 A fcntl wrapper that will deal with EINTR.
85 ********************************************************************/
87 int sys_fcntl_ptr(int fd, int cmd, void *arg)
89 int ret;
91 do {
92 ret = fcntl(fd, cmd, arg);
93 } while (ret == -1 && errno == EINTR);
94 return ret;
97 /*******************************************************************
98 A fcntl wrapper that will deal with EINTR.
99 ********************************************************************/
101 int sys_fcntl_long(int fd, int cmd, long arg)
103 int ret;
105 do {
106 ret = fcntl(fd, cmd, arg);
107 } while (ret == -1 && errno == EINTR);
108 return ret;
111 /****************************************************************************
112 Get/Set all the possible time fields from a stat struct as a timespec.
113 ****************************************************************************/
115 static struct timespec get_atimespec(const struct stat *pst)
117 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
118 struct timespec ret;
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
153 static struct timespec get_mtimespec(const struct stat *pst)
155 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
156 struct timespec ret;
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
191 static struct timespec get_ctimespec(const struct stat *pst)
193 #if !defined(HAVE_STAT_HIRES_TIMESTAMPS)
194 struct timespec ret;
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
229 /****************************************************************************
230 Return the best approximation to a 'create time' under UNIX from a stat
231 structure.
232 ****************************************************************************/
234 static struct timespec calc_create_time_stat(const struct stat *st)
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);
241 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
242 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
244 if(!null_timespec(ret1)) {
245 return ret1;
249 * One of ctime, mtime or atime was zero (probably atime).
250 * Just return MIN(ctime, mtime).
252 return ret;
255 /****************************************************************************
256 Return the best approximation to a 'create time' under UNIX from a stat_ex
257 structure.
258 ****************************************************************************/
260 static struct timespec calc_create_time_stat_ex(const struct stat_ex *st)
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;
267 ret = timespec_compare(&c_time, &m_time) < 0 ? c_time : m_time;
268 ret1 = timespec_compare(&ret, &a_time) < 0 ? ret : a_time;
270 if(!null_timespec(ret1)) {
271 return ret1;
275 * One of ctime, mtime or atime was zero (probably atime).
276 * Just return MIN(ctime, mtime).
278 return ret;
281 /****************************************************************************
282 Return the 'create time' from a stat struct if it exists (birthtime) or else
283 use the best approximation.
284 ****************************************************************************/
286 static void make_create_timespec(const struct stat *pst, struct stat_ex *dst,
287 bool fake_dir_create_times)
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;
294 dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_BTIME;
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
309 /* Deal with systems that don't initialize birthtime correctly.
310 * Pointed out by SATOH Fumiyasu <fumiyas@osstech.jp>.
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;
317 dst->st_ex_itime = dst->st_ex_btime;
318 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_ITIME;
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 ****************************************************************************/
327 void update_stat_ex_mtime(struct stat_ex *dst,
328 struct timespec write_ts)
330 dst->st_ex_mtime = write_ts;
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);
338 void update_stat_ex_create_time(struct stat_ex *dst,
339 struct timespec create_time)
341 dst->st_ex_btime = create_time;
342 dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_BTIME;
345 void update_stat_ex_itime(struct stat_ex *dst,
346 struct timespec itime)
348 dst->st_ex_itime = itime;
349 dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_ITIME;
352 void update_stat_ex_file_id(struct stat_ex *dst, uint64_t file_id)
354 dst->st_ex_file_id = file_id;
355 dst->st_ex_iflags &= ~ST_EX_IFLAG_CALCULATED_FILE_ID;
358 void init_stat_ex_from_stat (struct stat_ex *dst,
359 const struct stat *src,
360 bool fake_dir_create_times)
362 dst->st_ex_dev = src->st_dev;
363 dst->st_ex_ino = src->st_ino;
364 dst->st_ex_mode = src->st_mode;
365 dst->st_ex_nlink = src->st_nlink;
366 dst->st_ex_uid = src->st_uid;
367 dst->st_ex_gid = src->st_gid;
368 dst->st_ex_rdev = src->st_rdev;
369 dst->st_ex_size = src->st_size;
370 dst->st_ex_atime = get_atimespec(src);
371 dst->st_ex_mtime = get_mtimespec(src);
372 dst->st_ex_ctime = get_ctimespec(src);
373 dst->st_ex_iflags = 0;
374 make_create_timespec(src, dst, fake_dir_create_times);
375 #ifdef HAVE_STAT_ST_BLKSIZE
376 dst->st_ex_blksize = src->st_blksize;
377 #else
378 dst->st_ex_blksize = STAT_ST_BLOCKSIZE;
379 #endif
381 #ifdef HAVE_STAT_ST_BLOCKS
382 dst->st_ex_blocks = src->st_blocks;
383 #else
384 dst->st_ex_blocks = src->st_size / dst->st_ex_blksize + 1;
385 #endif
387 #ifdef HAVE_STAT_ST_FLAGS
388 dst->st_ex_flags = src->st_flags;
389 #else
390 dst->st_ex_flags = 0;
391 #endif
392 dst->st_ex_file_id = dst->st_ex_ino;
393 dst->st_ex_iflags |= ST_EX_IFLAG_CALCULATED_FILE_ID;
396 /*******************************************************************
397 A stat() wrapper.
398 ********************************************************************/
400 int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
401 bool fake_dir_create_times)
403 int ret;
404 struct stat statbuf;
405 ret = stat(fname, &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;
411 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
413 return ret;
416 /*******************************************************************
417 An fstat() wrapper.
418 ********************************************************************/
420 int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
422 int ret;
423 struct stat statbuf;
424 ret = fstat(fd, &statbuf);
425 if (ret == 0) {
426 /* we always want directories to appear zero size */
427 if (S_ISDIR(statbuf.st_mode)) {
428 statbuf.st_size = 0;
430 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
432 return ret;
435 /*******************************************************************
436 An lstat() wrapper.
437 ********************************************************************/
439 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
440 bool fake_dir_create_times)
442 int ret;
443 struct stat statbuf;
444 ret = lstat(fname, &statbuf);
445 if (ret == 0) {
446 /* we always want directories to appear zero size */
447 if (S_ISDIR(statbuf.st_mode)) {
448 statbuf.st_size = 0;
450 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
452 return ret;
455 /*******************************************************************
456 An posix_fallocate() wrapper.
457 ********************************************************************/
458 int sys_posix_fallocate(int fd, off_t offset, off_t len)
460 #if defined(HAVE_POSIX_FALLOCATE)
461 return posix_fallocate(fd, offset, len);
462 #elif defined(F_RESVSP64)
463 /* this handles XFS on IRIX */
464 struct flock64 fl;
465 off_t new_len = offset + len;
466 int ret;
467 struct stat64 sbuf;
469 /* unlikely to get a too large file on a 64bit system but ... */
470 if (new_len < 0)
471 return EFBIG;
473 fl.l_whence = SEEK_SET;
474 fl.l_start = offset;
475 fl.l_len = len;
477 ret=fcntl(fd, F_RESVSP64, &fl);
479 if (ret != 0)
480 return errno;
482 /* Make sure the file gets enlarged after we allocated space: */
483 fstat64(fd, &sbuf);
484 if (new_len > sbuf.st_size)
485 ftruncate64(fd, new_len);
486 return 0;
487 #else
488 return ENOSYS;
489 #endif
492 /*******************************************************************
493 An fallocate() function that matches the semantics of the Linux one.
494 ********************************************************************/
496 #ifdef HAVE_LINUX_FALLOC_H
497 #include <linux/falloc.h>
498 #endif
500 int sys_fallocate(int fd, uint32_t mode, off_t offset, off_t len)
502 #if defined(HAVE_LINUX_FALLOCATE)
503 int lmode = 0;
505 if (mode & VFS_FALLOCATE_FL_KEEP_SIZE) {
506 lmode |= FALLOC_FL_KEEP_SIZE;
507 mode &= ~VFS_FALLOCATE_FL_KEEP_SIZE;
510 #if defined(HAVE_FALLOC_FL_PUNCH_HOLE)
511 if (mode & VFS_FALLOCATE_FL_PUNCH_HOLE) {
512 lmode |= FALLOC_FL_PUNCH_HOLE;
513 mode &= ~VFS_FALLOCATE_FL_PUNCH_HOLE;
515 #endif /* HAVE_FALLOC_FL_PUNCH_HOLE */
517 if (mode != 0) {
518 DEBUG(2, ("unmapped fallocate flags: %lx\n",
519 (unsigned long)mode));
520 errno = EINVAL;
521 return -1;
523 return fallocate(fd, lmode, offset, len);
524 #else /* HAVE_LINUX_FALLOCATE */
525 /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
526 errno = ENOSYS;
527 return -1;
528 #endif /* HAVE_LINUX_FALLOCATE */
531 #ifdef HAVE_KERNEL_SHARE_MODES
532 #ifndef LOCK_MAND
533 #define LOCK_MAND 32 /* This is a mandatory flock */
534 #define LOCK_READ 64 /* ... Which allows concurrent read operations */
535 #define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
536 #define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
537 #endif
538 #endif
540 /*******************************************************************
541 A flock() wrapper that will perform the kernel flock.
542 ********************************************************************/
544 void kernel_flock(int fd, uint32_t share_mode, uint32_t access_mask)
546 #ifdef HAVE_KERNEL_SHARE_MODES
547 int kernel_mode = 0;
548 if (share_mode == FILE_SHARE_WRITE) {
549 kernel_mode = LOCK_MAND|LOCK_WRITE;
550 } else if (share_mode == FILE_SHARE_READ) {
551 kernel_mode = LOCK_MAND|LOCK_READ;
552 } else if (share_mode == FILE_SHARE_NONE) {
553 kernel_mode = LOCK_MAND;
555 if (kernel_mode) {
556 flock(fd, kernel_mode);
558 #endif
564 /*******************************************************************
565 An fdopendir wrapper.
566 ********************************************************************/
568 DIR *sys_fdopendir(int fd)
570 #if defined(HAVE_FDOPENDIR)
571 return fdopendir(fd);
572 #else
573 errno = ENOSYS;
574 return NULL;
575 #endif
578 /*******************************************************************
579 An mknod() wrapper.
580 ********************************************************************/
582 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
584 #if defined(HAVE_MKNOD)
585 return mknod(path, mode, dev);
586 #else
587 /* No mknod system call. */
588 errno = ENOSYS;
589 return -1;
590 #endif
593 /*******************************************************************
594 System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
595 on error (malloc fail usually).
596 ********************************************************************/
598 char *sys_getwd(void)
600 #ifdef GETCWD_TAKES_NULL
601 return getcwd(NULL, 0);
602 #elif defined(HAVE_GETCWD)
603 char *wd = NULL, *s = NULL;
604 size_t allocated = PATH_MAX;
606 while (1) {
607 s = SMB_REALLOC_ARRAY(s, char, allocated);
608 if (s == NULL) {
609 return NULL;
611 wd = getcwd(s, allocated);
612 if (wd) {
613 break;
615 if (errno != ERANGE) {
616 int saved_errno = errno;
617 SAFE_FREE(s);
618 errno = saved_errno;
619 break;
621 allocated *= 2;
622 if (allocated < PATH_MAX) {
623 SAFE_FREE(s);
624 break;
627 return wd;
628 #else
629 char *wd = NULL;
630 char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
631 if (s == NULL) {
632 return NULL;
634 wd = getwd(s);
635 if (wd == NULL) {
636 int saved_errno = errno;
637 SAFE_FREE(s);
638 errno = saved_errno;
640 return wd;
641 #endif
644 #if defined(HAVE_POSIX_CAPABILITIES)
646 /**************************************************************************
647 Try and abstract process capabilities (for systems that have them).
648 ****************************************************************************/
650 /* Set the POSIX capabilities needed for the given purpose into the effective
651 * capability set of the current process. Make sure they are always removed
652 * from the inheritable set, because there is no circumstance in which our
653 * children should inherit our elevated privileges.
655 static bool set_process_capability(enum smbd_capability capability,
656 bool enable)
658 cap_value_t cap_vals[2] = {0};
659 int num_cap_vals = 0;
661 cap_t cap;
663 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
664 /* On Linux, make sure that any capabilities we grab are sticky
665 * across UID changes. We expect that this would allow us to keep both
666 * the effective and permitted capability sets, but as of circa 2.6.16,
667 * only the permitted set is kept. It is a bug (which we work around)
668 * that the effective set is lost, but we still require the effective
669 * set to be kept.
671 if (!prctl(PR_GET_KEEPCAPS)) {
672 prctl(PR_SET_KEEPCAPS, 1);
674 #endif
676 cap = cap_get_proc();
677 if (cap == NULL) {
678 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
679 strerror(errno)));
680 return False;
683 switch (capability) {
684 case KERNEL_OPLOCK_CAPABILITY:
685 #ifdef CAP_NETWORK_MGT
686 /* IRIX has CAP_NETWORK_MGT for oplocks. */
687 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
688 #endif
689 break;
690 case DMAPI_ACCESS_CAPABILITY:
691 #ifdef CAP_DEVICE_MGT
692 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
693 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
694 #elif CAP_MKNOD
695 /* Linux has CAP_MKNOD for DMAPI access. */
696 cap_vals[num_cap_vals++] = CAP_MKNOD;
697 #endif
698 break;
699 case LEASE_CAPABILITY:
700 #ifdef CAP_LEASE
701 cap_vals[num_cap_vals++] = CAP_LEASE;
702 #endif
703 break;
704 case DAC_OVERRIDE_CAPABILITY:
705 #ifdef CAP_DAC_OVERRIDE
706 cap_vals[num_cap_vals++] = CAP_DAC_OVERRIDE;
707 #endif
710 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
712 if (num_cap_vals == 0) {
713 cap_free(cap);
714 return True;
717 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
718 enable ? CAP_SET : CAP_CLEAR);
720 /* We never want to pass capabilities down to our children, so make
721 * sure they are not inherited.
723 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
725 if (cap_set_proc(cap) == -1) {
726 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
727 strerror(errno)));
728 cap_free(cap);
729 return False;
732 cap_free(cap);
733 return True;
736 #endif /* HAVE_POSIX_CAPABILITIES */
738 /****************************************************************************
739 Gain the oplock capability from the kernel if possible.
740 ****************************************************************************/
742 void set_effective_capability(enum smbd_capability capability)
744 #if defined(HAVE_POSIX_CAPABILITIES)
745 set_process_capability(capability, True);
746 #endif /* HAVE_POSIX_CAPABILITIES */
749 void drop_effective_capability(enum smbd_capability capability)
751 #if defined(HAVE_POSIX_CAPABILITIES)
752 set_process_capability(capability, False);
753 #endif /* HAVE_POSIX_CAPABILITIES */
756 /**************************************************************************
757 Wrapper for random().
758 ****************************************************************************/
760 long sys_random(void)
762 #if defined(HAVE_RANDOM)
763 return (long)random();
764 #elif defined(HAVE_RAND)
765 return (long)rand();
766 #else
767 DEBUG(0,("Error - no random function available !\n"));
768 exit(1);
769 #endif
772 /**************************************************************************
773 Wrapper for srandom().
774 ****************************************************************************/
776 void sys_srandom(unsigned int seed)
778 #if defined(HAVE_SRANDOM)
779 srandom(seed);
780 #elif defined(HAVE_SRAND)
781 srand(seed);
782 #else
783 DEBUG(0,("Error - no srandom function available !\n"));
784 exit(1);
785 #endif
788 #ifndef NGROUPS_MAX
789 #define NGROUPS_MAX 32 /* Guess... */
790 #endif
792 /**************************************************************************
793 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
794 ****************************************************************************/
796 int groups_max(void)
798 #if defined(SYSCONF_SC_NGROUPS_MAX)
799 int ret = sysconf(_SC_NGROUPS_MAX);
800 return (ret == -1) ? NGROUPS_MAX : ret;
801 #else
802 return NGROUPS_MAX;
803 #endif
806 /**************************************************************************
807 Wrap setgroups and getgroups for systems that declare getgroups() as
808 returning an array of gid_t, but actuall return an array of int.
809 ****************************************************************************/
811 #if defined(HAVE_BROKEN_GETGROUPS)
813 #ifdef HAVE_BROKEN_GETGROUPS
814 #define GID_T int
815 #else
816 #define GID_T gid_t
817 #endif
819 static int sys_broken_getgroups(int setlen, gid_t *gidset)
821 GID_T *group_list;
822 int i, ngroups;
824 if(setlen == 0) {
825 return getgroups(0, NULL);
829 * Broken case. We need to allocate a
830 * GID_T array of size setlen.
833 if(setlen < 0) {
834 errno = EINVAL;
835 return -1;
838 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
839 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
840 return -1;
843 if((ngroups = getgroups(setlen, group_list)) < 0) {
844 int saved_errno = errno;
845 SAFE_FREE(group_list);
846 errno = saved_errno;
847 return -1;
851 * We're safe here as if ngroups > setlen then
852 * getgroups *must* return EINVAL.
853 * pubs.opengroup.org/onlinepubs/009695399/functions/getgroups.html
856 for(i = 0; i < ngroups; i++)
857 gidset[i] = (gid_t)group_list[i];
859 SAFE_FREE(group_list);
860 return ngroups;
863 static int sys_broken_setgroups(int setlen, gid_t *gidset)
865 GID_T *group_list;
866 int i ;
868 if (setlen == 0)
869 return 0 ;
871 if (setlen < 0 || setlen > groups_max()) {
872 errno = EINVAL;
873 return -1;
877 * Broken case. We need to allocate a
878 * GID_T array of size setlen.
881 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
882 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
883 return -1;
886 for(i = 0; i < setlen; i++)
887 group_list[i] = (GID_T) gidset[i];
889 if(samba_setgroups(setlen, group_list) != 0) {
890 int saved_errno = errno;
891 SAFE_FREE(group_list);
892 errno = saved_errno;
893 return -1;
896 SAFE_FREE(group_list);
897 return 0 ;
900 #endif /* HAVE_BROKEN_GETGROUPS */
902 /* This is a list of systems that require the first GID passed to setgroups(2)
903 * to be the effective GID. If your system is one of these, add it here.
905 #if defined (FREEBSD) || defined (DARWINOS)
906 #define USE_BSD_SETGROUPS
907 #endif
909 #if defined(USE_BSD_SETGROUPS)
910 /* Depending on the particular BSD implementation, the first GID that is
911 * passed to setgroups(2) will either be ignored or will set the credential's
912 * effective GID. In either case, the right thing to do is to guarantee that
913 * gidset[0] is the effective GID.
915 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
917 gid_t *new_gidset = NULL;
918 int max;
919 int ret;
921 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
922 max = groups_max();
924 /* No group list, just make sure we are setting the efective GID. */
925 if (setlen == 0) {
926 return samba_setgroups(1, &primary_gid);
929 /* If the primary gid is not the first array element, grow the array
930 * and insert it at the front.
932 if (gidset[0] != primary_gid) {
933 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
934 if (new_gidset == NULL) {
935 return -1;
938 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
939 new_gidset[0] = primary_gid;
940 setlen++;
943 if (setlen > max) {
944 DEBUG(3, ("forced to truncate group list from %d to %d\n",
945 setlen, max));
946 setlen = max;
949 #if defined(HAVE_BROKEN_GETGROUPS)
950 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
951 #else
952 ret = samba_setgroups(setlen, new_gidset ? new_gidset : gidset);
953 #endif
955 if (new_gidset) {
956 int errsav = errno;
957 SAFE_FREE(new_gidset);
958 errno = errsav;
961 return ret;
964 #endif /* USE_BSD_SETGROUPS */
966 /**************************************************************************
967 Wrapper for getgroups. Deals with broken (int) case.
968 ****************************************************************************/
970 int sys_getgroups(int setlen, gid_t *gidset)
972 #if defined(HAVE_BROKEN_GETGROUPS)
973 return sys_broken_getgroups(setlen, gidset);
974 #else
975 return getgroups(setlen, gidset);
976 #endif
979 /**************************************************************************
980 Wrapper for setgroups. Deals with broken (int) case and BSD case.
981 ****************************************************************************/
983 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
985 #if !defined(HAVE_SETGROUPS)
986 errno = ENOSYS;
987 return -1;
988 #endif /* HAVE_SETGROUPS */
990 #if defined(USE_BSD_SETGROUPS)
991 return sys_bsd_setgroups(primary_gid, setlen, gidset);
992 #elif defined(HAVE_BROKEN_GETGROUPS)
993 return sys_broken_setgroups(setlen, gidset);
994 #else
995 return samba_setgroups(setlen, gidset);
996 #endif
999 /****************************************************************************
1000 Return the major devicenumber for UNIX extensions.
1001 ****************************************************************************/
1003 uint32_t unix_dev_major(SMB_DEV_T dev)
1005 #if defined(HAVE_DEVICE_MAJOR_FN)
1006 return (uint32_t)major(dev);
1007 #else
1008 return (uint32_t)(dev >> 8);
1009 #endif
1012 /****************************************************************************
1013 Return the minor devicenumber for UNIX extensions.
1014 ****************************************************************************/
1016 uint32_t unix_dev_minor(SMB_DEV_T dev)
1018 #if defined(HAVE_DEVICE_MINOR_FN)
1019 return (uint32_t)minor(dev);
1020 #else
1021 return (uint32_t)(dev & 0xff);
1022 #endif
1025 /**************************************************************************
1026 Wrapper for realpath.
1027 ****************************************************************************/
1029 char *sys_realpath(const char *path)
1031 char *result;
1033 #ifdef REALPATH_TAKES_NULL
1034 result = realpath(path, NULL);
1035 #else
1036 result = SMB_MALLOC_ARRAY(char, PATH_MAX + 1);
1037 if (result) {
1038 char *resolved_path = realpath(path, result);
1039 if (!resolved_path) {
1040 SAFE_FREE(result);
1041 } else {
1042 /* SMB_ASSERT(result == resolved_path) ? */
1043 result = resolved_path;
1046 #endif
1047 return result;
1050 #if 0
1051 /*******************************************************************
1052 Return the number of CPUs.
1053 ********************************************************************/
1055 int sys_get_number_of_cores(void)
1057 int ret = -1;
1059 #if defined(HAVE_SYSCONF)
1060 #if defined(_SC_NPROCESSORS_ONLN)
1061 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
1062 #endif
1063 #if defined(_SC_NPROCESSORS_CONF)
1064 if (ret < 1) {
1065 ret = (int)sysconf(_SC_NPROCESSORS_CONF);
1067 #endif
1068 #elif defined(HAVE_SYSCTL) && defined(CTL_HW)
1069 int name[2];
1070 unsigned int len = sizeof(ret);
1072 name[0] = CTL_HW;
1073 #if defined(HW_AVAILCPU)
1074 name[1] = HW_AVAILCPU;
1076 if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
1077 ret = -1;
1079 #endif
1080 #if defined(HW_NCPU)
1081 if(ret < 1) {
1082 name[0] = CTL_HW;
1083 name[1] = HW_NCPU;
1084 if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
1085 ret = -1;
1088 #endif
1089 #endif
1090 if (ret < 1) {
1091 ret = 1;
1093 return ret;
1095 #endif