bsd-cygwin_util.c revision 294336
11539Srgrimes/*
21539Srgrimes * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
31539Srgrimes *
41539Srgrimes * Redistribution and use in source and binary forms, with or without
51539Srgrimes * modification, are permitted provided that the following conditions
61539Srgrimes * are met:
71539Srgrimes * 1. Redistributions of source code must retain the above copyright
81539Srgrimes *    notice, this list of conditions and the following disclaimer.
91539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
101539Srgrimes *    notice, this list of conditions and the following disclaimer in the
111539Srgrimes *    documentation and/or other materials provided with the distribution.
121539Srgrimes *
131539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
141539Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
151539Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
161539Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171539Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181539Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
191539Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201539Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211539Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
221539Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
231539Srgrimes *
241539Srgrimes * Created: Sat Sep 02 12:17:00 2000 cv
251539Srgrimes *
261539Srgrimes * This file contains functions for forcing opened file descriptors to
271539Srgrimes * binary mode on Windows systems.
281539Srgrimes */
291539Srgrimes
301539Srgrimes#define NO_BINARY_OPEN	/* Avoid redefining open to binary_open for this file */
311539Srgrimes#include "includes.h"
321539Srgrimes
331539Srgrimes#ifdef HAVE_CYGWIN
341539Srgrimes
358858Srgrimes#include <sys/types.h>
361539Srgrimes#include <fcntl.h>
371539Srgrimes#include <string.h>
381539Srgrimes#include <unistd.h>
391539Srgrimes
401539Srgrimes#include "xmalloc.h"
411539Srgrimes
428858Srgrimesint
431539Srgrimesbinary_open(const char *filename, int flags, ...)
441539Srgrimes{
451539Srgrimes	va_list ap;
461539Srgrimes	mode_t mode;
471539Srgrimes
481539Srgrimes	va_start(ap, flags);
491539Srgrimes	mode = va_arg(ap, mode_t);
501539Srgrimes	va_end(ap);
511539Srgrimes	return (open(filename, flags | O_BINARY, mode));
521539Srgrimes}
531539Srgrimes
541539Srgrimesint
5536888Spetercheck_ntsec(const char *filename)
5636888Speter{
5736888Speter	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
5850473Speter}
5936888Speter
6036888Speterconst char *
611539Srgrimescygwin_ssh_privsep_user()
621539Srgrimes{
631539Srgrimes  static char cyg_privsep_user[DNLEN + UNLEN + 2];
6484463Sbde
65102227Smike  if (!cyg_privsep_user[0])
6621055Speter    {
67102227Smike#ifdef CW_CYGNAME_FROM_WINNAME
68102227Smike      if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
69102227Smike			   sizeof cyg_privsep_user) != 0)
7072510Sume#endif
7172510Sume	strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
72102227Smike    }
73102227Smike  return cyg_privsep_user;
74102227Smike}
7584463Sbde
7684463Sbde#define NL(x) x, (sizeof (x) - 1)
7736888Speter#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
7836888Speter
7936888Speterstatic struct wenv {
801539Srgrimes	const char *name;
811539Srgrimes	size_t namelen;
821539Srgrimes} wenv_arr[] = {
831539Srgrimes	{ NL("ALLUSERSPROFILE=") },
841539Srgrimes	{ NL("COMPUTERNAME=") },
8510132Speter	{ NL("COMSPEC=") },
8610132Speter	{ NL("CYGWIN=") },
871539Srgrimes	{ NL("OS=") },
881539Srgrimes	{ NL("PATH=") },
891539Srgrimes	{ NL("PATHEXT=") },
901539Srgrimes	{ NL("PROGRAMFILES=") },
911539Srgrimes	{ NL("SYSTEMDRIVE=") },
9283047Sobrien	{ NL("SYSTEMROOT=") },
931539Srgrimes	{ NL("WINDIR=") }
941539Srgrimes};
951539Srgrimes
961539Srgrimeschar **
971539Srgrimesfetch_windows_environment(void)
9813771Smpp{
991539Srgrimes	char **e, **p;
1001539Srgrimes	unsigned int i, idx = 0;
1011539Srgrimes
1021539Srgrimes	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
1031539Srgrimes	for (e = environ; *e != NULL; ++e) {
1041539Srgrimes		for (i = 0; i < WENV_SIZ; ++i) {
10583047Sobrien			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
1061539Srgrimes				p[idx++] = *e;
1071539Srgrimes		}
1081539Srgrimes	}
1091539Srgrimes	p[idx] = NULL;
1101539Srgrimes	return p;
1111539Srgrimes}
11283047Sobrien
1131539Srgrimesvoid
1141539Srgrimesfree_windows_environment(char **p)
1151539Srgrimes{
1161539Srgrimes	free(p);
1171539Srgrimes}
1181539Srgrimes
11983047Sobrien#endif /* HAVE_CYGWIN */
1201539Srgrimes