xref: /illumos-gate/usr/src/cmd/bnu/gwd.c (revision 0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6)
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  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "uucp.h"
31 
32 /*
33  *	gwd - get working directory
34  *	Uid and Euid are global
35  *	return
36  *		0 - ok
37  *	 	FAIL - failed
38  */
39 
40 int
41 gwd(wkdir)
42 char *wkdir;
43 {
44 	FILE *fp;
45 	char cmd[BUFSIZ];
46 
47 	*wkdir = '\0';
48 	(void) sprintf(cmd, "%s pwd 2>&-", PATH);
49 
50 #ifndef V7
51 	(void) setuid(Uid);
52 	fp = popen(cmd, "r");
53 	(void) setuid(Euid);
54 #else
55 	fp = popen(cmd, "r");
56 #endif
57 
58 	if (fp == NULL)
59 		return(FAIL);
60 
61 	if (fgets(wkdir, MAXFULLNAME, fp) == NULL) {
62 		(void) pclose(fp);
63 		return(FAIL);
64 	}
65 	if (wkdir[strlen(wkdir)-1] == '\n')
66 		wkdir[strlen(wkdir)-1] = '\0';
67 	(void) pclose(fp);
68 	return(0);
69 }
70 
71 
72 /*
73  * uidstat(file, &statbuf)
74  * This is a stat call with the uid set from Euid to Uid.
75  * Used from uucp.c and uux.c to permit file copies
76  * from directories that may not be searchable by other.
77  * return:
78  *	same as stat()
79  */
80 
81 int
82 uidstat(file, buf)
83 char *file;
84 struct stat *buf;
85 {
86 #ifndef V7
87 	int ret;
88 
89 	(void) setuid(Uid);
90 	ret = stat(file, buf);
91 	(void) setuid(Euid);
92 	return(ret);
93 #else /* V7 */
94 	int	ret;
95 
96 	if (vfork() == 0) {
97 		(void) setuid(Uid);
98 		_exit(stat(file, buf));
99 	}
100 	wait(&ret);
101 	return(ret);
102 #endif /* V7 */
103 }
104