blob: 91a78b5776d48f6fcc3b08d721cc7b3994977270 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
2**
3** OpenBSD emulation routines
4**
5** Damien Miller <djm@ibs.com.au>
6**
7** Copyright 1999 Internet Business Solutions
8**
9** Permission is hereby granted, free of charge, to any person
10** obtaining a copy of this software and associated documentation
11** files (the "Software"), to deal in the Software without
12** restriction, including without limitation the rights to use, copy,
13** modify, merge, publish, distribute, sublicense, and/or sell copies
14** of the Software, and to permit persons to whom the Software is
15** furnished to do so, subject to the following conditions:
16**
17** The above copyright notice and this permission notice shall be
18** included in all copies or substantial portions of the Software.
19**
20** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
21** KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
22** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23** AND NONINFRINGEMENT. IN NO EVENT SHALL DAMIEN MILLER OR INTERNET
24** BUSINESS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
27** OR OTHER DEALINGS IN THE SOFTWARE.
28**
29** Except as contained in this notice, the name of Internet Business
30** Solutions shall not be used in advertising or otherwise to promote
31** the sale, use or other dealings in this Software without prior
32** written authorization from Internet Business Solutions.
33**
34*/
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <errno.h>
40#include <unistd.h>
41
42#include <sys/types.h>
43#include <sys/stat.h>
Damien Miller58fc4731999-11-19 12:05:01 +110044#include <sys/socket.h>
45#include <sys/un.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include <fcntl.h>
47
48#include "rc4.h"
49#include "xmalloc.h"
Damien Millerab18c411999-11-11 10:40:23 +110050#include "ssh.h"
Damien Millere413cba1999-10-28 14:12:54 +100051#include "config.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "helper.h"
53
Damien Miller58fc4731999-11-19 12:05:01 +110054#ifndef offsetof
55#define offsetof(type, member) ((size_t) &((type *)0)->member)
56#endif
57
Damien Millere413cba1999-10-28 14:12:54 +100058#ifndef HAVE_ARC4RANDOM
59
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060void get_random_bytes(unsigned char *buf, int len);
61
62static rc4_t *rc4 = NULL;
63
Damien Miller3d112ef1999-10-28 13:20:30 +100064unsigned int arc4random(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065{
Damien Miller3d112ef1999-10-28 13:20:30 +100066 unsigned int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
68 if (rc4 == NULL)
69 arc4random_stir();
70
Damien Miller3d112ef1999-10-28 13:20:30 +100071 rc4_getbytes(rc4, (unsigned char *)&r, sizeof(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
73 return(r);
74}
75
76void arc4random_stir(void)
77{
78 unsigned char rand_buf[32];
79
80 if (rc4 == NULL)
81 rc4 = xmalloc(sizeof(*rc4));
82
83 get_random_bytes(rand_buf, sizeof(rand_buf));
84 rc4_key(rc4, rand_buf, sizeof(rand_buf));
85}
86
87void get_random_bytes(unsigned char *buf, int len)
88{
Damien Miller58fc4731999-11-19 12:05:01 +110089 static int random_pool;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090 int c;
Damien Millerab18c411999-11-11 10:40:23 +110091#ifdef HAVE_EGD
92 char egd_message[2] = { 0x02, 0x00 };
Damien Miller58fc4731999-11-19 12:05:01 +110093 struct sockaddr_un addr;
94 int addr_len;
95
96 memset(&addr, '\0', sizeof(addr));
97 addr.sun_family = AF_UNIX;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller58fc4731999-11-19 12:05:01 +110099 /* FIXME: compile time check? */
100 if (sizeof(RANDOM_POOL) > sizeof(addr.sun_path))
101 fatal("Random pool path is too long");
102
Damien Millerd71b12e1999-11-22 15:24:34 +1100103 strcpy(addr.sun_path, RANDOM_POOL);
Damien Miller58fc4731999-11-19 12:05:01 +1100104
105 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(RANDOM_POOL);
106
107 random_pool = socket(AF_UNIX, SOCK_STREAM, 0);
108
Damien Millerab18c411999-11-11 10:40:23 +1100109 if (random_pool == -1)
Damien Miller58fc4731999-11-19 12:05:01 +1100110 fatal("Couldn't create AF_UNIX socket: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller58fc4731999-11-19 12:05:01 +1100112 if (connect(random_pool, (struct sockaddr*)&addr, addr_len) == -1)
Damien Millera75cb961999-11-22 13:55:36 +1100113 fatal("Couldn't connect to EGD socket \"%s\": %s", addr.sun_path, strerror(errno));
Damien Miller58fc4731999-11-19 12:05:01 +1100114
Damien Millerab18c411999-11-11 10:40:23 +1100115 if (len > 255)
116 fatal("Too many bytes to read from EGD");
117
118 /* Send blocking read request to EGD */
119 egd_message[1] = len;
120 c = write(random_pool, egd_message, sizeof(egd_message));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121 if (c == -1)
Damien Millerab18c411999-11-11 10:40:23 +1100122 fatal("Couldn't write to EGD socket \"%s\": %s", RANDOM_POOL, strerror(errno));
Damien Miller58fc4731999-11-19 12:05:01 +1100123
124#else /* HAVE_EGD */
125
126 random_pool = open(RANDOM_POOL, O_RDONLY);
127 if (random_pool == -1)
128 fatal("Couldn't open random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
129
Damien Millerab18c411999-11-11 10:40:23 +1100130#endif /* HAVE_EGD */
131
Damien Millerd71b12e1999-11-22 15:24:34 +1100132 do {
133 c = read(random_pool, buf, len);
134
135 if ((c == -1) && (errno != EINTR))
136 fatal("Couldn't read from random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
137 } while (c == -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
139 if (c != len)
Damien Millerab18c411999-11-11 10:40:23 +1100140 fatal("Short read from random pool \"%s\"", RANDOM_POOL);
141
142 close(random_pool);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143}
Damien Millere413cba1999-10-28 14:12:54 +1000144#endif /* !HAVE_ARC4RANDOM */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Millere413cba1999-10-28 14:12:54 +1000146#ifndef HAVE_SETPROCTITLE
147void setproctitle(const char *fmt, ...)
148{
149 /* FIXME */
150}
151#endif /* !HAVE_SETPROCTITLE */
Damien Millerd7702521999-11-22 16:11:05 +1100152
153#ifndef HAVE_SETENV
154int setenv(const char *name, const char *value, int overwrite)
155{
156 char *env_string;
157 int result;
158
159 /* Don't overwrite existing env. var if overwrite is 0 */
160 if (!overwrite && (getenv(name) != NULL))
161 return(0);
162
163 env_string = xmalloc(strlen(name) + strlen(value) + 2);
164 sprintf(env_string, "%s=%s", name, value);
165
166 result = putenv(env_string);
167
168 xfree(env_string);
169
170 return(result);
171}
172#endif /* !HAVE_SETENV */