xref: /illumos-gate/usr/src/lib/libnsl/rpc/auth_none.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
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 /*
36  * auth_none.c
37  * Creates a client authentication handle for passing "null"
38  * credentials and verifiers to remote systems.
39  */
40 
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <stdlib.h>
44 #include <rpc/types.h>
45 #include <rpc/xdr.h>
46 #include <rpc/auth.h>
47 #define	MAX_MARSHEL_SIZE 20
48 
49 
50 extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
51 
52 static struct auth_ops *authnone_ops(void);
53 
54 static struct authnone_private {
55 	AUTH	no_client;
56 	char	marshalled_client[MAX_MARSHEL_SIZE];
57 	uint_t	mcnt;
58 } *authnone_private;
59 
60 
61 AUTH *
62 authnone_create(void)
63 {
64 	struct authnone_private *ap;
65 	XDR xdr_stream;
66 	XDR *xdrs;
67 	extern mutex_t authnone_lock;
68 
69 	/* VARIABLES PROTECTED BY authnone_lock: ap */
70 
71 	(void) mutex_lock(&authnone_lock);
72 	ap = authnone_private;
73 	if (ap == NULL) {
74 		ap = calloc(1, sizeof (*ap));
75 		if (ap == NULL) {
76 			(void) mutex_unlock(&authnone_lock);
77 			return (NULL);
78 		}
79 		authnone_private = ap;
80 	}
81 	if (!ap->mcnt) {
82 		ap->no_client.ah_cred = ap->no_client.ah_verf = _null_auth;
83 		ap->no_client.ah_ops = authnone_ops();
84 		xdrs = &xdr_stream;
85 		xdrmem_create(xdrs, ap->marshalled_client,
86 			(uint_t)MAX_MARSHEL_SIZE, XDR_ENCODE);
87 		(void) xdr_opaque_auth(xdrs, &ap->no_client.ah_cred);
88 		(void) xdr_opaque_auth(xdrs, &ap->no_client.ah_verf);
89 		ap->mcnt = XDR_GETPOS(xdrs);
90 		XDR_DESTROY(xdrs);
91 	}
92 	(void) mutex_unlock(&authnone_lock);
93 	return (&ap->no_client);
94 }
95 
96 /*ARGSUSED*/
97 static bool_t
98 authnone_marshal(AUTH *client, XDR *xdrs)
99 {
100 	struct authnone_private *ap;
101 	bool_t res;
102 	extern mutex_t authnone_lock;
103 
104 	(void) mutex_lock(&authnone_lock);
105 	ap = authnone_private;
106 	if (ap == NULL) {
107 		(void) mutex_unlock(&authnone_lock);
108 		return (FALSE);
109 	}
110 	res = (*xdrs->x_ops->x_putbytes)(xdrs,
111 			ap->marshalled_client, ap->mcnt);
112 	(void) mutex_unlock(&authnone_lock);
113 	return (res);
114 }
115 
116 /* All these unused parameters are required to keep ANSI-C from grumbling */
117 /*ARGSUSED*/
118 static void
119 authnone_verf(AUTH *client)
120 {
121 }
122 
123 /*ARGSUSED*/
124 static bool_t
125 authnone_validate(AUTH *client, struct opaque_auth *opaque)
126 {
127 	return (TRUE);
128 }
129 
130 /*ARGSUSED*/
131 static bool_t
132 authnone_refresh(AUTH *client, void *dummy)
133 {
134 	return (FALSE);
135 }
136 
137 /*ARGSUSED*/
138 static void
139 authnone_destroy(AUTH *client)
140 {
141 }
142 
143 static struct auth_ops *
144 authnone_ops(void)
145 {
146 	static struct auth_ops ops;
147 	extern mutex_t ops_lock;
148 
149 /* VARIABLES PROTECTED BY ops_lock: ops */
150 
151 	(void) mutex_lock(&ops_lock);
152 	if (ops.ah_nextverf == NULL) {
153 		ops.ah_nextverf = authnone_verf;
154 		ops.ah_marshal = authnone_marshal;
155 		ops.ah_validate = authnone_validate;
156 		ops.ah_refresh = authnone_refresh;
157 		ops.ah_destroy = authnone_destroy;
158 	}
159 	(void) mutex_unlock(&ops_lock);
160 	return (&ops);
161 }
162