]> andersk Git - openssh.git/blob - helper.c
Removed old with-askpass option
[openssh.git] / helper.c
1 /*
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>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <fcntl.h>
47
48 #include "rc4.h"
49 #include "xmalloc.h"
50 #include "ssh.h"
51 #include "config.h"
52 #include "helper.h"
53
54 #ifndef offsetof
55 #define offsetof(type, member) ((size_t) &((type *)0)->member)
56 #endif
57
58 #ifndef HAVE_ARC4RANDOM
59
60 void get_random_bytes(unsigned char *buf, int len);
61
62 static rc4_t *rc4 = NULL;
63
64 unsigned int arc4random(void)
65 {
66         unsigned int r;
67
68         if (rc4 == NULL)
69                 arc4random_stir();
70         
71         rc4_getbytes(rc4, (unsigned char *)&r, sizeof(r));
72         
73         return(r);
74 }
75
76 void 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
87 void get_random_bytes(unsigned char *buf, int len)
88 {
89         static int random_pool;
90         int c;
91 #ifdef HAVE_EGD
92         char egd_message[2] = { 0x02, 0x00 };
93         struct sockaddr_un addr;
94         int addr_len;
95
96         memset(&addr, '\0', sizeof(addr));
97         addr.sun_family = AF_UNIX;
98         
99         /* FIXME: compile time check? */
100         if (sizeof(RANDOM_POOL) > sizeof(addr.sun_path))
101                 fatal("Random pool path is too long");
102         
103         strcpy(addr.sun_path, RANDOM_POOL);
104         
105         addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(RANDOM_POOL);
106         
107         random_pool = socket(AF_UNIX, SOCK_STREAM, 0);
108         
109         if (random_pool == -1)
110                 fatal("Couldn't create AF_UNIX socket: %s", strerror(errno));
111         
112         if (connect(random_pool, (struct sockaddr*)&addr, addr_len) == -1)
113                 fatal("Couldn't connect to EGD socket \"%s\": %s", addr.sun_path, strerror(errno));
114
115         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));
121         if (c == -1)
122                 fatal("Couldn't write to EGD socket \"%s\": %s", RANDOM_POOL, strerror(errno));
123
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
130 #endif /* HAVE_EGD */
131
132         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);
138
139         if (c != len)
140                 fatal("Short read from random pool \"%s\"", RANDOM_POOL);
141         
142         close(random_pool);
143 }
144 #endif /* !HAVE_ARC4RANDOM */
145
146 #ifndef HAVE_SETPROCTITLE
147 void setproctitle(const char *fmt, ...)
148 {
149         /* FIXME */
150 }
151 #endif /* !HAVE_SETPROCTITLE */
152
153 #ifndef HAVE_SETENV
154 int 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 */
This page took 0.262939 seconds and 5 git commands to generate.