blob: 2dcb3552183ae6f6cb683f6043d05b3ea96c19ad [file] [log] [blame]
Damien Millerbac2d8a2000-09-05 16:13:06 +11001/*
2 *
3 * cygwin_util.c
4 *
5 * Author: Corinna Vinschen <vinschen@cygnus.com>
6 *
7 * Copyright (c) 2000 Corinna Vinschen <vinschen@cygnus.com>, Duisburg, Germany
8 * All rights reserved
9 *
10 * Created: Sat Sep 02 12:17:00 2000 cv
11 *
12 * This file contains functions for forcing opened file descriptors to
13 * binary mode on Windows systems.
14 */
15
Damien Millere9cf3572001-02-09 12:55:35 +110016#include "includes.h"
17
18RCSID("$Id: bsd-cygwin_util.c,v 1.2 2001/02/09 01:55:36 djm Exp $");
Damien Millerbac2d8a2000-09-05 16:13:06 +110019
20#ifdef HAVE_CYGWIN
Damien Miller72c9a7e2000-09-24 11:10:13 +110021
Damien Millerbac2d8a2000-09-05 16:13:06 +110022#include <fcntl.h>
23#include <io.h>
Damien Millerb70b61f2000-09-16 16:25:12 +110024#include <stdlib.h>
25#include <sys/vfs.h>
26#include <windows.h>
27#define is_winnt (GetVersion() < 0x80000000)
Damien Millerbac2d8a2000-09-05 16:13:06 +110028
29int binary_open(const char *filename, int flags, mode_t mode)
30{
Damien Miller72c9a7e2000-09-24 11:10:13 +110031 return open(filename, flags | O_BINARY, mode);
Damien Millerbac2d8a2000-09-05 16:13:06 +110032}
33
34int binary_pipe(int fd[2])
35{
Damien Miller72c9a7e2000-09-24 11:10:13 +110036 int ret = pipe(fd);
37
38 if (!ret) {
39 setmode (fd[0], O_BINARY);
40 setmode (fd[1], O_BINARY);
41 }
42 return ret;
Damien Millerb70b61f2000-09-16 16:25:12 +110043}
44
Damien Miller72c9a7e2000-09-24 11:10:13 +110045int check_nt_auth(int pwd_authenticated, uid_t uid)
Damien Millerb70b61f2000-09-16 16:25:12 +110046{
47 /*
Damien Miller72c9a7e2000-09-24 11:10:13 +110048 * The only authentication which is able to change the user
49 * context on NT systems is the password authentication. So
50 * we deny all requsts for changing the user context if another
51 * authentication method is used.
52 * This may change in future when a special openssh
53 * subauthentication package is available.
54 */
Damien Millerb70b61f2000-09-16 16:25:12 +110055 if (is_winnt && !pwd_authenticated && geteuid() != uid)
56 return 0;
Damien Miller72c9a7e2000-09-24 11:10:13 +110057
Damien Millerb70b61f2000-09-16 16:25:12 +110058 return 1;
59}
60
Damien Miller72c9a7e2000-09-24 11:10:13 +110061int check_ntsec(const char *filename)
Damien Millerb70b61f2000-09-16 16:25:12 +110062{
63 char *cygwin;
64 int allow_ntea = 0;
65 int allow_ntsec = 0;
66 struct statfs fsstat;
67
68 /* Windows 95/98/ME don't support file system security at all. */
69 if (!is_winnt)
70 return 0;
71
72 /* Evaluate current CYGWIN settings. */
73 if ((cygwin = getenv("CYGWIN")) != NULL) {
74 if (strstr(cygwin, "ntea") && !strstr(cygwin, "nontea"))
75 allow_ntea = 1;
76 if (strstr(cygwin, "ntsec") && !strstr(cygwin, "nontsec"))
77 allow_ntsec = 1;
78 }
79
80 /*
81 * `ntea' is an emulation of POSIX attributes. It doesn't support
82 * real file level security as ntsec on NTFS file systems does
83 * but it supports FAT filesystems. `ntea' is minimum requirement
84 * for security checks.
85 */
86 if (allow_ntea)
87 return 1;
88
89 /*
90 * Retrieve file system flags. In Cygwin, file system flags are
91 * copied to f_type which has no meaning in Win32 itself.
92 */
93 if (statfs(filename, &fsstat))
94 return 1;
95
96 /*
97 * Only file systems supporting ACLs are able to set permissions.
98 * `ntsec' is the setting in Cygwin which switches using of NTFS
99 * ACLs to support POSIX permissions on files.
100 */
101 if (fsstat.f_type & FS_PERSISTENT_ACLS)
102 return allow_ntsec;
103
104 return 0;
Damien Millerbac2d8a2000-09-05 16:13:06 +1100105}
Damien Miller72c9a7e2000-09-24 11:10:13 +1100106
107#endif /* HAVE_CYGWIN */