xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_update.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /*	  All Rights Reserved   */
29 
30 /*
31  * Portions of this source code were derived from Berkeley
32  * under license from the Regents of the University of
33  * California.
34  */
35 
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 /*
39  * YP updater interface
40  */
41 #include <stdio.h>
42 #include <rpc/rpc.h>
43 #include "yp_b.h"
44 #include <rpcsvc/ypclnt.h>
45 #include <rpcsvc/ypupd.h>
46 #include <sys/types.h>
47 #include <rpc/trace.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(domain, map, op, key, keylen, data, datalen)
64 	char *domain;
65 	char *map;
66 	unsigned op;
67 	char *key;
68 	int keylen;
69 	char *data;
70 	int datalen;
71 {
72 	struct ypupdate_args args;
73 	u_int rslt;
74 	struct timeval total;
75 	CLIENT *client;
76 	char *ypmaster;
77 	char ypmastername[MAXNETNAMELEN+1];
78 	enum clnt_stat stat;
79 	u_int proc;
80 
81 	trace3(TR_yp_update, 0, keylen, datalen);
82 	switch (op) {
83 	case YPOP_DELETE:
84 		proc = YPU_DELETE;
85 		break;
86 	case YPOP_INSERT:
87 		proc = YPU_INSERT;
88 		break;
89 	case YPOP_CHANGE:
90 		proc = YPU_CHANGE;
91 		break;
92 	case YPOP_STORE:
93 		proc = YPU_STORE;
94 		break;
95 	default:
96 		trace1(TR_yp_update, 1);
97 		return (YPERR_BADARGS);
98 	}
99 	if (yp_master(domain, map, &ypmaster) != 0) {
100 		debug("no master found");
101 		trace1(TR_yp_update, 1);
102 		return (YPERR_BADDB);
103 	}
104 
105 	client = clnt_create(ypmaster, YPU_PROG, YPU_VERS, "circuit_n");
106 	if (client == NULL) {
107 #ifdef DEBUG
108 		/* CONSTCOND */
109 		if (debugging) {
110 			clnt_pcreateerror("client create failed");
111 		}
112 #endif /* DEBUG */
113 		free(ypmaster);
114 		trace1(TR_yp_update, 1);
115 		return (YPERR_RPC);
116 	}
117 
118 	if (! host2netname(ypmastername, ypmaster, domain)) {
119 		clnt_destroy(client);
120 		free(ypmaster);
121 		trace1(TR_yp_update, 1);
122 		return (YPERR_BADARGS);
123 	}
124 	client->cl_auth = authdes_seccreate(ypmastername, WINDOW,
125 				ypmaster, NULL);
126 	free(ypmaster);
127 	if (client->cl_auth == NULL) {
128 		debug("auth create failed");
129 		clnt_destroy(client);
130 		trace1(TR_yp_update, 1);
131 		return (YPERR_RPC);
132 	}
133 
134 	args.mapname = map;
135 	args.key.yp_buf_len = keylen;
136 	args.key.yp_buf_val = key;
137 	args.datum.yp_buf_len = datalen;
138 	args.datum.yp_buf_val = data;
139 
140 	total.tv_sec = TOTAL_TIMEOUT;
141 	total.tv_usec = 0;
142 	clnt_control(client, CLSET_TIMEOUT, (char *)&total);
143 	stat = clnt_call(client, proc,
144 		xdr_ypupdate_args, (char *)&args,
145 		xdr_u_int, (char *)&rslt, total);
146 
147 	if (stat != RPC_SUCCESS) {
148 #ifdef DEBUG
149 		debug("ypupdate RPC call failed");
150 		/* CONSTCOND */
151 		if (debugging)
152 			clnt_perror(client, "ypupdate call failed");
153 #endif /* DEBUG */
154 		rslt = YPERR_RPC;
155 	}
156 	auth_destroy(client->cl_auth);
157 	clnt_destroy(client);
158 	trace1(TR_yp_update, 1);
159 	return (rslt);
160 }
161