ef684b7645debfbcadce2b47c88b536cd3287c1c
[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. 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.
20  *
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.
32  *
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  */
36
37 #include <sys/param.h>
38 #include <sys/mtio.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #ifdef sunos
42 #include <sys/vnode.h>
43
44 #include <ufs/inode.h>
45 #else
46 #include <vfs/ufs/dinode.h>
47 #endif
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp.h>
53
54 #include <protocols/dumprestore.h>
55
56 #include <ctype.h>
57 #include <err.h>
58 #include <netdb.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <errno.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 #include "pathnames.h"
67 #include "dump.h"
68
69 #define TS_CLOSED       0
70 #define TS_OPEN         1
71
72 static  int rmtstate = TS_CLOSED;
73 static  int rmtape;
74 static  char *rmtpeer;
75
76 static  int okname(char *);
77 static  int rmtcall(const char *, const char *);
78 static  void rmtconnaborted(int);
79 static  int rmtgetb(void);
80 static  void rmtgetconn(void);
81 static  void rmtgets(char *, int);
82 static  int rmtreply(const char *);
83 #ifdef KERBEROS
84 int     krcmd(char **, int /*u_short*/, char *, char *, int *, char *);
85 #endif
86
87 static  int errfd = -1;
88
89 int
90 rmthost(const char *hostname)
91 {
92
93         if ((rmtpeer = strdup(hostname)) == NULL)
94                 err(1, "strdup failed");
95         signal(SIGPIPE, rmtconnaborted);
96         rmtgetconn();
97         if (rmtape < 0)
98                 return (0);
99         return (1);
100 }
101
102 static void
103 rmtconnaborted(int signo __unused)
104 {
105         msg("Lost connection to remote host.\n");
106         if (errfd != -1) {
107                 fd_set r;
108                 struct timeval t;
109
110                 FD_ZERO(&r);
111                 FD_SET(errfd, &r);
112                 t.tv_sec = 0;
113                 t.tv_usec = 0;
114                 if (select(errfd + 1, &r, NULL, NULL, &t)) {
115                         int i;
116                         char buf[2048];
117
118                         if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
119                                 buf[i] = '\0';
120                                 msg("on %s: %s%s", rmtpeer, buf,
121                                         buf[i - 1] == '\n' ? "" : "\n");
122                         }
123                 }
124         }
125
126         exit(X_ABORT);
127 }
128
129 void
130 rmtgetconn(void)
131 {
132         char *cp;
133         const char *rmt;
134         static struct servent *sp = NULL;
135         static struct passwd *pwd = NULL;
136         char *tuser;
137         int size;
138         int throughput;
139         int on;
140
141         if (sp == NULL) {
142                 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
143                 if (sp == NULL) {
144                         msg("%s/tcp: unknown service\n",
145                             dokerberos ? "kshell" : "shell");
146                         exit(X_STARTUP);
147                 }
148                 pwd = getpwuid(getuid());
149                 if (pwd == NULL) {
150                         msg("who are you?\n");
151                         exit(X_STARTUP);
152                 }
153         }
154         if ((cp = strchr(rmtpeer, '@')) != NULL) {
155                 tuser = rmtpeer;
156                 *cp = '\0';
157                 if (!okname(tuser))
158                         exit(X_STARTUP);
159                 rmtpeer = ++cp;
160         } else
161                 tuser = pwd->pw_name;
162         if ((rmt = getenv("RMT")) == NULL)
163                 rmt = _PATH_RMT;
164         msg("%s", "");
165 #ifdef KERBEROS
166         if (dokerberos)
167                 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd, NULL);
168         else
169 #endif
170                 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
171                               tuser, rmt, &errfd);
172         if (rmtape < 0) {
173                 msg("login to %s as %s failed.\n", rmtpeer, tuser);
174                 return;
175         }
176         fprintf(stderr, "Connection to %s established.\n", rmtpeer);
177         size = ntrec * TP_BSIZE;
178         if (size > 60 * 1024)           /* XXX */
179                 size = 60 * 1024;
180         /* Leave some space for rmt request/response protocol */
181         size += 2 * 1024;
182         while (size > TP_BSIZE &&
183             setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
184                     size -= TP_BSIZE;
185         setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
186         throughput = IPTOS_THROUGHPUT;
187         if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
188             &throughput, sizeof(throughput)) < 0)
189                 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
190         on = 1;
191         if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
192                 perror("TCP_NODELAY setsockopt");
193 }
194
195 static int
196 okname(char *cp0)
197 {
198         char *cp;
199         int c;
200
201         for (cp = cp0; *cp; cp++) {
202                 c = *cp;
203                 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
204                         msg("invalid user name %s\n", cp0);
205                         return (0);
206                 }
207         }
208         return (1);
209 }
210
211 int
212 rmtopen(const char *rtape, int mode)
213 {
214         char buf[256];
215
216         snprintf(buf, sizeof (buf), "O%.226s\n%d\n", rtape, mode);
217         rmtstate = TS_OPEN;
218         return (rmtcall(rtape, buf));
219 }
220
221 void
222 rmtclose(void)
223 {
224
225         if (rmtstate != TS_OPEN)
226                 return;
227         rmtcall("close", "C\n");
228         rmtstate = TS_CLOSED;
229 }
230
231 #ifdef RRESTORE
232 int
233 rmtread(char *buf, int count)
234 {
235         char line[30];
236         int n, i, cc;
237
238         snprintf(line, sizeof (line), "R%d\n", count);
239         n = rmtcall("read", line);
240         if (n < 0)
241                 /* rmtcall() properly sets errno for us on errors. */
242                 return (n);
243         for (i = 0; i < n; i += cc) {
244                 cc = read(rmtape, buf+i, n - i);
245                 if (cc <= 0)
246                         rmtconnaborted(0);
247         }
248         return (n);
249 }
250
251 int
252 rmtseek(int offset, int pos)
253 {
254         char line[80];
255
256         snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
257         return (rmtcall("seek", line));
258 }
259
260 int
261 rmtioctl(int cmd, int count)
262 {
263         char buf[256];
264
265         if (count < 0)
266                 return (-1);
267         snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
268         return (rmtcall("ioctl", buf));
269 }
270 #endif  /* RRESTORE */
271
272 int
273 rmtwrite(const void *buf, int count)
274 {
275         char line[30];
276
277         snprintf(line, sizeof (line), "W%d\n", count);
278         write(rmtape, line, strlen(line));
279         write(rmtape, buf, count);
280         return (rmtreply("write"));
281 }
282
283 static int
284 rmtcall(const char *cmd, const char *buf)
285 {
286         ssize_t len = (ssize_t)strlen(buf);
287
288         if (write(rmtape, buf, len) != len)
289                 rmtconnaborted(0);
290         return (rmtreply(cmd));
291 }
292
293 static int
294 rmtreply(const char *cmd)
295 {
296         char *cp;
297         char code[30], emsg[BUFSIZ];
298
299         rmtgets(code, sizeof (code));
300         if (*code == 'E' || *code == 'F') {
301                 rmtgets(emsg, sizeof (emsg));
302                 msg("%s: %s", cmd, emsg);
303                 errno = atoi(code + 1);
304                 if (*code == 'F')
305                         rmtstate = TS_CLOSED;
306                 return (-1);
307         }
308         if (*code != 'A') {
309                 /* Kill trailing newline */
310                 cp = code + strlen(code);
311                 if (cp > code && *--cp == '\n')
312                         *cp = '\0';
313
314                 msg("Protocol to remote tape server botched (code \"%s\").\n",
315                     code);
316                 rmtconnaborted(0);
317         }
318         return (atoi(code + 1));
319 }
320
321 int
322 rmtgetb(void)
323 {
324         char c;
325
326         if (read(rmtape, &c, 1) != 1)
327                 rmtconnaborted(0);
328         return (c);
329 }
330
331 /* Get a line (guaranteed to have a trailing newline). */
332 void
333 rmtgets(char *line, int len)
334 {
335         char *cp = line;
336
337         while (len > 1) {
338                 *cp = rmtgetb();
339                 if (*cp == '\n') {
340                         cp[1] = '\0';
341                         return;
342                 }
343                 cp++;
344                 len--;
345         }
346         *cp = '\0';
347         msg("Protocol to remote tape server botched.\n");
348         msg("(rmtgets got \"%s\").\n", line);
349         rmtconnaborted(0);
350 }