HAMMER commit - MFC ref count related panics when a HAMMER mount fails, etc.
[dragonfly.git] / libexec / ftp-proxy / getline.c
blob48b8d81e0a09b8946344067bbf6b2d9a3ce04242
1 /* $OpenBSD: getline.c,v 1.15 2003/06/28 01:04:57 deraadt Exp $ */
2 /* $DragonFly: src/libexec/ftp-proxy/getline.c,v 1.2 2005/02/24 15:38:09 joerg Exp $ */
4 /*
5 * Copyright (c) 1985, 1988 Regents of the University of California.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#)ftpcmd.y 5.24 (Berkeley) 2/25/91
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/telnet.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sysexits.h>
46 #include <syslog.h>
47 #include <unistd.h>
49 #include "util.h"
51 int refill_buffer(struct csiob *iobp);
54 * Refill the io buffer if we KNOW that data is available
56 * Returns 1 if any new data was obtained, 0 otherwise.
59 int
60 refill_buffer(struct csiob *iobp)
62 int rqlen, rlen;
64 if (!(iobp->data_available))
65 return(0);
67 if (iobp->got_eof)
68 return(0);
71 * The buffer has been entirely consumed if next_byte == io_buffer_len.
72 * Otherwise, there is some still-to-be-used data in io_buffer.
73 * Shuffle it to the start of the buffer.
74 * Note that next_byte will never exceed io_buffer_len.
75 * Also, note that we MUST use bcopy because the two regions could
76 * overlap (memcpy isn't defined to work properly with overlapping
77 * regions).
79 if (iobp->next_byte < iobp->io_buffer_len) {
80 int dst_ix = 0;
81 int src_ix = iobp->next_byte;
82 int amount = iobp->io_buffer_len - iobp->next_byte;
84 bcopy(&iobp->io_buffer[src_ix], &iobp->io_buffer[dst_ix],
85 amount);
86 iobp->io_buffer_len = amount;
87 } else if (iobp->next_byte == iobp->io_buffer_len)
88 iobp->io_buffer_len = 0;
89 else {
90 syslog(LOG_ERR, "next_byte(%d) > io_buffer_len(%d)",
91 iobp->next_byte, iobp->io_buffer_len);
92 exit(EX_OSERR);
95 iobp->next_byte = 0;
97 /* don't do tiny reads, grow first if we need to */
98 rqlen = iobp->io_buffer_size - iobp->io_buffer_len;
99 if (rqlen <= 128) {
100 unsigned char *tmp;
102 iobp->io_buffer_size += 128;
103 tmp = realloc(iobp->io_buffer, iobp->io_buffer_size);
104 if (tmp == NULL) {
105 syslog(LOG_INFO, "Insufficient memory");
106 exit(EX_UNAVAILABLE);
108 iobp->io_buffer = tmp;
109 rqlen = iobp->io_buffer_size - iobp->io_buffer_len;
113 * Always leave an unused byte at the end of the buffer
114 * because the debug output uses that byte from time to time
115 * to ensure that something that is being printed is \0 terminated.
117 rqlen -= 1;
119 doread:
120 rlen = read(iobp->fd, &iobp->io_buffer[iobp->io_buffer_len], rqlen);
121 iobp->data_available = 0;
122 switch (rlen) {
123 case -1:
124 if (errno == EAGAIN || errno == EINTR)
125 goto doread;
126 if (errno != ECONNRESET) {
127 syslog(LOG_INFO, "read() failed on socket from %s (%m)",
128 iobp->who);
129 exit(EX_DATAERR);
131 /* fall through to EOF case */
132 case 0:
133 iobp->got_eof = 1;
134 return(0);
135 break;
136 default:
137 iobp->io_buffer_len += rlen;
138 break;
140 return(1);
144 * telnet_getline - a hacked up version of fgets to ignore TELNET escape codes.
146 * This code is derived from the getline routine found in the UC Berkeley
147 * ftpd code.
152 telnet_getline(struct csiob *iobp, struct csiob *telnet_passthrough)
154 unsigned char ch;
155 int ix;
156 unsigned char tbuf[100];
158 iobp->line_buffer[0] = '\0';
161 * If the buffer is empty then refill it right away.
163 if (iobp->next_byte == iobp->io_buffer_len)
164 if (!refill_buffer(iobp))
165 return(0);
168 * Is there a telnet command in the buffer?
170 ch = iobp->io_buffer[iobp->next_byte];
171 if (ch == IAC) {
173 * Yes - buffer must have at least three bytes in it
175 if (iobp->io_buffer_len - iobp->next_byte < 3) {
176 if (!refill_buffer(iobp))
177 return(0);
178 if (iobp->io_buffer_len - iobp->next_byte < 3)
179 return(0);
182 iobp->next_byte++;
183 ch = iobp->io_buffer[iobp->next_byte++];
185 switch (ch) {
186 case WILL:
187 case WONT:
188 case DO:
189 case DONT:
190 tbuf[0] = IAC;
191 tbuf[1] = ch;
192 tbuf[2] = iobp->io_buffer[iobp->next_byte++];
193 (void)send(telnet_passthrough->fd, tbuf, 3,
194 telnet_passthrough->send_oob_flags);
195 break;
196 case IAC:
197 break;
198 default:
199 break;
201 return(1);
202 } else {
203 int clen;
206 * Is there a newline in the buffer?
208 for (ix = iobp->next_byte; ix < iobp->io_buffer_len;
209 ix += 1) {
210 if (iobp->io_buffer[ix] == '\n')
211 break;
212 if (iobp->io_buffer[ix] == '\0') {
213 syslog(LOG_INFO,
214 "got NUL byte from %s - bye!",
215 iobp->who);
216 exit(EX_DATAERR);
220 if (ix == iobp->io_buffer_len) {
221 if (!refill_buffer(iobp))
222 return(0);
224 * Empty line returned
225 * will try again soon!
227 return(1);
231 * Expand the line buffer if it isn't big enough. We
232 * use a fudge factor of 5 rather than trying to
233 * figure out exactly how to account for the '\0 \r\n' and
234 * such. The correct fudge factor is 0, 1 or 2 but
235 * anything higher also works. We also grow it by a
236 * bunch to avoid having to do this often. Yes this is
237 * nasty.
239 if (ix - iobp->next_byte > iobp->line_buffer_size - 5) {
240 unsigned char *tmp;
242 iobp->line_buffer_size = 256 + ix - iobp->next_byte;
243 tmp = realloc(iobp->line_buffer,
244 iobp->line_buffer_size);
245 if (tmp == NULL) {
246 syslog(LOG_INFO, "Insufficient memory");
247 exit(EX_UNAVAILABLE);
249 iobp->line_buffer = tmp;
252 /* +1 is for the newline */
253 clen = (ix+1) - iobp->next_byte;
254 memcpy(iobp->line_buffer, &iobp->io_buffer[iobp->next_byte],
255 clen);
256 iobp->next_byte += clen;
257 iobp->line_buffer[clen] = '\0';
258 return(1);