ReactOS  0.4.13-dev-241-g63286c6
auth_unix.c
Go to the documentation of this file.
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 //#include <sys/cdefs.h>
30 
31 /*
32  * auth_unix.c, Implements UNIX style authentication parameters.
33  *
34  * Copyright (C) 1984, Sun Microsystems, Inc.
35  *
36  * The system is very weak. The client uses no encryption for it's
37  * credentials and only sends null verifiers. The server sends backs
38  * null verifiers or optionally a verifier that suggests a new short hand
39  * for the credentials.
40  *
41  */
42 
43 /* NFSv4.1 client for Windows
44  * Copyright 2012 The Regents of the University of Michigan
45  *
46  * Olga Kornievskaia <aglo@umich.edu>
47  * Casey Bodley <cbodley@umich.edu>
48  *
49  * This library is free software; you can redistribute it and/or modify it
50  * under the terms of the GNU Lesser General Public License as published by
51  * the Free Software Foundation; either version 2.1 of the License, or (at
52  * your option) any later version.
53  *
54  * This library is distributed in the hope that it will be useful, but
55  * without any warranty; without even the implied warranty of merchantability
56  * or fitness for a particular purpose. See the GNU Lesser General Public
57  * License for more details.
58  *
59  * You should have received a copy of the GNU Lesser General Public License
60  * along with this library; if not, write to the Free Software Foundation,
61  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
62  */
63 
64 #include <wintirpc.h>
65 //#include <pthread.h>
66 #include <reentrant.h>
67 //#include <sys/param.h>
68 
69 #include <assert.h>
70 //#include <err.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 //#include <unistd.h>
74 #include <string.h>
75 
76 #include <rpc/types.h>
77 #include <rpc/xdr.h>
78 #include <rpc/auth.h>
79 #include <rpc/auth_unix.h>
80 
81 /* auth_unix.c */
82 static void authunix_nextverf (AUTH *);
83 static bool_t authunix_marshal (AUTH *, XDR *, u_int *seq);
84 static bool_t authunix_validate (AUTH *, struct opaque_auth *, u_int);
85 static bool_t authunix_refresh (AUTH *, void *);
86 static void authunix_destroy (AUTH *);
87 static void marshal_new_auth (AUTH *);
88 static struct auth_ops *authunix_ops (void);
89 
90 /*
91  * This struct is pointed to by the ah_private field of an auth_handle.
92  */
93 struct audata {
94  struct opaque_auth au_origcred; /* original credentials */
95  struct opaque_auth au_shcred; /* short hand cred */
96  u_long au_shfaults; /* short hand cache faults */
98  u_int au_mpos; /* xdr pos at end of marshed */
99 };
100 #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private)
101 
102 /*
103  * Create a unix style authenticator.
104  * Returns an auth handle with the given stuff in it.
105  */
106 AUTH *
107 authunix_create(machname, uid, gid, len, aup_gids)
108  char *machname;
109  uid_t uid;
110  gid_t gid;
111  int len;
112  gid_t *aup_gids;
113 {
114  struct authunix_parms aup;
115  char mymem[MAX_AUTH_BYTES];
116  struct timeval now;
117  XDR xdrs;
118  AUTH *auth;
119  struct audata *au;
120 
121  /*
122  * Allocate and set up auth handle
123  */
124  au = NULL;
125  auth = mem_alloc(sizeof(*auth));
126 #ifndef _KERNEL
127  if (auth == NULL) {
128  // XXX warnx("authunix_create: out of memory");
129  goto cleanup_authunix_create;
130  }
131 #endif
132  au = mem_alloc(sizeof(*au));
133 #ifndef _KERNEL
134  if (au == NULL) {
135  // XXX warnx("authunix_create: out of memory");
136  goto cleanup_authunix_create;
137  }
138 #endif
139  auth->ah_ops = authunix_ops();
140  auth->ah_private = (caddr_t)au;
141  auth->ah_verf = au->au_shcred = _null_auth;
142  au->au_shfaults = 0;
143  au->au_origcred.oa_base = NULL;
144 
145  /*
146  * fill in param struct from the given params
147  */
149  aup.aup_time = now.tv_sec;
150  aup.aup_machname = machname;
151  aup.aup_uid = uid;
152  aup.aup_gid = gid;
153  aup.aup_len = (u_int)len;
154  aup.aup_gids = aup_gids;
155 
156  /*
157  * Serialize the parameters into origcred
158  */
159  xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE);
160  if (! xdr_authunix_parms(&xdrs, &aup))
161  abort();
162  au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs);
163  au->au_origcred.oa_flavor = AUTH_UNIX;
164 #ifdef _KERNEL
165  au->au_origcred.oa_base = mem_alloc((u_int) len);
166 #else
167  if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
168  // XXX warnx("authunix_create: out of memory");
169  goto cleanup_authunix_create;
170  }
171 #endif
172  memmove(au->au_origcred.oa_base, mymem, (size_t)len);
173 
174  /*
175  * set auth handle to reflect new cred.
176  */
177  auth->ah_cred = au->au_origcred;
178  marshal_new_auth(auth);
179  return (auth);
180 #ifndef _KERNEL
181  cleanup_authunix_create:
182  if (auth)
183  mem_free(auth, sizeof(*auth));
184  if (au) {
185  if (au->au_origcred.oa_base)
186  mem_free(au->au_origcred.oa_base, (u_int)len);
187  mem_free(au, sizeof(*au));
188  }
189  return (NULL);
190 #endif
191 }
192 
193 /*
194  * Returns an auth handle with parameters determined by doing lots of
195  * syscalls.
196  */
197 AUTH *
199 {
200  int len;
201  char machname[MAXHOSTNAMELEN + 1];
202  uid_t uid;
203  gid_t gid;
204  gid_t gids[NGRPS];
205 
206  if (gethostname(machname, sizeof machname) == -1)
207  abort();
208  machname[sizeof(machname) - 1] = 0;
209 #if 0
210  uid = geteuid();
211  gid = getegid();
212  if ((len = getgroups(NGRPS, gids)) < 0)
213  abort();
214 #else
215  // XXX Need to figure out what to do here!
216  uid = 666;
217  gid = 777;
218  gids[0] = 0;
219  len = 0;
220 #endif
221  /* XXX: interface problem; those should all have been unsigned */
222  return (authunix_create(machname, uid, gid, len, gids));
223 }
224 
225 /*
226  * authunix operations
227  */
228 
229 /* ARGSUSED */
230 static void
232  AUTH *auth;
233 {
234  /* no action necessary */
235 }
236 
237 static bool_t
238 authunix_marshal(auth, xdrs, seq)
239  AUTH *auth;
240  XDR *xdrs;
241  u_int *seq;
242 {
243  struct audata *au;
244 
245  assert(auth != NULL);
246  assert(xdrs != NULL);
247 
248  au = AUTH_PRIVATE(auth);
249  return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos));
250 }
251 
252 static bool_t
253 authunix_validate(auth, verf, seq)
254  AUTH *auth;
255  struct opaque_auth *verf;
256  u_int seq;
257 {
258  struct audata *au;
259  XDR xdrs;
260 
261  assert(auth != NULL);
262  assert(verf != NULL);
263 
264  if (verf->oa_flavor == AUTH_SHORT) {
265  au = AUTH_PRIVATE(auth);
266  xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
267  XDR_DECODE);
268 
269  if (au->au_shcred.oa_base != NULL) {
270  mem_free(au->au_shcred.oa_base,
271  au->au_shcred.oa_length);
272  au->au_shcred.oa_base = NULL;
273  }
274  if (xdr_opaque_auth(&xdrs, &au->au_shcred)) {
275  auth->ah_cred = au->au_shcred;
276  } else {
277  xdrs.x_op = XDR_FREE;
278  (void)xdr_opaque_auth(&xdrs, &au->au_shcred);
279  au->au_shcred.oa_base = NULL;
280  auth->ah_cred = au->au_origcred;
281  }
282  marshal_new_auth(auth);
283  }
284  return (TRUE);
285 }
286 
287 static bool_t
289 {
290  struct audata *au = AUTH_PRIVATE(auth);
291  struct authunix_parms aup;
292  struct timeval now;
293  XDR xdrs;
294  int stat;
295 
296  assert(auth != NULL);
297 
298  if (auth->ah_cred.oa_base == au->au_origcred.oa_base) {
299  /* there is no hope. Punt */
300  return (FALSE);
301  }
302  au->au_shfaults ++;
303 
304  /* first deserialize the creds back into a struct authunix_parms */
305  aup.aup_machname = NULL;
306  aup.aup_gids = NULL;
307  xdrmem_create(&xdrs, au->au_origcred.oa_base,
308  au->au_origcred.oa_length, XDR_DECODE);
309  stat = xdr_authunix_parms(&xdrs, &aup);
310  if (! stat)
311  goto done;
312 
313  /* update the time and serialize in place */
315  aup.aup_time = now.tv_sec;
316  xdrs.x_op = XDR_ENCODE;
317  XDR_SETPOS(&xdrs, 0);
318  stat = xdr_authunix_parms(&xdrs, &aup);
319  if (! stat)
320  goto done;
321  auth->ah_cred = au->au_origcred;
322  marshal_new_auth(auth);
323 done:
324  /* free the struct authunix_parms created by deserializing */
325  xdrs.x_op = XDR_FREE;
326  (void)xdr_authunix_parms(&xdrs, &aup);
327  XDR_DESTROY(&xdrs);
328  return (stat);
329 }
330 
331 static void
333  AUTH *auth;
334 {
335  struct audata *au;
336 
337  assert(auth != NULL);
338 
339  au = AUTH_PRIVATE(auth);
340  mem_free(au->au_origcred.oa_base, au->au_origcred.oa_length);
341 
342  if (au->au_shcred.oa_base != NULL)
343  mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length);
344 
345  mem_free(auth->ah_private, sizeof(struct audata));
346 
347  if (auth->ah_verf.oa_base != NULL)
348  mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length);
349 
350  mem_free(auth, sizeof(*auth));
351 }
352 
353 /*
354  * Marshals (pre-serializes) an auth struct.
355  * sets private data, au_marshed and au_mpos
356  */
357 static void
359  AUTH *auth;
360 {
361  XDR xdr_stream;
362  XDR *xdrs = &xdr_stream;
363  struct audata *au;
364 
365  assert(auth != NULL);
366 
367  au = AUTH_PRIVATE(auth);
369  if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) ||
370  (! xdr_opaque_auth(xdrs, &(auth->ah_verf))))
371  assert(0); // XXX
372  // XXX warnx("auth_none.c - Fatal marshalling problem");
373  else
374  au->au_mpos = XDR_GETPOS(xdrs);
375  XDR_DESTROY(xdrs);
376 }
377 
378 static bool_t
379 #ifndef __REACTOS__
381 #else
383 #endif
384 {
385  return ((*func)(xdrs, args));
386 }
387 #ifdef __REACTOS__
388 static bool_t
389 authunix_unwrap(AUTH *auth, XDR *xdrs, xdrproc_t func, caddr_t args, u_int seq)
390 {
391  return ((*func)(xdrs, args));
392 }
393 #endif
394 
395 static struct auth_ops *
397 {
398  static struct auth_ops ops;
399  extern mutex_t ops_lock;
400 
401  /* VARIABLES PROTECTED BY ops_lock: ops */
402 
404  if (ops.ah_nextverf == NULL) {
405  ops.ah_nextverf = authunix_nextverf;
406  ops.ah_marshal = authunix_marshal;
407  ops.ah_validate = authunix_validate;
408  ops.ah_refresh = authunix_refresh;
409  ops.ah_destroy = authunix_destroy;
410  ops.ah_wrap = authunix_wrap;
411 #ifndef __REACTOS__
412  ops.ah_unwrap = authunix_wrap;
413 #else
414  ops.ah_unwrap = authunix_unwrap;
415 #endif
416  }
418  return (&ops);
419 }
GLenum func
Definition: glext.h:6028
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
bool_t xdr_authunix_parms(XDR *xdrs, struct authunix_parms *p)
Definition: authunix_prot.c:50
#define MAX_AUTH_BYTES
Definition: auth.h:77
#define TRUE
Definition: types.h:120
#define XDR_GETPOS(xdrs)
Definition: xdr.h:199
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
unsigned long u_long
Definition: linux.h:269
u_int oa_length
Definition: auth.h:198
mutex_t ops_lock
Definition: mt_misc.c:71
int32_t bool_t
Definition: types.h:101
static void authunix_nextverf(AUTH *)
Definition: auth_unix.c:231
caddr_t oa_base
Definition: auth.h:197
struct opaque_auth au_origcred
Definition: auth_unix.c:94
static bool_t authunix_validate(AUTH *, struct opaque_auth *, u_int)
Definition: auth_unix.c:253
Definition: auth.h:205
static void marshal_new_auth(AUTH *)
Definition: auth_unix.c:358
#define assert(x)
Definition: debug.h:53
AUTH * authunix_create(char *machname, uid_t uid, gid_t gid, int len, gid_t *aup_gids)
Definition: auth_unix.c:107
char * aup_machname
Definition: auth_unix.h:61
gid_t aup_gid
Definition: auth_unix.h:63
static bool_t authunix_marshal(AUTH *, XDR *, u_int *seq)
Definition: auth_unix.c:238
Definition: match.c:390
long uid_t
Definition: various.h:8
char * caddr_t
Definition: rosdhcp.h:36
Definition: xdr.h:103
AUTH * authunix_create_default()
Definition: auth_unix.c:198
#define mutex_lock(m)
Definition: reentrant.h:128
#define mutex_unlock(m)
Definition: reentrant.h:129
#define gettimeofday(tv, tz)
Definition: adns_win32.h:159
bool_t(* xdrproc_t)(XDR *,...)
Definition: xdr.h:144
#define mem_alloc(bsize)
Definition: types.h:123
static bool_t authunix_wrap(AUTH *auth, XDR *xdrs, xdrproc_t func, caddr_t args, u_int seq)
Definition: auth_unix.c:380
struct opaque_auth _null_auth
UINT gid_t
Definition: types.h:89
time_t now
Definition: finger.c:65
INT WSAAPI gethostname(OUT char FAR *name, IN INT namelen)
Definition: getxbyxx.c:397
static struct auth_ops * authunix_ops(void)
Definition: auth_unix.c:396
smooth NULL
Definition: ftsmooth.c:416
void * ah_private
Definition: auth.h:225
static bool_t authunix_refresh(AUTH *, void *)
Definition: auth_unix.c:288
static void authunix_destroy(AUTH *)
Definition: auth_unix.c:332
uid_t aup_uid
Definition: auth_unix.h:62
u_int aup_len
Definition: auth_unix.h:64
struct opaque_auth ah_verf
Definition: auth.h:207
enum xdr_op x_op
Definition: xdr.h:104
#define AUTH_UNIX
Definition: auth.h:404
EXPORT gid_t getegid()
Definition: gid.c:37
gid_t * aup_gids
Definition: auth_unix.h:65
Definition: xdr.h:85
Definition: stat.h:55
GLenum GLsizei len
Definition: glext.h:6722
EXPORT uid_t geteuid()
Definition: uid.c:37
#define XDR_SETPOS(xdrs, pos)
Definition: xdr.h:204
_CRTIMP int __cdecl stat(const char *_Filename, struct stat *_Stat)
Definition: stat.h:345
#define MAXHOSTNAMELEN
Definition: ftp.c:31
#define caddr_t
Definition: ftp.c:24
UINT32 u_int
Definition: types.h:82
enum_t oa_flavor
Definition: auth.h:196
u_long au_shfaults
Definition: auth_unix.c:96
#define abort()
Definition: i386-dis.c:35
struct __auth::auth_ops * ah_ops
#define XDR_PUTBYTES(xdrs, addr, len)
Definition: xdr.h:194
void xdrmem_create(XDR *xdrs, char *addr, u_int size, enum xdr_op op)
Definition: xdr_mem.c:94
bool_t xdr_opaque_auth()
Definition: xdr.h:87
struct opaque_auth ah_cred
Definition: auth.h:206
Definition: xdr.h:86
#define mem_free(ptr, bsize)
Definition: types.h:124
#define AUTH_SHORT
Definition: auth.h:405
char au_marshed[MAX_AUTH_BYTES]
Definition: auth_unix.c:97
Definition: module.h:446
u_int au_mpos
Definition: auth_unix.c:98
#define NGRPS
Definition: auth_unix.h:54
u_long aup_time
Definition: auth_unix.h:60
#define XDR_DESTROY(xdrs)
Definition: xdr.h:214
struct opaque_auth au_shcred
Definition: auth_unix.c:95
#define AUTH_PRIVATE(auth)
Definition: auth_unix.c:100