1 /* rndegd.c  -	interface to the EGD
2  *	Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include "types.h"
33 #include "util.h"
34 #include "ttyio.h"
35 #include "algorithms.h"
36 #include "cipher.h"
37 #include "i18n.h"
38 
39 
40 #ifndef offsetof
41 #define offsetof(type, member) ((size_t) &((type *)0)->member)
42 #endif
43 
44 static int egd_socket = -1;
45 
46 static int
do_write(int fd,void * buf,size_t nbytes)47 do_write( int fd, void *buf, size_t nbytes )
48 {
49     size_t nleft = nbytes;
50     int nwritten;
51 
52     while( nleft > 0 ) {
53 	nwritten = write( fd, buf, nleft);
54 	if( nwritten < 0 ) {
55 	    if( errno == EINTR )
56 		continue;
57 	    return -1;
58 	}
59 	nleft -= nwritten;
60 	buf = (char*)buf + nwritten;
61     }
62     return 0;
63 }
64 
65 static int
do_read(int fd,void * buf,size_t nbytes)66 do_read( int fd, void *buf, size_t nbytes )
67 {
68   int n, nread = 0;
69 
70   while (nbytes)
71     {
72       do {
73         n = read(fd, (char*)buf + nread, nbytes );
74       } while( n == -1 && errno == EINTR );
75       if( n == -1 )
76         return nread? nread:-1;
77       else if( n == 0 ) {
78         /* EGD probably died. */
79         errno = ECONNRESET;
80         return -1;
81       }
82       nread += n;
83       nbytes -= n;
84     }
85   return nread;
86 }
87 
88 /* Connect to the EGD and return the file descriptor.  Return -1 on
89    error.  With NOFAIL set to true, silently fail and return the
90    error, otherwise print an error message and die. */
91 int
rndegd_connect_socket(int nofail)92 rndegd_connect_socket (int nofail)
93 {
94   int fd;
95   const char *bname = NULL;
96   char *name;
97   struct sockaddr_un addr;
98   int addr_len;
99 
100   if (egd_socket != -1)
101     {
102       close (egd_socket);
103       egd_socket = -1;
104     }
105 
106 #ifdef EGD_SOCKET_NAME
107   bname = EGD_SOCKET_NAME;
108 #endif
109   if ( !bname || !*bname )
110     bname = "=entropy";
111 
112   if ( *bname == '=' && bname[1] )
113     name = make_filename( g10_opt_homedir, bname+1 , NULL );
114   else
115     name = make_filename( bname , NULL );
116 
117   if ( strlen(name)+1 >= sizeof addr.sun_path )
118     g10_log_fatal ("EGD socketname is too long\n");
119 
120   memset( &addr, 0, sizeof addr );
121   addr.sun_family = AF_UNIX;
122   strcpy( addr.sun_path, name );
123   addr_len = (offsetof( struct sockaddr_un, sun_path )
124               + strlen( addr.sun_path ));
125 
126   fd = socket(AF_UNIX, SOCK_STREAM, 0);
127   if (fd == -1 && !nofail)
128     g10_log_fatal("can't create unix domain socket: %s\n",
129                   strerror(errno) );
130   else if (connect (fd, (struct sockaddr*)&addr, addr_len) == -1)
131     {
132       if (!nofail)
133         g10_log_fatal("can't connect to `%s': %s\n",
134                       name, strerror(errno) );
135       close (fd);
136       fd = -1;
137     }
138   xfree(name);
139   if (fd != -1)
140     egd_socket = fd;
141   return fd;
142 }
143 
144 
145 /****************
146  * Note: we always use the highest level.
147  * TO boost the performance we may want to add some
148  * additional code for level 1
149  *
150  * Using a level of 0 should never block and better add nothing
151  * to the pool.  So this is just a dummy for EGD.
152  */
153 int
rndegd_gather_random(void (* add)(const void *,size_t,int),int requester,size_t length,int level)154 rndegd_gather_random( void (*add)(const void*, size_t, int), int requester,
155 					  size_t length, int level )
156 {
157     int fd = egd_socket;
158     int n;
159     byte buffer[256+2];
160     int nbytes;
161     int do_restart = 0;
162 
163     if( !length )
164 	return 0;
165     if( !level )
166 	return 0;
167 
168   restart:
169     if (fd == -1 || do_restart)
170       fd = rndegd_connect_socket (0);
171 
172     do_restart = 0;
173 
174     nbytes = length < 255? length : 255;
175     /* first time we do it with a non blocking request */
176     buffer[0] = 1; /* non blocking */
177     buffer[1] = nbytes;
178     if( do_write( fd, buffer, 2 ) == -1 )
179 	g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
180     n = do_read( fd, buffer, 1 );
181     if( n == -1 ) {
182 	g10_log_error("read error on EGD: %s\n", strerror(errno));
183 	do_restart = 1;
184 	goto restart;
185     }
186     n = buffer[0];
187     if( n ) {
188 	n = do_read( fd, buffer, n );
189 	if( n == -1 ) {
190 	    g10_log_error("read error on EGD: %s\n", strerror(errno));
191 	    do_restart = 1;
192 	    goto restart;
193 	}
194 	(*add)( buffer, n, requester );
195 	length -= n;
196     }
197 
198     if( length ) {
199 	tty_printf(
200 	 _("Please wait, entropy is being gathered. Do some work if it would\n"
201 	   "keep you from getting bored, because it will improve the quality\n"
202 	   "of the entropy.\n") );
203     }
204     while( length ) {
205 	nbytes = length < 255? length : 255;
206 
207 	buffer[0] = 2; /* blocking */
208 	buffer[1] = nbytes;
209 	if( do_write( fd, buffer, 2 ) == -1 )
210 	    g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
211 	n = do_read( fd, buffer, nbytes );
212 	if( n == -1 ) {
213 	    g10_log_error("read error on EGD: %s\n", strerror(errno));
214 	    do_restart = 1;
215 	    goto restart;
216 	}
217 	(*add)( buffer, n, requester );
218 	length -= n;
219     }
220     wipememory(buffer, sizeof(buffer) );
221 
222     return 0; /* success */
223 }
224