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