xref: /illumos-gate/usr/src/lib/libnsl/yp/yp_update.c (revision ad4335f7b330ad9b975ac61078b04b64707ffe5c)
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 2006 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 /*
38  * YP updater interface
39  */
40 #include "mt.h"
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 <stdlib.h>
48 
49 #define	WINDOW (60*60)
50 #define	TOTAL_TIMEOUT	300
51 
52 #ifdef DEBUG
53 #define	debugging 1
54 #define	debug(msg)  (void) fprintf(stderr, "%s\n", msg);
55 #else
56 #define	debugging 0
57 #define	debug(msg)
58 #endif
59 extern AUTH *authdes_seccreate();
60 
61 int
62 yp_update(char *domain, char *map, unsigned op, char *key, int keylen,
63 							char *data, int datalen)
64 {
65 	struct ypupdate_args args;
66 	uint_t rslt;
67 	struct timeval total;
68 	CLIENT *client;
69 	char *ypmaster;
70 	char ypmastername[MAXNETNAMELEN+1];
71 	enum clnt_stat stat;
72 	uint_t proc;
73 
74 	switch (op) {
75 	case YPOP_DELETE:
76 		proc = YPU_DELETE;
77 		break;
78 	case YPOP_INSERT:
79 		proc = YPU_INSERT;
80 		break;
81 	case YPOP_CHANGE:
82 		proc = YPU_CHANGE;
83 		break;
84 	case YPOP_STORE:
85 		proc = YPU_STORE;
86 		break;
87 	default:
88 		return (YPERR_BADARGS);
89 	}
90 	if (yp_master(domain, map, &ypmaster) != 0) {
91 		debug("no master found");
92 		return (YPERR_BADDB);
93 	}
94 
95 	client = clnt_create(ypmaster, YPU_PROG, YPU_VERS, "circuit_n");
96 	if (client == NULL) {
97 #ifdef DEBUG
98 		/* CONSTCOND */
99 		if (debugging) {
100 			clnt_pcreateerror("client create failed");
101 		}
102 #endif /* DEBUG */
103 		free(ypmaster);
104 		return (YPERR_RPC);
105 	}
106 
107 	if (!host2netname(ypmastername, ypmaster, domain)) {
108 		clnt_destroy(client);
109 		free(ypmaster);
110 		return (YPERR_BADARGS);
111 	}
112 	client->cl_auth = authdes_seccreate(ypmastername, WINDOW,
113 				ypmaster, NULL);
114 	free(ypmaster);
115 	if (client->cl_auth == NULL) {
116 		debug("auth create failed");
117 		clnt_destroy(client);
118 		return (YPERR_RPC);
119 	}
120 
121 	args.mapname = map;
122 	args.key.yp_buf_len = keylen;
123 	args.key.yp_buf_val = key;
124 	args.datum.yp_buf_len = datalen;
125 	args.datum.yp_buf_val = data;
126 
127 	total.tv_sec = TOTAL_TIMEOUT;
128 	total.tv_usec = 0;
129 	clnt_control(client, CLSET_TIMEOUT, (char *)&total);
130 	stat = clnt_call(client, proc,
131 		xdr_ypupdate_args, (char *)&args,
132 		xdr_u_int, (char *)&rslt, total);
133 
134 	if (stat != RPC_SUCCESS) {
135 #ifdef DEBUG
136 		debug("ypupdate RPC call failed");
137 		/* CONSTCOND */
138 		if (debugging)
139 			clnt_perror(client, "ypupdate call failed");
140 #endif /* DEBUG */
141 		rslt = YPERR_RPC;
142 	}
143 	auth_destroy(client->cl_auth);
144 	clnt_destroy(client);
145 	return (rslt);
146 }
147