1 | /* Created by: Robert French |
---|
2 | * |
---|
3 | * $Id: mount.c,v 1.8 1992-07-17 11:39:17 miki Exp $ |
---|
4 | * |
---|
5 | * Copyright (c) 1988 by the Massachusetts Institute of Technology. |
---|
6 | */ |
---|
7 | |
---|
8 | static char *rcsid_mount_c = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/attach/mount.c,v 1.8 1992-07-17 11:39:17 miki Exp $"; |
---|
9 | |
---|
10 | #include "attach.h" |
---|
11 | |
---|
12 | #ifdef MOUNT_CMD |
---|
13 | |
---|
14 | #ifdef _IBMR2 |
---|
15 | #include <sys/id.h> |
---|
16 | #endif |
---|
17 | |
---|
18 | mountfs(at, fsname, mopt, errorout) |
---|
19 | struct _attachtab *at; |
---|
20 | char *fsname; |
---|
21 | struct mntopts *mopt; |
---|
22 | int errorout; |
---|
23 | { |
---|
24 | int status; |
---|
25 | |
---|
26 | #ifdef _IBMR2 |
---|
27 | if (setuidx(ID_REAL|ID_EFFECTIVE, 0)) |
---|
28 | #else |
---|
29 | if (setreuid(0,0)) |
---|
30 | #endif |
---|
31 | { |
---|
32 | fprintf(stderr, "Unable to change the uid to 0\n"); |
---|
33 | return(FAILURE); |
---|
34 | } |
---|
35 | |
---|
36 | switch (fork()) { |
---|
37 | case -1: |
---|
38 | fprintf(stderr, "Unable to fork\n"); |
---|
39 | return(FAILURE); |
---|
40 | /* NOTREACHED */ |
---|
41 | case 0: |
---|
42 | execl(MOUNT_CMD, MOUNT_CMD, |
---|
43 | "-o", stropt(*mopt), |
---|
44 | fsname, at->mntpt, |
---|
45 | (char *)0); |
---|
46 | exit(1); |
---|
47 | /* NOTREACHED */ |
---|
48 | default: |
---|
49 | wait(&status); |
---|
50 | break; |
---|
51 | } |
---|
52 | |
---|
53 | return(status ? FAILURE : SUCCESS); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | #else /* !MOUNT_CMD */ |
---|
58 | |
---|
59 | #ifndef ultrix |
---|
60 | #include <mntent.h> |
---|
61 | #endif |
---|
62 | #if defined(_AIX) && (AIXV < 30) |
---|
63 | #include <sys/dstat.h> |
---|
64 | #include <rpc/rpcmount.h> |
---|
65 | #include <rpc/nfsmount.h> |
---|
66 | struct ufs_args { char *fspec;}; |
---|
67 | #endif |
---|
68 | #ifdef _AUX_SOURCE |
---|
69 | #define mount(type,dir,flags,data) fsmount(type,dir,flags,data) |
---|
70 | #endif |
---|
71 | |
---|
72 | extern int sys_nerr; |
---|
73 | extern char *sys_errlist[]; |
---|
74 | |
---|
75 | /* |
---|
76 | * Mount a filesystem |
---|
77 | */ |
---|
78 | mountfs(at, fsname, mopt, errorout) |
---|
79 | struct _attachtab *at; |
---|
80 | char *fsname; |
---|
81 | struct mntopts *mopt; |
---|
82 | int errorout; |
---|
83 | { |
---|
84 | struct mntent mnt; |
---|
85 | union data { |
---|
86 | #if defined(UFS) || defined(RVD) |
---|
87 | struct ufs_args ufs_args; |
---|
88 | #endif |
---|
89 | #ifdef NFS |
---|
90 | struct nfs_args nfs_args; |
---|
91 | #endif |
---|
92 | } data; |
---|
93 | |
---|
94 | mnt.mnt_fsname = fsname; |
---|
95 | mnt.mnt_dir = at->mntpt; |
---|
96 | #if !defined(_AIX) && !defined(ultrix) |
---|
97 | mnt.mnt_type = (at->fs->mount_type==MOUNT_NFS) ? MNTTYPE_NFS |
---|
98 | : MNTTYPE_42; |
---|
99 | #endif |
---|
100 | mnt.mnt_opts = stropt(*mopt); |
---|
101 | mnt.mnt_freq = 0; |
---|
102 | #if defined(_AIX) && (AIXV<30) |
---|
103 | mnt.mnt_type = at->fs->mount_type == MOUNT_NFS ? "nfs" : "ufs"; |
---|
104 | mnt.mnt_checkno = 0; |
---|
105 | #else |
---|
106 | mnt.mnt_passno = 0; |
---|
107 | #endif |
---|
108 | #if defined(sun) |
---|
109 | mnt.mnt_type = at->fs->mount_type == MOUNT_NFS ? "nfs" : "ufs"; |
---|
110 | #endif |
---|
111 | bzero(&data, sizeof(data)); |
---|
112 | /* Already mounted? Why lose? */ |
---|
113 | if (mounted(&mnt)) { |
---|
114 | fprintf(stderr, |
---|
115 | "%s: Already mounted, continuing...\n", |
---|
116 | at->hesiodname); |
---|
117 | if (keep_mount) |
---|
118 | at->flags |= FLAG_PERMANENT; |
---|
119 | return (SUCCESS); |
---|
120 | } |
---|
121 | #if defined(UFS) || defined(RVD) |
---|
122 | if (at->fs->mount_type == MOUNT_UFS) |
---|
123 | if (mount_42(&mnt, &data.ufs_args) == FAILURE) |
---|
124 | return (FAILURE); |
---|
125 | #endif |
---|
126 | #ifdef NFS |
---|
127 | if (at->fs->mount_type == MOUNT_NFS) { |
---|
128 | if (mount_nfs(at, mopt, &data.nfs_args, |
---|
129 | errorout) == FAILURE) |
---|
130 | return (FAILURE); |
---|
131 | #ifdef _AIX |
---|
132 | if (nfs_mount(mnt.mnt_dir, &data.nfs_args, mopt->flags) != 0) { |
---|
133 | if (errorout) |
---|
134 | fprintf(stderr, |
---|
135 | "%s: Can't mount %s on %s - %s\n", |
---|
136 | at->hesiodname, fsname, mnt.mnt_dir, |
---|
137 | sys_errlist[errno]); |
---|
138 | return (FAILURE); |
---|
139 | } else { |
---|
140 | /* We need to get the filesystem's gfs for mtab */ |
---|
141 | struct dstat st_buf; |
---|
142 | if(dstat(mnt.mnt_dir, &st_buf, sizeof(st_buf)) != 0) { |
---|
143 | if (errorout) |
---|
144 | fprintf(stderr, |
---|
145 | "%s: Can't stat %s to verify mount: %s\n", |
---|
146 | at->hesiodname, mnt.mnt_dir, sys_errlist[errno]); |
---|
147 | return (FAILURE); |
---|
148 | } else { |
---|
149 | mnt.mnt_gfs = st_buf.dst_gfs; |
---|
150 | goto done; |
---|
151 | } |
---|
152 | } |
---|
153 | #endif |
---|
154 | } |
---|
155 | #endif /* NFS */ |
---|
156 | |
---|
157 | #ifdef ultrix |
---|
158 | if (mount(mnt.mnt_fsname, mnt.mnt_dir, mopt->flags, |
---|
159 | at->fs->mount_type, (char *)&data) < 0) { |
---|
160 | #else /* !ultrix */ |
---|
161 | #ifdef _AIX |
---|
162 | if (mount(mnt.mnt_fsname, mnt.mnt_dir, mopt->flags) < 0) { |
---|
163 | #else |
---|
164 | #if defined(sun) |
---|
165 | if (mount(mnt.mnt_type, mnt.mnt_dir, M_NEWTYPE | mopt->flags, &data) < 0) { |
---|
166 | #else |
---|
167 | if (mount(at->fs->mount_type, mnt.mnt_dir, mopt->flags, &data) < 0) { |
---|
168 | #endif /* sun */ |
---|
169 | #endif |
---|
170 | #endif /* ultrix */ |
---|
171 | if (errorout) { |
---|
172 | fprintf(stderr, |
---|
173 | "%s: Can't mount %s on %s - %s\n", |
---|
174 | at->hesiodname, fsname, mnt.mnt_dir, |
---|
175 | sys_errlist[errno]); |
---|
176 | error_status = ERR_SOMETHING; |
---|
177 | } |
---|
178 | return (FAILURE); |
---|
179 | } |
---|
180 | |
---|
181 | #ifdef _AIX |
---|
182 | else { |
---|
183 | struct dstat st_buf; |
---|
184 | if(dstat(mnt.mnt_dir, &st_buf, sizeof(st_buf)) != 0) { |
---|
185 | if (errorout) |
---|
186 | fprintf(stderr, |
---|
187 | "%s: Can't stat %s to verify mount: %s\n", |
---|
188 | at->hesiodname, mnt.mnt_dir, sys_errlist[errno]); |
---|
189 | return (FAILURE); |
---|
190 | } else { |
---|
191 | mnt.mnt_gfs = st_buf.dst_gfs; |
---|
192 | } |
---|
193 | } |
---|
194 | #endif |
---|
195 | #ifndef ultrix |
---|
196 | done: |
---|
197 | addtomtab(&mnt); |
---|
198 | #endif /* !ultrix */ |
---|
199 | return (SUCCESS); |
---|
200 | } |
---|
201 | |
---|
202 | #if defined(UFS) || defined(RVD) |
---|
203 | /* |
---|
204 | * Prepare to mount a 4.2 style file system |
---|
205 | */ |
---|
206 | |
---|
207 | mount_42(mnt, args) |
---|
208 | struct mntent *mnt; |
---|
209 | struct ufs_args *args; |
---|
210 | { |
---|
211 | #ifndef ultrix |
---|
212 | args->fspec = mnt->mnt_fsname; |
---|
213 | #endif |
---|
214 | return (SUCCESS); |
---|
215 | } |
---|
216 | |
---|
217 | #endif /* UFS || RVD */ |
---|
218 | |
---|
219 | #ifdef NFS |
---|
220 | /* |
---|
221 | * Mount an NFS file system |
---|
222 | */ |
---|
223 | mount_nfs(at, mopt, args, errorout) |
---|
224 | struct _attachtab *at; |
---|
225 | struct mntopts *mopt; |
---|
226 | struct nfs_args *args; |
---|
227 | int errorout; |
---|
228 | { |
---|
229 | static struct fhstatus fhs; |
---|
230 | static struct sockaddr_in sin; |
---|
231 | struct timeval timeout; |
---|
232 | CLIENT *client; |
---|
233 | enum clnt_stat rpc_stat; |
---|
234 | char *hostdir = at->hostdir; |
---|
235 | |
---|
236 | if (errored_out(at->hostaddr[0])) { |
---|
237 | if (errorout) |
---|
238 | fprintf(stderr, |
---|
239 | "%s: Ignoring %s due to previous host errors\n", |
---|
240 | at->hesiodname, at->host); |
---|
241 | return (FAILURE); |
---|
242 | } |
---|
243 | |
---|
244 | if ((client = rpc_create(at->hostaddr[0], &sin)) == NULL) { |
---|
245 | if (errorout) |
---|
246 | fprintf(stderr, "%s: Server %s not responding\n", |
---|
247 | at->hesiodname, at->host); |
---|
248 | return (FAILURE); |
---|
249 | } |
---|
250 | |
---|
251 | client->cl_auth = spoofunix_create_default(spoofhost, real_uid); |
---|
252 | |
---|
253 | timeout.tv_usec = 0; |
---|
254 | timeout.tv_sec = 20; |
---|
255 | rpc_stat = clnt_call(client, MOUNTPROC_MNT, xdr_path, &hostdir, |
---|
256 | xdr_fhstatus, &fhs, timeout); |
---|
257 | if (rpc_stat != RPC_SUCCESS) { |
---|
258 | mark_errored(at->hostaddr[0]); |
---|
259 | if (debug_flag) |
---|
260 | clnt_perror(client, "RPC return status"); |
---|
261 | if (!errorout) |
---|
262 | return(FAILURE); |
---|
263 | switch (rpc_stat) { |
---|
264 | case RPC_TIMEDOUT: |
---|
265 | fprintf(stderr, |
---|
266 | "%s: Timeout while contacting mount daemon on %s\n", |
---|
267 | at->hesiodname, at->host); |
---|
268 | break; |
---|
269 | case RPC_AUTHERROR: |
---|
270 | fprintf(stderr, "%s: Authentication failed\n", |
---|
271 | at->hesiodname, at->host); |
---|
272 | break; |
---|
273 | case RPC_PMAPFAILURE: |
---|
274 | fprintf(stderr, "%s: Can't find mount daemon on %s\n", |
---|
275 | at->hesiodname, at->host); |
---|
276 | break; |
---|
277 | case RPC_PROGUNAVAIL: |
---|
278 | case RPC_PROGNOTREGISTERED: |
---|
279 | fprintf(stderr, |
---|
280 | "%s: Mount daemon not available on %s\n", |
---|
281 | at->hesiodname, at->host); |
---|
282 | break; |
---|
283 | default: |
---|
284 | fprintf(stderr, |
---|
285 | "%s: System error contacting server %s\n", |
---|
286 | at->hesiodname, at->host); |
---|
287 | break; |
---|
288 | } |
---|
289 | return (FAILURE); |
---|
290 | } |
---|
291 | |
---|
292 | if (errno = fhs.fhs_status) { |
---|
293 | if (errorout) { |
---|
294 | if (errno == EACCES) { |
---|
295 | fprintf(stderr, |
---|
296 | "%s: Mount access denied by server %s\n", |
---|
297 | at->hesiodname, at->host); |
---|
298 | error_status = ERR_ATTACHNOTALLOWED; |
---|
299 | } else if (errno < sys_nerr) { |
---|
300 | error_status = (errno == ENOENT) ? |
---|
301 | ERR_ATTACHNOFILSYS : ERR_ATTACHNOTALLOWED; |
---|
302 | fprintf(stderr, |
---|
303 | "%s: Error message returned from server %s: %s\n", |
---|
304 | at->hesiodname, at->host, |
---|
305 | sys_errlist[errno]); |
---|
306 | } else { |
---|
307 | error_status = ERR_ATTACHNOTALLOWED; |
---|
308 | fprintf(stderr, |
---|
309 | "%s: Error status %d returned from server %s\n", |
---|
310 | at->hesiodname, errno, at->host); |
---|
311 | } |
---|
312 | } |
---|
313 | return (FAILURE); |
---|
314 | } |
---|
315 | |
---|
316 | *args = mopt->tsa.nfs; |
---|
317 | args->hostname = at->host; |
---|
318 | args->fh = &fhs.fhs_fh; |
---|
319 | args->flags |= NFSMNT_HOSTNAME; |
---|
320 | if (mopt->nfs_port) |
---|
321 | sin.sin_port = mopt->nfs_port; |
---|
322 | else |
---|
323 | sin.sin_port = htons(NFS_PORT); /* XXX should use portmapper */ |
---|
324 | args->addr = &sin; |
---|
325 | #ifdef ultrix |
---|
326 | args->optstr = stropt(*mopt); |
---|
327 | #endif |
---|
328 | return (SUCCESS); |
---|
329 | } |
---|
330 | #endif |
---|
331 | |
---|
332 | #ifdef ultrix |
---|
333 | mounted(mntck) |
---|
334 | struct mntent *mntck; |
---|
335 | { |
---|
336 | int done = 0; |
---|
337 | int fsloc = 0; |
---|
338 | int found = 0; |
---|
339 | struct fs_data fsdata; |
---|
340 | |
---|
341 | while (getmountent(&fsloc, &fsdata, 1) > 0) { |
---|
342 | if (!strcmp(fsdata.fd_path, mntck->mnt_dir) && |
---|
343 | !strcmp(fsdata.fd_devname, mntck->mnt_fsname)) { |
---|
344 | return(1); |
---|
345 | break; |
---|
346 | } |
---|
347 | } |
---|
348 | return(0); |
---|
349 | } |
---|
350 | |
---|
351 | #else /* !ultrix */ |
---|
352 | |
---|
353 | /* |
---|
354 | * Add an entry to /etc/mtab |
---|
355 | */ |
---|
356 | |
---|
357 | addtomtab(mnt) |
---|
358 | struct mntent *mnt; |
---|
359 | { |
---|
360 | FILE *mnted; |
---|
361 | |
---|
362 | lock_mtab(); |
---|
363 | mnted = setmntent(mtab_fn, "r+"); |
---|
364 | if (!mnted || addmntent(mnted, mnt)) { |
---|
365 | fprintf(stderr, "Can't append to %s: %s\n", mtab_fn, |
---|
366 | sys_errlist[errno]); |
---|
367 | unlock_mtab(); |
---|
368 | exit(ERR_FATAL); |
---|
369 | } |
---|
370 | endmntent(mnted); |
---|
371 | unlock_mtab(); |
---|
372 | } |
---|
373 | |
---|
374 | mounted(mntck) |
---|
375 | struct mntent *mntck; |
---|
376 | { |
---|
377 | int found = 0; |
---|
378 | struct mntent *mnt; |
---|
379 | FILE *mnttab; |
---|
380 | |
---|
381 | lock_mtab(); |
---|
382 | mnttab = setmntent(mtab_fn, "r"); |
---|
383 | if (!mnttab) { |
---|
384 | fprintf(stderr, "Can't open %s for read\n", mtab_fn); |
---|
385 | exit(ERR_FATAL); |
---|
386 | } |
---|
387 | while (mnt = getmntent(mnttab)) { |
---|
388 | if (!strcmp(mnt->mnt_type, MNTTYPE_IGNORE)) |
---|
389 | continue; |
---|
390 | if ((!strcmp(mntck->mnt_dir, mnt->mnt_dir)) && |
---|
391 | (!strcmp(mntck->mnt_type, mnt->mnt_type))) { |
---|
392 | if (!strcmp(mnt->mnt_type, MNTTYPE_NFS)) { |
---|
393 | if (nfs_fsname_compare(mntck->mnt_fsname, |
---|
394 | mnt->mnt_fsname)) { |
---|
395 | found = 1; |
---|
396 | break; |
---|
397 | } |
---|
398 | } else if (!strcmp(mntck->mnt_fsname, |
---|
399 | mnt->mnt_fsname)) { |
---|
400 | found = 1; |
---|
401 | break; |
---|
402 | } |
---|
403 | } |
---|
404 | } |
---|
405 | endmntent(mnttab); |
---|
406 | unlock_mtab(); |
---|
407 | return (found); |
---|
408 | } |
---|
409 | |
---|
410 | /* |
---|
411 | * Returns true if two NFS fsnames are the same. |
---|
412 | */ |
---|
413 | nfs_fsname_compare(fsname1, fsname2) |
---|
414 | char *fsname1; |
---|
415 | char *fsname2; |
---|
416 | { |
---|
417 | char host1[BUFSIZ], host2[BUFSIZ]; |
---|
418 | char *rmdir1, *rmdir2; |
---|
419 | |
---|
420 | (void) strcpy(host1, fsname1); |
---|
421 | (void) strcpy(host2, fsname2); |
---|
422 | if (rmdir1 = index(host1, ':')) |
---|
423 | *rmdir1++ = '\0'; |
---|
424 | if (rmdir2 = index(host2, ':')) |
---|
425 | *rmdir2++ = '\0'; |
---|
426 | if (host_compare(host1, host2)) { |
---|
427 | if (!rmdir1 || !rmdir2) |
---|
428 | return(0); |
---|
429 | return(!strcmp(rmdir1, rmdir2)); |
---|
430 | } else |
---|
431 | return(0); |
---|
432 | } |
---|
433 | |
---|
434 | #endif /* !ultrix */ |
---|
435 | |
---|
436 | #endif /* !MOUNT_CMD */ |
---|