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