]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_user.c
ecf36f6a44c9c11e42081a926ddcac06d4bca92d
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * user information (passwd) lookup
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
32 #include <rpc/types.h>
35 #include <netinet/in.h>
37 #include "_lu_types.h"
40 #include "lu_overrides.h"
42 static lookup_state pw_state
= LOOKUP_CACHE
;
43 static struct passwd global_pw
;
44 static int global_free
= 1;
45 static char *pw_data
= NULL
;
46 static unsigned pw_datalen
;
47 static int pw_nentries
;
48 static int pw_start
= 1;
54 if (global_free
== 1) return;
56 free(global_pw
.pw_name
);
57 free(global_pw
.pw_passwd
);
58 free(global_pw
.pw_class
);
59 free(global_pw
.pw_gecos
);
60 free(global_pw
.pw_dir
);
61 free(global_pw
.pw_shell
);
67 convert_pw(_lu_passwd
*lu_pw
)
71 global_pw
.pw_name
= strdup(lu_pw
->pw_name
);
72 global_pw
.pw_passwd
= strdup(lu_pw
->pw_passwd
);
73 global_pw
.pw_uid
= lu_pw
->pw_uid
;
74 global_pw
.pw_gid
= lu_pw
->pw_gid
;
75 global_pw
.pw_change
= lu_pw
->pw_change
;
76 global_pw
.pw_class
= strdup(lu_pw
->pw_class
);
77 global_pw
.pw_gecos
= strdup(lu_pw
->pw_gecos
);
78 global_pw
.pw_dir
= strdup(lu_pw
->pw_dir
);
79 global_pw
.pw_shell
= strdup(lu_pw
->pw_shell
);
80 global_pw
.pw_expire
= lu_pw
->pw_expire
;
85 static struct passwd
*
92 unit lookup_buf
[MAX_INLINE_UNITS
];
96 if (_lookup_link(_lu_port
, "getpwuid_A", &proc
) != KERN_SUCCESS
)
103 datalen
= MAX_INLINE_UNITS
;
104 if (_lookup_one(_lu_port
, proc
, (unit
*)&uid
, 1, lookup_buf
, &datalen
)
110 datalen
*= BYTES_PER_XDR_UNIT
;
111 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
113 if (!xdr__lu_passwd_ptr(&xdr
, &lu_pw
) || (lu_pw
== NULL
))
122 xdr_free(xdr__lu_passwd_ptr
, &lu_pw
);
126 static struct passwd
*
127 lu_getpwnam(const char *name
)
130 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
133 _lu_passwd_ptr lu_pw
;
134 static int proc
= -1;
135 unit lookup_buf
[MAX_INLINE_UNITS
];
139 if (_lookup_link(_lu_port
, "getpwnam_A", &proc
) != KERN_SUCCESS
)
145 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
146 if (!xdr__lu_string(&outxdr
, &name
))
148 xdr_destroy(&outxdr
);
152 datalen
= MAX_INLINE_UNITS
;
153 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
154 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
157 xdr_destroy(&outxdr
);
161 xdr_destroy(&outxdr
);
163 datalen
*= BYTES_PER_XDR_UNIT
;
164 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
167 if (!xdr__lu_passwd_ptr(&inxdr
, &lu_pw
) || (lu_pw
== NULL
))
176 xdr_free(xdr__lu_passwd_ptr
, &lu_pw
);
182 lu_putpwpasswd(char *login
, char *old_passwd
, char *new_passwd
)
187 static int proc
= -1;
188 char output_buf
[3 * (_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
)];
189 unit lookup_buf
[MAX_INLINE_UNITS
];
194 if (_lookup_link(_lu_port
, "putpwpasswd", &proc
) != KERN_SUCCESS
)
200 xdrmem_create(&outxdr
, output_buf
, sizeof(output_buf
), XDR_ENCODE
);
201 if (!xdr__lu_string(&outxdr
, &login
) ||
202 !xdr__lu_string(&outxdr
, &old_passwd
) ||
203 !xdr__lu_string(&outxdr
, &new_passwd
))
205 xdr_destroy(&outxdr
);
209 datalen
= MAX_INLINE_UNITS
;
210 if (_lookup_one(_lu_port
, proc
, output_buf
,
211 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
214 xdr_destroy(&outxdr
);
218 xdr_destroy(&outxdr
);
220 datalen
*= BYTES_PER_XDR_UNIT
;
221 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
222 if (!xdr_int(&xdr
, &changed
))
241 vm_deallocate(mach_task_self(), (vm_address_t
)pw_data
, pw_datalen
);
254 static struct passwd
*
257 static int proc
= -1;
266 if (_lookup_link(_lu_port
, "getpwent_A", &proc
) != KERN_SUCCESS
)
273 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &pw_data
, &pw_datalen
)
281 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
282 pw_datalen
*= BYTES_PER_XDR_UNIT
;
285 xdrmem_create(&pw_xdr
, pw_data
, pw_datalen
,
287 if (!xdr_int(&pw_xdr
, &pw_nentries
))
289 xdr_destroy(&pw_xdr
);
295 if (pw_nentries
== 0)
297 xdr_destroy(&pw_xdr
);
302 bzero(&lu_pw
, sizeof(lu_pw
));
303 if (!xdr__lu_passwd(&pw_xdr
, &lu_pw
))
305 xdr_destroy(&pw_xdr
);
312 xdr_free(xdr__lu_passwd
, &lu_pw
);
316 static char *loginName
= NULL
;
317 static uid_t loginUid
= -1;
319 extern char *getlogin(void);
325 if (loginName
== NULL
) {
326 char *l
= getlogin();
328 struct passwd
*p
= getpwnam(l
);
330 loginUid
= p
->pw_uid
;
335 if (uid
== loginUid
) {
336 LOOKUP1(lu_getpwnam
, _old_getpwnam
, loginName
, struct passwd
);
339 LOOKUP1(lu_getpwuid
, _old_getpwuid
, uid
, struct passwd
);
343 getpwnam(const char *name
)
345 LOOKUP1(lu_getpwnam
, _old_getpwnam
, name
, struct passwd
);
350 * putpwpasswd() is not supported with anything other than netinfo
352 * old_passwd is clear text.
353 * new_passwd is encrypted.
355 #define _old_passwd(name, oldpass, newpass) 0
357 putpwpasswd(char *login
, char *old_passwd
, char *new_passwd
)
359 if (_lu_running()) return (lu_putpwpasswd(login
, old_passwd
, new_passwd
));
360 return (old_passwd(login
, old_passwd
, new_passwd
));
367 GETENT(lu_getpwent
, _old_getpwent
, &pw_state
, struct passwd
);
373 INTSETSTATEVOID(lu_setpwent
, _old_setpwent
, &pw_state
);
379 UNSETSTATE(lu_endpwent
, _old_endpwent
, &pw_state
);