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

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