[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.14

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