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

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