libcli/auth: add netlogon_creds_cli_ServerGetTrustInfo*()
[Samba.git] / source3 / lib / system.c
blob7531d771ce98ba4964149955af02d7ede4e23146
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_calculated_birthtime = false;
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
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_calculated_birthtime = true;
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 ****************************************************************************/
324 void update_stat_ex_mtime(struct stat_ex *dst,
325 struct timespec write_ts)
327 dst->st_ex_mtime = write_ts;
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);
335 void update_stat_ex_create_time(struct stat_ex *dst,
336 struct timespec create_time)
338 dst->st_ex_btime = create_time;
339 dst->st_ex_calculated_birthtime = false;
342 void init_stat_ex_from_stat (struct stat_ex *dst,
343 const struct stat *src,
344 bool fake_dir_create_times)
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
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
370 #ifdef HAVE_STAT_ST_FLAGS
371 dst->st_ex_flags = src->st_flags;
372 #else
373 dst->st_ex_flags = 0;
374 #endif
377 /*******************************************************************
378 A stat() wrapper.
379 ********************************************************************/
381 int sys_stat(const char *fname, SMB_STRUCT_STAT *sbuf,
382 bool fake_dir_create_times)
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;
392 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
394 return ret;
397 /*******************************************************************
398 An fstat() wrapper.
399 ********************************************************************/
401 int sys_fstat(int fd, SMB_STRUCT_STAT *sbuf, bool fake_dir_create_times)
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;
411 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
413 return ret;
416 /*******************************************************************
417 An lstat() wrapper.
418 ********************************************************************/
420 int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf,
421 bool fake_dir_create_times)
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;
431 init_stat_ex_from_stat(sbuf, &statbuf, fake_dir_create_times);
433 return ret;
436 /*******************************************************************
437 An posix_fallocate() wrapper.
438 ********************************************************************/
439 int sys_posix_fallocate(int fd, off_t offset, off_t len)
441 #if defined(HAVE_POSIX_FALLOCATE) && !defined(HAVE_BROKEN_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;
450 /* unlikely to get a too large file on a 64bit system but ... */
451 if (new_len < 0)
452 return EFBIG;
454 fl.l_whence = SEEK_SET;
455 fl.l_start = offset;
456 fl.l_len = len;
458 ret=fcntl(fd, F_RESVSP64, &fl);
460 if (ret != 0)
461 return errno;
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
473 /*******************************************************************
474 An fallocate() function that matches the semantics of the Linux one.
475 ********************************************************************/
477 #ifdef HAVE_LINUX_FALLOC_H
478 #include <linux/falloc.h>
479 #endif
481 int sys_fallocate(int fd, enum vfs_fallocate_mode mode, off_t offset, off_t len)
483 #if defined(HAVE_LINUX_FALLOCATE64) || defined(HAVE_LINUX_FALLOCATE)
484 int lmode;
485 switch (mode) {
486 case VFS_FALLOCATE_EXTEND_SIZE:
487 lmode = 0;
488 break;
489 case VFS_FALLOCATE_KEEP_SIZE:
490 lmode = FALLOC_FL_KEEP_SIZE;
491 break;
492 default:
493 errno = EINVAL;
494 return -1;
496 #if defined(HAVE_LINUX_FALLOCATE)
497 return fallocate(fd, lmode, offset, len);
498 #endif
499 #else
500 /* TODO - plumb in fallocate from other filesysetms like VXFS etc. JRA. */
501 errno = ENOSYS;
502 return -1;
503 #endif
506 #if HAVE_KERNEL_SHARE_MODES
507 #ifndef LOCK_MAND
508 #define LOCK_MAND 32 /* This is a mandatory flock */
509 #define LOCK_READ 64 /* ... Which allows concurrent read operations */
510 #define LOCK_WRITE 128 /* ... Which allows concurrent write operations */
511 #define LOCK_RW 192 /* ... Which allows concurrent read & write ops */
512 #endif
513 #endif
515 /*******************************************************************
516 A flock() wrapper that will perform the kernel flock.
517 ********************************************************************/
519 void kernel_flock(int fd, uint32 share_mode, uint32 access_mask)
521 #if HAVE_KERNEL_SHARE_MODES
522 int kernel_mode = 0;
523 if (share_mode == FILE_SHARE_WRITE) {
524 kernel_mode = LOCK_MAND|LOCK_WRITE;
525 } else if (share_mode == FILE_SHARE_READ) {
526 kernel_mode = LOCK_MAND|LOCK_READ;
527 } else if (share_mode == FILE_SHARE_NONE) {
528 kernel_mode = LOCK_MAND;
530 if (kernel_mode) {
531 flock(fd, kernel_mode);
533 #endif
539 /*******************************************************************
540 An fdopendir wrapper.
541 ********************************************************************/
543 DIR *sys_fdopendir(int fd)
545 #if defined(HAVE_FDOPENDIR)
546 return fdopendir(fd);
547 #else
548 errno = ENOSYS;
549 return NULL;
550 #endif
553 /*******************************************************************
554 An mknod() wrapper.
555 ********************************************************************/
557 int sys_mknod(const char *path, mode_t mode, SMB_DEV_T dev)
559 #if defined(HAVE_MKNOD)
560 return mknod(path, mode, dev);
561 #else
562 /* No mknod system call. */
563 errno = ENOSYS;
564 return -1;
565 #endif
568 /*******************************************************************
569 The wait() calls vary between systems
570 ********************************************************************/
572 int sys_waitpid(pid_t pid,int *status,int options)
574 #ifdef HAVE_WAITPID
575 return waitpid(pid,status,options);
576 #else /* HAVE_WAITPID */
577 return wait4(pid, status, options, NULL);
578 #endif /* HAVE_WAITPID */
581 /*******************************************************************
582 System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
583 on error (malloc fail usually).
584 ********************************************************************/
586 char *sys_getwd(void)
588 #ifdef GETCWD_TAKES_NULL
589 return getcwd(NULL, 0);
590 #elif HAVE_GETCWD
591 char *wd = NULL, *s = NULL;
592 size_t allocated = PATH_MAX;
594 while (1) {
595 s = SMB_REALLOC_ARRAY(s, char, allocated);
596 if (s == NULL) {
597 return NULL;
599 wd = getcwd(s, allocated);
600 if (wd) {
601 break;
603 if (errno != ERANGE) {
604 SAFE_FREE(s);
605 break;
607 allocated *= 2;
608 if (allocated < PATH_MAX) {
609 SAFE_FREE(s);
610 break;
613 return wd;
614 #else
615 char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
616 if (s == NULL) {
617 return NULL;
619 return getwd(s);
620 #endif
623 #if defined(HAVE_POSIX_CAPABILITIES)
625 /**************************************************************************
626 Try and abstract process capabilities (for systems that have them).
627 ****************************************************************************/
629 /* Set the POSIX capabilities needed for the given purpose into the effective
630 * capability set of the current process. Make sure they are always removed
631 * from the inheritable set, because there is no circumstance in which our
632 * children should inherit our elevated privileges.
634 static bool set_process_capability(enum smbd_capability capability,
635 bool enable)
637 cap_value_t cap_vals[2] = {0};
638 int num_cap_vals = 0;
640 cap_t cap;
642 #if defined(HAVE_PRCTL) && defined(PR_GET_KEEPCAPS) && defined(PR_SET_KEEPCAPS)
643 /* On Linux, make sure that any capabilities we grab are sticky
644 * across UID changes. We expect that this would allow us to keep both
645 * the effective and permitted capability sets, but as of circa 2.6.16,
646 * only the permitted set is kept. It is a bug (which we work around)
647 * that the effective set is lost, but we still require the effective
648 * set to be kept.
650 if (!prctl(PR_GET_KEEPCAPS)) {
651 prctl(PR_SET_KEEPCAPS, 1);
653 #endif
655 cap = cap_get_proc();
656 if (cap == NULL) {
657 DEBUG(0,("set_process_capability: cap_get_proc failed: %s\n",
658 strerror(errno)));
659 return False;
662 switch (capability) {
663 case KERNEL_OPLOCK_CAPABILITY:
664 #ifdef CAP_NETWORK_MGT
665 /* IRIX has CAP_NETWORK_MGT for oplocks. */
666 cap_vals[num_cap_vals++] = CAP_NETWORK_MGT;
667 #endif
668 break;
669 case DMAPI_ACCESS_CAPABILITY:
670 #ifdef CAP_DEVICE_MGT
671 /* IRIX has CAP_DEVICE_MGT for DMAPI access. */
672 cap_vals[num_cap_vals++] = CAP_DEVICE_MGT;
673 #elif CAP_MKNOD
674 /* Linux has CAP_MKNOD for DMAPI access. */
675 cap_vals[num_cap_vals++] = CAP_MKNOD;
676 #endif
677 break;
678 case LEASE_CAPABILITY:
679 #ifdef CAP_LEASE
680 cap_vals[num_cap_vals++] = CAP_LEASE;
681 #endif
682 break;
683 case DAC_OVERRIDE_CAPABILITY:
684 #ifdef CAP_DAC_OVERRIDE
685 cap_vals[num_cap_vals++] = CAP_DAC_OVERRIDE;
686 #endif
689 SMB_ASSERT(num_cap_vals <= ARRAY_SIZE(cap_vals));
691 if (num_cap_vals == 0) {
692 cap_free(cap);
693 return True;
696 cap_set_flag(cap, CAP_EFFECTIVE, num_cap_vals, cap_vals,
697 enable ? CAP_SET : CAP_CLEAR);
699 /* We never want to pass capabilities down to our children, so make
700 * sure they are not inherited.
702 cap_set_flag(cap, CAP_INHERITABLE, num_cap_vals, cap_vals, CAP_CLEAR);
704 if (cap_set_proc(cap) == -1) {
705 DEBUG(0, ("set_process_capability: cap_set_proc failed: %s\n",
706 strerror(errno)));
707 cap_free(cap);
708 return False;
711 cap_free(cap);
712 return True;
715 #endif /* HAVE_POSIX_CAPABILITIES */
717 /****************************************************************************
718 Gain the oplock capability from the kernel if possible.
719 ****************************************************************************/
721 void set_effective_capability(enum smbd_capability capability)
723 #if defined(HAVE_POSIX_CAPABILITIES)
724 set_process_capability(capability, True);
725 #endif /* HAVE_POSIX_CAPABILITIES */
728 void drop_effective_capability(enum smbd_capability capability)
730 #if defined(HAVE_POSIX_CAPABILITIES)
731 set_process_capability(capability, False);
732 #endif /* HAVE_POSIX_CAPABILITIES */
735 /**************************************************************************
736 Wrapper for random().
737 ****************************************************************************/
739 long sys_random(void)
741 #if defined(HAVE_RANDOM)
742 return (long)random();
743 #elif defined(HAVE_RAND)
744 return (long)rand();
745 #else
746 DEBUG(0,("Error - no random function available !\n"));
747 exit(1);
748 #endif
751 /**************************************************************************
752 Wrapper for srandom().
753 ****************************************************************************/
755 void sys_srandom(unsigned int seed)
757 #if defined(HAVE_SRANDOM)
758 srandom(seed);
759 #elif defined(HAVE_SRAND)
760 srand(seed);
761 #else
762 DEBUG(0,("Error - no srandom function available !\n"));
763 exit(1);
764 #endif
767 #ifndef NGROUPS_MAX
768 #define NGROUPS_MAX 32 /* Guess... */
769 #endif
771 /**************************************************************************
772 Returns equivalent to NGROUPS_MAX - using sysconf if needed.
773 ****************************************************************************/
775 int groups_max(void)
777 #if defined(SYSCONF_SC_NGROUPS_MAX)
778 int ret = sysconf(_SC_NGROUPS_MAX);
779 return (ret == -1) ? NGROUPS_MAX : ret;
780 #else
781 return NGROUPS_MAX;
782 #endif
785 /**************************************************************************
786 Wrap setgroups and getgroups for systems that declare getgroups() as
787 returning an array of gid_t, but actuall return an array of int.
788 ****************************************************************************/
790 #if defined(HAVE_BROKEN_GETGROUPS)
792 #ifdef HAVE_BROKEN_GETGROUPS
793 #define GID_T int
794 #else
795 #define GID_T gid_t
796 #endif
798 static int sys_broken_getgroups(int setlen, gid_t *gidset)
800 GID_T gid;
801 GID_T *group_list;
802 int i, ngroups;
804 if(setlen == 0) {
805 return getgroups(setlen, &gid);
809 * Broken case. We need to allocate a
810 * GID_T array of size setlen.
813 if(setlen < 0) {
814 errno = EINVAL;
815 return -1;
818 if (setlen == 0)
819 setlen = groups_max();
821 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
822 DEBUG(0,("sys_getgroups: Malloc fail.\n"));
823 return -1;
826 if((ngroups = getgroups(setlen, group_list)) < 0) {
827 int saved_errno = errno;
828 SAFE_FREE(group_list);
829 errno = saved_errno;
830 return -1;
833 for(i = 0; i < ngroups; i++)
834 gidset[i] = (gid_t)group_list[i];
836 SAFE_FREE(group_list);
837 return ngroups;
840 static int sys_broken_setgroups(int setlen, gid_t *gidset)
842 GID_T *group_list;
843 int i ;
845 if (setlen == 0)
846 return 0 ;
848 if (setlen < 0 || setlen > groups_max()) {
849 errno = EINVAL;
850 return -1;
854 * Broken case. We need to allocate a
855 * GID_T array of size setlen.
858 if((group_list = SMB_MALLOC_ARRAY(GID_T, setlen)) == NULL) {
859 DEBUG(0,("sys_setgroups: Malloc fail.\n"));
860 return -1;
863 for(i = 0; i < setlen; i++)
864 group_list[i] = (GID_T) gidset[i];
866 if(samba_setgroups(setlen, group_list) != 0) {
867 int saved_errno = errno;
868 SAFE_FREE(group_list);
869 errno = saved_errno;
870 return -1;
873 SAFE_FREE(group_list);
874 return 0 ;
877 #endif /* HAVE_BROKEN_GETGROUPS */
879 /* This is a list of systems that require the first GID passed to setgroups(2)
880 * to be the effective GID. If your system is one of these, add it here.
882 #if defined (FREEBSD) || defined (DARWINOS)
883 #define USE_BSD_SETGROUPS
884 #endif
886 #if defined(USE_BSD_SETGROUPS)
887 /* Depending on the particular BSD implementation, the first GID that is
888 * passed to setgroups(2) will either be ignored or will set the credential's
889 * effective GID. In either case, the right thing to do is to guarantee that
890 * gidset[0] is the effective GID.
892 static int sys_bsd_setgroups(gid_t primary_gid, int setlen, const gid_t *gidset)
894 gid_t *new_gidset = NULL;
895 int max;
896 int ret;
898 /* setgroups(2) will fail with EINVAL if we pass too many groups. */
899 max = groups_max();
901 /* No group list, just make sure we are setting the efective GID. */
902 if (setlen == 0) {
903 return samba_setgroups(1, &primary_gid);
906 /* If the primary gid is not the first array element, grow the array
907 * and insert it at the front.
909 if (gidset[0] != primary_gid) {
910 new_gidset = SMB_MALLOC_ARRAY(gid_t, setlen + 1);
911 if (new_gidset == NULL) {
912 return -1;
915 memcpy(new_gidset + 1, gidset, (setlen * sizeof(gid_t)));
916 new_gidset[0] = primary_gid;
917 setlen++;
920 if (setlen > max) {
921 DEBUG(3, ("forced to truncate group list from %d to %d\n",
922 setlen, max));
923 setlen = max;
926 #if defined(HAVE_BROKEN_GETGROUPS)
927 ret = sys_broken_setgroups(setlen, new_gidset ? new_gidset : gidset);
928 #else
929 ret = samba_setgroups(setlen, new_gidset ? new_gidset : gidset);
930 #endif
932 if (new_gidset) {
933 int errsav = errno;
934 SAFE_FREE(new_gidset);
935 errno = errsav;
938 return ret;
941 #endif /* USE_BSD_SETGROUPS */
943 /**************************************************************************
944 Wrapper for getgroups. Deals with broken (int) case.
945 ****************************************************************************/
947 int sys_getgroups(int setlen, gid_t *gidset)
949 #if defined(HAVE_BROKEN_GETGROUPS)
950 return sys_broken_getgroups(setlen, gidset);
951 #else
952 return getgroups(setlen, gidset);
953 #endif
956 /**************************************************************************
957 Wrapper for setgroups. Deals with broken (int) case and BSD case.
958 ****************************************************************************/
960 int sys_setgroups(gid_t UNUSED(primary_gid), int setlen, gid_t *gidset)
962 #if !defined(HAVE_SETGROUPS)
963 errno = ENOSYS;
964 return -1;
965 #endif /* HAVE_SETGROUPS */
967 #if defined(USE_BSD_SETGROUPS)
968 return sys_bsd_setgroups(primary_gid, setlen, gidset);
969 #elif defined(HAVE_BROKEN_GETGROUPS)
970 return sys_broken_setgroups(setlen, gidset);
971 #else
972 return samba_setgroups(setlen, gidset);
973 #endif
976 /**************************************************************************
977 Extract a command into an arg list.
978 ****************************************************************************/
980 static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
982 char *trunc_cmd;
983 char *saveptr;
984 char *ptr;
985 int argcl;
986 char **argl = NULL;
987 int i;
989 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
990 DEBUG(0, ("talloc failed\n"));
991 goto nomem;
994 if(!(ptr = strtok_r(trunc_cmd, " \t", &saveptr))) {
995 TALLOC_FREE(trunc_cmd);
996 errno = EINVAL;
997 return NULL;
1001 * Count the args.
1004 for( argcl = 1; ptr; ptr = strtok_r(NULL, " \t", &saveptr))
1005 argcl++;
1007 TALLOC_FREE(trunc_cmd);
1009 if (!(argl = talloc_array(mem_ctx, char *, argcl + 1))) {
1010 goto nomem;
1014 * Now do the extraction.
1017 if (!(trunc_cmd = talloc_strdup(mem_ctx, command))) {
1018 goto nomem;
1021 ptr = strtok_r(trunc_cmd, " \t", &saveptr);
1022 i = 0;
1024 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1025 goto nomem;
1028 while((ptr = strtok_r(NULL, " \t", &saveptr)) != NULL) {
1030 if (!(argl[i++] = talloc_strdup(argl, ptr))) {
1031 goto nomem;
1035 argl[i++] = NULL;
1036 TALLOC_FREE(trunc_cmd);
1037 return argl;
1039 nomem:
1040 DEBUG(0, ("talloc failed\n"));
1041 TALLOC_FREE(trunc_cmd);
1042 TALLOC_FREE(argl);
1043 errno = ENOMEM;
1044 return NULL;
1047 /**************************************************************************
1048 Wrapper for popen. Safer as it doesn't search a path.
1049 Modified from the glibc sources.
1050 modified by tridge to return a file descriptor. We must kick our FILE* habit
1051 ****************************************************************************/
1053 typedef struct _popen_list
1055 int fd;
1056 pid_t child_pid;
1057 struct _popen_list *next;
1058 } popen_list;
1060 static popen_list *popen_chain;
1062 int sys_popen(const char *command)
1064 int parent_end, child_end;
1065 int pipe_fds[2];
1066 popen_list *entry = NULL;
1067 char **argl = NULL;
1068 int ret;
1070 if (!*command) {
1071 errno = EINVAL;
1072 return -1;
1075 ret = pipe(pipe_fds);
1076 if (ret < 0) {
1077 DEBUG(0, ("sys_popen: error opening pipe: %s\n",
1078 strerror(errno)));
1079 return -1;
1082 parent_end = pipe_fds[0];
1083 child_end = pipe_fds[1];
1085 entry = SMB_MALLOC_P(popen_list);
1086 if (entry == NULL) {
1087 DEBUG(0, ("sys_popen: malloc failed\n"));
1088 goto err_exit;
1091 ZERO_STRUCTP(entry);
1094 * Extract the command and args into a NULL terminated array.
1097 argl = extract_args(NULL, command);
1098 if (argl == NULL) {
1099 DEBUG(0, ("sys_popen: extract_args() failed: %s\n", strerror(errno)));
1100 goto err_exit;
1103 entry->child_pid = fork();
1105 if (entry->child_pid == -1) {
1106 DEBUG(0, ("sys_popen: fork failed: %s\n", strerror(errno)));
1107 goto err_exit;
1110 if (entry->child_pid == 0) {
1113 * Child !
1116 int child_std_end = STDOUT_FILENO;
1117 popen_list *p;
1119 close(parent_end);
1120 if (child_end != child_std_end) {
1121 dup2 (child_end, child_std_end);
1122 close (child_end);
1126 * POSIX.2: "popen() shall ensure that any streams from previous
1127 * popen() calls that remain open in the parent process are closed
1128 * in the new child process."
1131 for (p = popen_chain; p; p = p->next)
1132 close(p->fd);
1134 ret = execv(argl[0], argl);
1135 if (ret == -1) {
1136 DEBUG(0, ("sys_popen: ERROR executing command "
1137 "'%s': %s\n", command, strerror(errno)));
1139 _exit (127);
1143 * Parent.
1146 close (child_end);
1147 TALLOC_FREE(argl);
1149 /* Link into popen_chain. */
1150 entry->next = popen_chain;
1151 popen_chain = entry;
1152 entry->fd = parent_end;
1154 return entry->fd;
1156 err_exit:
1158 SAFE_FREE(entry);
1159 TALLOC_FREE(argl);
1160 close(pipe_fds[0]);
1161 close(pipe_fds[1]);
1162 return -1;
1165 /**************************************************************************
1166 Wrapper for pclose. Modified from the glibc sources.
1167 ****************************************************************************/
1169 int sys_pclose(int fd)
1171 int wstatus;
1172 popen_list **ptr = &popen_chain;
1173 popen_list *entry = NULL;
1174 pid_t wait_pid;
1175 int status = -1;
1177 /* Unlink from popen_chain. */
1178 for ( ; *ptr != NULL; ptr = &(*ptr)->next) {
1179 if ((*ptr)->fd == fd) {
1180 entry = *ptr;
1181 *ptr = (*ptr)->next;
1182 status = 0;
1183 break;
1187 if (status < 0 || close(entry->fd) < 0)
1188 return -1;
1191 * As Samba is catching and eating child process
1192 * exits we don't really care about the child exit
1193 * code, a -1 with errno = ECHILD will do fine for us.
1196 do {
1197 wait_pid = sys_waitpid (entry->child_pid, &wstatus, 0);
1198 } while (wait_pid == -1 && errno == EINTR);
1200 SAFE_FREE(entry);
1202 if (wait_pid == -1)
1203 return -1;
1204 return wstatus;
1207 /****************************************************************************
1208 Return the major devicenumber for UNIX extensions.
1209 ****************************************************************************/
1211 uint32 unix_dev_major(SMB_DEV_T dev)
1213 #if defined(HAVE_DEVICE_MAJOR_FN)
1214 return (uint32)major(dev);
1215 #else
1216 return (uint32)(dev >> 8);
1217 #endif
1220 /****************************************************************************
1221 Return the minor devicenumber for UNIX extensions.
1222 ****************************************************************************/
1224 uint32 unix_dev_minor(SMB_DEV_T dev)
1226 #if defined(HAVE_DEVICE_MINOR_FN)
1227 return (uint32)minor(dev);
1228 #else
1229 return (uint32)(dev & 0xff);
1230 #endif
1233 #if 0
1234 /*******************************************************************
1235 Return the number of CPUs.
1236 ********************************************************************/
1238 int sys_get_number_of_cores(void)
1240 int ret = -1;
1242 #if defined(HAVE_SYSCONF)
1243 #if defined(_SC_NPROCESSORS_ONLN)
1244 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
1245 #endif
1246 #if defined(_SC_NPROCESSORS_CONF)
1247 if (ret < 1) {
1248 ret = (int)sysconf(_SC_NPROCESSORS_CONF);
1250 #endif
1251 #elif defined(HAVE_SYSCTL) && defined(CTL_HW)
1252 int name[2];
1253 unsigned int len = sizeof(ret);
1255 name[0] = CTL_HW;
1256 #if defined(HW_AVAILCPU)
1257 name[1] = HW_AVAILCPU;
1259 if (sysctl(name, 2, &ret, &len, NULL, 0) == -1) {
1260 ret = -1;
1262 #endif
1263 #if defined(HW_NCPU)
1264 if(ret < 1) {
1265 name[0] = CTL_HW;
1266 name[1] = HW_NCPU;
1267 if (sysctl(nm, 2, &count, &len, NULL, 0) == -1) {
1268 ret = -1;
1271 #endif
1272 #endif
1273 if (ret < 1) {
1274 ret = 1;
1276 return ret;
1278 #endif