xref: /illumos-gate/usr/src/lib/libnsl/dial/strsave.c (revision 61961e0f)
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 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "uucp.h"
34 
35 #if defined(sparc)
36 #define	_STAT _stat
37 #else  /* !sparc */
38 #define	_STAT stat
39 #endif /* sparc */
40 
41 extern int _stat(const char *, struct stat *);
42 
43 /* copy str into data space -- caller should report errors. */
44 
45 static char *
46 strsave(const char *str)
47 {
48 	char *rval;
49 
50 	rval = malloc(strlen(str) + 1);
51 	if (rval != 0)
52 		(void) strcpy(rval, str);
53 	return (rval);
54 }
55 
56 /*
57  * Determine if the effective user id has the appropriate permission
58  *	on a file.  Modeled after access(2).
59  *	amode:
60  *		00	just checks for file existence.
61  *		04	checks read permission.
62  *		02	checks write permission.
63  *		01	checks execute/search permission.
64  *		other bits are ignored quietly.
65  */
66 
67 static int
68 eaccess(char *path, mode_t amode)
69 {
70 	struct stat	s;
71 	uid_t euid;
72 
73 	if (_STAT(path, &s) == -1)
74 		return (-1);		/* can't stat file */
75 	amode &= 07;
76 
77 	if ((euid = geteuid()) == 0)		/* root can do all */
78 		return (0);
79 	if (euid == s.st_uid)
80 	    s.st_mode >>= 6;		/* use owner bits */
81 	else if (getegid() == s.st_gid)
82 	    s.st_mode >>= 3;		/* use group bits */
83 
84 	if ((amode & s.st_mode) == amode)
85 		return (0);		/* access permitted */
86 	errno = EACCES;
87 	return (-1);
88 }
89