1 /* ========================================================================
2 * Copyright 2008 Mark Crispin
3 * ========================================================================
7 * Program: SSL standard I/O routines for server use
11 * Date: 22 September 1998
12 * Last Edited: 19 November 2008
14 * Previous versions of this file were
16 * Copyright 1988-2006 University of Washington
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
27 * Returns: character or EOF
37 } while ((ret
== EOF
) && !feof (stdin
) && ferror (stdin
) &&
41 if (!ssl_getdata (sslstdio
->sslstream
)) return EOF
;
42 /* one last byte available */
43 sslstdio
->sslstream
->ictr
--;
44 return (int) *(sslstdio
->sslstream
->iptr
)++;
49 * Accepts: destination string pointer
50 * number of bytes available
51 * Returns: destination string pointer or NIL if EOF
54 char *PSIN (char *s
,int n
)
57 if (start_tls
) { /* doing a start TLS? */
58 ssl_server_init (start_tls
);/* enter the mode */
59 start_tls
= NIL
; /* don't do this again */
65 ret
= fgets (s
,n
,stdin
);
66 } while (!ret
&& !feof (stdin
) && ferror (stdin
) && (errno
== EINTR
));
69 for (i
= c
= 0, n
-- ; (c
!= '\n') && (i
< n
); sslstdio
->sslstream
->ictr
--) {
70 if ((sslstdio
->sslstream
->ictr
<= 0) && !ssl_getdata (sslstdio
->sslstream
))
71 return NIL
; /* read error */
72 c
= s
[i
++] = *(sslstdio
->sslstream
->iptr
)++;
74 s
[i
] = '\0'; /* tie off string */
80 * Accepts: destination string pointer
81 * number of bytes to read
82 * Returns: T if success, NIL otherwise
85 long PSINR (char *s
,unsigned long n
)
88 if (start_tls
) { /* doing a start TLS? */
89 ssl_server_init (start_tls
);/* enter the mode */
90 start_tls
= NIL
; /* don't do this again */
92 if (sslstdio
) return ssl_getbuffer (sslstdio
->sslstream
,n
,s
);
94 while (n
&& ((i
= fread (s
,1,n
,stdin
)) || (errno
== EINTR
))) s
+= i
,n
-= i
;
95 return n
? NIL
: LONGT
;
99 /* Wait for stdin input
100 * Accepts: timeout in seconds
101 * Returns: T if have input on stdin, else NIL
104 long INWAIT (long seconds
)
106 return (sslstdio
? ssl_server_input_wait
: server_input_wait
) (seconds
);
111 * Returns: character written or EOF
116 if (!sslstdio
) return putchar (c
);
117 /* flush buffer if full */
118 if (!sslstdio
->octr
&& PFLUSH ()) return EOF
;
119 sslstdio
->octr
--; /* count down one character */
120 *sslstdio
->optr
++ = c
; /* write character */
121 return c
; /* return that character */
126 * Accepts: destination string pointer
127 * Returns: 0 or EOF if error
132 if (!sslstdio
) return fputs (s
,stdout
);
133 while (*s
) { /* flush buffer if full */
134 if (!sslstdio
->octr
&& PFLUSH ()) return EOF
;
135 *sslstdio
->optr
++ = *s
++; /* write one more character */
136 sslstdio
->octr
--; /* count down one character */
138 return 0; /* success */
142 * Accepts: source sized text
143 * Returns: 0 or EOF if error
146 int PSOUTR (SIZEDTEXT
*s
)
148 unsigned char *t
= s
->data
;
149 unsigned long i
= s
->size
;
151 if (sslstdio
) while (i
) { /* until request satisfied */
152 /* flush buffer if full */
153 if (!sslstdio
->octr
&& PFLUSH ()) break;
154 /* blat as big a chucnk as we can */
155 memcpy (sslstdio
->optr
,t
,j
= min (i
,sslstdio
->octr
));
156 sslstdio
->optr
+= j
; /* account for chunk */
161 else while (i
&& ((j
= fwrite (t
,1,i
,stdout
)) || (errno
== EINTR
)))
163 return i
? EOF
: NIL
;
168 * Returns: 0 or EOF if error
173 if (!sslstdio
) return fflush (stdout
);
174 /* force out buffer */
175 if (!ssl_sout (sslstdio
->sslstream
,sslstdio
->obuf
,
176 SSLBUFLEN
- sslstdio
->octr
)) return EOF
;
177 /* renew output buffer */
178 sslstdio
->optr
= sslstdio
->obuf
;
179 sslstdio
->octr
= SSLBUFLEN
;
180 return 0; /* success */