Annotation of src/usr.bin/ypcat/ypcat.c, Revision 1.13
1.13 ! sobrado 1: /* $OpenBSD: ypcat.c,v 1.12 2006/04/02 01:49:18 deraadt Exp $ */
1.3 deraadt 2:
1.1 deraadt 3: /*
1.3 deraadt 4: * Copyright (c) 1992, 1993, 1996 Theo de Raadt <deraadt@theos.com>
1.1 deraadt 5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26: * SUCH DAMAGE.
27: */
28:
1.12 deraadt 29: #ifndef lint
1.13 ! sobrado 30: static char rcsid[] = "$OpenBSD: ypcat.c,v 1.12 2006/04/02 01:49:18 deraadt Exp $";
1.1 deraadt 31: #endif
32:
33: #include <sys/param.h>
34: #include <sys/types.h>
35: #include <sys/socket.h>
1.6 deraadt 36: #include <unistd.h>
37: #include <string.h>
1.1 deraadt 38: #include <stdio.h>
1.11 david 39: #include <stdlib.h>
1.1 deraadt 40: #include <ctype.h>
41:
42: #include <rpc/rpc.h>
43: #include <rpc/xdr.h>
1.2 deraadt 44: #include <rpcsvc/yp.h>
1.1 deraadt 45: #include <rpcsvc/ypclnt.h>
1.10 deraadt 46:
47: void usage(void);
48: int printit(u_long, char *, int, char *, int, void *);
1.1 deraadt 49:
50: struct ypalias {
51: char *alias, *name;
52: } ypaliases[] = {
53: { "passwd", "passwd.byname" },
1.7 deraadt 54: { "master.passwd", "master.passwd.byname" },
1.1 deraadt 55: { "group", "group.byname" },
56: { "networks", "networks.byaddr" },
57: { "hosts", "hosts.byaddr" },
58: { "protocols", "protocols.bynumber" },
59: { "services", "services.byname" },
60: { "aliases", "mail.aliases" },
61: { "ethers", "ethers.byname" },
62: };
63:
64: int key;
65:
1.6 deraadt 66: void
1.8 deraadt 67: usage(void)
1.1 deraadt 68: {
1.13 ! sobrado 69: fprintf(stderr,
! 70: "usage: ypcat [-kt] [-d domainname] mapname\n"
! 71: " ypcat -x\n");
1.1 deraadt 72: exit(1);
73: }
74:
1.6 deraadt 75: int
1.8 deraadt 76: printit(u_long instatus, char *inkey, int inkeylen, char *inval, int invallen,
77: void *indata)
1.1 deraadt 78: {
1.5 deraadt 79: if (instatus != YP_TRUE)
1.1 deraadt 80: return instatus;
1.5 deraadt 81: if (key)
1.1 deraadt 82: printf("%*.*s ", inkeylen, inkeylen, inkey);
83: printf("%*.*s\n", invallen, invallen, inval);
84: return 0;
85: }
86:
87: int
1.8 deraadt 88: main(int argc, char *argv[])
1.1 deraadt 89: {
1.8 deraadt 90: char *domain = NULL, *inmap;
1.1 deraadt 91: struct ypall_callback ypcb;
92: extern char *optarg;
93: extern int optind;
1.8 deraadt 94: int notrans, c, r, i;
1.1 deraadt 95:
96: notrans = key = 0;
1.5 deraadt 97: while ((c=getopt(argc, argv, "xd:kt")) != -1)
98: switch (c) {
1.1 deraadt 99: case 'x':
1.5 deraadt 100: for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1 deraadt 101: printf("Use \"%s\" for \"%s\"\n",
1.5 deraadt 102: ypaliases[i].alias, ypaliases[i].name);
1.1 deraadt 103: exit(0);
104: case 'd':
1.2 deraadt 105: domain = optarg;
1.1 deraadt 106: break;
107: case 't':
108: notrans++;
109: break;
110: case 'k':
111: key++;
112: break;
113: default:
114: usage();
115: }
116:
1.5 deraadt 117: if (optind + 1 != argc )
1.1 deraadt 118: usage();
1.4 deraadt 119:
1.5 deraadt 120: if (!domain)
121: yp_get_default_domain(&domain);
1.1 deraadt 122:
123: inmap = argv[optind];
1.3 deraadt 124: if (!notrans) {
1.5 deraadt 125: for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
126: if (strcmp(inmap, ypaliases[i].alias) == 0)
1.3 deraadt 127: inmap = ypaliases[i].name;
128: }
1.1 deraadt 129: ypcb.foreach = printit;
130: ypcb.data = NULL;
131:
1.2 deraadt 132: r = yp_all(domain, inmap, &ypcb);
1.5 deraadt 133: switch (r) {
1.1 deraadt 134: case 0:
135: break;
136: case YPERR_YPBIND:
137: fprintf(stderr, "ypcat: not running ypbind\n");
138: exit(1);
139: default:
140: fprintf(stderr, "No such map %s. Reason: %s\n",
1.5 deraadt 141: inmap, yperr_string(r));
1.1 deraadt 142: exit(1);
143: }
144: exit(0);
145: }