dump(8): Remove some old code.
[dragonfly.git] / sbin / dump / dumprmt.c
1 /*-
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)dumprmt.c        8.3 (Berkeley) 4/28/95
30  * $FreeBSD: src/sbin/dump/dumprmt.c,v 1.14.2.1 2000/07/01 06:31:52 ps Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/mtio.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <vfs/ufs/dinode.h>
38
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/tcp.h>
43
44 #include <protocols/dumprestore.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <netdb.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 #include "pathnames.h"
57 #include "dump.h"
58
59 #define TS_CLOSED       0
60 #define TS_OPEN         1
61
62 static  int rmtstate = TS_CLOSED;
63 static  int rmtape;
64 static  char *rmtpeer;
65
66 static  int okname(char *);
67 static  int rmtcall(const char *, const char *);
68 static  void rmtconnaborted(int);
69 static  int rmtgetb(void);
70 static  void rmtgetconn(void);
71 static  void rmtgets(char *, int);
72 static  int rmtreply(const char *);
73 #ifdef KERBEROS
74 int     krcmd(char **, int /*u_short*/, char *, char *, int *, char *);
75 #endif
76
77 static  int errfd = -1;
78
79 int
80 rmthost(const char *hostname)
81 {
82
83         if ((rmtpeer = strdup(hostname)) == NULL)
84                 err(1, "strdup failed");
85         signal(SIGPIPE, rmtconnaborted);
86         rmtgetconn();
87         if (rmtape < 0)
88                 return (0);
89         return (1);
90 }
91
92 static void
93 rmtconnaborted(int signo __unused)
94 {
95         msg("Lost connection to remote host.\n");
96         if (errfd != -1) {
97                 fd_set r;
98                 struct timeval t;
99
100                 FD_ZERO(&r);
101                 FD_SET(errfd, &r);
102                 t.tv_sec = 0;
103                 t.tv_usec = 0;
104                 if (select(errfd + 1, &r, NULL, NULL, &t)) {
105                         int i;
106                         char buf[2048];
107
108                         if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
109                                 buf[i] = '\0';
110                                 msg("on %s: %s%s", rmtpeer, buf,
111                                         buf[i - 1] == '\n' ? "" : "\n");
112                         }
113                 }
114         }
115
116         exit(X_ABORT);
117 }
118
119 void
120 rmtgetconn(void)
121 {
122         char *cp;
123         const char *rmt;
124         static struct servent *sp = NULL;
125         static struct passwd *pwd = NULL;
126         char *tuser;
127         int size;
128         int throughput;
129         int on;
130
131         if (sp == NULL) {
132                 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
133                 if (sp == NULL) {
134                         msg("%s/tcp: unknown service\n",
135                             dokerberos ? "kshell" : "shell");
136                         exit(X_STARTUP);
137                 }
138                 pwd = getpwuid(getuid());
139                 if (pwd == NULL) {
140                         msg("who are you?\n");
141                         exit(X_STARTUP);
142                 }
143         }
144         if ((cp = strchr(rmtpeer, '@')) != NULL) {
145                 tuser = rmtpeer;
146                 *cp = '\0';
147                 if (!okname(tuser))
148                         exit(X_STARTUP);
149                 rmtpeer = ++cp;
150         } else
151                 tuser = pwd->pw_name;
152         if ((rmt = getenv("RMT")) == NULL)
153                 rmt = _PATH_RMT;
154         msg("%s", "");
155 #ifdef KERBEROS
156         if (dokerberos)
157                 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd, NULL);
158         else
159 #endif
160                 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
161                               tuser, rmt, &errfd);
162         if (rmtape < 0) {
163                 msg("login to %s as %s failed.\n", rmtpeer, tuser);
164                 return;
165         }
166         fprintf(stderr, "Connection to %s established.\n", rmtpeer);
167         size = ntrec * TP_BSIZE;
168         if (size > 60 * 1024)           /* XXX */
169                 size = 60 * 1024;
170         /* Leave some space for rmt request/response protocol */
171         size += 2 * 1024;
172         while (size > TP_BSIZE &&
173             setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
174                     size -= TP_BSIZE;
175         setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
176         throughput = IPTOS_THROUGHPUT;
177         if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
178             &throughput, sizeof(throughput)) < 0)
179                 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
180         on = 1;
181         if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
182                 perror("TCP_NODELAY setsockopt");
183 }
184
185 static int
186 okname(char *cp0)
187 {
188         char *cp;
189         int c;
190
191         for (cp = cp0; *cp; cp++) {
192                 c = *cp;
193                 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
194                         msg("invalid user name %s\n", cp0);
195                         return (0);
196                 }
197         }
198         return (1);
199 }
200
201 int
202 rmtopen(const char *rtape, int mode)
203 {
204         char buf[256];
205
206         snprintf(buf, sizeof (buf), "O%.226s\n%d\n", rtape, mode);
207         rmtstate = TS_OPEN;
208         return (rmtcall(rtape, buf));
209 }
210
211 void
212 rmtclose(void)
213 {
214
215         if (rmtstate != TS_OPEN)
216                 return;
217         rmtcall("close", "C\n");
218         rmtstate = TS_CLOSED;
219 }
220
221 #ifdef RRESTORE
222 int
223 rmtread(char *buf, int count)
224 {
225         char line[30];
226         int n, i, cc;
227
228         snprintf(line, sizeof (line), "R%d\n", count);
229         n = rmtcall("read", line);
230         if (n < 0)
231                 /* rmtcall() properly sets errno for us on errors. */
232                 return (n);
233         for (i = 0; i < n; i += cc) {
234                 cc = read(rmtape, buf+i, n - i);
235                 if (cc <= 0)
236                         rmtconnaborted(0);
237         }
238         return (n);
239 }
240
241 int
242 rmtseek(int offset, int pos)
243 {
244         char line[80];
245
246         snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
247         return (rmtcall("seek", line));
248 }
249
250 int
251 rmtioctl(int cmd, int count)
252 {
253         char buf[256];
254
255         if (count < 0)
256                 return (-1);
257         snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
258         return (rmtcall("ioctl", buf));
259 }
260 #endif  /* RRESTORE */
261
262 int
263 rmtwrite(const void *buf, int count)
264 {
265         char line[30];
266
267         snprintf(line, sizeof (line), "W%d\n", count);
268         write(rmtape, line, strlen(line));
269         write(rmtape, buf, count);
270         return (rmtreply("write"));
271 }
272
273 static int
274 rmtcall(const char *cmd, const char *buf)
275 {
276         ssize_t len = (ssize_t)strlen(buf);
277
278         if (write(rmtape, buf, len) != len)
279                 rmtconnaborted(0);
280         return (rmtreply(cmd));
281 }
282
283 static int
284 rmtreply(const char *cmd)
285 {
286         char *cp;
287         char code[30], emsg[BUFSIZ];
288
289         rmtgets(code, sizeof (code));
290         if (*code == 'E' || *code == 'F') {
291                 rmtgets(emsg, sizeof (emsg));
292                 msg("%s: %s", cmd, emsg);
293                 errno = atoi(code + 1);
294                 if (*code == 'F')
295                         rmtstate = TS_CLOSED;
296                 return (-1);
297         }
298         if (*code != 'A') {
299                 /* Kill trailing newline */
300                 cp = code + strlen(code);
301                 if (cp > code && *--cp == '\n')
302                         *cp = '\0';
303
304                 msg("Protocol to remote tape server botched (code \"%s\").\n",
305                     code);
306                 rmtconnaborted(0);
307         }
308         return (atoi(code + 1));
309 }
310
311 int
312 rmtgetb(void)
313 {
314         char c;
315
316         if (read(rmtape, &c, 1) != 1)
317                 rmtconnaborted(0);
318         return (c);
319 }
320
321 /* Get a line (guaranteed to have a trailing newline). */
322 void
323 rmtgets(char *line, int len)
324 {
325         char *cp = line;
326
327         while (len > 1) {
328                 *cp = rmtgetb();
329                 if (*cp == '\n') {
330                         cp[1] = '\0';
331                         return;
332                 }
333                 cp++;
334                 len--;
335         }
336         *cp = '\0';
337         msg("Protocol to remote tape server botched.\n");
338         msg("(rmtgets got \"%s\").\n", line);
339         rmtconnaborted(0);
340 }