forked from ubuntu/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more distro-specific functionality to DistributionInfo class (ubu…
…ntu#33) * Add more distro-specific functionality to DistributionInfo class * merge change from PR ubuntu#28 * Add missing cpp file * Incorperate PR feedback
- Loading branch information
Ben Hillis
authored
Mar 30, 2018
1 parent
3ff006d
commit 4df4319
Showing
7 changed files
with
109 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// Copyright (C) Microsoft. All rights reserved. | ||
// Licensed under the terms described in the LICENSE file in the root of this project. | ||
// | ||
|
||
#include "stdafx.h" | ||
|
||
bool DistributionInfo::CreateUser(const std::wstring& userName) | ||
{ | ||
// Create the user account. | ||
DWORD exitCode; | ||
std::wstring commandLine = L"/usr/sbin/adduser --quiet --gecos '' " + userName; | ||
HRESULT hr = g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode); | ||
if ((FAILED(hr)) || (exitCode != 0)) { | ||
return false; | ||
} | ||
|
||
// Add the user account to any relevant groups. | ||
commandLine = L"/usr/sbin/usermod -aG adm,cdrom,sudo,dip,plugdev " + userName; | ||
hr = g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode); | ||
if ((FAILED(hr)) || (exitCode != 0)) { | ||
|
||
// Delete the user if the group add command failed. | ||
commandLine = L"/usr/sbin/deluser " + userName; | ||
g_wslApi.WslLaunchInteractive(commandLine.c_str(), true, &exitCode); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
ULONG DistributionInfo::QueryUid(const std::wstring& userName) | ||
{ | ||
// Create a pipe to read the output of the launched process. | ||
HANDLE readPipe; | ||
HANDLE writePipe; | ||
SECURITY_ATTRIBUTES sa{sizeof(sa), nullptr, true}; | ||
ULONG uid = UID_INVALID; | ||
if (CreatePipe(&readPipe, &writePipe, &sa, 0)) { | ||
// Query the UID of the supplied username. | ||
std::wstring command = L"/usr/bin/id -u " + userName; | ||
int returnValue = 0; | ||
HANDLE child; | ||
HRESULT hr = g_wslApi.WslLaunch(command.c_str(), true, GetStdHandle(STD_INPUT_HANDLE), writePipe, GetStdHandle(STD_ERROR_HANDLE), &child); | ||
if (SUCCEEDED(hr)) { | ||
// Wait for the child to exit and ensure process exited successfully. | ||
WaitForSingleObject(child, INFINITE); | ||
DWORD exitCode; | ||
if ((GetExitCodeProcess(child, &exitCode) == false) || (exitCode != 0)) { | ||
hr = E_INVALIDARG; | ||
} | ||
|
||
CloseHandle(child); | ||
if (SUCCEEDED(hr)) { | ||
char buffer[64]; | ||
DWORD bytesRead; | ||
|
||
// Read the output of the command from the pipe and convert to a UID. | ||
if (ReadFile(readPipe, buffer, (sizeof(buffer) - 1), &bytesRead, nullptr)) { | ||
buffer[bytesRead] = ANSI_NULL; | ||
try { | ||
uid = std::stoul(buffer, nullptr, 10); | ||
|
||
} catch( ... ) { } | ||
} | ||
} | ||
} | ||
|
||
CloseHandle(readPipe); | ||
CloseHandle(writePipe); | ||
} | ||
|
||
return uid; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters