diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-11-15 18:32:03 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2009-11-15 18:32:03 +0100 |
| commit | cb3baab306e5951dc3a176fd9061f596a05b4729 (patch) | |
| tree | 1074fd193e9be7e62aa431effde391213705fc36 /libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c | |
| parent | c10a5c533a5b71c03f0e8d52dea81eb77dbebfd7 (diff) | |
| download | tuxcmd-modules-cb3baab306e5951dc3a176fd9061f596a05b4729.tar.xz | |
Rebase libarchive to 2.7.1
Diffstat (limited to 'libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c')
| -rw-r--r-- | libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c b/libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c new file mode 100644 index 0000000..d31f112 --- /dev/null +++ b/libarchive/libarchive-2.7.1/tar/bsdtar_cygwin.c @@ -0,0 +1,141 @@ +/*- + * Copyright (c) 2009 Michihiro NAKAJIMA + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#if defined(__CYGWIN__) +#define _WIN32_WINNT 0x0500 +#define WINVER 0x0500 + +#include "bsdtar_platform.h" +#include <errno.h> +#include <stddef.h> +#include <sys/utime.h> +#include <sys/stat.h> +#include <process.h> +#include <stdlib.h> +#include <wchar.h> +#include <windows.h> +#include <sddl.h> + +#include "bsdtar.h" + +#ifndef LIST_H +static int +_is_privileged(HANDLE thandle, const char *sidlist[]) +{ + TOKEN_USER *tuser; + TOKEN_GROUPS *tgrp; + DWORD bytes; + PSID psid; + DWORD i, g; + int member; + + psid = NULL; + tuser = NULL; + tgrp = NULL; + member = 0; + for (i = 0; sidlist[i] != NULL && member == 0; i++) { + if (psid != NULL) + LocalFree(psid); + /* mingw/cygwin: incorrectly prototypes arg 1 as LPSTR + * instead of LPCSTR. Work around it here + */ + if (ConvertStringSidToSidA((char *)sidlist[i], &psid) == 0) { + errno = EPERM; + return (-1); + } + if (tuser == NULL) { + GetTokenInformation(thandle, TokenUser, NULL, 0, &bytes); + tuser = malloc(bytes); + if (tuser == NULL) { + errno = ENOMEM; + member = -1; + break; + } + if (GetTokenInformation(thandle, TokenUser, tuser, bytes, &bytes) == 0) { + errno = EPERM; + member = -1; + break; + } + } + member = EqualSid(tuser->User.Sid, psid); + if (member) + break; + if (tgrp == NULL) { + GetTokenInformation(thandle, TokenGroups, NULL, 0, &bytes); + tgrp = malloc(bytes); + if (tgrp == NULL) { + errno = ENOMEM; + member = -1; + break; + } + if (GetTokenInformation(thandle, TokenGroups, tgrp, bytes, &bytes) == 0) { + errno = EPERM; + member = -1; + break; + } + } + for (g = 0; g < tgrp->GroupCount; g++) { + member = EqualSid(tgrp->Groups[g].Sid, psid); + if (member) + break; + } + } + LocalFree(psid); + free(tuser); + free(tgrp); + + return (member); +} + +int +bsdtar_is_privileged(struct bsdtar *bsdtar) +{ + HANDLE thandle; + int ret; + const char *sidlist[] = { + "S-1-5-32-544", /* Administrators */ + "S-1-5-32-551", /* Backup Operators */ + NULL + }; + + (void)bsdtar;/* UNUSED */ + if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &thandle) == 0) { + bsdtar_warnc(bsdtar, EPERM, "Failed to check privilege"); + return (0); + } + ret = _is_privileged(thandle, sidlist); + if (ret < 0) { + bsdtar_warnc(bsdtar, errno, "Failed to check privilege"); + return (0); + } + return (ret); +} + + +#endif /* LIST_H */ + +#endif |
