1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define APR_WANT_MEMFUNC
19 #include "apr_general.h"
21 #include "apr_arch_misc.h"
23 #if APR_HAVE_SYS_TYPES_H
24 #include <sys/types.h>
26 #if APR_HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
38 #if defined(HAVE_UUID_H)
40 #elif defined(HAVE_UUID_UUID_H)
41 #include <uuid/uuid.h>
42 #elif defined(HAVE_SYS_UUID_H)
52 #if defined(HAVE_UUID_CREATE)
54 APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data)
64 memcpy(uuid_data, &g, sizeof(uuid_t));
69 #elif defined(HAVE_UUID_GENERATE)
71 APR_DECLARE(apr_status_t) apr_os_uuid_get(unsigned char *uuid_data)
77 memcpy(uuid_data, g, sizeof(uuid_t));
83 #endif /* APR_HAS_OS_UUID */
87 APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf,
94 /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then
95 * gives EOF, so reading 'length' bytes may require opening the
96 * device several times. */
101 if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1)
105 rc = read(fd, buf, length);
106 } while (rc == -1 && errno == EINTR);
115 fd = -1; /* force open() again */
121 } while (length > 0);
125 static UCHAR randbyte();
128 for (idx=0; idx<length; idx++)
129 buf[idx] = randbyte();
131 #elif defined(HAVE_EGD)
132 /* use EGD-compatible socket daemon (such as EGD or PRNGd).
134 * 0x00 (get entropy level)
135 * 0xMM (msb) 0xmm 0xll 0xLL (lsb)
136 * 0x01 (read entropy nonblocking) 0xNN (bytes requested)
137 * 0xMM (bytes granted) MM bytes
138 * 0x02 (read entropy blocking) 0xNN (bytes desired)
140 * 0x03 (write entropy) 0xMM 0xLL (bits of entropy) 0xNN (bytes of data)
142 * (no response - write only)
144 * 0xMM (length of PID string, not null-terminated) MM chars
146 static const char *egd_sockets[] = { EGD_DEFAULT_SOCKET, NULL };
147 const char **egdsockname = NULL;
149 int egd_socket, egd_path_len, rv, bad_errno;
150 struct sockaddr_un addr;
151 apr_socklen_t egd_addr_len;
152 apr_size_t resp_expected;
153 unsigned char req[2], resp[255];
154 unsigned char *curbuf = buf;
156 for (egdsockname = egd_sockets; *egdsockname && length > 0; egdsockname++) {
157 egd_path_len = strlen(*egdsockname);
159 if (egd_path_len > sizeof(addr.sun_path)) {
163 memset(&addr, 0, sizeof(struct sockaddr_un));
164 addr.sun_family = AF_UNIX;
165 memcpy(addr.sun_path, *egdsockname, egd_path_len);
166 egd_addr_len = APR_OFFSETOF(struct sockaddr_un, sun_path) +
169 egd_socket = socket(PF_UNIX, SOCK_STREAM, 0);
171 if (egd_socket == -1) {
175 rv = connect(egd_socket, (struct sockaddr*)&addr, egd_addr_len);
182 /* EGD can only return 255 bytes of data at a time. Silly. */
185 req[0] = 2; /* We'll block for now. */
186 req[1] = length > 255 ? 255: length;
188 srv = write(egd_socket, req, 2);
191 shutdown(egd_socket, SHUT_RDWR);
197 shutdown(egd_socket, SHUT_RDWR);
202 resp_expected = req[1];
203 srv = read(egd_socket, resp, resp_expected);
206 shutdown(egd_socket, SHUT_RDWR);
211 memcpy(curbuf, resp, srv);
216 shutdown(egd_socket, SHUT_RDWR);
221 /* We must have iterated through the list of sockets,
222 * and no go. Return the errno.
227 #elif defined(HAVE_TRUERAND) /* use truerand */
229 extern int randbyte(void); /* from the truerand library */
232 /* this will increase the startup time of the server, unfortunately...
233 * (generating 20 bytes takes about 8 seconds)
235 for (idx=0; idx<length; idx++)
236 buf[idx] = (unsigned char) randbyte();
238 #endif /* DEV_RANDOM */
247 #include "randbyte_os2.inc"
250 #endif /* APR_HAS_RANDOM */