-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxf_voldump.c
227 lines (201 loc) · 6.15 KB
/
xf_voldump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* CMUCS AFStools
* dumpscan - routines for scanning and manipulating AFS volume dumps
*
* Copyright (c) 1998, 2001, 2003 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or [email protected]
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/* xf_voldump.c - XFILE module for AFS volume dumps */
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include "xfiles.h"
#include "xf_errs.h"
#include <rx/xdr.h>
#include <rx/rx.h>
#include <rx/rx_null.h>
#include <rx/rxkad.h>
#include <afs/auth.h>
#include <afs/cellconfig.h>
#include <afs/vlserver.h>
#include <afs/afsint.h>
#include <afs/volser.h>
#ifndef AFSCONF_CLIENTNAME
#include <afs/dirpath.h>
#define AFSCONF_CLIENTNAME AFSDIR_CLIENT_ETC_DIRPATH
#endif
#define O_MODE_MASK (O_RDONLY | O_WRONLY | O_RDWR)
struct vdinfo {
struct rx_connection *conn; /* our connection */
struct rx_call *call; /* our active call */
afs_int32 tid; /* volser transaction ID */
XFILE rx; /* underlying rx xfile */
int destconn; /* if set, destroy connection on close */
};
static afs_uint32 xf_voldump_do_read(XFILE *X, void *buf, afs_uint32 count)
{
struct vdinfo *i = X->refcon;
return xfread(&(i->rx), buf, count);
}
static afs_uint32 xf_voldump_do_write(XFILE *X, void *buf, afs_uint32 count)
{
struct vdinfo *i = X->refcon;
return xfwrite(&(i->rx), buf, count);
}
static afs_uint32 xf_voldump_do_close(XFILE *X)
{
struct vdinfo *i = X->refcon;
afs_uint32 code, rcode, xcode;
code = xfclose(&(i->rx));
code = rx_EndCall(i->call, code);
xcode = AFSVolEndTrans(i->conn, i->tid, &rcode);
if (!code) code = xcode ? xcode : rcode;
if (i->destconn) rx_DestroyConnection(i->conn);
free(i);
return code;
}
afs_uint32 xfopen_voldump(XFILE *X, struct rx_connection *conn,
afs_int32 part, afs_int32 volid, afs_int32 date)
{
struct vdinfo *i;
afs_uint32 code, rcode;
memset(X, 0, sizeof(*X));
if (!(i = (struct vdinfo *)malloc(sizeof(struct vdinfo)))) return ENOMEM;
memset(i, 0, sizeof(struct vdinfo));
i->conn = conn;
code = AFSVolTransCreate(i->conn, volid, part, ITBusy, &(i->tid));
if (code) {
free(i);
return code;
}
i->call = rx_NewCall(i->conn);
if ((code = StartAFSVolDump(i->call, i->tid, date))
|| (code = xfopen_rxcall(&(i->rx), O_RDONLY, i->call))) {
rx_EndCall(i->call, 0);
AFSVolEndTrans(i->conn, i->tid, &rcode);
free(i);
return code;
}
X->do_read = xf_voldump_do_read;
X->do_write = xf_voldump_do_write;
X->do_close = xf_voldump_do_close;
X->is_writable = i->rx.is_writable;
return 0;
}
afs_uint32 xfon_voldump(XFILE *X, int flag, char *name)
{
struct hostent *he;
struct rx_securityClass *class;
struct rx_connection *conn;
struct ktc_principal sname;
struct ktc_token token;
struct afsconf_dir *confdir;
afs_uint32 code, server_addr;
afs_int32 volid, partid, date;
int isnum, index;
char *x, *y;
/* Parse out the optional date and server location */
if (code = rx_Init(0)) return code;
if (!(name = strdup(name))) return ENOMEM;
if (x = strrchr(name, ',')) {
*x++ = 0;
date = atoi(x);
} else {
date = 0;
}
if (x = strrchr(name, '@')) {
int a, b, c, d;
*x++ = 0;
if (!(y = strchr(x, '/'))) {
free(name);
return VL_BADPARTITION;
}
*y++ = 0;
if (sscanf(x, "%d.%d.%d.%d", &a, &b, &c, &d) == 4
&& a >= 0 && a <= 255 && b >= 0 && b <= 255
&& c >= 0 && c <= 255 && d >= 0 && d <= 255) {
server_addr = (a << 24) | (b << 16) | (c << 8) | d;
server_addr = htonl(server_addr);
} else {
he = gethostbyname(x);
if (!he) {
free(name);
return VL_BADSERVER;
}
memcpy(&server_addr, he->h_addr, sizeof(server_addr));
}
partid = volutil_GetPartitionID(y);
if (partid < 0) {
free(name);
return VL_BADPARTITION;
}
}
/* Get tokens and set up a security object */
confdir = afsconf_Open(AFSCONF_CLIENTNAME);
if (!confdir) {
free(name);
return AFSCONF_NODB;
}
if (code = afsconf_GetLocalCell(confdir, sname.cell, MAXKTCNAMELEN)) {
free(name);
return code;
}
afsconf_Close(confdir);
strcpy(sname.name, "afs");
sname.instance[0] = 0;
code = ktc_GetToken(&sname, &token, sizeof(token), 0);
if (code) {
class = rxnull_NewClientSecurityObject();
index = 0;
} else {
class = rxkad_NewClientSecurityObject(rxkad_clear, &token.sessionKey,
token.kvno, token.ticketLen, token.ticket);
index = 2;
}
/* Figure out the volume ID, looking it up in the VLDB if neccessary.
* Also look up the server and partition, if they were not specified.
*/
for (isnum = 1, y = name; *y; y++)
if (*y < '0' || *y > '9') isnum = 0;
if (isnum) {
volid = atoi(name);
if (!x) {
fprintf(stderr, "XXX: need to lookup volume by ID!\n");
exit(-1);
}
} else {
fprintf(stderr, "XXX: need to lookup volume by name!\n");
exit(-1);
}
free(name);
/* Establish a connection and start the call */
conn = rx_NewConnection(server_addr, htons(AFSCONF_VOLUMEPORT),
VOLSERVICE_ID, class, index);
code = xfopen_voldump(X, conn, partid, volid, date);
if (!code) ((struct vdinfo *)(X->refcon))->destconn = 1;
return code;
}