Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report the correct result if the "server" service is not running #4

Merged
merged 1 commit into from Mar 15, 2017
Merged

Report the correct result if the "server" service is not running #4

merged 1 commit into from Mar 15, 2017

Conversation

jviotti
Copy link

@jviotti jviotti commented Feb 20, 2017

If the "server" service is not running, this module will report false
even when the user is the administrator.

By investigating further, net.exe session fails with the following
message:

The Server service is not started.

More help is available by typing NET HELPMSG 2114.

which is caught by the .catch() block, which returns false without
inspecting the error object.

The main issue is that net.exe session returns an exit code 2 both
when the user has no permissions and when the server service is not
running, making it impossibly to distinguish between both scenarios
without inspecting the output of the command.

The StackOverflow question linked from the source code addresses this
concern in another answer, by proposing the use of the following
command:

fsutil dirty query %systemdrive% >nul

fsutil is ensured to be present on Windows XP and later, and returns
an exit code 0 if the user is the administrator, and exit code 1
otherwise.

This commit introduces the recommended fsutil command, and falls back
to an fltmc approach discussed on the same thread if fsutil doesn't
exist (e.g: we get ENOENT for it).

Fixes: #3
See: http://stackoverflow.com/a/21295806/1641422
Signed-off-by: Juan Cruz Viotti jviotti@openmailbox.org

@lurch
Copy link

lurch commented Feb 20, 2017

#3 (comment)

AIUI it'd be better to fall back to sfc if fsutil isn't available, because net session is still just as likely to fail?

@jviotti
Copy link
Author

jviotti commented Feb 20, 2017

Up to @sindresorhus. I'm happy with both approaches.

@jviotti
Copy link
Author

jviotti commented Feb 24, 2017

Ping @sindresorhus

@sindresorhus
Copy link
Owner

Sorry, too many PRs to deal with.

AIUI it'd be better to fall back to sfc if fsutil isn't available, because net session is still just as likely to fail?

👍

If the "server" service is not running, this module will report `false`
even when the user is the administrator.

By investigating further, `net.exe session` fails with the following
message:

```
The Server service is not started.

More help is available by typing NET HELPMSG 2114.
```

which is caught by the `.catch()` block, which returns `false` without
inspecting the error object.

The main issue is that `net.exe session` returns an exit code 2 both
when the user has no permissions and when the server service is not
running, making it impossibly to distinguish between both scenarios
without inspecting the output of the command.

The StackOverflow question linked from the source code addresses this
concern in another answer, by proposing the use of the following
command:

```
fsutil dirty query %systemdrive% >nul
```

`fsutil` is ensured to be present on Windows XP and later, and returns
an exit code 0 if the user is the administrator, and exit code 1
otherwise.

This commit introduces the recommended `fsutil` command, and falls back
to an `fltmc` approach discussed on the same thread if `fsutil` doesn't
exist (e.g: we get `ENOENT` for it).

Fixes: #3
See: http://stackoverflow.com/a/21295806/1641422
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
@jviotti
Copy link
Author

jviotti commented Mar 14, 2017

@sindresorhus @lurch Turns out the sfc approach doesn't work on any of my Windows systems (I get an error level 1 even when I'm an admin). The same thread discussed yet another approach, based on fltmc, which seems to work fine for me, so I added that one as a fallback instead.

@sindresorhus
Copy link
Owner

Thanks :)

@jviotti jviotti deleted the server-service-incorrect-result branch March 15, 2017 14:43
jviotti pushed a commit to balena-io/etcher that referenced this pull request Mar 15, 2017
This version contains a fix for correctly checking whether a process is
running with administrator right or not, without depending on the
"Server" service.

Fixes: #1180
See: sindresorhus/is-admin#4
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
jviotti pushed a commit to balena-io/etcher that referenced this pull request Mar 15, 2017
This version contains a fix for correctly checking whether a process is
running with administrator right or not, without depending on the
"Server" service.

Fixes: #1180
See: sindresorhus/is-admin#4
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
jviotti pushed a commit to balena-io/etcher that referenced this pull request Mar 20, 2017
This version contains a fix for correctly checking whether a process is
running with administrator right or not, without depending on the
"Server" service.

Fixes: #1180
See: sindresorhus/is-admin#4
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
jviotti pushed a commit to balena-io/etcher that referenced this pull request Mar 21, 2017
This version contains a fix for correctly checking whether a process is
running with administrator right or not, without depending on the
"Server" service.

Fixes: #1180
See: sindresorhus/is-admin#4
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants