xref: /onnv-gate/usr/src/lib/libnsl/yp/yp_update.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /*	  All Rights Reserved   */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
330Sstevel@tonic-gate  * under license from the Regents of the University of
340Sstevel@tonic-gate  * California.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * YP updater interface
410Sstevel@tonic-gate  */
42*1219Sraf #include "mt.h"
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <rpc/rpc.h>
450Sstevel@tonic-gate #include "yp_b.h"
460Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
470Sstevel@tonic-gate #include <rpcsvc/ypupd.h>
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #define	WINDOW (60*60)
520Sstevel@tonic-gate #define	TOTAL_TIMEOUT	300
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #ifdef DEBUG
550Sstevel@tonic-gate #define	debugging 1
560Sstevel@tonic-gate #define	debug(msg)  (void) fprintf(stderr, "%s\n", msg);
570Sstevel@tonic-gate #else
580Sstevel@tonic-gate #define	debugging 0
590Sstevel@tonic-gate #define	debug(msg)
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate extern AUTH *authdes_seccreate();
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int
yp_update(char * domain,char * map,unsigned op,char * key,int keylen,char * data,int datalen)64132Srobinson yp_update(char *domain, char *map, unsigned op, char *key, int keylen,
65132Srobinson 							char *data, int datalen)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	struct ypupdate_args args;
68132Srobinson 	uint_t rslt;
690Sstevel@tonic-gate 	struct timeval total;
700Sstevel@tonic-gate 	CLIENT *client;
710Sstevel@tonic-gate 	char *ypmaster;
720Sstevel@tonic-gate 	char ypmastername[MAXNETNAMELEN+1];
730Sstevel@tonic-gate 	enum clnt_stat stat;
74132Srobinson 	uint_t proc;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	switch (op) {
770Sstevel@tonic-gate 	case YPOP_DELETE:
780Sstevel@tonic-gate 		proc = YPU_DELETE;
790Sstevel@tonic-gate 		break;
800Sstevel@tonic-gate 	case YPOP_INSERT:
810Sstevel@tonic-gate 		proc = YPU_INSERT;
820Sstevel@tonic-gate 		break;
830Sstevel@tonic-gate 	case YPOP_CHANGE:
840Sstevel@tonic-gate 		proc = YPU_CHANGE;
850Sstevel@tonic-gate 		break;
860Sstevel@tonic-gate 	case YPOP_STORE:
870Sstevel@tonic-gate 		proc = YPU_STORE;
880Sstevel@tonic-gate 		break;
890Sstevel@tonic-gate 	default:
900Sstevel@tonic-gate 		return (YPERR_BADARGS);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	if (yp_master(domain, map, &ypmaster) != 0) {
930Sstevel@tonic-gate 		debug("no master found");
940Sstevel@tonic-gate 		return (YPERR_BADDB);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	client = clnt_create(ypmaster, YPU_PROG, YPU_VERS, "circuit_n");
980Sstevel@tonic-gate 	if (client == NULL) {
990Sstevel@tonic-gate #ifdef DEBUG
1000Sstevel@tonic-gate 		/* CONSTCOND */
1010Sstevel@tonic-gate 		if (debugging) {
1020Sstevel@tonic-gate 			clnt_pcreateerror("client create failed");
1030Sstevel@tonic-gate 		}
1040Sstevel@tonic-gate #endif /* DEBUG */
1050Sstevel@tonic-gate 		free(ypmaster);
1060Sstevel@tonic-gate 		return (YPERR_RPC);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
109132Srobinson 	if (!host2netname(ypmastername, ypmaster, domain)) {
1100Sstevel@tonic-gate 		clnt_destroy(client);
1110Sstevel@tonic-gate 		free(ypmaster);
1120Sstevel@tonic-gate 		return (YPERR_BADARGS);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	client->cl_auth = authdes_seccreate(ypmastername, WINDOW,
1150Sstevel@tonic-gate 				ypmaster, NULL);
1160Sstevel@tonic-gate 	free(ypmaster);
1170Sstevel@tonic-gate 	if (client->cl_auth == NULL) {
1180Sstevel@tonic-gate 		debug("auth create failed");
1190Sstevel@tonic-gate 		clnt_destroy(client);
1200Sstevel@tonic-gate 		return (YPERR_RPC);
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	args.mapname = map;
1240Sstevel@tonic-gate 	args.key.yp_buf_len = keylen;
1250Sstevel@tonic-gate 	args.key.yp_buf_val = key;
1260Sstevel@tonic-gate 	args.datum.yp_buf_len = datalen;
1270Sstevel@tonic-gate 	args.datum.yp_buf_val = data;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	total.tv_sec = TOTAL_TIMEOUT;
1300Sstevel@tonic-gate 	total.tv_usec = 0;
1310Sstevel@tonic-gate 	clnt_control(client, CLSET_TIMEOUT, (char *)&total);
1320Sstevel@tonic-gate 	stat = clnt_call(client, proc,
1330Sstevel@tonic-gate 		xdr_ypupdate_args, (char *)&args,
1340Sstevel@tonic-gate 		xdr_u_int, (char *)&rslt, total);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (stat != RPC_SUCCESS) {
1370Sstevel@tonic-gate #ifdef DEBUG
1380Sstevel@tonic-gate 		debug("ypupdate RPC call failed");
1390Sstevel@tonic-gate 		/* CONSTCOND */
1400Sstevel@tonic-gate 		if (debugging)
1410Sstevel@tonic-gate 			clnt_perror(client, "ypupdate call failed");
1420Sstevel@tonic-gate #endif /* DEBUG */
1430Sstevel@tonic-gate 		rslt = YPERR_RPC;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	auth_destroy(client->cl_auth);
1460Sstevel@tonic-gate 	clnt_destroy(client);
1470Sstevel@tonic-gate 	return (rslt);
1480Sstevel@tonic-gate }
149