ac0516e422816519339f1953c6862bb61a6eb681
[debian/uanytun.git] / src / daemon.h
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methodes used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *  
17  *
18  *  Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3 as
24  *  published by the Free Software Foundation.
25  *
26  *  uAnytun is distributed in the hope that it will be useful,
27  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *  GNU General Public License for more details.
30  *
31  *  You should have received a copy of the GNU General Public License
32  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
33  */
34
35 #ifndef _DAEMON_H_
36 #define _DAEMON_H_
37
38 #include <poll.h>
39 #include <fcntl.h>
40 #include <pwd.h>
41 #include <grp.h>
42 #include <sys/wait.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 struct priv_info_struct {
47   struct passwd* pw_;
48   struct group* gr_;
49 };
50 typedef struct priv_info_struct priv_info_t;
51
52 int priv_init(priv_info_t* priv, const char* username, const char* groupname)
53 {
54   if(!priv)
55     return -1;
56
57   priv->pw_ = NULL;
58   priv->gr_ = NULL;
59
60   priv->pw_ = getpwnam(username);
61   if(!priv->pw_) {
62     log_printf(ERROR, "unkown user %s", username);
63     return -1;
64   }
65
66   if(groupname)
67     priv->gr_ = getgrnam(groupname);
68   else
69     priv->gr_ = getgrgid(priv->pw_->pw_gid);
70
71   if(!priv->gr_) {
72     log_printf(ERROR, "unkown group %s", groupname);
73     return -1;
74   }
75
76   return 0;
77 }
78
79 int priv_drop(priv_info_t* priv)
80 {
81   if(!priv || !priv->pw_ || !priv->gr_) {
82     log_printf(ERROR, "privileges not initialized properly");
83     return -1;
84   }
85
86   if(setgid(priv->gr_->gr_gid))  {
87     log_printf(ERROR, "setgid('%s') failed: %s", priv->gr_->gr_name, strerror(errno));
88     return -1;
89   }
90
91   gid_t gr_list[1];
92   gr_list[0] = priv->gr_->gr_gid;
93   if(setgroups (1, gr_list)) {
94     log_printf(ERROR, "setgroups(['%s']) failed: %s", priv->gr_->gr_name, strerror(errno));
95     return -1;
96   }
97
98   if(setuid(priv->pw_->pw_uid)) {
99     log_printf(ERROR, "setuid('%s') failed: %s", priv->pw_->pw_name, strerror(errno));
100     return -1;
101   }
102
103   log_printf(NOTICE, "dropped privileges to %s:%s", priv->pw_->pw_name, priv->gr_->gr_name);
104   return 0;
105 }
106
107
108 int do_chroot(const char* chrootdir)
109 {
110   if(getuid() != 0) {
111     log_printf(ERROR, "this programm has to be run as root in order to run in a chroot");
112     return -1;
113   }
114
115   if(chroot(chrootdir)) {
116     log_printf(ERROR, "can't chroot to %s: %s", chrootdir, strerror(errno));
117     return -1;
118   }
119   log_printf(NOTICE, "we are in chroot jail (%s) now", chrootdir);
120   if(chdir("/")) {
121     log_printf(ERROR, "can't change to /: %s", strerror(errno));
122     return -1;
123   }
124 }
125
126 void daemonize()
127 {
128   pid_t pid;
129
130   pid = fork();
131   if(pid < 0) {
132     log_printf(ERROR, "daemonizing failed at fork(): %s, exitting", strerror(errno));
133     exit(-1);
134   }
135   if(pid) exit(0);
136
137   umask(0);
138
139   if(setsid() < 0) {
140     log_printf(ERROR, "daemonizing failed at setsid(): %s, exitting", strerror(errno));
141     exit(-1);
142   }
143
144   pid = fork();
145   if(pid < 0) {
146     log_printf(ERROR, "daemonizing failed at fork(): %s, exitting", strerror(errno));
147     exit(-1);
148   }
149   if(pid) exit(0);
150
151   if ((chdir("/")) < 0) {
152     log_printf(ERROR, "daemonizing failed at chdir(): %s, exitting", strerror(errno));
153     exit(-1);
154   }
155
156   int fd;
157   for (fd=0;fd<=2;fd++) // close all file descriptors
158     close(fd);
159   fd = open("/dev/null",O_RDWR);        // stdin
160   if(fd == -1)
161     log_printf(WARNING, "can't open stdin (chroot and no link to /dev/null?)");
162   else {
163     if(dup(fd) == -1)   // stdout
164       log_printf(WARNING, "can't open stdout");
165     if(dup(fd) == -1)   // stderr
166       log_printf(WARNING, "can't open stderr");
167   }
168   umask(027);
169 }
170
171 #endif
172