xref: /onnv-gate/usr/src/lib/libnsl/dial/strsave.c (revision 1219:f89f56c2d9ac)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22132Srobinson 
23132Srobinson /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25132Srobinson  * Use is subject to license terms.
26132Srobinson  */
27132Srobinson 
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
31132Srobinson #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
33*1219Sraf #include "mt.h"
340Sstevel@tonic-gate #include "uucp.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /* copy str into data space -- caller should report errors. */
370Sstevel@tonic-gate 
38132Srobinson static char *
strsave(const char * str)39132Srobinson strsave(const char *str)
400Sstevel@tonic-gate {
41132Srobinson 	char *rval;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	rval = malloc(strlen(str) + 1);
440Sstevel@tonic-gate 	if (rval != 0)
45132Srobinson 		(void) strcpy(rval, str);
460Sstevel@tonic-gate 	return (rval);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
49132Srobinson /*
50132Srobinson  * Determine if the effective user id has the appropriate permission
51132Srobinson  *	on a file.  Modeled after access(2).
52132Srobinson  *	amode:
53132Srobinson  *		00	just checks for file existence.
54132Srobinson  *		04	checks read permission.
55132Srobinson  *		02	checks write permission.
56132Srobinson  *		01	checks execute/search permission.
57132Srobinson  *		other bits are ignored quietly.
58132Srobinson  */
590Sstevel@tonic-gate 
60132Srobinson static int
eaccess(char * path,mode_t amode)61132Srobinson eaccess(char *path, mode_t amode)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	struct stat	s;
640Sstevel@tonic-gate 	uid_t euid;
650Sstevel@tonic-gate 
66*1219Sraf 	if (stat(path, &s) == -1)
670Sstevel@tonic-gate 		return (-1);		/* can't stat file */
680Sstevel@tonic-gate 	amode &= 07;
690Sstevel@tonic-gate 
70132Srobinson 	if ((euid = geteuid()) == 0)		/* root can do all */
71132Srobinson 		return (0);
720Sstevel@tonic-gate 	if (euid == s.st_uid)
730Sstevel@tonic-gate 	    s.st_mode >>= 6;		/* use owner bits */
740Sstevel@tonic-gate 	else if (getegid() == s.st_gid)
750Sstevel@tonic-gate 	    s.st_mode >>= 3;		/* use group bits */
760Sstevel@tonic-gate 
77132Srobinson 	if ((amode & s.st_mode) == amode)
780Sstevel@tonic-gate 		return (0);		/* access permitted */
790Sstevel@tonic-gate 	errno = EACCES;
800Sstevel@tonic-gate 	return (-1);
810Sstevel@tonic-gate }
82