Imported Upstream version 0.2.5
[platform/upstream/libtirpc.git] / src / 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
29 /*
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char *sccsid = "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro";
32 static char *sccsid = "@(#)auth_none.c  2.1 88/07/29 4.0 RPCSRC";
33 #endif
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: src/lib/libc/rpc/auth_none.c,v 1.12 2002/03/22 23:18:35 obrien Exp $");
36 */
37
38
39 /*
40  * auth_none.c
41  * Creates a client authentication handle for passing "null"
42  * credentials and verifiers to remote systems.
43  *
44  * Copyright (C) 1984, Sun Microsystems, Inc.
45  */
46 #include <pthread.h>
47 #include <reentrant.h>
48 #include <assert.h>
49 #include <stdlib.h>
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52 #include <rpc/auth.h>
53
54 #define MAX_MARSHAL_SIZE 20
55
56 /*
57  * Authenticator operations routines
58  */
59
60 static bool_t authnone_marshal (AUTH *, XDR *);
61 static void authnone_verf (AUTH *);
62 static bool_t authnone_validate (AUTH *, struct opaque_auth *);
63 static bool_t authnone_refresh (AUTH *, void *);
64 static void authnone_destroy (AUTH *);
65
66 extern bool_t xdr_opaque_auth();
67
68 static struct auth_ops *authnone_ops();
69
70 static struct authnone_private {
71         AUTH    no_client;
72         char    marshalled_client[MAX_MARSHAL_SIZE];
73         u_int   mcnt;
74 } *authnone_private;
75
76 AUTH *
77 authnone_create()
78 {
79         struct authnone_private *ap = authnone_private;
80         XDR xdr_stream;
81         XDR *xdrs;
82         extern mutex_t authnone_lock;
83
84         mutex_lock(&authnone_lock);
85         if (ap == 0) {
86                 ap = (struct authnone_private *)calloc(1, sizeof (*ap));
87                 if (ap == 0) {
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 rv = FALSE;
114         extern mutex_t authnone_lock;
115
116         assert(xdrs != NULL);
117
118         mutex_lock(&authnone_lock);
119         ap = authnone_private;
120         if (ap) {
121                 rv = (*xdrs->x_ops->x_putbytes)(xdrs, ap->marshalled_client,
122                                                 ap->mcnt);
123         }
124         mutex_unlock(&authnone_lock);
125         return (rv);
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 bool_t
158 authnone_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
159 {
160         return ((*xfunc)(xdrs, xwhere));
161 }
162
163 static struct auth_ops *
164 authnone_ops()
165 {
166         static struct auth_ops ops;
167         extern mutex_t ops_lock;
168  
169 /* VARIABLES PROTECTED BY ops_lock: ops */
170  
171         mutex_lock(&ops_lock);
172         if (ops.ah_nextverf == NULL) {
173                 ops.ah_nextverf = authnone_verf;
174                 ops.ah_marshal = authnone_marshal;
175                 ops.ah_validate = authnone_validate;
176                 ops.ah_refresh = authnone_refresh;
177                 ops.ah_destroy = authnone_destroy;
178                 ops.ah_wrap = authnone_wrap;
179                 ops.ah_unwrap = authnone_wrap;
180         }
181         mutex_unlock(&ops_lock);
182         return (&ops);
183 }