xref: /titanic_51/usr/src/lib/libnsl/dial/strsave.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
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 2006 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 "mt.h"
34 #include "uucp.h"
35 
36 /* copy str into data space -- caller should report errors. */
37 
38 static char *
39 strsave(const char *str)
40 {
41 	char *rval;
42 
43 	rval = malloc(strlen(str) + 1);
44 	if (rval != 0)
45 		(void) strcpy(rval, str);
46 	return (rval);
47 }
48 
49 /*
50  * Determine if the effective user id has the appropriate permission
51  *	on a file.  Modeled after access(2).
52  *	amode:
53  *		00	just checks for file existence.
54  *		04	checks read permission.
55  *		02	checks write permission.
56  *		01	checks execute/search permission.
57  *		other bits are ignored quietly.
58  */
59 
60 static int
61 eaccess(char *path, mode_t amode)
62 {
63 	struct stat	s;
64 	uid_t euid;
65 
66 	if (stat(path, &s) == -1)
67 		return (-1);		/* can't stat file */
68 	amode &= 07;
69 
70 	if ((euid = geteuid()) == 0)		/* root can do all */
71 		return (0);
72 	if (euid == s.st_uid)
73 	    s.st_mode >>= 6;		/* use owner bits */
74 	else if (getegid() == s.st_gid)
75 	    s.st_mode >>= 3;		/* use group bits */
76 
77 	if ((amode & s.st_mode) == amode)
78 		return (0);		/* access permitted */
79 	errno = EACCES;
80 	return (-1);
81 }
82