access.c revision 66963
1/****************************************************************************
2 * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Author: Thomas E. Dickey <dickey@clark.net> 1998,2000                   *
31 ****************************************************************************/
32
33#include <curses.priv.h>
34#include <tic.h>
35
36MODULE_ID("$Id: access.c,v 1.4 2000/10/08 01:25:06 tom Exp $")
37
38char *
39_nc_basename(char *path)
40{
41    char *result = strrchr(path, '/');
42#ifdef __EMX__
43    if (result == 0)
44	result = strrchr(path, '\\');
45#endif
46    if (result == 0)
47	result = path;
48    else
49	result++;
50    return result;
51}
52
53int
54_nc_access(const char *path, int mode)
55{
56    if (access(path, mode) < 0) {
57	if ((mode & W_OK) != 0
58	    && errno == ENOENT
59	    && strlen(path) < PATH_MAX) {
60	    char head[PATH_MAX];
61	    char *leaf = _nc_basename(strcpy(head, path));
62
63	    if (leaf == 0)
64		leaf = head;
65	    *leaf = '\0';
66	    if (head == leaf)
67		(void) strcpy(head, ".");
68
69	    return access(head, R_OK | W_OK | X_OK);
70	}
71	return -1;
72    }
73    return 0;
74}
75
76#ifndef USE_ROOT_ENVIRON
77/*
78 * Returns true if we allow application to use environment variables that are
79 * used for searching lists of directories, etc.
80 */
81int
82_nc_env_access(void)
83{
84#if HAVE_ISSETUGID
85    if (issetugid())
86	return FALSE;
87#elif HAVE_GETEUID && HAVE_GETEGID
88    if (getuid() != geteuid()
89     || getgid() != getegid())
90	return FALSE;
91#endif
92    return getuid() != 0;	/* ...finally, disallow root */
93}
94#endif
95