xref: /freebsd/lib/libc/rpc/auth_none.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: auth_none.c,v 1.13 2000/01/22 22:19:17 mycroft Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND 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 THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)auth_none.c	2.1 88/07/29 4.0 RPCSRC";
36 #endif
37 /*
38  * auth_none.c
39  * Creates a client authentication handle for passing "null"
40  * credentials and verifiers to remote systems.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  */
44 
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <assert.h>
48 #include <stdlib.h>
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 #include <rpc/auth.h>
52 #include "un-namespace.h"
53 #include "mt_misc.h"
54 
55 #define MAX_MARSHAL_SIZE 20
56 
57 /*
58  * Authenticator operations routines
59  */
60 
61 static bool_t authnone_marshal (AUTH *, XDR *);
62 static void authnone_verf (AUTH *);
63 static bool_t authnone_validate (AUTH *, struct opaque_auth *);
64 static bool_t authnone_refresh (AUTH *, void *);
65 static void authnone_destroy (AUTH *);
66 
67 extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
68 
69 static struct auth_ops *authnone_ops(void);
70 
71 static struct authnone_private {
72 	AUTH	no_client;
73 	char	marshalled_client[MAX_MARSHAL_SIZE];
74 	u_int	mcnt;
75 } *authnone_private;
76 
77 AUTH *
78 authnone_create(void)
79 {
80 	struct authnone_private *ap = authnone_private;
81 	XDR xdr_stream;
82 	XDR *xdrs;
83 
84 	mutex_lock(&authnone_lock);
85 	if (ap == NULL) {
86 		ap = calloc(1, sizeof (*ap));
87 		if (ap == NULL) {
88 			mutex_unlock(&authnone_lock);
89 			return (0);
90 		}
91 		authnone_private = ap;
92 	}
93 	if (!ap->mcnt) {
94 		ap->no_client.ah_cred = ap->no_client.ah_verf = _null_auth;
95 		ap->no_client.ah_ops = authnone_ops();
96 		xdrs = &xdr_stream;
97 		xdrmem_create(xdrs, ap->marshalled_client,
98 		    (u_int)MAX_MARSHAL_SIZE, XDR_ENCODE);
99 		(void)xdr_opaque_auth(xdrs, &ap->no_client.ah_cred);
100 		(void)xdr_opaque_auth(xdrs, &ap->no_client.ah_verf);
101 		ap->mcnt = XDR_GETPOS(xdrs);
102 		XDR_DESTROY(xdrs);
103 	}
104 	mutex_unlock(&authnone_lock);
105 	return (&ap->no_client);
106 }
107 
108 /*ARGSUSED*/
109 static bool_t
110 authnone_marshal(AUTH *client, XDR *xdrs)
111 {
112 	struct authnone_private *ap;
113 	bool_t dummy;
114 
115 	assert(xdrs != NULL);
116 
117 	ap = authnone_private;
118 	if (ap == NULL) {
119 		mutex_unlock(&authnone_lock);
120 		return (FALSE);
121 	}
122 	dummy = (*xdrs->x_ops->x_putbytes)(xdrs,
123 	    ap->marshalled_client, ap->mcnt);
124 	mutex_unlock(&authnone_lock);
125 	return (dummy);
126 }
127 
128 /* All these unused parameters are required to keep ANSI-C from grumbling */
129 /*ARGSUSED*/
130 static void
131 authnone_verf(AUTH *client)
132 {
133 }
134 
135 /*ARGSUSED*/
136 static bool_t
137 authnone_validate(AUTH *client, struct opaque_auth *opaque)
138 {
139 
140 	return (TRUE);
141 }
142 
143 /*ARGSUSED*/
144 static bool_t
145 authnone_refresh(AUTH *client, void *dummy)
146 {
147 
148 	return (FALSE);
149 }
150 
151 /*ARGSUSED*/
152 static void
153 authnone_destroy(AUTH *client)
154 {
155 }
156 
157 static struct auth_ops *
158 authnone_ops(void)
159 {
160 	static struct auth_ops ops;
161 
162 /* VARIABLES PROTECTED BY ops_lock: ops */
163 
164 	mutex_lock(&ops_lock);
165 	if (ops.ah_nextverf == NULL) {
166 		ops.ah_nextverf = authnone_verf;
167 		ops.ah_marshal = authnone_marshal;
168 		ops.ah_validate = authnone_validate;
169 		ops.ah_refresh = authnone_refresh;
170 		ops.ah_destroy = authnone_destroy;
171 	}
172 	mutex_unlock(&ops_lock);
173 	return (&ops);
174 }
175