]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/librpcsvc/yp_update.c
This commit was generated by cvs2svn to compensate for changes in r104912,
[FreeBSD/FreeBSD.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
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include <stdlib.h>
43 #include <rpc/rpc.h>
44 #include <rpcsvc/yp_prot.h>
45 #include <rpcsvc/ypclnt.h>
46 #include <rpcsvc/ypupdate_prot.h>
47 #include <rpc/key_prot.h>
48
49 #ifndef WINDOW
50 #define WINDOW (60*60)
51 #endif
52
53 #ifndef TIMEOUT
54 #define TIMEOUT 300
55 #endif
56
57 int
58 yp_update(char *domain, char *map, unsigned int ypop, char *key, int keylen,
59     char *data, int datalen)
60 {
61         char *master;
62         int rval;
63         unsigned int res;
64         struct ypupdate_args upargs;
65         struct ypdelete_args delargs;
66         CLIENT *clnt;
67         char netname[MAXNETNAMELEN+1];
68         des_block des_key;
69         struct timeval timeout;
70
71         /* Get the master server name for 'domain.' */
72         if ((rval = yp_master(domain, map, &master)))
73                 return(rval);
74
75         /* Check that ypupdated is running there. */
76         if (getrpcport(master, YPU_PROG, YPU_VERS, ypop))
77                 return(YPERR_DOMAIN);
78
79         /* Get a handle. */
80         if ((clnt = clnt_create(master, YPU_PROG, YPU_VERS, "tcp")) == NULL)
81                 return(YPERR_RPC);
82
83         /*
84          * Assemble netname of server.
85          * NOTE: It's difficult to discern from the documentation, but
86          * when you make a Secure RPC call, the netname you pass should
87          * be the netname of the guy on the other side, not your own
88          * netname. This is how the client side knows what public key
89          * to use for the initial exchange. Passing your own netname
90          * only works if the server on the other side is running under
91          * your UID.
92          */
93         if (!host2netname(netname, master, domain)) {
94                 clnt_destroy(clnt);
95                 return(YPERR_BADARGS);
96         }
97
98         /* Make up a DES session key. */
99         key_gendes(&des_key);
100
101         /* Set up DES authentication. */
102         if ((clnt->cl_auth = (AUTH *)authdes_create(netname, WINDOW, NULL,
103                         &des_key)) == NULL) {
104                 clnt_destroy(clnt);
105                 return(YPERR_RESRC);
106         }
107
108         /* Set a timeout for clnt_call(). */
109         timeout.tv_usec = 0;
110         timeout.tv_sec = TIMEOUT;
111
112         /*
113          * Make the call. Note that we use clnt_call() here rather than
114          * the rpcgen-erated client stubs. We could use those stubs, but
115          * then we'd have to do some gymnastics to get at the error
116          * information to figure out what error code to send back to the
117          * caller. With clnt_call(), we get the error status returned to
118          * us right away, and we only have to exert a small amount of
119          * extra effort.
120          */
121         switch (ypop) {
122         case YPOP_CHANGE:
123                 upargs.mapname = map;
124                 upargs.key.yp_buf_len = keylen;
125                 upargs.key.yp_buf_val = key;
126                 upargs.datum.yp_buf_len = datalen;
127                 upargs.datum.yp_buf_val = data;
128
129                 if ((rval = clnt_call(clnt, YPU_CHANGE, xdr_ypupdate_args,
130                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
131                         if (rval == RPC_AUTHERROR)
132                                 res = YPERR_ACCESS;
133                         else
134                                 res = YPERR_RPC;
135                 }
136
137                 break;
138         case YPOP_INSERT:
139                 upargs.mapname = map;
140                 upargs.key.yp_buf_len = keylen;
141                 upargs.key.yp_buf_val = key;
142                 upargs.datum.yp_buf_len = datalen;
143                 upargs.datum.yp_buf_val = data;
144
145                 if ((rval = clnt_call(clnt, YPU_INSERT, xdr_ypupdate_args,
146                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
147                         if (rval == RPC_AUTHERROR)
148                                 res = YPERR_ACCESS;
149                         else
150                                 res = YPERR_RPC;
151                 }
152
153                 break;
154         case YPOP_DELETE:
155                 delargs.mapname = map;
156                 delargs.key.yp_buf_len = keylen;
157                 delargs.key.yp_buf_val = key;
158
159                 if ((rval = clnt_call(clnt, YPU_DELETE, xdr_ypdelete_args,
160                         &delargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
161                         if (rval == RPC_AUTHERROR)
162                                 res = YPERR_ACCESS;
163                         else
164                                 res = YPERR_RPC;
165                 }
166
167                 break;
168         case YPOP_STORE:
169                 upargs.mapname = map;
170                 upargs.key.yp_buf_len = keylen;
171                 upargs.key.yp_buf_val = key;
172                 upargs.datum.yp_buf_len = datalen;
173                 upargs.datum.yp_buf_val = data;
174
175                 if ((rval = clnt_call(clnt, YPU_STORE, xdr_ypupdate_args,
176                         &upargs, xdr_u_int, &res, timeout)) != RPC_SUCCESS) {
177                         if (rval == RPC_AUTHERROR)
178                                 res = YPERR_ACCESS;
179                         else
180                                 res = YPERR_RPC;
181                 }
182
183                 break;
184         default:
185                 res = YPERR_BADARGS;
186                 break;
187         }
188
189         /* All done: tear down the connection. */
190         auth_destroy(clnt->cl_auth);
191         clnt_destroy(clnt);
192         free(master);
193
194         return(res);
195 }