MFC: Fix a panic on boot that can occur if you hit keys on the keyboard
[dragonfly.git] / sbin / dump / dumprmt.c
blobe91477241ee3b8a2d04d64e564e22835506213b7
1 /*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)dumprmt.c 8.3 (Berkeley) 4/28/95
34 * $FreeBSD: src/sbin/dump/dumprmt.c,v 1.14.2.1 2000/07/01 06:31:52 ps Exp $
35 * $DragonFly: src/sbin/dump/dumprmt.c,v 1.13 2005/04/14 10:17:23 y0netan1 Exp $
38 #include <sys/param.h>
39 #include <sys/mtio.h>
40 #include <sys/socket.h>
41 #include <sys/time.h>
42 #ifdef sunos
43 #include <sys/vnode.h>
45 #include <ufs/inode.h>
46 #else
47 #include <vfs/ufs/dinode.h>
48 #endif
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
55 #include <protocols/dumprestore.h>
57 #include <ctype.h>
58 #include <err.h>
59 #include <netdb.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #ifdef __STDC__
63 #include <errno.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67 #endif
69 #include "pathnames.h"
70 #include "dump.h"
72 #define TS_CLOSED 0
73 #define TS_OPEN 1
75 static int rmtstate = TS_CLOSED;
76 static int rmtape;
77 static char *rmtpeer;
79 static int okname(char *);
80 static int rmtcall(const char *, const char *);
81 static void rmtconnaborted(int);
82 static int rmtgetb(void);
83 static void rmtgetconn(void);
84 static void rmtgets(char *, int);
85 static int rmtreply(const char *);
86 #ifdef KERBEROS
87 int krcmd(char **, int /*u_short*/, char *, char *, int *, char *);
88 #endif
90 static int errfd = -1;
92 int
93 rmthost(const char *hostname)
96 if ((rmtpeer = strdup(hostname)) == NULL)
97 err(1, "strdup failed");
98 signal(SIGPIPE, rmtconnaborted);
99 rmtgetconn();
100 if (rmtape < 0)
101 return (0);
102 return (1);
105 static void
106 rmtconnaborted(int signo __unused)
108 msg("Lost connection to remote host.\n");
109 if (errfd != -1) {
110 fd_set r;
111 struct timeval t;
113 FD_ZERO(&r);
114 FD_SET(errfd, &r);
115 t.tv_sec = 0;
116 t.tv_usec = 0;
117 if (select(errfd + 1, &r, NULL, NULL, &t)) {
118 int i;
119 char buf[2048];
121 if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
122 buf[i] = '\0';
123 msg("on %s: %s%s", rmtpeer, buf,
124 buf[i - 1] == '\n' ? "" : "\n");
129 exit(X_ABORT);
132 void
133 rmtgetconn(void)
135 char *cp;
136 const char *rmt;
137 static struct servent *sp = NULL;
138 static struct passwd *pwd = NULL;
139 char *tuser;
140 int size;
141 int throughput;
142 int on;
144 if (sp == NULL) {
145 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
146 if (sp == NULL) {
147 msg("%s/tcp: unknown service\n",
148 dokerberos ? "kshell" : "shell");
149 exit(X_STARTUP);
151 pwd = getpwuid(getuid());
152 if (pwd == NULL) {
153 msg("who are you?\n");
154 exit(X_STARTUP);
157 if ((cp = strchr(rmtpeer, '@')) != NULL) {
158 tuser = rmtpeer;
159 *cp = '\0';
160 if (!okname(tuser))
161 exit(X_STARTUP);
162 rmtpeer = ++cp;
163 } else
164 tuser = pwd->pw_name;
165 if ((rmt = getenv("RMT")) == NULL)
166 rmt = _PATH_RMT;
167 msg("%s", "");
168 #ifdef KERBEROS
169 if (dokerberos)
170 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd,
171 (char *)0);
172 else
173 #endif
174 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
175 tuser, rmt, &errfd);
176 if (rmtape < 0) {
177 msg("login to %s as %s failed.\n", rmtpeer, tuser);
178 return;
180 fprintf(stderr, "Connection to %s established.\n", rmtpeer);
181 size = ntrec * TP_BSIZE;
182 if (size > 60 * 1024) /* XXX */
183 size = 60 * 1024;
184 /* Leave some space for rmt request/response protocol */
185 size += 2 * 1024;
186 while (size > TP_BSIZE &&
187 setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
188 size -= TP_BSIZE;
189 setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
190 throughput = IPTOS_THROUGHPUT;
191 if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
192 &throughput, sizeof(throughput)) < 0)
193 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
194 on = 1;
195 if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
196 perror("TCP_NODELAY setsockopt");
199 static int
200 okname(char *cp0)
202 char *cp;
203 int c;
205 for (cp = cp0; *cp; cp++) {
206 c = *cp;
207 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
208 msg("invalid user name %s\n", cp0);
209 return (0);
212 return (1);
216 rmtopen(const char *rtape, int mode)
218 char buf[256];
220 snprintf(buf, sizeof (buf), "O%.226s\n%d\n", rtape, mode);
221 rmtstate = TS_OPEN;
222 return (rmtcall(rtape, buf));
225 void
226 rmtclose(void)
229 if (rmtstate != TS_OPEN)
230 return;
231 rmtcall("close", "C\n");
232 rmtstate = TS_CLOSED;
235 #ifdef RRESTORE
237 rmtread(char *buf, int count)
239 char line[30];
240 int n, i, cc;
242 snprintf(line, sizeof (line), "R%d\n", count);
243 n = rmtcall("read", line);
244 if (n < 0)
245 /* rmtcall() properly sets errno for us on errors. */
246 return (n);
247 for (i = 0; i < n; i += cc) {
248 cc = read(rmtape, buf+i, n - i);
249 if (cc <= 0)
250 rmtconnaborted(0);
252 return (n);
256 rmtseek(int offset, int pos)
258 char line[80];
260 snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
261 return (rmtcall("seek", line));
265 rmtioctl(int cmd, int count)
267 char buf[256];
269 if (count < 0)
270 return (-1);
271 snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
272 return (rmtcall("ioctl", buf));
274 #endif /* RRESTORE */
277 rmtwrite(const void *buf, int count)
279 char line[30];
281 snprintf(line, sizeof (line), "W%d\n", count);
282 write(rmtape, line, strlen(line));
283 write(rmtape, buf, count);
284 return (rmtreply("write"));
287 static int
288 rmtcall(const char *cmd, const char *buf)
290 ssize_t len = (ssize_t)strlen(buf);
292 if (write(rmtape, buf, len) != len)
293 rmtconnaborted(0);
294 return (rmtreply(cmd));
297 static int
298 rmtreply(const char *cmd)
300 char *cp;
301 char code[30], emsg[BUFSIZ];
303 rmtgets(code, sizeof (code));
304 if (*code == 'E' || *code == 'F') {
305 rmtgets(emsg, sizeof (emsg));
306 msg("%s: %s", cmd, emsg);
307 errno = atoi(code + 1);
308 if (*code == 'F')
309 rmtstate = TS_CLOSED;
310 return (-1);
312 if (*code != 'A') {
313 /* Kill trailing newline */
314 cp = code + strlen(code);
315 if (cp > code && *--cp == '\n')
316 *cp = '\0';
318 msg("Protocol to remote tape server botched (code \"%s\").\n",
319 code);
320 rmtconnaborted(0);
322 return (atoi(code + 1));
326 rmtgetb(void)
328 char c;
330 if (read(rmtape, &c, 1) != 1)
331 rmtconnaborted(0);
332 return (c);
335 /* Get a line (guaranteed to have a trailing newline). */
336 void
337 rmtgets(char *line, int len)
339 char *cp = line;
341 while (len > 1) {
342 *cp = rmtgetb();
343 if (*cp == '\n') {
344 cp[1] = '\0';
345 return;
347 cp++;
348 len--;
350 *cp = '\0';
351 msg("Protocol to remote tape server botched.\n");
352 msg("(rmtgets got \"%s\").\n", line);
353 rmtconnaborted(0);