xref: /freebsd/crypto/openssh/openbsd-compat/bsd-cygwin_util.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*
2  * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * Created: Sat Sep 02 12:17:00 2000 cv
25  *
26  * This file contains functions for forcing opened file descriptors to
27  * binary mode on Windows systems.
28  */
29 
30 #define NO_BINARY_OPEN	/* Avoid redefining open to binary_open for this file */
31 #include "includes.h"
32 
33 #ifdef HAVE_CYGWIN
34 
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "xmalloc.h"
41 
42 int
43 binary_open(const char *filename, int flags, ...)
44 {
45 	va_list ap;
46 	mode_t mode;
47 
48 	va_start(ap, flags);
49 	mode = va_arg(ap, mode_t);
50 	va_end(ap);
51 	return (open(filename, flags | O_BINARY, mode));
52 }
53 
54 int
55 check_ntsec(const char *filename)
56 {
57 	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
58 }
59 
60 const char *
61 cygwin_ssh_privsep_user()
62 {
63   static char cyg_privsep_user[DNLEN + UNLEN + 2];
64 
65   if (!cyg_privsep_user[0])
66     {
67 #ifdef CW_CYGNAME_FROM_WINNAME
68       if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
69 			   sizeof cyg_privsep_user) != 0)
70 #endif
71 	strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
72     }
73   return cyg_privsep_user;
74 }
75 
76 #define NL(x) x, (sizeof (x) - 1)
77 #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
78 
79 static struct wenv {
80 	const char *name;
81 	size_t namelen;
82 } wenv_arr[] = {
83 	{ NL("ALLUSERSPROFILE=") },
84 	{ NL("COMPUTERNAME=") },
85 	{ NL("COMSPEC=") },
86 	{ NL("CYGWIN=") },
87 	{ NL("OS=") },
88 	{ NL("PATH=") },
89 	{ NL("PATHEXT=") },
90 	{ NL("PROGRAMFILES=") },
91 	{ NL("SYSTEMDRIVE=") },
92 	{ NL("SYSTEMROOT=") },
93 	{ NL("WINDIR=") }
94 };
95 
96 char **
97 fetch_windows_environment(void)
98 {
99 	char **e, **p;
100 	unsigned int i, idx = 0;
101 
102 	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
103 	for (e = environ; *e != NULL; ++e) {
104 		for (i = 0; i < WENV_SIZ; ++i) {
105 			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
106 				p[idx++] = *e;
107 		}
108 	}
109 	p[idx] = NULL;
110 	return p;
111 }
112 
113 void
114 free_windows_environment(char **p)
115 {
116 	free(p);
117 }
118 
119 #endif /* HAVE_CYGWIN */
120