xref: /titanic_44/usr/src/lib/libnsl/rpc/auth_none.c (revision 62a24de03df1f2399ceda704cb3874dabc98bbbd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * auth_none.c
39  * Creates a client authentication handle for passing "null"
40  * credentials and verifiers to remote systems.
41  */
42 
43 #include "mt.h"
44 #include "rpc_mt.h"
45 #include <stdlib.h>
46 #include <rpc/types.h>
47 #include <rpc/xdr.h>
48 #include <rpc/auth.h>
49 #define	MAX_MARSHEL_SIZE 20
50 
51 
52 extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
53 
54 static struct auth_ops *authnone_ops(void);
55 
56 static struct authnone_private {
57 	AUTH	no_client;
58 	char	marshalled_client[MAX_MARSHEL_SIZE];
59 	uint_t	mcnt;
60 } *authnone_private;
61 
62 
63 AUTH *
64 authnone_create(void)
65 {
66 	struct authnone_private *ap;
67 	XDR xdr_stream;
68 	XDR *xdrs;
69 	extern mutex_t authnone_lock;
70 
71 	/* VARIABLES PROTECTED BY authnone_lock: ap */
72 
73 	(void) mutex_lock(&authnone_lock);
74 	ap = authnone_private;
75 	if (ap == NULL) {
76 		ap = calloc(1, sizeof (*ap));
77 		if (ap == NULL) {
78 			(void) mutex_unlock(&authnone_lock);
79 			return (NULL);
80 		}
81 		authnone_private = ap;
82 	}
83 	if (!ap->mcnt) {
84 		ap->no_client.ah_cred = ap->no_client.ah_verf = _null_auth;
85 		ap->no_client.ah_ops = authnone_ops();
86 		xdrs = &xdr_stream;
87 		xdrmem_create(xdrs, ap->marshalled_client,
88 			(uint_t)MAX_MARSHEL_SIZE, XDR_ENCODE);
89 		(void) xdr_opaque_auth(xdrs, &ap->no_client.ah_cred);
90 		(void) xdr_opaque_auth(xdrs, &ap->no_client.ah_verf);
91 		ap->mcnt = XDR_GETPOS(xdrs);
92 		XDR_DESTROY(xdrs);
93 	}
94 	(void) mutex_unlock(&authnone_lock);
95 	return (&ap->no_client);
96 }
97 
98 /*ARGSUSED*/
99 static bool_t
100 authnone_marshal(AUTH *client, XDR *xdrs)
101 {
102 	struct authnone_private *ap;
103 	bool_t res;
104 	extern mutex_t authnone_lock;
105 
106 	(void) mutex_lock(&authnone_lock);
107 	ap = authnone_private;
108 	if (ap == NULL) {
109 		(void) mutex_unlock(&authnone_lock);
110 		return (FALSE);
111 	}
112 	res = (*xdrs->x_ops->x_putbytes)(xdrs,
113 			ap->marshalled_client, ap->mcnt);
114 	(void) mutex_unlock(&authnone_lock);
115 	return (res);
116 }
117 
118 /* All these unused parameters are required to keep ANSI-C from grumbling */
119 /*ARGSUSED*/
120 static void
121 authnone_verf(AUTH *client)
122 {
123 }
124 
125 /*ARGSUSED*/
126 static bool_t
127 authnone_validate(AUTH *client, struct opaque_auth *opaque)
128 {
129 	return (TRUE);
130 }
131 
132 /*ARGSUSED*/
133 static bool_t
134 authnone_refresh(AUTH *client, void *dummy)
135 {
136 	return (FALSE);
137 }
138 
139 /*ARGSUSED*/
140 static void
141 authnone_destroy(AUTH *client)
142 {
143 }
144 
145 static struct auth_ops *
146 authnone_ops(void)
147 {
148 	static struct auth_ops ops;
149 	extern mutex_t ops_lock;
150 
151 /* VARIABLES PROTECTED BY ops_lock: ops */
152 
153 	(void) mutex_lock(&ops_lock);
154 	if (ops.ah_nextverf == NULL) {
155 		ops.ah_nextverf = authnone_verf;
156 		ops.ah_marshal = authnone_marshal;
157 		ops.ah_validate = authnone_validate;
158 		ops.ah_refresh = authnone_refresh;
159 		ops.ah_destroy = authnone_destroy;
160 	}
161 	(void) mutex_unlock(&ops_lock);
162 	return (&ops);
163 }
164