blob: a2d82126df9802db7981169a430ee84fad0b5965 [file] [log] [blame]
Damien Millerbac2d8a2000-09-05 16:13:06 +11001/*
Darren Tuckerc9627cd2013-04-01 12:40:48 +11002 * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
Damien Millerbac2d8a2000-09-05 16:13:06 +11003 *
Kevin Steveseb363022002-04-15 22:00:51 +00004 * 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.
Damien Millerbac2d8a2000-09-05 16:13:06 +110023 *
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
Darren Tuckerc9627cd2013-04-01 12:40:48 +110030#define NO_BINARY_OPEN /* Avoid redefining open to binary_open for this file */
Damien Millere9cf3572001-02-09 12:55:35 +110031#include "includes.h"
32
Damien Millerbac2d8a2000-09-05 16:13:06 +110033#ifdef HAVE_CYGWIN
Damien Miller72c9a7e2000-09-24 11:10:13 +110034
Darren Tucker92350102006-08-05 19:08:16 +100035#include <sys/types.h>
Darren Tucker92350102006-08-05 19:08:16 +100036#include <fcntl.h>
Darren Tuckerc9627cd2013-04-01 12:40:48 +110037#include <string.h>
Darren Tucker92350102006-08-05 19:08:16 +100038#include <unistd.h>
Darren Tucker92350102006-08-05 19:08:16 +100039
Darren Tucker14c372d2004-08-30 20:42:08 +100040#include "xmalloc.h"
Ben Lindstromcff94be2001-07-18 16:19:48 +000041
Damien Miller31741252003-05-19 00:13:38 +100042int
43binary_open(const char *filename, int flags, ...)
Damien Millerbac2d8a2000-09-05 16:13:06 +110044{
Damien Miller2deb3f62001-02-18 12:30:55 +110045 va_list ap;
46 mode_t mode;
47
48 va_start(ap, flags);
49 mode = va_arg(ap, mode_t);
50 va_end(ap);
Damien Miller31741252003-05-19 00:13:38 +100051 return (open(filename, flags | O_BINARY, mode));
Damien Millerbac2d8a2000-09-05 16:13:06 +110052}
53
Damien Miller31741252003-05-19 00:13:38 +100054int
55check_ntsec(const char *filename)
Damien Millerb70b61f2000-09-16 16:25:12 +110056{
Darren Tucker9a3f2b42008-07-17 19:03:49 +100057 return (pathconf(filename, _PC_POSIX_PERMISSIONS));
Damien Millerbac2d8a2000-09-05 16:13:06 +110058}
Damien Miller72c9a7e2000-09-24 11:10:13 +110059
Damien Miller6482d902014-05-27 14:34:42 +100060const char *
61cygwin_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 strcpy (cyg_privsep_user, "sshd");
72 }
73 return cyg_privsep_user;
74}
75
Darren Tucker14c372d2004-08-30 20:42:08 +100076#define NL(x) x, (sizeof (x) - 1)
77#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
78
79static struct wenv {
80 const char *name;
81 size_t namelen;
82} wenv_arr[] = {
83 { NL("ALLUSERSPROFILE=") },
Darren Tucker14c372d2004-08-30 20:42:08 +100084 { NL("COMPUTERNAME=") },
85 { NL("COMSPEC=") },
Darren Tuckerae8c91e2005-05-25 19:42:10 +100086 { NL("CYGWIN=") },
Darren Tucker14c372d2004-08-30 20:42:08 +100087 { NL("OS=") },
88 { NL("PATH=") },
89 { NL("PATHEXT=") },
Damien Miller7b7901c2012-02-14 06:38:36 +110090 { NL("PROGRAMFILES=") },
Darren Tucker14c372d2004-08-30 20:42:08 +100091 { NL("SYSTEMDRIVE=") },
92 { NL("SYSTEMROOT=") },
Darren Tuckerae8c91e2005-05-25 19:42:10 +100093 { NL("WINDIR=") }
Darren Tucker14c372d2004-08-30 20:42:08 +100094};
95
96char **
97fetch_windows_environment(void)
98{
99 char **e, **p;
Darren Tucker84af6152006-02-12 11:59:08 +1100100 unsigned int i, idx = 0;
Darren Tucker14c372d2004-08-30 20:42:08 +1000101
Darren Tuckerd8093e42006-05-04 16:24:34 +1000102 p = xcalloc(WENV_SIZ + 1, sizeof(char *));
Darren Tucker14c372d2004-08-30 20:42:08 +1000103 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
113void
114free_windows_environment(char **p)
115{
Darren Tuckerf60845f2013-06-02 08:07:31 +1000116 free(p);
Darren Tucker14c372d2004-08-30 20:42:08 +1000117}
118
Damien Miller72c9a7e2000-09-24 11:10:13 +1100119#endif /* HAVE_CYGWIN */