xref: /titanic_44/usr/src/lib/libnsl/dial/strsave.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
27 
28 #include "uucp.h"
29 #include <rpc/trace.h>
30 
31 /* #include <errno.h> */
32 /* #include <malloc.h> */
33 /* #include <string.h> */
34 /* #include <sys/types.h> */
35 /* #include <sys/stat.h> */
36 
37 #if defined(sparc)
38 #define _STAT _stat
39 #else  /* !sparc */
40 #define _STAT stat
41 #endif /* sparc */
42 
43 extern int _stat(const char *, struct stat *);
44 
45 /* copy str into data space -- caller should report errors. */
46 
47 GLOBAL char *
48 strsave(str)
49 #if defined(__STDC__)
50 register const char *str;
51 #else
52 register char *str;
53 #endif
54 {
55 	register char *rval;
56 
57 	trace1(TR_strsave, 0);
58 	rval = malloc(strlen(str) + 1);
59 	if (rval != 0)
60 		strcpy(rval, str);
61 	trace1(TR_strsave, 1);
62 	return (rval);
63 }
64 
65 /*	Determine if the effective user id has the appropriate permission
66 	on a file.  Modeled after access(2).
67 	amode:
68 		00	just checks for file existence.
69 		04	checks read permission.
70 		02	checks write permission.
71 		01	checks execute/search permission.
72 		other bits are ignored quietly.
73 */
74 
75 GLOBAL int
76 eaccess(path, amode)
77 char		*path;
78 register mode_t	amode;
79 {
80 	struct stat	s;
81 	uid_t euid;
82 
83 	trace1(TR_eaccess, 0);
84 	if (_STAT(path, &s) == -1) {
85 		trace1(TR_eaccess, 1);
86 		return (-1);		/* can't stat file */
87 	}
88 	amode &= 07;
89 
90 	if ((euid = geteuid()) == 0) {		/* root can do all */
91 		trace1(TR_eaccess, 1);
92 	        return (0);
93 	}
94 	if (euid == s.st_uid)
95 	    s.st_mode >>= 6;		/* use owner bits */
96 	else if (getegid() == s.st_gid)
97 	    s.st_mode >>= 3;		/* use group bits */
98 
99 	if ((amode & s.st_mode) == amode) {
100 		trace1(TR_eaccess, 1);
101 		return (0);		/* access permitted */
102 	}
103 	errno = EACCES;
104 	trace1(TR_eaccess, 1);
105 	return (-1);
106 }
107