add uid and gid URL param to specify alternate uid and/or gid
[deb_libnfs.git] / lib / init.c
1 /*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU Lesser General Public License for more details.
10
11 You should have received a copy of the GNU Lesser General Public License
12 along with this program; if not, see <http://www.gnu.org/licenses/>.
13 */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #ifdef AROS
19 #include "aros_compat.h"
20 #endif
21
22 #ifdef WIN32
23 #include "win32_compat.h"
24 #endif
25
26 #define _GNU_SOURCE
27
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <assert.h>
41 #include <time.h>
42 #include "slist.h"
43 #include "libnfs-zdr.h"
44 #include "libnfs.h"
45 #include "libnfs-raw.h"
46 #include "libnfs-private.h"
47
48 struct rpc_context *rpc_init_context(void)
49 {
50 struct rpc_context *rpc;
51 static uint32_t salt = 0;
52
53 rpc = malloc(sizeof(struct rpc_context));
54 if (rpc == NULL) {
55 return NULL;
56 }
57 memset(rpc, 0, sizeof(struct rpc_context));
58
59 rpc->magic = RPC_CONTEXT_MAGIC;
60 rpc->encodebuflen = 65536;
61 rpc->encodebuf = malloc(rpc->encodebuflen);
62 if (rpc->encodebuf == NULL) {
63 free(rpc);
64 return NULL;
65 }
66
67 rpc->auth = authunix_create_default();
68 if (rpc->auth == NULL) {
69 free(rpc->encodebuf);
70 free(rpc);
71 return NULL;
72 }
73 rpc->xid = salt + time(NULL) + getpid() << 16;
74 salt += 0x01000000;
75 rpc->fd = -1;
76 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
77 #ifdef WIN32
78 rpc->uid = 65534;
79 rpc->gid = 65534;
80 #else
81 rpc->uid = getuid();
82 rpc->gid = getgid();
83 #endif
84
85 return rpc;
86 }
87
88
89 struct rpc_context *rpc_init_udp_context(void)
90 {
91 struct rpc_context *rpc;
92
93 rpc = rpc_init_context();
94 if (rpc != NULL) {
95 rpc->is_udp = 1;
96 }
97
98 return rpc;
99 }
100
101 void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
102 {
103 assert(rpc->magic == RPC_CONTEXT_MAGIC);
104
105 if (rpc->auth != NULL) {
106 auth_destroy(rpc->auth);
107 }
108 rpc->auth = auth;
109 }
110
111 static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
112 if (uid != rpc->uid || gid != rpc->gid) {
113 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
114 if (auth != NULL) {
115 rpc_set_auth(rpc, auth);
116 rpc->uid = uid;
117 rpc->gid = gid;
118 }
119 }
120 }
121
122 void rpc_set_uid(struct rpc_context *rpc, int uid) {
123 rpc_set_uid_gid(rpc, uid, rpc->gid);
124 }
125
126 void rpc_set_gid(struct rpc_context *rpc, int gid) {
127 rpc_set_uid_gid(rpc, rpc->uid, gid);
128 }
129
130 void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
131 {
132 va_list ap;
133
134 assert(rpc->magic == RPC_CONTEXT_MAGIC);
135
136 if (rpc->error_string != NULL) {
137 free(rpc->error_string);
138 }
139 va_start(ap, error_string);
140 rpc->error_string = malloc(1024);
141 vsnprintf(rpc->error_string, 1024, error_string, ap);
142 va_end(ap);
143 }
144
145 char *rpc_get_error(struct rpc_context *rpc)
146 {
147 assert(rpc->magic == RPC_CONTEXT_MAGIC);
148
149 return rpc->error_string;
150 }
151
152 void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
153 {
154 struct rpc_pdu *pdu;
155
156 assert(rpc->magic == RPC_CONTEXT_MAGIC);
157
158 while((pdu = rpc->outqueue) != NULL) {
159 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
160 SLIST_REMOVE(&rpc->outqueue, pdu);
161 rpc_free_pdu(rpc, pdu);
162 }
163 while((pdu = rpc->waitpdu) != NULL) {
164 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
165 SLIST_REMOVE(&rpc->waitpdu, pdu);
166 rpc_free_pdu(rpc, pdu);
167 }
168 }
169
170 static void rpc_free_fragment(struct rpc_fragment *fragment)
171 {
172 if (fragment->data != NULL) {
173 free(fragment->data);
174 }
175 free(fragment);
176 }
177
178 void rpc_free_all_fragments(struct rpc_context *rpc)
179 {
180 assert(rpc->magic == RPC_CONTEXT_MAGIC);
181
182 while (rpc->fragments != NULL) {
183 struct rpc_fragment *fragment = rpc->fragments;
184
185 SLIST_REMOVE(&rpc->fragments, fragment);
186 rpc_free_fragment(fragment);
187 }
188 }
189
190 int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
191 {
192 struct rpc_fragment *fragment;
193
194 assert(rpc->magic == RPC_CONTEXT_MAGIC);
195
196 fragment = malloc(sizeof(struct rpc_fragment));
197 if (fragment == NULL) {
198 return -1;
199 }
200
201 fragment->size = size;
202 fragment->data = malloc(fragment->size);
203 if(fragment->data == NULL) {
204 free(fragment);
205 return -1;
206 }
207
208 memcpy(fragment->data, data, fragment->size);
209 SLIST_ADD_END(&rpc->fragments, fragment);
210 return 0;
211 }
212
213 void rpc_destroy_context(struct rpc_context *rpc)
214 {
215 struct rpc_pdu *pdu;
216
217 assert(rpc->magic == RPC_CONTEXT_MAGIC);
218
219 while((pdu = rpc->outqueue) != NULL) {
220 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
221 SLIST_REMOVE(&rpc->outqueue, pdu);
222 rpc_free_pdu(rpc, pdu);
223 }
224 while((pdu = rpc->waitpdu) != NULL) {
225 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
226 SLIST_REMOVE(&rpc->waitpdu, pdu);
227 rpc_free_pdu(rpc, pdu);
228 }
229
230 rpc_free_all_fragments(rpc);
231
232 auth_destroy(rpc->auth);
233 rpc->auth =NULL;
234
235 if (rpc->fd != -1) {
236 close(rpc->fd);
237 }
238
239 if (rpc->encodebuf != NULL) {
240 free(rpc->encodebuf);
241 rpc->encodebuf = NULL;
242 }
243
244 if (rpc->error_string != NULL) {
245 free(rpc->error_string);
246 rpc->error_string = NULL;
247 }
248
249 if (rpc->udp_dest != NULL) {
250 free(rpc->udp_dest);
251 rpc->udp_dest = NULL;
252 }
253
254 rpc->magic = 0;
255 free(rpc);
256 }
257
258