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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/*
* Copyright (c) 1990 Jan-Simon Pendry
* Copyright (c) 1990 Imperial College of Science, Technology & Medicine
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Jan-Simon Pendry at Imperial College, London.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: @(#)amq.c 8.1 (Berkeley) 6/7/93
* $Id: amq.c,v 1.10 2002/09/10 05:39:26 deraadt Exp $
*/
/*
* Automounter query tool
*/
#ifndef lint
char copyright[] = "\
@(#)Copyright (c) 1990 Jan-Simon Pendry\n\
@(#)Copyright (c) 1990 Imperial College of Science, Technology & Medicine\n\
@(#)Copyright (c) 1990, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
static char rcsid[] = "$Id: amq.c,v 1.10 2002/09/10 05:39:26 deraadt Exp $";
static char sccsid[] = "@(#)amq.c 8.1 (Berkeley) 6/7/93";
#endif /* not lint */
#include "am.h"
#include "amq.h"
#include <stdio.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
static int privsock(int);
static int flush_flag;
static int minfo_flag;
static int unmount_flag;
static int stats_flag;
static int getvers_flag;
static char *debug_opts;
static char *logfile;
static char *mount_map;
static char *xlog_optstr;
static char localhost[] = "localhost";
static char *def_server = localhost;
extern int optind;
extern char *optarg;
static struct timeval tmo = { 10, 0 };
#define TIMEOUT tmo
enum show_opt { Full, Stats, Calc, Short, ShowDone };
/*
* If (e) is Calc then just calculate the sizes
* Otherwise display the mount node on stdout
*/
static void
show_mti(amq_mount_tree *mt, enum show_opt e, int *mwid, int *dwid,
int *twid)
{
switch (e) {
case Calc: {
int mw = strlen(mt->mt_mountinfo);
int dw = strlen(mt->mt_directory);
int tw = strlen(mt->mt_type);
if (mw > *mwid)
*mwid = mw;
if (dw > *dwid)
*dwid = dw;
if (tw > *twid)
*twid = tw;
break;
}
case Full: {
struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
printf("%-*.*s %-*.*s %-*.*s %s\n\t%-5d %-7d %-6d"
" %-7d %-7d %-6d %02d/%02d/%02d %02d:%02d:%02d\n",
*dwid, *dwid, *mt->mt_directory ? mt->mt_directory : "/",
*twid, *twid, mt->mt_type, *mwid, *mwid,
mt->mt_mountinfo, mt->mt_mountpoint, mt->mt_mountuid,
mt->mt_getattr, mt->mt_lookup, mt->mt_readdir,
mt->mt_readlink, mt->mt_statfs,
tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec);
break;
}
case Stats: {
struct tm *tp = localtime((time_t *) &mt->mt_mounttime);
printf("%-*.*s %-5d %-7d %-6d %-7d %-7d %-6d"
" %02d/%02d/%02d %02d:%02d:%02d\n",
*dwid, *dwid, *mt->mt_directory ? mt->mt_directory : "/",
mt->mt_mountuid, mt->mt_getattr, mt->mt_lookup,
mt->mt_readdir, mt->mt_readlink, mt->mt_statfs,
tp->tm_year > 99 ? tp->tm_year - 100 : tp->tm_year,
tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min, tp->tm_sec);
break;
}
case Short: {
printf("%-*.*s %-*.*s %-*.*s %s\n",
*dwid, *dwid, *mt->mt_directory ? mt->mt_directory : "/",
*twid, *twid, mt->mt_type, *mwid, *mwid,
mt->mt_mountinfo, mt->mt_mountpoint);
break;
}
}
}
/*
* Display a mount tree.
*/
static void
show_mt(amq_mount_tree *mt, enum show_opt e, int *mwid, int *dwid,
int *pwid)
{
while (mt) {
show_mti(mt, e, mwid, dwid, pwid);
show_mt(mt->mt_next, e, mwid, dwid, pwid);
mt = mt->mt_child;
}
}
static void
show_mi(amq_mount_info_list *ml, enum show_opt e, int *mwid,
int *dwid, int *twid)
{
int i;
switch (e) {
case Calc: {
for (i = 0; i < ml->amq_mount_info_list_len; i++) {
amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
int mw = strlen(mi->mi_mountinfo);
int dw = strlen(mi->mi_mountpt);
int tw = strlen(mi->mi_type);
if (mw > *mwid)
*mwid = mw;
if (dw > *dwid)
*dwid = dw;
if (tw > *twid)
*twid = tw;
}
break;
}
case Full: {
for (i = 0; i < ml->amq_mount_info_list_len; i++) {
amq_mount_info *mi = &ml->amq_mount_info_list_val[i];
printf("%-*.*s %-*.*s %-*.*s %-3d %s is %s",
*mwid, *mwid, mi->mi_mountinfo,
*dwid, *dwid, mi->mi_mountpt,
*twid, *twid, mi->mi_type,
mi->mi_refc, mi->mi_fserver,
mi->mi_up > 0 ? "up" :
mi->mi_up < 0 ? "starting" : "down");
if (mi->mi_error > 0) {
#ifdef HAS_STRERROR
printf(" (%s)", strerror(mi->mi_error));
#else
extern char *sys_errlist[];
extern int sys_nerr;
if (mi->mi_error < sys_nerr)
printf(" (%s)", sys_errlist[mi->mi_error]);
else
printf(" (Error %d)", mi->mi_error);
#endif
} else if (mi->mi_error < 0) {
fputs(" (in progress)", stdout);
}
fputc('\n', stdout);
}
break;
}
}
}
/*
* Display general mount statistics
*/
static void
show_ms(amq_mount_stats *ms)
{
printf("requests stale mount mount unmount\n"
"deferred fhandles ok failed failed\n"
"%-9d %-9d %-9d %-9d %-9d\n",
ms->as_drops, ms->as_stale, ms->as_mok, ms->as_merr, ms->as_uerr);
}
static bool_t
xdr_pri_free(xdrproc_t xdr_args, void *args_ptr)
{
XDR xdr;
xdr.x_op = XDR_FREE;
return ((*xdr_args)(&xdr, args_ptr));
}
/*
* MAIN
*/
int
main(int argc, char *argv[])
{
int nodefault = 0, opt_ch, errs = 0, s;
struct sockaddr_in server_addr;
struct hostent *hp;
CLIENT *clnt;
char *server;
/*
* Parse arguments
*/
while ((opt_ch = getopt(argc, argv, "fh:l:msuvx:D:M:")) != -1)
switch (opt_ch) {
case 'f':
flush_flag = 1;
nodefault = 1;
break;
case 'h':
def_server = optarg;
break;
case 'l':
logfile = optarg;
nodefault = 1;
break;
case 'm':
minfo_flag = 1;
nodefault = 1;
break;
case 's':
stats_flag = 1;
nodefault = 1;
break;
case 'u':
unmount_flag = 1;
nodefault = 1;
break;
case 'v':
getvers_flag = 1;
nodefault = 1;
break;
case 'x':
xlog_optstr = optarg;
nodefault = 1;
break;
case 'D':
debug_opts = optarg;
nodefault = 1;
break;
case 'M':
mount_map = optarg;
nodefault = 1;
break;
default:
errs = 1;
break;
}
if (optind == argc) {
if (unmount_flag)
errs = 1;
}
if (errs) {
show_usage:
fprintf(stderr, "usage: %s [-h host] [[-f] [-m] [-v] [-s]]"
" | [[-u] directory ...]] |\n"
"\t[-l logfile|\"syslog\"] [-x log_flags] "
"[-D dbg_opts] [-M mapent]\n", __progname);
exit(1);
}
server = def_server;
/*
* Get address of server
*/
if ((hp = gethostbyname(server)) == 0 && strcmp(server, localhost) != 0) {
fprintf(stderr, "%s: Can't get address of %s\n", __progname, server);
exit(1);
}
bzero(&server_addr, sizeof server_addr);
server_addr.sin_family = AF_INET;
if (hp) {
bcopy((void *)hp->h_addr, (void *)&server_addr.sin_addr,
sizeof(server_addr.sin_addr));
} else {
/* fake "localhost" */
server_addr.sin_addr.s_addr = htonl(0x7f000001);
}
/*
* Create RPC endpoint
*/
s = privsock(SOCK_STREAM);
clnt = clnttcp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, &s, 0, 0);
if (clnt == 0) {
close(s);
s = privsock(SOCK_DGRAM);
clnt = clntudp_create(&server_addr, AMQ_PROGRAM,
AMQ_VERSION, TIMEOUT, &s);
}
if (clnt == 0) {
fprintf(stderr, "%s: ", __progname);
clnt_pcreateerror(server);
exit(1);
}
/*
* Control debugging
*/
if (debug_opts) {
int *rc;
amq_setopt opt;
opt.as_opt = AMOPT_DEBUG;
opt.as_str = debug_opts;
rc = amqproc_setopt_1(&opt, clnt);
if (rc && *rc < 0) {
fprintf(stderr,
"%s: daemon not compiled for debug", __progname);
errs = 1;
} else if (!rc || *rc > 0) {
fprintf(stderr,
"%s: debug setting for \"%s\" failed\n",
__progname, debug_opts);
errs = 1;
}
}
/*
* Control logging
*/
if (xlog_optstr) {
int *rc;
amq_setopt opt;
opt.as_opt = AMOPT_XLOG;
opt.as_str = xlog_optstr;
rc = amqproc_setopt_1(&opt, clnt);
if (!rc || *rc) {
fprintf(stderr, "%s: setting log level to \"%s\" failed\n",
__progname, xlog_optstr);
errs = 1;
}
}
/*
* Control log file
*/
if (logfile) {
int *rc;
amq_setopt opt;
opt.as_opt = AMOPT_LOGFILE;
opt.as_str = logfile;
rc = amqproc_setopt_1(&opt, clnt);
if (!rc || *rc) {
fprintf(stderr, "%s: setting logfile to \"%s\" failed\n",
__progname, logfile);
errs = 1;
}
}
/*
* Flush map cache
*/
if (flush_flag) {
int *rc;
amq_setopt opt;
opt.as_opt = AMOPT_FLUSHMAPC;
opt.as_str = "";
rc = amqproc_setopt_1(&opt, clnt);
if (!rc || *rc) {
fprintf(stderr,
"%s: amd on %s cannot flush the map cache\n",
__progname, server);
errs = 1;
}
}
/*
* Mount info
*/
if (minfo_flag) {
int dummy;
amq_mount_info_list *ml = amqproc_getmntfs_1(&dummy, clnt);
if (ml) {
int mwid = 0, dwid = 0, twid = 0;
show_mi(ml, Calc, &mwid, &dwid, &twid);
mwid++; dwid++; twid++;
show_mi(ml, Full, &mwid, &dwid, &twid);
} else {
fprintf(stderr, "%s: amd on %s cannot provide mount info\n",
__progname, server);
}
}
/*
* Mount map
*/
if (mount_map) {
int *rc;
do {
rc = amqproc_mount_1(&mount_map, clnt);
} while (rc && *rc < 0);
if (!rc || *rc > 0) {
if (rc)
errno = *rc;
else
errno = ETIMEDOUT;
fprintf(stderr, "%s: could not start new ", __progname);
perror("autmount point");
}
}
/*
* Get Version
*/
if (getvers_flag) {
amq_string *spp = amqproc_getvers_1((void *)0, clnt);
if (spp && *spp) {
printf("%s.\n", *spp);
free(*spp);
} else {
fprintf(stderr, "%s: failed to get version information\n",
__progname);
errs = 1;
}
}
/*
* Apply required operation to all remaining arguments
*/
if (optind < argc) {
do {
char *fs = argv[optind++];
if (unmount_flag) {
/*
* Unmount request
*/
amqproc_umnt_1(&fs, clnt);
} else {
/*
* Stats request
*/
amq_mount_tree_p *mtp = amqproc_mnttree_1(&fs, clnt);
if (mtp) {
amq_mount_tree *mt = *mtp;
if (mt) {
int mwid = 0, dwid = 0, twid = 0;
show_mt(mt, Calc, &mwid, &dwid, &twid);
mwid++;
dwid++;
twid++;
printf("%-*.*s Uid Getattr "
"Lookup RdDir RdLnk "
"Statfs Mounted@\n",
dwid, dwid, "What");
show_mt(mt, Stats, &mwid, &dwid, &twid);
} else {
fprintf(stderr,
"%s: %s not automounted\n",
__progname, fs);
}
xdr_pri_free(xdr_amq_mount_tree_p, mtp);
} else {
fprintf(stderr, "%s: ", __progname);
clnt_perror(clnt, server);
errs = 1;
}
}
} while (optind < argc);
} else if (unmount_flag) {
goto show_usage;
} else if (stats_flag) {
amq_mount_stats *ms = amqproc_stats_1((void *)0, clnt);
if (ms) {
show_ms(ms);
} else {
fprintf(stderr, "%s: ", __progname);
clnt_perror(clnt, server);
errs = 1;
}
} else if (!nodefault) {
amq_mount_tree_list *mlp = amqproc_export_1((void *)0, clnt);
if (mlp) {
enum show_opt e = Calc;
int mwid = 0, dwid = 0, pwid = 0;
while (e != ShowDone) {
int i;
for (i = 0; i < mlp->amq_mount_tree_list_len; i++) {
show_mt(mlp->amq_mount_tree_list_val[i],
e, &mwid, &dwid, &pwid);
}
mwid++;
dwid++;
pwid++;
if (e == Calc)
e = Short;
else if (e == Short)
e = ShowDone;
}
} else {
fprintf(stderr, "%s: ", __progname);
clnt_perror(clnt, server);
errs = 1;
}
}
exit(errs);
}
/*
* udpresport creates a datagram socket and attempts to bind it to a
* secure port.
* returns: The bound socket, or -1 to indicate an error.
*/
static int
inetresport(int ty)
{
struct sockaddr_in addr;
int alport, sock;
/* Use internet address family */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
if ((sock = socket(AF_INET, ty, 0)) < 0)
return -1;
for (alport = IPPORT_RESERVED-1; alport > IPPORT_RESERVED/2 + 1; alport--) {
addr.sin_port = htons((u_short)alport);
if (bind(sock, (struct sockaddr *)&addr, sizeof (addr)) >= 0)
return sock;
if (errno != EADDRINUSE) {
close(sock);
return -1;
}
}
close(sock);
errno = EAGAIN;
return -1;
}
/*
* Privsock() calls inetresport() to attempt to bind a socket to a secure
* port. If inetresport() fails, privsock returns a magic socket number which
* indicates to RPC that it should make its own socket.
* returns: A privileged socket # or RPC_ANYSOCK.
*/
static int
privsock(int ty)
{
int sock = inetresport(ty);
if (sock < 0) {
errno = 0;
/* Couldn't get a secure port, let RPC make an insecure one */
sock = RPC_ANYSOCK;
}
return sock;
}
#ifdef DEBUG
void
xfree(char *f, char *l, void *p)
{
free(p);
}
#endif /* DEBUG */
|