add uid and gid URL param to specify alternate uid and/or gid
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
2606f9bb 4
84004dbf
RS
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*/
00748f36
RS
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
84004dbf 17
108c622a
RS
18#ifdef AROS
19#include "aros_compat.h"
20#endif
21
a8a1b858
M
22#ifdef WIN32
23#include "win32_compat.h"
bff8fe46
RS
24#endif
25
a8a1b858 26#define _GNU_SOURCE
eecdc4f3 27
00748f36
RS
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
bff8fe46
RS
32#ifdef HAVE_STRINGS_H
33#include <strings.h>
34#endif
35
84004dbf
RS
36#include <stdio.h>
37#include <stdarg.h>
84004dbf 38#include <string.h>
98f5fee8 39#include <stdlib.h>
f3a75078 40#include <assert.h>
a2756193 41#include <time.h>
84004dbf 42#include "slist.h"
763cd6e3 43#include "libnfs-zdr.h"
84004dbf
RS
44#include "libnfs.h"
45#include "libnfs-raw.h"
46#include "libnfs-private.h"
47
48struct rpc_context *rpc_init_context(void)
49{
50 struct rpc_context *rpc;
a2756193 51 static uint32_t salt = 0;
84004dbf
RS
52
53 rpc = malloc(sizeof(struct rpc_context));
54 if (rpc == NULL) {
84004dbf
RS
55 return NULL;
56 }
ea98629a 57 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 58
f3a75078 59 rpc->magic = RPC_CONTEXT_MAGIC;
84004dbf
RS
60 rpc->encodebuflen = 65536;
61 rpc->encodebuf = malloc(rpc->encodebuflen);
62 if (rpc->encodebuf == NULL) {
84004dbf
RS
63 free(rpc);
64 return NULL;
65 }
66
eecdc4f3 67 rpc->auth = authunix_create_default();
84004dbf 68 if (rpc->auth == NULL) {
84004dbf
RS
69 free(rpc->encodebuf);
70 free(rpc);
71 return NULL;
72 }
6fda9871 73 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 74 salt += 0x01000000;
84004dbf 75 rpc->fd = -1;
1c8b4547 76 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
9126c9c0
PL
77#ifdef WIN32
78 rpc->uid = 65534;
79 rpc->gid = 65534;
80#else
81 rpc->uid = getuid();
82 rpc->gid = getgid();
83#endif
84004dbf
RS
84
85 return rpc;
86}
87
88
9e00b8c6
RS
89struct 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
67ba2239 101void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 102{
f3a75078
RS
103 assert(rpc->magic == RPC_CONTEXT_MAGIC);
104
84004dbf
RS
105 if (rpc->auth != NULL) {
106 auth_destroy(rpc->auth);
107 }
8704724f 108 rpc->auth = auth;
84004dbf
RS
109}
110
9126c9c0
PL
111static 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
122void rpc_set_uid(struct rpc_context *rpc, int uid) {
123 rpc_set_uid_gid(rpc, uid, rpc->gid);
124}
125
126void rpc_set_gid(struct rpc_context *rpc, int gid) {
127 rpc_set_uid_gid(rpc, rpc->uid, gid);
128}
84004dbf
RS
129
130void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
131{
132 va_list ap;
84004dbf 133
f3a75078
RS
134 assert(rpc->magic == RPC_CONTEXT_MAGIC);
135
84004dbf
RS
136 if (rpc->error_string != NULL) {
137 free(rpc->error_string);
138 }
139 va_start(ap, error_string);
b85c7de2
RS
140 rpc->error_string = malloc(1024);
141 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
142 va_end(ap);
143}
144
145char *rpc_get_error(struct rpc_context *rpc)
146{
f3a75078
RS
147 assert(rpc->magic == RPC_CONTEXT_MAGIC);
148
84004dbf
RS
149 return rpc->error_string;
150}
151
152void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
153{
154 struct rpc_pdu *pdu;
155
f3a75078
RS
156 assert(rpc->magic == RPC_CONTEXT_MAGIC);
157
84004dbf
RS
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
d678b73e
RS
170static void rpc_free_fragment(struct rpc_fragment *fragment)
171{
172 if (fragment->data != NULL) {
173 free(fragment->data);
174 }
175 free(fragment);
176}
177
178void rpc_free_all_fragments(struct rpc_context *rpc)
179{
f3a75078
RS
180 assert(rpc->magic == RPC_CONTEXT_MAGIC);
181
d678b73e
RS
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
183451cf 190int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
191{
192 struct rpc_fragment *fragment;
193
f3a75078
RS
194 assert(rpc->magic == RPC_CONTEXT_MAGIC);
195
d678b73e
RS
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}
84004dbf
RS
212
213void rpc_destroy_context(struct rpc_context *rpc)
214{
215 struct rpc_pdu *pdu;
216
f3a75078
RS
217 assert(rpc->magic == RPC_CONTEXT_MAGIC);
218
84004dbf
RS
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
d678b73e
RS
230 rpc_free_all_fragments(rpc);
231
84004dbf
RS
232 auth_destroy(rpc->auth);
233 rpc->auth =NULL;
234
235 if (rpc->fd != -1) {
eecdc4f3 236 close(rpc->fd);
84004dbf
RS
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
7ff2f3a0
RS
249 if (rpc->udp_dest != NULL) {
250 free(rpc->udp_dest);
251 rpc->udp_dest = NULL;
252 }
253
f3a75078 254 rpc->magic = 0;
84004dbf
RS
255 free(rpc);
256}
257
258