Initial import from FreeBSD RELENG_4:
[games.git] / lib / librpcsvc / yp_update.c
1 /*
2  * Copyright (c) 1995, 1996
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * ypupdate client-side library function.
33  *
34  * Written by Bill Paul <wpaul@ctr.columbia.edu>
35  * Center for Telecommunications Research
36  * Columbia University, New York City
37  *
38  * $FreeBSD: src/lib/librpcsvc/yp_update.c,v 1.2.2.1 2002/02/15 00:46:54 des Exp $
39  */
40
41 #include <stdlib.h>
42 #include <rpc/rpc.h>
43 #include <rpcsvc/yp_prot.h>
44 #include <rpcsvc/ypclnt.h>
45 #include <rpcsvc/ypupdate_prot.h>
46 #include <rpc/key_prot.h>
47
48 #ifndef lint
49 static const char rcsid[] = "$FreeBSD: src/lib/librpcsvc/yp_update.c,v 1.2.2.1 2002/02/15 00:46:54 des Exp $";
50 #endif
51
52 #ifndef WINDOW
53 #define WINDOW (60*60)
54 #endif
55
56 #ifndef TIMEOUT
57 #define TIMEOUT 300
58 #endif
59
60 int
61 yp_update(domain, map, ypop, key, keylen, data, datalen)
62         char            *domain;
63         char            *map;
64         unsigned int    ypop;
65         char            *key;
66         int             keylen;
67         char            *data;
68         int             datalen;
69 {
70         char *master;
71         int rval;
72         unsigned int res;
73         struct ypupdate_args upargs;
74         struct ypdelete_args delargs;
75         CLIENT *clnt;
76         char netname[MAXNETNAMELEN+1];
77         des_block des_key;
78         struct timeval timeout;
79
80         /* Get the master server name for 'domain.' */
81         if ((rval = yp_master(domain, map, &master)))
82                 return(rval);
83
84         /* Check that ypupdated is running there. */
85         if (getrpcport(master, YPU_PROG, YPU_VERS, ypop))
86                 return(YPERR_DOMAIN);
87
88         /* Get a handle. */
89         if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL)
90                 return(YPERR_RPC);
91
92         /*
93          * Assemble netname of server.
94          * NOTE: It's difficult to discern from the documentation, but
95          * when you make a Secure RPC call, the netname you pass should
96          * be the netname of the guy on the other side, not your own
97          * netname. This is how the client side knows what public key
98          * to use for the initial exchange. Passing your own netname
99          * only works if the server on the other side is running under
100          * your UID.
101          */
102         if (!host2netname(netname, master, domain)) {
103                 clnt_destroy(clnt);
104                 return(YPERR_BADARGS);
105         }
106
107         /* Make up a DES session key. */
108         key_gendes(&des_key);
109
110         /* Set up DES authentication. */
111         if ((clnt->cl_auth = (AUTH *)authdes_create(netname, WINDOW, NULL,
112                         &des_key)) == NULL) {
113                 clnt_destroy(clnt);
114                 return(YPERR_RESRC);
115         }
116
117         /* Set a timeout for clnt_call(). */
118         timeout.tv_usec = 0;
119         timeout.tv_sec = TIMEOUT;
120
121         /*
122          * Make the call. Note that we use clnt_call() here rather than
123          * the rpcgen-erated client stubs. We could use those stubs, but
124          * then we'd have to do some gymnastics to get at the error
125          * information to figure out what error code to send back to the
126          * caller. With clnt_call(), we get the error status returned to
127          * us right away, and we only have to exert a small amount of
128          * extra effort.
129          */
130         switch (ypop) {
131         case YPOP_CHANGE:
132                 upargs.mapname = map;
133                 upargs.key.yp_buf_len = keylen;
134                 upargs.key.yp_buf_val = key;
135                 upargs.datum.yp_buf_len = datalen;
136                 upargs.datum.yp_buf_val = data;
137
138                 if ((rval = clnt_call(clnt, YPU_CHANGE, xdr_ypupdate_args,
139                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
140                         if (rval == RPC_AUTHERROR)
141                                 res = YPERR_ACCESS;
142                         else
143                                 res = YPERR_RPC;
144                 }
145
146                 break;
147         case YPOP_INSERT:
148                 upargs.mapname = map;
149                 upargs.key.yp_buf_len = keylen;
150                 upargs.key.yp_buf_val = key;
151                 upargs.datum.yp_buf_len = datalen;
152                 upargs.datum.yp_buf_val = data;
153
154                 if ((rval = clnt_call(clnt, YPU_INSERT, xdr_ypupdate_args,
155                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
156                         if (rval == RPC_AUTHERROR)
157                                 res = YPERR_ACCESS;
158                         else
159                                 res = YPERR_RPC;
160                 }
161
162                 break;
163         case YPOP_DELETE:
164                 delargs.mapname = map;
165                 delargs.key.yp_buf_len = keylen;
166                 delargs.key.yp_buf_val = key;
167
168                 if ((rval = clnt_call(clnt, YPU_DELETE, xdr_ypdelete_args,
169                         &delargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
170                         if (rval == RPC_AUTHERROR)
171                                 res = YPERR_ACCESS;
172                         else
173                                 res = YPERR_RPC;
174                 }
175
176                 break;
177         case YPOP_STORE:
178                 upargs.mapname = map;
179                 upargs.key.yp_buf_len = keylen;
180                 upargs.key.yp_buf_val = key;
181                 upargs.datum.yp_buf_len = datalen;
182                 upargs.datum.yp_buf_val = data;
183
184                 if ((rval = clnt_call(clnt, YPU_STORE, xdr_ypupdate_args,
185                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
186                         if (rval == RPC_AUTHERROR)
187                                 res = YPERR_ACCESS;
188                         else
189                                 res = YPERR_RPC;
190                 }
191
192                 break;
193         default:
194                 res = YPERR_BADARGS;
195                 break;
196         }
197
198         /* All done: tear down the connection. */
199         auth_destroy(clnt->cl_auth);
200         clnt_destroy(clnt);
201         free(master);
202
203         return(res);
204 }