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