dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fs.d / nfs / lib / ref_subr.c
blobabfea2b6fde123cee2f860c6f3d1fe0e021948f4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <strings.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/errno.h>
34 #include <limits.h>
35 #include <libnvpair.h>
36 #include <dlfcn.h>
37 #include <link.h>
38 #include <rp_plugin.h>
39 #include <fcntl.h>
40 #include <uuid/uuid.h>
41 #include <rpc/types.h>
42 #include <rpc/xdr.h>
43 #include <rpc/auth.h>
44 #include <rpc/clnt.h>
45 #include <rpc/rpc_msg.h>
46 #include <sys/param.h>
47 #include <nfs/nfs4.h>
48 #include <rpcsvc/nfs4_prot.h>
51 * str_to_utf8 - converts a null-terminated C string to a utf8 string
53 utf8string *
54 str_to_utf8(char *nm, utf8string *str)
56 int len;
58 if (str == NULL)
59 return (NULL);
61 if (nm == NULL || *nm == '\0') {
62 str->utf8string_len = 0;
63 str->utf8string_val = NULL;
64 return (NULL);
67 len = strlen(nm);
69 str->utf8string_val = malloc(len);
70 if (str->utf8string_val == NULL) {
71 str->utf8string_len = 0;
72 return (NULL);
74 str->utf8string_len = len;
75 bcopy(nm, str->utf8string_val, len);
77 return (str);
81 * Converts a utf8 string to a C string.
82 * kmem_allocs a new string if not supplied
84 char *
85 utf8_to_str(utf8string *str, uint_t *lenp, char *s)
87 char *sp;
88 char *u8p;
89 int len;
90 int i;
92 if (str == NULL)
93 return (NULL);
95 u8p = str->utf8string_val;
96 len = str->utf8string_len;
97 if (len <= 0 || u8p == NULL) {
98 if (s)
99 *s = '\0';
100 return (NULL);
103 sp = s;
104 if (sp == NULL)
105 sp = malloc(len + 1);
106 if (sp == NULL)
107 return (NULL);
110 * At least check for embedded nulls
112 for (i = 0; i < len; i++) {
113 sp[i] = u8p[i];
114 if (u8p[i] == '\0') {
115 if (s == NULL)
116 free(sp);
117 return (NULL);
120 sp[len] = '\0';
121 *lenp = len + 1;
123 return (sp);
126 void
127 print_referral_summary(fs_locations4 *fsl)
129 int i, j;
130 uint_t l;
131 char *s;
132 fs_location4 *fs;
134 if (fsl == NULL) {
135 printf("NULL\n");
136 return;
139 for (i = 0; i < fsl->locations.locations_len; i++) {
140 if (i > 0)
141 printf("\n");
142 fs = &fsl->locations.locations_val[i];
143 for (j = 0; j < fs->server.server_len; j++) {
144 s = utf8_to_str(&fs->server.server_val[j], &l, NULL);
145 if (j > 0)
146 printf(",");
147 printf("%s", s ? s : "");
148 free(s);
150 printf(":");
151 for (j = 0; j < fs->rootpath.pathname4_len; j++) {
152 s = utf8_to_str(&fs->rootpath.pathname4_val[j],
153 &l, NULL);
154 printf("/%s", s ? s : "");
155 free(s);
157 if (fs->rootpath.pathname4_len == 0)
158 printf("/");
160 printf("\n");
164 * There is a kernel copy of this routine in nfs4_srv.c.
165 * Changes should be kept in sync.
167 static int
168 nfs4_create_components(char *path, component4 *comp4)
170 int slen, plen, ncomp;
171 char *ori_path, *nxtc, buf[MAXNAMELEN];
173 if (path == NULL)
174 return (0);
176 plen = strlen(path) + 1; /* include the terminator */
177 ori_path = path;
178 ncomp = 0;
180 /* count number of components in the path */
181 for (nxtc = path; nxtc < ori_path + plen; nxtc++) {
182 if (*nxtc == '/' || *nxtc == '\0' || *nxtc == '\n') {
183 if ((slen = nxtc - path) == 0) {
184 path = nxtc + 1;
185 continue;
188 if (comp4 != NULL) {
189 bcopy(path, buf, slen);
190 buf[slen] = '\0';
191 if (str_to_utf8(buf, &comp4[ncomp]) == NULL)
192 return (0);
195 ncomp++; /* 1 valid component */
196 path = nxtc + 1;
198 if (*nxtc == '\0' || *nxtc == '\n')
199 break;
202 return (ncomp);
206 * There is a kernel copy of this routine in nfs4_srv.c.
207 * Changes should be kept in sync.
210 make_pathname4(char *path, pathname4 *pathname)
212 int ncomp;
213 component4 *comp4;
215 if (pathname == NULL)
216 return (0);
218 if (path == NULL) {
219 pathname->pathname4_val = NULL;
220 pathname->pathname4_len = 0;
221 return (0);
224 /* count number of components to alloc buffer */
225 if ((ncomp = nfs4_create_components(path, NULL)) == 0) {
226 pathname->pathname4_val = NULL;
227 pathname->pathname4_len = 0;
228 return (0);
230 comp4 = calloc(ncomp * sizeof (component4), 1);
231 if (comp4 == NULL) {
232 pathname->pathname4_val = NULL;
233 pathname->pathname4_len = 0;
234 return (0);
237 /* copy components into allocated buffer */
238 ncomp = nfs4_create_components(path, comp4);
240 pathname->pathname4_val = comp4;
241 pathname->pathname4_len = ncomp;
243 return (ncomp);
246 bool_t
247 xdr_component4(register XDR *xdrs, component4 *objp)
250 if (!xdr_utf8string(xdrs, objp))
251 return (FALSE);
252 return (TRUE);
255 bool_t
256 xdr_utf8string(register XDR *xdrs, utf8string *objp)
259 if (xdrs->x_op != XDR_FREE)
260 return (xdr_bytes(xdrs, (char **)&objp->utf8string_val,
261 (uint_t *)&objp->utf8string_len, NFS4_MAX_UTF8STRING));
262 return (TRUE);
265 bool_t
266 xdr_pathname4(register XDR *xdrs, pathname4 *objp)
269 if (!xdr_array(xdrs, (char **)&objp->pathname4_val,
270 (uint_t *)&objp->pathname4_len, NFS4_MAX_PATHNAME4,
271 sizeof (component4), (xdrproc_t)xdr_component4))
272 return (FALSE);
273 return (TRUE);
276 bool_t
277 xdr_fs_location4(register XDR *xdrs, fs_location4 *objp)
280 if (xdrs->x_op == XDR_DECODE) {
281 objp->server.server_val = NULL;
282 objp->rootpath.pathname4_val = NULL;
284 if (!xdr_array(xdrs, (char **)&objp->server.server_val,
285 (uint_t *)&objp->server.server_len, ~0,
286 sizeof (utf8string), (xdrproc_t)xdr_utf8string))
287 return (FALSE);
288 if (!xdr_pathname4(xdrs, &objp->rootpath))
289 return (FALSE);
290 return (TRUE);
293 bool_t
294 xdr_fs_locations4(register XDR *xdrs, fs_locations4 *objp)
297 if (xdrs->x_op == XDR_DECODE) {
298 objp->fs_root.pathname4_len = 0;
299 objp->fs_root.pathname4_val = NULL;
300 objp->locations.locations_val = NULL;
302 if (!xdr_pathname4(xdrs, &objp->fs_root))
303 return (FALSE);
304 if (!xdr_array(xdrs, (char **)&objp->locations.locations_val,
305 (uint_t *)&objp->locations.locations_len, ~0,
306 sizeof (fs_location4), (xdrproc_t)xdr_fs_location4))
307 return (FALSE);
308 return (TRUE);