[svn-upgrade] Integrating new upstream version, iodine (0.5.0)
[debian/iodine.git] / src / common.c
1 /* Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
2  * Copyright (c) 2007 Albert Lee <trisk@acm.jhu.edu>.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <arpa/inet.h>
18 #include <arpa/nameser.h>
19 #include <netinet/in.h>
20 #ifdef DARWIN
21 #include <arpa/nameser8_compat.h>
22 #endif
23 #include <time.h>
24 #include <err.h>
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <termios.h>
37
38 #include "common.h"
39
40 /* daemon(3) exists only in 4.4BSD or later, and in GNU libc */
41 #if !(defined(BSD) && (BSD >= 199306)) && !defined(__GLIBC__)
42 static int daemon(int nochdir, int noclose)
43 {
44         int fd, i;
45  
46         switch (fork()) {
47                 case 0:
48                         break;
49                 case -1:
50                         return -1;
51                 default:
52                         _exit(0);
53         }
54  
55         if (!nochdir) {
56                 chdir("/");
57         }
58  
59         if (setsid() < 0) {
60                 return -1;
61         }
62         
63         if (!noclose) {
64                 if ((fd = open("/dev/null", O_RDWR)) >= 0) {
65                         for (i = 0; i < 3; i++) {
66                                 dup2(fd, i);
67                         }
68                         if (fd > 2) {
69                                 close(fd);
70                         }
71                 }
72         }
73         return 0;
74 }
75 #endif
76
77 #if defined(__BEOS__) && !defined(__HAIKU__)
78 int setgroups(int count, int *groups)
79 {
80         /* errno = ENOSYS; */
81         return -1;
82 }
83 #endif
84
85 int 
86 open_dns(int localport, in_addr_t listen_ip) 
87 {
88         struct sockaddr_in addr;
89         int flag;
90         int fd;
91
92         memset(&addr, 0, sizeof(addr));
93         addr.sin_family = AF_INET;
94         addr.sin_port = htons(localport);
95         /* listen_ip already in network byte order from inet_addr, or 0 */
96         addr.sin_addr.s_addr = listen_ip; 
97
98         if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
99                 err(1, "socket");
100
101         flag = 1;
102 #ifdef SO_REUSEPORT
103         setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag));
104 #endif
105         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
106
107         /* To get destination address from each UDP datagram, see iodined.c:read_dns() */
108         setsockopt(fd, IPPROTO_IP, DSTADDR_SOCKOPT, &flag, sizeof(flag));
109
110         if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) 
111                 err(1, "bind");
112
113         printf("Opened UDP socket\n");
114
115         return fd;
116 }
117
118 void
119 close_dns(int fd)
120 {
121         close(fd);
122 }
123
124 void
125 do_chroot(char *newroot)
126 {
127 #if !defined(__BEOS__) || defined(__HAIKU__)
128         if (chroot(newroot) != 0 || chdir("/") != 0)
129                 err(1, "%s", newroot);
130
131         seteuid(geteuid());
132         setuid(getuid());
133 #else
134         warnx("chroot not available");
135 #endif
136 }
137
138 void
139 do_detach()
140 {
141         printf("Detaching from terminal...\n");
142         daemon(0, 0);
143         umask(0);
144         alarm(0);
145 }
146
147 void
148 read_password(char *buf, size_t len)
149 {
150         struct termios old;
151         struct termios tp;
152         char pwd[80];
153
154         tcgetattr(0, &tp);
155         old = tp;
156         
157         tp.c_lflag &= (~ECHO);
158         tcsetattr(0, TCSANOW, &tp);
159
160         printf("Enter password: ");
161         fflush(stdout);
162         scanf("%79s", pwd);
163         printf("\n");
164
165         tcsetattr(0, TCSANOW, &old);    
166
167         strncpy(buf, pwd, len);
168         buf[len-1] = '\0';
169 }
170
171 int
172 check_topdomain(char *str)
173 {
174        int i;
175
176        if(str[0] == '.') /* special case */
177                return 1;
178
179        for( i = 0; i < strlen(str); i++) {
180                if( isalpha(str[i]) || isdigit(str[i]) || str[i] == '-' || str[i] == '.' )
181                        continue;
182                else 
183                        return 1;
184        }
185        return 0;
186 }