1 /**
2    @file daemon_util.h
3    @brief common unix system utility functions
4 
5    This file is part of harvid
6 
7    @author Robin Gareus <robin@gareus.org>
8    @copyright
9 
10    Copyright (C) 2002,2003,2008-2013 Robin Gareus <robin@gareus.org>
11 
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16 
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 #ifndef _dutil_H
26 #define _dutil_H
27 
28 #ifdef HAVE_WINDOWS
29 typedef int gid_t;
30 typedef int uid_t;
31 #else
32 #include <unistd.h>
33 #endif
34 
35 /**
36  * fork the current process in the background and close
37  * standard-file descriptors.
38  * @return 0 if successful, -1 on error (fork failed)
39  */
40 int daemonize (void);
41 
42 
43 /**
44  * resolve unix-user-name or ID to integer
45  * @param setuid_user unix user name or ID
46  * @return 0 on error, uid otherwise (root cannot be looked up)
47  */
48 uid_t resolve_uid(const char *setuid_user);
49 
50 /**
51  * resolve unix-group-name or ID to integer
52  * @param setgid_group unix group name or ID
53  * @return 0 on error, gid otherwise (root cannot be looked up)
54  */
55 gid_t resolve_gid(const char *setgid_group);
56 
57 /**
58  * assume a differernt user identity - drop root privileges.
59  * @param uid unix user id
60  * @param gid unix group id
61  * @return 0 if successful, negative number on error
62  */
63 int drop_privileges(const gid_t uid, const gid_t gid);
64 
65 /**
66  * change root - jail the daemon to confined path on the system
67  * @param chroot_dir root directory of the server.
68  * @return 0 if successful, negative number on error
69  */
70 int do_chroot (char *chroot_dir);
71 #endif
72