Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / kip / common.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "kip.h"
35
36 RCSID("$Id: common.c,v 1.13.2.4 2000/10/18 23:31:51 assar Exp $");
37
38 sig_atomic_t disconnect = 0;
39 int isserver = 0;
40
41 /*
42  * Copy packets from `tundev' to `netdev' or vice versa.
43  * Mode is used when reading from `tundev'
44  */
45
46 int
47 copy_packets (int tundev, int netdev, int mtu, des_cblock *iv,
48               des_key_schedule schedule)
49 {
50      des_cblock iv1, iv2;
51      int num1 = 0, num2 = 0;
52      u_char *buf;
53
54      buf = malloc (mtu + 2);
55      if (buf == NULL) {
56          warnx("malloc(%d) failed", mtu);
57          return 1;
58      }
59
60      memcpy (&iv1, iv, sizeof(iv1));
61      memcpy (&iv2, iv, sizeof(iv2));
62      while(!disconnect) {
63           fd_set fdset;
64           int ret, len;
65
66           if (tundev >= FD_SETSIZE || netdev >= FD_SETSIZE) {
67               warnx ("fd too large");
68               return 1;
69           }
70
71           FD_ZERO(&fdset);
72           FD_SET(tundev, &fdset);
73           FD_SET(netdev, &fdset);
74
75           ret = select (max(tundev, netdev)+1, &fdset, NULL, NULL, NULL);
76           if (ret < 0) {
77               if (errno == EINTR)
78                   continue;
79               warn ("select");
80               return 1;
81           }
82           if (FD_ISSET(tundev, &fdset)) {
83                ret = read (tundev, buf + 2, mtu);
84                if (ret == 0)
85                     return 0;
86                if (ret < 0) {
87                     if (errno == EINTR)
88                          continue;
89                     else { 
90                         warn("read");
91                         return ret;
92                     }
93                }
94                buf[0] = ret >> 8;
95                buf[1] = ret & 0xFF;
96                ret += 2;
97                des_cfb64_encrypt (buf, buf, ret, schedule,
98                                   &iv1, &num1, DES_ENCRYPT);
99                ret = krb_net_write (netdev, buf, ret);
100                if (ret < 0) {
101                    warn("write");
102                    return ret;
103                }
104           }
105           if (FD_ISSET(netdev, &fdset)) {
106                ret = read (netdev, buf, 2);
107                if (ret == 0)
108                     return 0;
109                if (ret < 0) {
110                     if (errno == EINTR)
111                          continue;
112                     else { 
113                         warn("read");
114                         return ret;
115                     }
116                }
117                des_cfb64_encrypt (buf, buf, 2, schedule,
118                                   &iv2, &num2, DES_DECRYPT);
119                len = (buf[0] << 8 ) | buf[1];
120                if (len > mtu) {
121                    fatal (-1, "buffer too large", schedule, &iv2);
122                    return -1;
123                }
124
125                if (len == 0) {
126                    len = read (netdev, buf, mtu);
127                    if (len < 1)
128                        len = 1;
129                    buf[len-1] = '\0';
130
131                    fatal (-1, buf, schedule, &iv2);
132                    return -1;
133                }
134
135                ret = krb_net_read (netdev, buf + 2, len);
136                if (ret == 0)
137                     return 0;
138                if (ret < 0) {
139                     if (errno == EINTR)
140                          continue;
141                     else { 
142                         warn("read");
143                         return ret;
144                     }
145                }
146                des_cfb64_encrypt (buf + 2, buf + 2, len, schedule,
147                                   &iv2, &num2, DES_DECRYPT);
148                ret = krb_net_write (tundev, buf + 2, len);
149                if (ret < 0) {
150                    warn("write");
151                    return ret;
152                }
153           }
154      }
155      return 0;
156 }
157
158 /*
159  * Signal handler that justs waits for the children when they die.
160  */
161
162 RETSIGTYPE
163 childhandler (int sig)
164 {
165      pid_t pid;
166      int status;
167
168      do { 
169           pid = waitpid (-1, &status, WNOHANG|WUNTRACED);
170      } while(pid > 0);
171      signal (SIGCHLD, childhandler);
172      SIGRETURN(0);
173 }
174
175 /*
176  * Find a free tunnel device and open it.
177  * Return the interface name in `name, len'.
178  */
179
180 int
181 tunnel_open (char *name, size_t len)
182 {
183      int fd;
184      int i;
185      char devname[256];
186
187      for (i = 0; i < 256; ++i) {
188           snprintf (devname, len, "%s%s%d", _PATH_DEV, TUNDEV, i);
189           fd = open (devname, O_RDWR, 0);
190           if (fd >= 0)
191                break;
192           if (errno == ENOENT || errno == ENODEV) {
193               warn("open %s", name);
194               return fd;
195           }
196      }
197      if (fd < 0)
198          warn("open %s" ,name);
199      else
200          snprintf (name, len, "%s%d", TUNDEV, i);
201      return fd;
202 }
203
204 /*
205  * run the command `cmd' with (...).  return 0 if succesful or error
206  * otherwise (and copy an error messages into `msg, len')
207  */
208
209 int
210 kip_exec (const char *cmd, char *msg, size_t len, ...)
211 {
212     pid_t pid;
213     char **argv;
214     va_list ap;
215
216     va_start(ap, len);
217     argv = vstrcollect(&ap);
218     va_end(ap);
219
220     pid = fork();
221     switch (pid) {
222     case -1:
223         snprintf (msg, len, "fork: %s", strerror(errno));
224         return errno;
225     case 0: {
226         int fd = open (_PATH_DEVNULL, O_RDWR, 0600);
227         if (fd < 0) {
228             snprintf (msg, len, "open " _PATH_DEVNULL ": %s", strerror(errno));
229             return errno;
230         }
231
232         close (STDIN_FILENO);
233         close (STDOUT_FILENO);
234         close (STDERR_FILENO);
235         
236         dup2 (fd, STDIN_FILENO);
237         dup2 (fd, STDOUT_FILENO);
238         dup2 (fd, STDERR_FILENO);
239
240         execvp (cmd, argv);
241         snprintf (msg, len, "execvp %s: %s", cmd, strerror(errno));
242         return errno;
243     }
244     default: {
245         int status;
246
247         while (waitpid(pid, &status, 0) < 0)
248             if (errno != EINTR) {
249                 snprintf (msg, len, "waitpid: %s", strerror(errno));
250                 return errno;
251             }
252
253         if (WIFEXITED(status)) {
254             if (WEXITSTATUS(status) == 0) {
255                 return 0;
256             } else {
257                 snprintf (msg, len, "child returned with %d", 
258                           WEXITSTATUS(status));
259                 return 1;
260             }
261         } else if (WIFSIGNALED(status)) {
262 #ifndef WCOREDUMP
263 #define WCOREDUMP(X) 0
264 #endif
265             snprintf (msg, len, "terminated by signal num %d %s",
266                       WTERMSIG(status), 
267                       WCOREDUMP(status) ? " coredumped" : "");
268             return 1;
269         } else if (WIFSTOPPED(status)) {
270             snprintf (msg, len, "process stoped by signal %d",
271                       WSTOPSIG(status));
272             return 1;
273         } else {
274             snprintf (msg, len, "child died in mysterious circumstances");
275             return 1;
276         }
277     }
278     }
279 }
280
281 /*
282  * fatal error `s' occured.
283  */
284
285 void
286 fatal (int fd, const char *s, des_key_schedule schedule, des_cblock *iv)
287 {
288      int16_t err = 0;
289      int num = 0;
290
291      if (fd != -1) {
292          des_cfb64_encrypt ((unsigned char*) &err, (unsigned char*) &err,
293                             sizeof(err), schedule, iv, &num, DES_ENCRYPT);
294
295          write (fd, &err, sizeof(err));
296          write (fd, s, strlen(s)+1);
297      }
298      if (isserver)
299          syslog(LOG_ERR, "%s", s);
300      else
301          warnx ("fatal error: %s", s);
302 }