[BACK]Return to yp_first.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / yp

Annotation of src/lib/libc/yp/yp_first.c, Revision 1.8

1.8     ! jtc         1: /*     $NetBSD: yp_first.c,v 1.7 1997/07/13 20:28:11 christos Exp $     */
1.1       jtc         2:
                      3: /*
                      4:  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by Theo de Raadt.
                     18:  * 4. The name of the author may not be used to endorse or promote products
                     19:  *    derived from this software without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     22:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     23:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     25:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     31:  * SUCH DAMAGE.
                     32:  */
                     33:
1.7       christos   34: #include <sys/cdefs.h>
1.1       jtc        35: #if defined(LIBC_SCCS) && !defined(lint)
1.8     ! jtc        36: __RCSID("$NetBSD: yp_first.c,v 1.7 1997/07/13 20:28:11 christos Exp $");
1.1       jtc        37: #endif
                     38:
1.8     ! jtc        39: #include "namespace.h"
1.1       jtc        40: #include <stdlib.h>
1.2       cgd        41: #include <string.h>
1.1       jtc        42: #include <rpc/rpc.h>
                     43: #include <rpcsvc/yp_prot.h>
                     44: #include <rpcsvc/ypclnt.h>
1.7       christos   45: #include "local.h"
1.1       jtc        46:
                     47: extern struct timeval _yplib_timeout;
1.3       christos   48: extern int _yplib_nerrs;
1.1       jtc        49:
1.8     ! jtc        50: #ifdef __weak_alias
        !            51: __weak_alias(yp_first,_yp_first);
        !            52: __weak_alias(yp_next,_yp_next);
        !            53: #endif
        !            54:
1.1       jtc        55: int
                     56: yp_first(indomain, inmap, outkey, outkeylen, outval, outvallen)
                     57:        const char     *indomain;
                     58:        const char     *inmap;
                     59:        char          **outkey;
                     60:        int            *outkeylen;
                     61:        char          **outval;
                     62:        int            *outvallen;
                     63: {
                     64:        struct ypresp_key_val yprkv;
                     65:        struct ypreq_nokey yprnk;
                     66:        struct dom_binding *ysd;
1.3       christos   67:        int r, nerrs = 0;
1.1       jtc        68:
1.4       lukem      69:        if (outkey == NULL || outval == NULL)
                     70:                return YPERR_BADARGS;
                     71:        *outkey = *outval = NULL;
                     72:        *outkeylen = *outvallen = 0;
1.6       lukem      73:        if (_yp_invalid_domain(indomain))
1.1       jtc        74:                return YPERR_BADARGS;
                     75:        if (inmap == NULL || *inmap == '\0'
                     76:            || strlen(inmap) > YPMAXMAP)
                     77:                return YPERR_BADARGS;
                     78:
                     79: again:
                     80:        if (_yp_dobind(indomain, &ysd) != 0)
                     81:                return YPERR_DOMAIN;
                     82:
                     83:        yprnk.domain = indomain;
                     84:        yprnk.map = inmap;
                     85:        (void)memset(&yprkv, 0, sizeof yprkv);
                     86:
                     87:        r = clnt_call(ysd->dom_client, YPPROC_FIRST,
                     88:                   xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv,
                     89:                   _yplib_timeout);
                     90:        if (r != RPC_SUCCESS) {
1.3       christos   91:                if (++nerrs == _yplib_nerrs) {
                     92:                        clnt_perror(ysd->dom_client, "yp_first: clnt_call");
                     93:                        nerrs = 0;
                     94:                }
1.1       jtc        95:                ysd->dom_vers = -1;
                     96:                goto again;
                     97:        }
                     98:        if (!(r = ypprot_err(yprkv.status))) {
                     99:                *outkeylen = yprkv.keydat.dsize;
                    100:                if ((*outkey = malloc(*outkeylen + 1)) == NULL)
                    101:                        r = YPERR_RESRC;
                    102:                else {
                    103:                        (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
                    104:                        (*outkey)[*outkeylen] = '\0';
                    105:                }
                    106:                *outvallen = yprkv.valdat.dsize;
                    107:                if ((*outval = malloc(*outvallen + 1)) == NULL)
                    108:                        r = YPERR_RESRC;
                    109:                else {
                    110:                        (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
                    111:                        (*outval)[*outvallen] = '\0';
                    112:                }
                    113:        }
                    114:        xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
1.8     ! jtc       115:        __yp_unbind(ysd);
1.5       lukem     116:        if (r != 0) {
                    117:                if (*outkey) {
                    118:                        free(*outkey);
                    119:                        *outkey = NULL;
                    120:                }
                    121:                if (*outval) {
                    122:                        free(*outval);
                    123:                        *outval = NULL;
                    124:                }
                    125:        }
1.1       jtc       126:        return r;
                    127: }
                    128:
                    129: int
                    130: yp_next(indomain, inmap, inkey, inkeylen, outkey, outkeylen, outval, outvallen)
                    131:        const char     *indomain;
                    132:        const char     *inmap;
                    133:        const char     *inkey;
                    134:        int             inkeylen;
                    135:        char          **outkey;
                    136:        int            *outkeylen;
                    137:        char          **outval;
                    138:        int            *outvallen;
                    139: {
                    140:        struct ypresp_key_val yprkv;
                    141:        struct ypreq_key yprk;
                    142:        struct dom_binding *ysd;
1.3       christos  143:        int r, nerrs = 0;
1.1       jtc       144:
1.4       lukem     145:        if (outkey == NULL || outval == NULL)
                    146:                return YPERR_BADARGS;
                    147:        *outkey = *outval = NULL;
                    148:        *outkeylen = *outvallen = 0;
                    149:
1.6       lukem     150:        if (_yp_invalid_domain(indomain))
1.1       jtc       151:                return YPERR_BADARGS;
                    152:        if (inmap == NULL || *inmap == '\0'
                    153:            || strlen(inmap) > YPMAXMAP)
                    154:                return YPERR_BADARGS;
                    155:
                    156: again:
                    157:        if (_yp_dobind(indomain, &ysd) != 0)
                    158:                return YPERR_DOMAIN;
                    159:
                    160:        yprk.domain = indomain;
                    161:        yprk.map = inmap;
                    162:        yprk.keydat.dptr = inkey;
                    163:        yprk.keydat.dsize = inkeylen;
                    164:        (void)memset(&yprkv, 0, sizeof yprkv);
                    165:
                    166:        r = clnt_call(ysd->dom_client, YPPROC_NEXT,
                    167:                      xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv,
                    168:                      _yplib_timeout);
                    169:        if (r != RPC_SUCCESS) {
1.3       christos  170:                if (++nerrs == _yplib_nerrs) {
                    171:                        clnt_perror(ysd->dom_client, "yp_next: clnt_call");
                    172:                        nerrs = 0;
                    173:                }
1.1       jtc       174:                ysd->dom_vers = -1;
                    175:                goto again;
                    176:        }
                    177:        if (!(r = ypprot_err(yprkv.status))) {
                    178:                *outkeylen = yprkv.keydat.dsize;
                    179:                if ((*outkey = malloc(*outkeylen + 1)) == NULL)
                    180:                        r = YPERR_RESRC;
                    181:                else {
                    182:                        (void)memcpy(*outkey, yprkv.keydat.dptr, *outkeylen);
                    183:                        (*outkey)[*outkeylen] = '\0';
                    184:                }
                    185:                *outvallen = yprkv.valdat.dsize;
                    186:                if ((*outval = malloc(*outvallen + 1)) == NULL)
                    187:                        r = YPERR_RESRC;
                    188:                else {
                    189:                        (void)memcpy(*outval, yprkv.valdat.dptr, *outvallen);
                    190:                        (*outval)[*outvallen] = '\0';
                    191:                }
                    192:        }
                    193:        xdr_free(xdr_ypresp_key_val, (char *) &yprkv);
1.8     ! jtc       194:        __yp_unbind(ysd);
1.5       lukem     195:        if (r != 0) {
                    196:                if (*outkey) {
                    197:                        free(*outkey);
                    198:                        *outkey = NULL;
                    199:                }
                    200:                if (*outval) {
                    201:                        free(*outval);
                    202:                        *outval = NULL;
                    203:                }
                    204:        }
1.1       jtc       205:        return r;
                    206: }

CVSweb <webmaster@jp.NetBSD.org>