xref: /freebsd/contrib/sendmail/test/t_pathconf.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*
2  * Copyright (c) 1999 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  */
10 
11 /*
12 **  The following test program tries the pathconf(2) routine.  It should
13 **  be run in a non-NFS-mounted directory (e.g., /tmp) and on remote (NFS)
14 **  mounted directories running both NFS-v2 and NFS-v3 from systems that
15 **  both do and do not permit file giveaway.
16 */
17 
18 #include <sys/types.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #ifdef EX_OK
26 # undef EX_OK		/* unistd.h may have another use for this */
27 #endif
28 #include <sysexits.h>
29 
30 #ifndef lint
31 static char id[] = "@(#)$Id: t_pathconf.c,v 8.7 2013-11-22 20:52:01 ca Exp $";
32 #endif
33 
34 int
35 main(argc, argv)
36 	int argc;
37 	char **argv;
38 {
39 	int fd;
40 	int i;
41 	char tbuf[100];
42 	extern int errno;
43 
44 	if (geteuid() == 0)
45 	{
46 		printf("*** Run me as a non-root user! ***\n");
47 		exit(EX_USAGE);
48 	}
49 
50 	strcpy(tbuf, "TXXXXXX");
51 	fd = mkstemp(tbuf);
52 	if (fd < 0)
53 	{
54 		printf("*** Could not create test file %s\n", tbuf);
55 		exit(EX_CANTCREAT);
56 	}
57 	errno = 0;
58 	i = pathconf(".", _PC_CHOWN_RESTRICTED);
59 	printf("pathconf(.) returns %2d, errno = %d\n", i, errno);
60 	errno = 0;
61 	i = pathconf(tbuf, _PC_CHOWN_RESTRICTED);
62 	printf("pathconf(%s) returns %2d, errno = %d\n", tbuf, i, errno);
63 	errno = 0;
64 	i = fpathconf(fd, _PC_CHOWN_RESTRICTED);
65 	printf("fpathconf(%s) returns %2d, errno = %d\n", tbuf, i, errno);
66 	if (errno == 0 && i >= 0)
67 	{
68 		/* so it claims that it doesn't work -- try anyhow */
69 		printf("  fpathconf claims that chown is safe ");
70 		if (fchown(fd, 1, 1) >= 0)
71 			printf("*** but fchown works anyhow! ***\n");
72 		else
73 			printf("and fchown agrees\n");
74 	}
75 	else
76 	{
77 		/* well, let's see what really happens */
78 		printf("  fpathconf claims that chown is not safe ");
79 		if (fchown(fd, 1, 1) >= 0)
80 			printf("as indeed it is not\n");
81 		else
82 			printf("*** but in fact it is safe ***\n");
83 	}
84 	(void) unlink(tbuf);
85 	exit(EX_OK);
86 }
87