ypmatch.c revision 1.1 1 #include <sys/param.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <stdio.h>
5 #include <ctype.h>
6
7 #include <rpc/rpc.h>
8 #include <rpc/xdr.h>
9 #include <rpcsvc/yp_prot.h>
10 #include <rpcsvc/ypclnt.h>
11
12 struct ypalias {
13 char *alias, *name;
14 } ypaliases[] = {
15 { "passwd", "passwd.byname" },
16 { "group", "group.byname" },
17 { "networks", "networks.byaddr" },
18 { "hosts", "hosts.byaddr" },
19 { "protocols", "protocols.bynumber" },
20 { "services", "services.byname" },
21 { "aliases", "mail.aliases" },
22 { "ethers", "ethers.byname" },
23 };
24
25 usage()
26 {
27 fprintf(stderr, "Usage:\n");
28 fprintf(stderr, "\typmatch [-d domain] [-t] [-k] key [key ...] mname\n");
29 fprintf(stderr, "\typmatch -x\n");
30 fprintf(stderr, "where\n");
31 fprintf(stderr, "\tmname may be either a mapname or a nickname for a map\n");
32 fprintf(stderr, "\t-t inhibits map nickname translation\n");
33 fprintf(stderr, "\t-k prints keys as well as values.\n");
34 fprintf(stderr, "\t-x dumps the map nickname translation table.\n");
35 exit(1);
36 }
37
38 int
39 main(argc, argv)
40 char **argv;
41 {
42 char *domainname;
43 char *inkey, *inmap, *outbuf;
44 extern char *optarg;
45 extern int optind;
46 int outbuflen, key, notrans;
47 int c, r, i;
48
49 notrans = key = 0;
50 yp_get_default_domain(&domainname);
51
52 while( (c=getopt(argc, argv, "xd:kt")) != -1)
53 switch(c) {
54 case 'x':
55 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
56 printf("Use \"%s\" for \"%s\"\n",
57 ypaliases[i].alias,
58 ypaliases[i].name);
59 exit(0);
60 case 'd':
61 domainname = optarg;
62 break;
63 case 't':
64 notrans++;
65 break;
66 case 'k':
67 key++;
68 break;
69 default:
70 usage();
71 }
72
73 if( (argc-optind) < 2 )
74 usage();
75
76 inmap = argv[argc-1];
77 for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
78 if( strcmp(inmap, ypaliases[i].alias) == 0)
79 inmap = ypaliases[i].name;
80 for(; optind < argc-1; optind++) {
81 inkey = argv[optind];
82
83 r = yp_match(domainname, inmap, inkey,
84 strlen(inkey), &outbuf, &outbuflen);
85 switch(r) {
86 case 0:
87 if(key)
88 printf("%s ", inkey);
89 printf("%*.*s\n", outbuflen, outbuflen, outbuf);
90 break;
91 case YPERR_YPBIND:
92 fprintf(stderr, "yp_match: not running ypbind\n");
93 exit(1);
94 default:
95 fprintf(stderr, "Can't match key %s in map %s. Reason: %s\n",
96 inkey, inmap, yperr_string(r));
97 break;
98 }
99 }
100 exit(0);
101 }
102