8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libtecla / common / homedir.c
blobceafc4b76c0e4bf523dbc0204cda74fb53c2bb4e
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3 *
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, and/or sell copies of the Software, and to permit persons
11 * to whom the Software is furnished to do so, provided that the above
12 * copyright notice(s) and this permission notice appear in all copies of
13 * the Software and that both the above copyright notice(s) and this
14 * permission notice appear in supporting documentation.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * Except as contained in this notice, the name of a copyright holder
27 * shall not be used in advertising or otherwise to promote the sale, use
28 * or other dealings in this Software without prior written authorization
29 * of the copyright holder.
33 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
34 * Use is subject to license terms.
37 #pragma ident "%Z%%M% %I% %E% SMI"
40 * If file-system access is to be excluded, this module has no function,
41 * so all of its code should be excluded.
43 #ifndef WITHOUT_FILE_SYSTEM
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <errno.h>
50 #include <unistd.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <pwd.h>
55 #include "pathutil.h"
56 #include "homedir.h"
57 #include "errmsg.h"
60 * Use the reentrant POSIX threads versions of the password lookup functions?
62 #if defined(PREFER_REENTRANT) && defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199506L
63 #define THREAD_COMPATIBLE 1
65 * Under Solaris we can use thr_main() to determine whether
66 * threads are actually running, and thus when it is necessary
67 * to avoid non-reentrant features.
69 #if defined __sun && defined __SVR4
70 #include <thread.h> /* Solaris thr_main() */
71 #endif
72 #endif
75 * Provide a password buffer size fallback in case the max size reported
76 * by sysconf() is said to be indeterminate.
78 #define DEF_GETPW_R_SIZE_MAX 1024
81 * The resources needed to lookup and record a home directory are
82 * maintained in objects of the following type.
84 struct HomeDir {
85 ErrMsg *err; /* The error message report buffer */
86 char *buffer; /* A buffer for reading password entries and */
87 /* directory paths. */
88 int buflen; /* The allocated size of buffer[] */
89 #ifdef THREAD_COMPATIBLE
90 struct passwd pwd; /* The password entry of a user */
91 #endif
94 static const char *hd_getpwd(HomeDir *home);
96 /*.......................................................................
97 * Create a new HomeDir object.
99 * Output:
100 * return HomeDir * The new object, or NULL on error.
102 HomeDir *_new_HomeDir(void)
104 HomeDir *home; /* The object to be returned */
105 size_t pathlen; /* The estimated maximum size of a pathname */
107 * Allocate the container.
109 home = (HomeDir *) malloc(sizeof(HomeDir));
110 if(!home) {
111 errno = ENOMEM;
112 return NULL;
115 * Before attempting any operation that might fail, initialize the
116 * container at least up to the point at which it can safely be passed
117 * to _del_HomeDir().
119 home->err = NULL;
120 home->buffer = NULL;
121 home->buflen = 0;
123 * Allocate a place to record error messages.
125 home->err = _new_ErrMsg();
126 if(!home->err)
127 return _del_HomeDir(home);
129 * Allocate the buffer that is used by the reentrant POSIX password-entry
130 * lookup functions.
132 #ifdef THREAD_COMPATIBLE
134 * Get the length of the buffer needed by the reentrant version
135 * of getpwnam().
137 #ifndef _SC_GETPW_R_SIZE_MAX
138 home->buflen = DEF_GETPW_R_SIZE_MAX;
139 #else
140 errno = 0;
141 home->buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
143 * If the limit isn't available, substitute a suitably large fallback value.
145 if(home->buflen < 0 || errno)
146 home->buflen = DEF_GETPW_R_SIZE_MAX;
147 #endif
148 #endif
150 * If the existing buffer length requirement is too restrictive to record
151 * a pathname, increase its length.
153 pathlen = _pu_pathname_dim();
154 if(pathlen > home->buflen)
155 home->buflen = pathlen;
157 * Allocate a work buffer.
159 home->buffer = (char *) malloc(home->buflen);
160 if(!home->buffer) {
161 errno = ENOMEM;
162 return _del_HomeDir(home);
164 return home;
167 /*.......................................................................
168 * Delete a HomeDir object.
170 * Input:
171 * home HomeDir * The object to be deleted.
172 * Output:
173 * return HomeDir * The deleted object (always NULL).
175 HomeDir *_del_HomeDir(HomeDir *home)
177 if(home) {
178 home->err = _del_ErrMsg(home->err);
179 if(home->buffer)
180 free(home->buffer);
181 free(home);
183 return NULL;
186 /*.......................................................................
187 * Lookup the home directory of a given user in the password file.
189 * Input:
190 * home HomeDir * The resources needed to lookup the home directory.
191 * user const char * The name of the user to lookup, or "" to lookup
192 * the home directory of the person running the
193 * program.
194 * Output:
195 * return const char * The home directory. If the library was compiled
196 * with threads, this string is part of the HomeDir
197 * object and will change on subsequent calls. If
198 * the library wasn't compiled to be reentrant,
199 * then the string is a pointer into a static string
200 * in the C library and will change not only on
201 * subsequent calls to this function, but also if
202 * any calls are made to the C library password
203 * file lookup functions. Thus to be safe, you should
204 * make a copy of this string before calling any
205 * other function that might do a password file
206 * lookup.
208 * On error, NULL is returned and a description
209 * of the error can be acquired by calling
210 * _hd_last_home_dir_error().
212 const char *_hd_lookup_home_dir(HomeDir *home, const char *user)
214 const char *home_dir; /* A pointer to the home directory of the user */
216 * If no username has been specified, arrange to lookup the current
217 * user.
219 int login_user = !user || *user=='\0';
221 * Check the arguments.
223 if(!home) {
224 errno = EINVAL;
225 return NULL;
228 * Handle the ksh "~+". This expands to the absolute path of the
229 * current working directory.
231 if(!login_user && strcmp(user, "+") == 0) {
232 home_dir = hd_getpwd(home);
233 if(!home_dir) {
234 _err_record_msg(home->err, "Can't determine current directory",
235 END_ERR_MSG);
236 return NULL;
238 return home_dir;
241 * When looking up the home directory of the current user, see if the
242 * HOME environment variable is set, and if so, return its value.
244 if(login_user) {
245 home_dir = getenv("HOME");
246 if(home_dir)
247 return home_dir;
250 * Look up the password entry of the user.
251 * First the POSIX threads version - this is painful!
253 #ifdef THREAD_COMPATIBLE
255 struct passwd *ret; /* The returned pointer to pwd */
256 int status; /* The return value of getpwnam_r() */
258 * Look up the password entry of the specified user.
260 if(login_user)
261 status = getpwuid_r(geteuid(), &home->pwd, home->buffer, home->buflen,
262 &ret);
263 else
264 status = getpwnam_r(user, &home->pwd, home->buffer, home->buflen, &ret);
265 if(status || !ret) {
266 _err_record_msg(home->err, "User '", user, "' doesn't exist.",
267 END_ERR_MSG);
268 return NULL;
271 * Get a pointer to the string that holds the home directory.
273 home_dir = home->pwd.pw_dir;
276 * Now the classic unix version.
278 #else
280 struct passwd *pwd = login_user ? getpwuid(geteuid()) : getpwnam(user);
281 if(!pwd) {
282 _err_record_msg(home->err, "User '", user, "' doesn't exist.",
283 END_ERR_MSG);
284 return NULL;
287 * Get a pointer to the home directory.
289 home_dir = pwd->pw_dir;
291 #endif
292 return home_dir;
295 /*.......................................................................
296 * Return a description of the last error that caused _hd_lookup_home_dir()
297 * to return NULL.
299 * Input:
300 * home HomeDir * The resources needed to record the home directory.
301 * Output:
302 * return char * The description of the last error.
304 const char *_hd_last_home_dir_error(HomeDir *home)
306 return home ? _err_get_msg(home->err) : "NULL HomeDir argument";
309 /*.......................................................................
310 * The _hd_scan_user_home_dirs() function calls a user-provided function
311 * for each username known by the system, passing the function both
312 * the name and the home directory of the user.
314 * Input:
315 * home HomeDir * The resource object for reading home
316 * directories.
317 * prefix const char * Only information for usernames that
318 * start with this prefix will be
319 * returned. Note that the empty
320 & string "", matches all usernames.
321 * data void * Anonymous data to be passed to the
322 * callback function.
323 * callback_fn HOME_DIR_FN(*) The function to call for each user.
324 * Output:
325 * return int 0 - Successful completion.
326 * 1 - An error occurred. A description
327 * of the error can be obtained by
328 * calling _hd_last_home_dir_error().
330 int _hd_scan_user_home_dirs(HomeDir *home, const char *prefix,
331 void *data, HOME_DIR_FN(*callback_fn))
333 int waserr = 0; /* True after errors */
334 int prefix_len; /* The length of prefix[] */
336 * Check the arguments.
338 if(!home || !prefix || !callback_fn) {
339 if(home) {
340 _err_record_msg(home->err,
341 "_hd_scan_user_home_dirs: Missing callback function",
342 END_ERR_MSG);
344 return 1;
347 * Get the length of the username prefix.
349 prefix_len = strlen(prefix);
351 * There are no reentrant versions of getpwent() etc for scanning
352 * the password file, so disable username completion when the
353 * library is compiled to be reentrant.
355 #if defined(PREFER_REENTRANT) && defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199506L
356 #if defined __sun && defined __SVR4
357 if(0)
358 #else
359 if(1)
360 #endif
362 struct passwd pwd_buffer; /* A returned password entry */
363 struct passwd *pwd; /* A pointer to pwd_buffer */
364 char buffer[512]; /* The buffer in which the string members of */
365 /* pwd_buffer are stored. */
367 * See if the prefix that is being completed is a complete username.
369 if(!waserr && getpwnam_r(prefix, &pwd_buffer, buffer, sizeof(buffer),
370 &pwd) == 0 && pwd != NULL) {
371 waserr = callback_fn(data, pwd->pw_name, pwd->pw_dir,
372 _err_get_msg(home->err), ERR_MSG_LEN);
375 * See if the username of the current user minimally matches the prefix.
377 if(!waserr && getpwuid_r(getuid(), &pwd_buffer, buffer, sizeof(buffer),
378 &pwd) == 0 && pwd != NULL &&
379 strncmp(prefix, pwd->pw_name, prefix_len)==0) {
380 waserr = callback_fn(data, pwd->pw_name, pwd->pw_dir,
381 _err_get_msg(home->err), ERR_MSG_LEN);
384 * Reentrancy not required?
386 } else
387 #endif
389 struct passwd pwd_buffer; /* A returned password entry */
390 struct passwd *pwd; /* The pointer to the latest password entry */
392 * Open the password file.
394 setpwent();
396 * Read the contents of the password file, looking for usernames
397 * that start with the specified prefix, and adding them to the
398 * list of matches.
400 #if defined __sun && defined __SVR4
401 while((pwd = getpwent_r(&pwd_buffer, home->buffer, home->buflen)) != NULL && !waserr) {
402 #else
403 while((pwd = getpwent()) != NULL && !waserr) {
404 #endif
405 if(strncmp(prefix, pwd->pw_name, prefix_len) == 0) {
406 waserr = callback_fn(data, pwd->pw_name, pwd->pw_dir,
407 _err_get_msg(home->err), ERR_MSG_LEN);
411 * Close the password file.
413 endpwent();
416 * Under ksh ~+ stands for the absolute pathname of the current working
417 * directory.
419 if(!waserr && strncmp(prefix, "+", prefix_len) == 0) {
420 const char *pwd = hd_getpwd(home);
421 if(pwd) {
422 waserr = callback_fn(data, "+", pwd, _err_get_msg(home->err),ERR_MSG_LEN);
423 } else {
424 waserr = 1;
425 _err_record_msg(home->err, "Can't determine current directory.",
426 END_ERR_MSG);
429 return waserr;
432 /*.......................................................................
433 * Return the value of getenv("PWD") if this points to the current
434 * directory, or the return value of getcwd() otherwise. The reason for
435 * prefering PWD over getcwd() is that the former preserves the history
436 * of symbolic links that have been traversed to reach the current
437 * directory. This function is designed to provide the equivalent
438 * expansion of the ksh ~+ directive, which normally returns its value
439 * of PWD.
441 * Input:
442 * home HomeDir * The resource object for reading home directories.
443 * Output:
444 * return const char * A pointer to either home->buffer, where the
445 * pathname is recorded, the string returned by
446 * getenv("PWD"), or NULL on error.
448 static const char *hd_getpwd(HomeDir *home)
451 * Get the absolute path of the current working directory.
453 char *cwd = getcwd(home->buffer, home->buflen);
455 * Some shells set PWD with the path of the current working directory.
456 * This will differ from cwd in that it won't have had symbolic links
457 * expanded.
459 const char *pwd = getenv("PWD");
461 * If PWD was set, and it points to the same directory as cwd, return
462 * its value. Note that it won't be the same if the current shell or
463 * the current program has changed directories, after inheriting PWD
464 * from a parent shell.
466 struct stat cwdstat, pwdstat;
467 if(pwd && cwd && stat(cwd, &cwdstat)==0 && stat(pwd, &pwdstat)==0 &&
468 cwdstat.st_dev == pwdstat.st_dev && cwdstat.st_ino == pwdstat.st_ino)
469 return pwd;
471 * Also return pwd if getcwd() failed, since it represents the best
472 * information that we have access to.
474 if(!cwd)
475 return pwd;
477 * In the absence of a valid PWD, return cwd.
479 return cwd;
482 #endif /* ifndef WITHOUT_FILE_SYSTEM */