[BACK]Return to ypwhich.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ypwhich

Annotation of src/usr.bin/ypwhich/ypwhich.c, Revision 1.1.1.1

1.1       deraadt     1: /*
                      2:  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This product includes software developed by Theo de Raadt.
                     16:  * 4. The name of the author may not be used to endorse or promote
                     17:  *    products derived from this software without specific prior written
                     18:  *    permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     21:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     22:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     24:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef LINT
                     34: static char rcsid[] = "$Id: ypwhich.c,v 1.5 1995/05/21 15:42:54 mycroft Exp $";
                     35: #endif
                     36:
                     37: #include <sys/param.h>
                     38: #include <sys/types.h>
                     39: #include <sys/socket.h>
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <ctype.h>
                     43: #include <netdb.h>
                     44: #include <rpc/rpc.h>
                     45: #include <rpc/xdr.h>
                     46: #include <rpcsvc/yp_prot.h>
                     47: #include <rpcsvc/ypclnt.h>
                     48:
                     49: extern bool_t xdr_domainname();
                     50:
                     51: struct ypalias {
                     52:        char *alias, *name;
                     53: } ypaliases[] = {
                     54:        { "passwd", "passwd.byname" },
                     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: usage()
                     65: {
                     66:        fprintf(stderr, "Usage:\n");
                     67:        fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
                     68:        fprintf(stderr, "\typwhich -x\n");
                     69:        exit(1);
                     70: }
                     71:
                     72:
                     73: /*
                     74:  * Like yp_bind except can query a specific host
                     75:  */
                     76: bind_host(dom, sin)
                     77: char *dom;
                     78: struct sockaddr_in *sin;
                     79: {
                     80:        struct hostent *hent = NULL;
                     81:        struct ypbind_resp ypbr;
                     82:        struct dom_binding *ysd;
                     83:        struct timeval tv;
                     84:        CLIENT *client;
                     85:        int sock, r;
                     86:        u_long ss_addr;
                     87:
                     88:        sock = RPC_ANYSOCK;
                     89:        tv.tv_sec = 15;
                     90:        tv.tv_usec = 0;
                     91:        client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
                     92:        if(client==NULL) {
                     93:                fprintf(stderr, "can't clntudp_create: %s\n",
                     94:                        yperr_string(YPERR_YPBIND));
                     95:                return YPERR_YPBIND;
                     96:        }
                     97:
                     98:        tv.tv_sec = 5;
                     99:        tv.tv_usec = 0;
                    100:        r = clnt_call(client, YPBINDPROC_DOMAIN,
                    101:                xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
                    102:        if( r != RPC_SUCCESS) {
                    103:                fprintf(stderr, "can't clnt_call: %s\n",
                    104:                        yperr_string(YPERR_YPBIND));
                    105:                clnt_destroy(client);
                    106:                return YPERR_YPBIND;
                    107:        } else {
                    108:                if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
                    109:                        fprintf(stderr, "can't yp_bind: Reason: %s\n",
                    110:                                yperr_string(ypbr.ypbind_status));
                    111:                        clnt_destroy(client);
                    112:                        return r;
                    113:                }
                    114:        }
                    115:        clnt_destroy(client);
                    116:
                    117:        ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
                    118:        /*printf("%08x\n", ss_addr);*/
                    119:        hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
                    120:        if (hent)
                    121:                printf("%s\n", hent->h_name);
                    122:        else
                    123:                printf("%s\n", inet_ntoa(ss_addr));
                    124:        return 0;
                    125: }
                    126:
                    127: int
                    128: main(argc, argv)
                    129: char **argv;
                    130: {
                    131:        char *domainname, *master, *map;
                    132:        struct ypmaplist *ypml, *y;
                    133:        struct hostent *hent;
                    134:        struct sockaddr_in sin;
                    135:        int notrans, mode, getmap;
                    136:        int c, r, i;
                    137:
                    138:        yp_get_default_domain(&domainname);
                    139:
                    140:        map = NULL;
                    141:        getmap = notrans = mode = 0;
                    142:        while( (c=getopt(argc, argv, "xd:mt")) != -1)
                    143:                switch(c) {
                    144:                case 'x':
                    145:                        for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    146:                                printf("Use \"%s\" for \"%s\"\n",
                    147:                                        ypaliases[i].alias,
                    148:                                        ypaliases[i].name);
                    149:                        exit(0);
                    150:                case 'd':
                    151:                        domainname = optarg;
                    152:                        break;
                    153:                case 't':
                    154:                        notrans++;
                    155:                        break;
                    156:                case 'm':
                    157:                        mode++;
                    158:                        break;
                    159:                default:
                    160:                        usage();
                    161:                }
                    162:        argc -= optind;
                    163:        argv += optind;
                    164:
                    165:        if(mode==0) {
                    166:                switch(argc) {
                    167:                case 0:
                    168:                        bzero(&sin, sizeof sin);
                    169:                        sin.sin_family = AF_INET;
                    170:                        sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
                    171:
                    172:                        if(bind_host(domainname, &sin))
                    173:                                exit(1);
                    174:                        break;
                    175:                case 1:
                    176:                        bzero(&sin, sizeof sin);
                    177:                        sin.sin_family = AF_INET;
                    178:                        if (inet_aton(argv[0], &sin.sin_addr) == 0) {
                    179:                                hent = gethostbyname(argv[0]);
                    180:                                if(!hent) {
                    181:                                        fprintf(stderr, "ypwhich: host %s unknown\n",
                    182:                                            argv[0]);
                    183:                                        exit(1);
                    184:                                }
                    185:                                bcopy((char *)hent->h_addr,
                    186:                                        (char *)&sin.sin_addr, sizeof sin.sin_addr);
                    187:                        }
                    188:                        if(bind_host(domainname, &sin))
                    189:                                exit(1);
                    190:                        break;
                    191:                default:
                    192:                        usage();
                    193:                }
                    194:                exit(0);
                    195:        }
                    196:
                    197:        if( argc > 1)
                    198:                usage();
                    199:
                    200:        if(argv[0]) {
                    201:                map = argv[0];
                    202:                for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
                    203:                        if( strcmp(map, ypaliases[i].alias) == 0)
                    204:                                map = ypaliases[i].name;
                    205:                r = yp_master(domainname, map, &master);
                    206:                switch(r) {
                    207:                case 0:
                    208:                        printf("%s\n", master);
                    209:                        free(master);
                    210:                        break;
                    211:                case YPERR_YPBIND:
                    212:                        fprintf(stderr, "ypwhich: not running ypbind\n");
                    213:                        exit(1);
                    214:                default:
                    215:                        fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
                    216:                                map, yperr_string(r));
                    217:                        exit(1);
                    218:                }
                    219:                exit(0);
                    220:        }
                    221:
                    222:        ypml = NULL;
                    223:        r = yp_maplist(domainname, &ypml);
                    224:        switch(r) {
                    225:        case 0:
                    226:                for(y=ypml; y; ) {
                    227:                        ypml = y;
                    228:                        r = yp_master(domainname, ypml->ypml_name, &master);
                    229:                        switch(r) {
                    230:                        case 0:
                    231:                                printf("%s %s\n", ypml->ypml_name, master);
                    232:                                free(master);
                    233:                                break;
                    234:                        default:
                    235:                                fprintf(stderr,
                    236:                                        "YP: can't find the master of %s: Reason: %s\n",
                    237:                                        ypml->ypml_name, yperr_string(r));
                    238:                                break;
                    239:                        }
                    240:                        y = ypml->ypml_next;
                    241:                        free(ypml);
                    242:                }
                    243:                break;
                    244:        case YPERR_YPBIND:
                    245:                fprintf(stderr, "ypwhich: not running ypbind\n");
                    246:                exit(1);
                    247:        default:
                    248:                fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
                    249:                        domainname, yperr_string(r));
                    250:                exit(1);
                    251:        }
                    252:        exit(0);
                    253: }