]> git.wh0rd.org Git - dump.git/blob - common/dumprmt.c
Version 0.4b5.
[dump.git] / common / dumprmt.c
1 /*
2  *      Ported to Linux's Second Extended File System as part of the
3  *      dump and restore backup suit
4  *      Remy Card <card@Linux.EU.Org>, 1994-1997
5  *      Stelian Pop <pop@cybercable.fr>, 1999
6  *
7  */
8
9 /*-
10  * Copyright (c) 1980, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *      This product includes software developed by the University of
24  *      California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)dumprmt.c   8.3 (Berkeley) 4/28/95";
45 #endif
46 static const char rcsid[] =
47         "$Id: dumprmt.c,v 1.2 1999/10/11 12:53:20 stelian Exp $";
48 #endif /* not lint */
49
50 #ifdef __linux__
51 #include <sys/types.h>
52 #include <linux/types.h>
53 #endif
54 #include <sys/param.h>
55 #include <sys/mtio.h>
56 #include <sys/socket.h>
57 #include <sys/time.h>
58 #ifdef __linux__
59 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
61 #include <signal.h>
62 #else
63 #ifdef sunos
64 #include <sys/vnode.h>
65
66 #include <ufs/inode.h>
67 #else
68 #include <ufs/ufs/dinode.h>
69 #endif
70 #endif
71
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/tcp.h>
76
77 #include <protocols/dumprestore.h>
78
79 #include <ctype.h>
80 #include <netdb.h>
81 #include <pwd.h>
82 #include <stdio.h>
83 #ifdef __STDC__
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #endif
88
89 #ifdef  __linux__
90 #include <ext2fs/ext2fs.h>
91 #endif
92
93 #include "pathnames.h"
94 #include "dump.h"
95
96 #define TS_CLOSED       0
97 #define TS_OPEN         1
98
99 static  int rmtstate = TS_CLOSED;
100 static  int rmtape;
101 static  char *rmtpeer;
102
103 static  int okname __P((char *));
104 static  int rmtcall __P((char *, char *));
105 static  void rmtconnaborted __P((/* int, int */));
106 static  int rmtgetb __P((void));
107 static  void rmtgetconn __P((void));
108 static  void rmtgets __P((char *, int));
109 static  int rmtreply __P((char *));
110 #ifdef KERBEROS
111 int     krcmd __P((char **, int /*u_short*/, char *, char *, int *, char *));
112 #endif
113
114 static  int errfd = -1;
115 extern  int dokerberos;
116 extern  int ntrec;              /* blocking factor on tape */
117
118 int
119 rmthost(host)
120         char *host;
121 {
122
123         rmtpeer = malloc(strlen(host) + 1);
124         if (rmtpeer)
125                 strcpy(rmtpeer, host);
126         else
127                 rmtpeer = host;
128         signal(SIGPIPE, rmtconnaborted);
129         rmtgetconn();
130         if (rmtape < 0)
131                 return (0);
132         return (1);
133 }
134
135 static void
136 rmtconnaborted()
137 {
138         msg("Lost connection to remote host.\n");
139         if (errfd != -1) {
140                 fd_set r;
141                 struct timeval t;
142
143                 FD_ZERO(&r);
144                 FD_SET(errfd, &r);
145                 t.tv_sec = 0;
146                 t.tv_usec = 0;
147                 if (select(errfd + 1, &r, NULL, NULL, &t)) {
148                         int i;
149                         char buf[2048];
150
151                         if ((i = read(errfd, buf, sizeof(buf) - 1)) > 0) {
152                                 buf[i] = '\0';
153                                 msg("on %s: %s%s", rmtpeer, buf,
154                                         buf[i - 1] == '\n' ? "" : "\n");
155                         }
156                 }
157         }
158
159         exit(X_ABORT);
160 }
161
162 void
163 rmtgetconn()
164 {
165         register char *cp;
166         register const char *rmt;
167         static struct servent *sp = NULL;
168         static struct passwd *pwd = NULL;
169         char *tuser;
170         int size;
171         int throughput;
172         int on;
173
174         if (sp == NULL) {
175                 sp = getservbyname(dokerberos ? "kshell" : "shell", "tcp");
176                 if (sp == NULL) {
177                         msg("%s/tcp: unknown service\n",
178                             dokerberos ? "kshell" : "shell");
179                         exit(X_STARTUP);
180                 }
181                 pwd = getpwuid(getuid());
182                 if (pwd == NULL) {
183                         msg("who are you?\n");
184                         exit(X_STARTUP);
185                 }
186         }
187         if ((cp = strchr(rmtpeer, '@')) != NULL) {
188                 tuser = rmtpeer;
189                 *cp = '\0';
190                 if (!okname(tuser))
191                         exit(X_STARTUP);
192                 rmtpeer = ++cp;
193         } else
194                 tuser = pwd->pw_name;
195         if ((rmt = getenv("RMT")) == NULL)
196                 rmt = _PATH_RMT;
197         msg("");
198 #ifdef KERBEROS
199         if (dokerberos)
200                 rmtape = krcmd(&rmtpeer, sp->s_port, tuser, rmt, &errfd,
201                                (char *)0);
202         else
203 #endif
204                 rmtape = rcmd(&rmtpeer, (u_short)sp->s_port, pwd->pw_name,
205                               tuser, rmt, &errfd);
206         if (rmtape < 0) {
207                 msg("login to %s as %s failed.\n", rmtpeer, tuser);
208                 return;
209         }
210         (void)fprintf(stderr, "Connection to %s established.\n", rmtpeer);
211         size = ntrec * TP_BSIZE;
212         if (size > 60 * 1024)           /* XXX */
213                 size = 60 * 1024;
214         /* Leave some space for rmt request/response protocol */
215         size += 2 * 1024;
216         while (size > TP_BSIZE &&
217             setsockopt(rmtape, SOL_SOCKET, SO_SNDBUF, &size, sizeof (size)) < 0)
218                     size -= TP_BSIZE;
219         (void)setsockopt(rmtape, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
220         throughput = IPTOS_THROUGHPUT;
221         if (setsockopt(rmtape, IPPROTO_IP, IP_TOS,
222             &throughput, sizeof(throughput)) < 0)
223                 perror("IP_TOS:IPTOS_THROUGHPUT setsockopt");
224         on = 1;
225         if (setsockopt(rmtape, IPPROTO_TCP, TCP_NODELAY, &on, sizeof (on)) < 0)
226                 perror("TCP_NODELAY setsockopt");
227 }
228
229 static int
230 okname(cp0)
231         char *cp0;
232 {
233         register char *cp;
234         register int c;
235
236         for (cp = cp0; *cp; cp++) {
237                 c = *cp;
238                 if (!isascii(c) || !(isalnum(c) || c == '_' || c == '-')) {
239                         msg("invalid user name %s\n", cp0);
240                         return (0);
241                 }
242         }
243         return (1);
244 }
245
246 int
247 rmtopen(tape, mode)
248         char *tape;
249         int mode;
250 {
251         char buf[256];
252
253         (void)snprintf(buf, sizeof (buf), "O%.226s\n%d\n", tape, mode);
254         rmtstate = TS_OPEN;
255         return (rmtcall(tape, buf));
256 }
257
258 void
259 rmtclose()
260 {
261
262         if (rmtstate != TS_OPEN)
263                 return;
264         rmtcall("close", "C\n");
265         rmtstate = TS_CLOSED;
266 }
267
268 int
269 rmtread(buf, count)
270         char *buf;
271         int count;
272 {
273         char line[30];
274         int n, i, cc;
275
276         (void)snprintf(line, sizeof (line), "R%d\n", count);
277         n = rmtcall("read", line);
278         if (n < 0)
279                 /* rmtcall() properly sets errno for us on errors. */
280                 return (n);
281         for (i = 0; i < n; i += cc) {
282                 cc = read(rmtape, buf+i, n - i);
283                 if (cc <= 0)
284                         rmtconnaborted();
285         }
286         return (n);
287 }
288
289 int
290 rmtwrite(buf, count)
291         char *buf;
292         int count;
293 {
294         char line[30];
295
296         (void)snprintf(line, sizeof (line), "W%d\n", count);
297         write(rmtape, line, strlen(line));
298         write(rmtape, buf, count);
299         return (rmtreply("write"));
300 }
301
302 void
303 rmtwrite0(count)
304         int count;
305 {
306         char line[30];
307
308         (void)snprintf(line, sizeof (line), "W%d\n", count);
309         write(rmtape, line, strlen(line));
310 }
311
312 void
313 rmtwrite1(buf, count)
314         char *buf;
315         int count;
316 {
317
318         write(rmtape, buf, count);
319 }
320
321 int
322 rmtwrite2()
323 {
324
325         return (rmtreply("write"));
326 }
327
328 int
329 rmtseek(offset, pos)
330         int offset, pos;
331 {
332         char line[80];
333
334         (void)snprintf(line, sizeof (line), "L%d\n%d\n", offset, pos);
335         return (rmtcall("seek", line));
336 }
337
338 struct  mtget mts;
339
340 struct mtget *
341 rmtstatus()
342 {
343         register int i;
344         register char *cp;
345
346         if (rmtstate != TS_OPEN)
347                 return (NULL);
348         rmtcall("status", "S\n");
349         for (i = 0, cp = (char *)&mts; i < sizeof(mts); i++)
350                 *cp++ = rmtgetb();
351         return (&mts);
352 }
353
354 int
355 rmtioctl(cmd, count)
356         int cmd, count;
357 {
358         char buf[256];
359
360         if (count < 0)
361                 return (-1);
362         (void)snprintf(buf, sizeof (buf), "I%d\n%d\n", cmd, count);
363         return (rmtcall("ioctl", buf));
364 }
365
366 static int
367 rmtcall(cmd, buf)
368         char *cmd, *buf;
369 {
370
371         if (write(rmtape, buf, strlen(buf)) != strlen(buf))
372                 rmtconnaborted();
373         return (rmtreply(cmd));
374 }
375
376 static int
377 rmtreply(cmd)
378         char *cmd;
379 {
380         register char *cp;
381         char code[30], emsg[BUFSIZ];
382         extern int errno;
383
384         rmtgets(code, sizeof (code));
385         if (*code == 'E' || *code == 'F') {
386                 rmtgets(emsg, sizeof (emsg));
387                 msg("%s: %s", cmd, emsg);
388                 errno = atoi(code + 1);
389                 if (*code == 'F')
390                         rmtstate = TS_CLOSED;
391                 return (-1);
392         }
393         if (*code != 'A') {
394                 /* Kill trailing newline */
395                 cp = code + strlen(code);
396                 if (cp > code && *--cp == '\n')
397                         *cp = '\0';
398
399                 msg("Protocol to remote tape server botched (code \"%s\").\n",
400                     code);
401                 rmtconnaborted();
402         }
403         return (atoi(code + 1));
404 }
405
406 int
407 rmtgetb()
408 {
409         char c;
410
411         if (read(rmtape, &c, 1) != 1)
412                 rmtconnaborted();
413         return (c);
414 }
415
416 /* Get a line (guaranteed to have a trailing newline). */
417 void
418 rmtgets(line, len)
419         char *line;
420         int len;
421 {
422         register char *cp = line;
423
424         while (len > 1) {
425                 *cp = rmtgetb();
426                 if (*cp == '\n') {
427                         cp[1] = '\0';
428                         return;
429                 }
430                 cp++;
431                 len--;
432         }
433         *cp = '\0';
434         msg("Protocol to remote tape server botched.\n");
435         msg("(rmtgets got \"%s\").\n", line);
436         rmtconnaborted();
437 }