xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_update.c (revision 61961e0f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /*	  All Rights Reserved   */
30 
31 /*
32  * Portions of this source code were derived from Berkeley
33  * under license from the Regents of the University of
34  * California.
35  */
36 
37 #pragma ident	"%Z%%M%	%I%	%E% SMI"
38 
39 /*
40  * YP updater interface
41  */
42 #include <stdio.h>
43 #include <rpc/rpc.h>
44 #include "yp_b.h"
45 #include <rpcsvc/ypclnt.h>
46 #include <rpcsvc/ypupd.h>
47 #include <sys/types.h>
48 #include <stdlib.h>
49 
50 #define	WINDOW (60*60)
51 #define	TOTAL_TIMEOUT	300
52 
53 #ifdef DEBUG
54 #define	debugging 1
55 #define	debug(msg)  (void) fprintf(stderr, "%s\n", msg);
56 #else
57 #define	debugging 0
58 #define	debug(msg)
59 #endif
60 extern AUTH *authdes_seccreate();
61 
62 int
63 yp_update(char *domain, char *map, unsigned op, char *key, int keylen,
64 							char *data, int datalen)
65 {
66 	struct ypupdate_args args;
67 	uint_t rslt;
68 	struct timeval total;
69 	CLIENT *client;
70 	char *ypmaster;
71 	char ypmastername[MAXNETNAMELEN+1];
72 	enum clnt_stat stat;
73 	uint_t proc;
74 
75 	switch (op) {
76 	case YPOP_DELETE:
77 		proc = YPU_DELETE;
78 		break;
79 	case YPOP_INSERT:
80 		proc = YPU_INSERT;
81 		break;
82 	case YPOP_CHANGE:
83 		proc = YPU_CHANGE;
84 		break;
85 	case YPOP_STORE:
86 		proc = YPU_STORE;
87 		break;
88 	default:
89 		return (YPERR_BADARGS);
90 	}
91 	if (yp_master(domain, map, &ypmaster) != 0) {
92 		debug("no master found");
93 		return (YPERR_BADDB);
94 	}
95 
96 	client = clnt_create(ypmaster, YPU_PROG, YPU_VERS, "circuit_n");
97 	if (client == NULL) {
98 #ifdef DEBUG
99 		/* CONSTCOND */
100 		if (debugging) {
101 			clnt_pcreateerror("client create failed");
102 		}
103 #endif /* DEBUG */
104 		free(ypmaster);
105 		return (YPERR_RPC);
106 	}
107 
108 	if (!host2netname(ypmastername, ypmaster, domain)) {
109 		clnt_destroy(client);
110 		free(ypmaster);
111 		return (YPERR_BADARGS);
112 	}
113 	client->cl_auth = authdes_seccreate(ypmastername, WINDOW,
114 				ypmaster, NULL);
115 	free(ypmaster);
116 	if (client->cl_auth == NULL) {
117 		debug("auth create failed");
118 		clnt_destroy(client);
119 		return (YPERR_RPC);
120 	}
121 
122 	args.mapname = map;
123 	args.key.yp_buf_len = keylen;
124 	args.key.yp_buf_val = key;
125 	args.datum.yp_buf_len = datalen;
126 	args.datum.yp_buf_val = data;
127 
128 	total.tv_sec = TOTAL_TIMEOUT;
129 	total.tv_usec = 0;
130 	clnt_control(client, CLSET_TIMEOUT, (char *)&total);
131 	stat = clnt_call(client, proc,
132 		xdr_ypupdate_args, (char *)&args,
133 		xdr_u_int, (char *)&rslt, total);
134 
135 	if (stat != RPC_SUCCESS) {
136 #ifdef DEBUG
137 		debug("ypupdate RPC call failed");
138 		/* CONSTCOND */
139 		if (debugging)
140 			clnt_perror(client, "ypupdate call failed");
141 #endif /* DEBUG */
142 		rslt = YPERR_RPC;
143 	}
144 	auth_destroy(client->cl_auth);
145 	clnt_destroy(client);
146 	return (rslt);
147 }
148