1*df57947fSPedro F. Giffuni /*-
2*df57947fSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni *
447593e96SBill Paul * Copyright (c) 1995, 1996
547593e96SBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
647593e96SBill Paul *
747593e96SBill Paul * Redistribution and use in source and binary forms, with or without
847593e96SBill Paul * modification, are permitted provided that the following conditions
947593e96SBill Paul * are met:
1047593e96SBill Paul * 1. Redistributions of source code must retain the above copyright
1147593e96SBill Paul * notice, this list of conditions and the following disclaimer.
1247593e96SBill Paul * 2. Redistributions in binary form must reproduce the above copyright
1347593e96SBill Paul * notice, this list of conditions and the following disclaimer in the
1447593e96SBill Paul * documentation and/or other materials provided with the distribution.
1547593e96SBill Paul * 3. All advertising materials mentioning features or use of this software
1647593e96SBill Paul * must display the following acknowledgement:
1747593e96SBill Paul * This product includes software developed by Bill Paul.
1847593e96SBill Paul * 4. Neither the name of the author nor the names of any co-contributors
1947593e96SBill Paul * may be used to endorse or promote products derived from this software
2047593e96SBill Paul * without specific prior written permission.
2147593e96SBill Paul *
2247593e96SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2347593e96SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2447593e96SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2547593e96SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2647593e96SBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2747593e96SBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2847593e96SBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2947593e96SBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3047593e96SBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3147593e96SBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3247593e96SBill Paul * SUCH DAMAGE.
3347593e96SBill Paul *
3447593e96SBill Paul * ypupdate client-side library function.
3547593e96SBill Paul *
3647593e96SBill Paul * Written by Bill Paul <wpaul@ctr.columbia.edu>
3747593e96SBill Paul * Center for Telecommunications Research
3847593e96SBill Paul * Columbia University, New York City
3947593e96SBill Paul */
4047593e96SBill Paul
41542d87feSMatthew Dillon #include <sys/cdefs.h>
4247593e96SBill Paul #include <stdlib.h>
4347593e96SBill Paul #include <rpc/rpc.h>
4447593e96SBill Paul #include <rpcsvc/yp_prot.h>
4547593e96SBill Paul #include <rpcsvc/ypclnt.h>
4647593e96SBill Paul #include <rpcsvc/ypupdate_prot.h>
4747593e96SBill Paul #include <rpc/key_prot.h>
4847593e96SBill Paul
4947593e96SBill Paul #ifndef WINDOW
5047593e96SBill Paul #define WINDOW (60*60)
5147593e96SBill Paul #endif
5247593e96SBill Paul
5347593e96SBill Paul #ifndef TIMEOUT
5447593e96SBill Paul #define TIMEOUT 300
5547593e96SBill Paul #endif
5647593e96SBill Paul
5747593e96SBill Paul int
yp_update(char * domain,char * map,unsigned int ypop,char * key,int keylen,char * data,int datalen)58dc584ddbSDag-Erling Smørgrav yp_update(char *domain, char *map, unsigned int ypop, char *key, int keylen,
59dc584ddbSDag-Erling Smørgrav char *data, int datalen)
6047593e96SBill Paul {
6147593e96SBill Paul char *master;
6247593e96SBill Paul int rval;
6347593e96SBill Paul unsigned int res;
6447593e96SBill Paul struct ypupdate_args upargs;
6547593e96SBill Paul struct ypdelete_args delargs;
6647593e96SBill Paul CLIENT *clnt;
6747593e96SBill Paul char netname[MAXNETNAMELEN+1];
6847593e96SBill Paul des_block des_key;
6947593e96SBill Paul struct timeval timeout;
7047593e96SBill Paul
7147593e96SBill Paul /* Get the master server name for 'domain.' */
7247593e96SBill Paul if ((rval = yp_master(domain, map, &master)))
7347593e96SBill Paul return(rval);
7447593e96SBill Paul
7547593e96SBill Paul /* Check that ypupdated is running there. */
7647593e96SBill Paul if (getrpcport(master, YPU_PROG, YPU_VERS, ypop))
7747593e96SBill Paul return(YPERR_DOMAIN);
7847593e96SBill Paul
7947593e96SBill Paul /* Get a handle. */
8047593e96SBill Paul if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL)
8147593e96SBill Paul return(YPERR_RPC);
8247593e96SBill Paul
8347593e96SBill Paul /*
8447593e96SBill Paul * Assemble netname of server.
8547593e96SBill Paul * NOTE: It's difficult to discern from the documentation, but
8647593e96SBill Paul * when you make a Secure RPC call, the netname you pass should
8747593e96SBill Paul * be the netname of the guy on the other side, not your own
8847593e96SBill Paul * netname. This is how the client side knows what public key
8947593e96SBill Paul * to use for the initial exchange. Passing your own netname
9047593e96SBill Paul * only works if the server on the other side is running under
9147593e96SBill Paul * your UID.
9247593e96SBill Paul */
9347593e96SBill Paul if (!host2netname(netname, master, domain)) {
9447593e96SBill Paul clnt_destroy(clnt);
9547593e96SBill Paul return(YPERR_BADARGS);
9647593e96SBill Paul }
9747593e96SBill Paul
9847593e96SBill Paul /* Make up a DES session key. */
9947593e96SBill Paul key_gendes(&des_key);
10047593e96SBill Paul
10147593e96SBill Paul /* Set up DES authentication. */
10247593e96SBill Paul if ((clnt->cl_auth = (AUTH *)authdes_create(netname, WINDOW, NULL,
10347593e96SBill Paul &des_key)) == NULL) {
10447593e96SBill Paul clnt_destroy(clnt);
10547593e96SBill Paul return(YPERR_RESRC);
10647593e96SBill Paul }
10747593e96SBill Paul
10847593e96SBill Paul /* Set a timeout for clnt_call(). */
10947593e96SBill Paul timeout.tv_usec = 0;
11047593e96SBill Paul timeout.tv_sec = TIMEOUT;
11147593e96SBill Paul
11247593e96SBill Paul /*
11347593e96SBill Paul * Make the call. Note that we use clnt_call() here rather than
11447593e96SBill Paul * the rpcgen-erated client stubs. We could use those stubs, but
11547593e96SBill Paul * then we'd have to do some gymnastics to get at the error
11647593e96SBill Paul * information to figure out what error code to send back to the
11747593e96SBill Paul * caller. With clnt_call(), we get the error status returned to
11847593e96SBill Paul * us right away, and we only have to exert a small amount of
11947593e96SBill Paul * extra effort.
12047593e96SBill Paul */
12147593e96SBill Paul switch (ypop) {
12247593e96SBill Paul case YPOP_CHANGE:
12347593e96SBill Paul upargs.mapname = map;
12447593e96SBill Paul upargs.key.yp_buf_len = keylen;
12547593e96SBill Paul upargs.key.yp_buf_val = key;
12647593e96SBill Paul upargs.datum.yp_buf_len = datalen;
12747593e96SBill Paul upargs.datum.yp_buf_val = data;
12847593e96SBill Paul
1294f759369SPeter Wemm if ((rval = clnt_call(clnt, YPU_CHANGE,
1304f759369SPeter Wemm (xdrproc_t)xdr_ypupdate_args, &upargs,
1314f759369SPeter Wemm (xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
13247593e96SBill Paul if (rval == RPC_AUTHERROR)
13347593e96SBill Paul res = YPERR_ACCESS;
13447593e96SBill Paul else
13547593e96SBill Paul res = YPERR_RPC;
13647593e96SBill Paul }
13747593e96SBill Paul
13847593e96SBill Paul break;
13947593e96SBill Paul case YPOP_INSERT:
14047593e96SBill Paul upargs.mapname = map;
14147593e96SBill Paul upargs.key.yp_buf_len = keylen;
14247593e96SBill Paul upargs.key.yp_buf_val = key;
14347593e96SBill Paul upargs.datum.yp_buf_len = datalen;
14447593e96SBill Paul upargs.datum.yp_buf_val = data;
14547593e96SBill Paul
1464f759369SPeter Wemm if ((rval = clnt_call(clnt, YPU_INSERT,
1474f759369SPeter Wemm (xdrproc_t)xdr_ypupdate_args, &upargs,
1484f759369SPeter Wemm (xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
14947593e96SBill Paul if (rval == RPC_AUTHERROR)
15047593e96SBill Paul res = YPERR_ACCESS;
15147593e96SBill Paul else
15247593e96SBill Paul res = YPERR_RPC;
15347593e96SBill Paul }
15447593e96SBill Paul
15547593e96SBill Paul break;
15647593e96SBill Paul case YPOP_DELETE:
15747593e96SBill Paul delargs.mapname = map;
15847593e96SBill Paul delargs.key.yp_buf_len = keylen;
15947593e96SBill Paul delargs.key.yp_buf_val = key;
16047593e96SBill Paul
1614f759369SPeter Wemm if ((rval = clnt_call(clnt, YPU_DELETE,
1624f759369SPeter Wemm (xdrproc_t)xdr_ypdelete_args, &delargs,
1634f759369SPeter Wemm (xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
16447593e96SBill Paul if (rval == RPC_AUTHERROR)
16547593e96SBill Paul res = YPERR_ACCESS;
16647593e96SBill Paul else
16747593e96SBill Paul res = YPERR_RPC;
16847593e96SBill Paul }
16947593e96SBill Paul
17047593e96SBill Paul break;
17147593e96SBill Paul case YPOP_STORE:
17247593e96SBill Paul upargs.mapname = map;
17347593e96SBill Paul upargs.key.yp_buf_len = keylen;
17447593e96SBill Paul upargs.key.yp_buf_val = key;
17547593e96SBill Paul upargs.datum.yp_buf_len = datalen;
17647593e96SBill Paul upargs.datum.yp_buf_val = data;
17747593e96SBill Paul
1784f759369SPeter Wemm if ((rval = clnt_call(clnt, YPU_STORE,
1794f759369SPeter Wemm (xdrproc_t)xdr_ypupdate_args, &upargs,
1804f759369SPeter Wemm (xdrproc_t)xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
18147593e96SBill Paul if (rval == RPC_AUTHERROR)
18247593e96SBill Paul res = YPERR_ACCESS;
18347593e96SBill Paul else
18447593e96SBill Paul res = YPERR_RPC;
18547593e96SBill Paul }
18647593e96SBill Paul
18747593e96SBill Paul break;
18847593e96SBill Paul default:
18947593e96SBill Paul res = YPERR_BADARGS;
19047593e96SBill Paul break;
19147593e96SBill Paul }
19247593e96SBill Paul
19347593e96SBill Paul /* All done: tear down the connection. */
19447593e96SBill Paul auth_destroy(clnt->cl_auth);
19547593e96SBill Paul clnt_destroy(clnt);
19647593e96SBill Paul free(master);
19747593e96SBill Paul
19847593e96SBill Paul return(res);
19947593e96SBill Paul }
200