-
Notifications
You must be signed in to change notification settings - Fork 18k
x/term: "Access is denied." error with ReadPassword on Windows #46164
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
Comments
cc @alexbrainman @mattn @zx2c4 @gopherbot add OS-Windows |
@codesoap your version of the code is not expected to work. Try using https://pkg.go.dev/golang.org/x/term#Terminal.ReadPassword on Windows. Alex |
@alexbrainman Thanks for your response. I now tried using https://pkg.go.dev/golang.org/x/term#Terminal.ReadPassword:
This time using the
If I leave out the call of |
@codesoap sorry for confusing you. It has been many years since I looked at this code. Here is your original version with small mod: package main
import (
"fmt"
"golang.org/x/term"
"os"
)
func main() {
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
fmt.Println(string(pw))
} Here is this program in action:
I typed I hope it helps. Alex |
I probably didn't describe my problem in enough detail. I'm intentionally not using Using |
I am not familiar with https://github.com/FiloSottile/age package. Can you provide a small piece of code that works on UNIX and does not work on Windows? Hopefully this will help me understand what you are trying to achieve. Thank you. Alex |
Thanks a lot for keeping at it! I hope this program clarifies what I'm trying to achieve. It works on UNIX-like systems, but does not work on Windows after substituting
This is how I can use the program on a UNIX-like system; I am prompted to enter the password interactively: $ echo foo | ./readpw
Enter password:
Decrypted message:
foo |
I tried it too, I get But regardless, you are using 3 different devices in your program: Alex |
@codesoap does this help? You might also want to raise your Q on StackOverflow or golang-nuts for more ideas... |
@networkimprov Thanks for the input, but I'm not sure if this is relevant to my problem, since I have no problem with stdin (
I'm not sure if I understood you correctly, but using 3 different devices in the same program definitely works on Windows. If I modify my last provided program to use
This is what I get (on Windows):
|
The "access denied" seems like a Go bug. But you may need to use x/sys/windows to call WinAPIs to work around it. Ask on StackOverflow, and if that doesn't help, the WinAPI docs are the definitive resource. cc @mattn |
Document for CreateFile says: The following table shows various settings of dwDesiredAccess and lpFileName.
|
Thanks for looking into it as well, @mattn ! I'm afraid I don't understand what you wrote very well, because I'm not familiar with Windows' API. I'll take away that what I'm trying to do will not work with I'm not sure if this issue should be closed or left open, so I'll leave it up to someone more knowledgeable than me to close it if appropriate. |
@codesoap here is version of your program that works:
I copied part of that code from https://stackoverflow.com/questions/377152/what-does-createfileconin-do Alex |
Wow, thanks for all your effort, @alexbrainman ! The program you posted does exactly what I was looking for. The only thing I'll change is that I'll use As I said before, I can't really gauge whether this issue still has value or should be closed, so I'll leave for someone else to close or pick up. |
What happens if you use |
No worries.
So the problem here appears that os.Open does not work with files like I think this scenario is too specific. And we easily solved the problem by calling Windows API directly. I think this is good enough. So closing the issue. Feel free to re-open if you disagree. Alex |
@networkimprov Unfortunately this still gives the same "Access is denied." error. |
Alex, how about detecting |
@magical Found out something quite curious at FiloSottile/age#274 (comment) : Apparently it will work if
The same does not work with
Using Could this be a bug in the Windows API? It doesn't make sense to me that |
Assuming Alex is not inclined to change the os.Open flags for |
@networkimprov I think an example in x/term would be more appropriate. |
Why bother? This happened to me first time in my life. I prefer just using CreateFile Windows API - this way I can do exactly what MS documentation requires.
No it is not bug in Windows. os.OpenFile uses Windows CreateFile API. The implementation maps os.OpenFile parameters into CreateFile parameters. This mapping was implemented mainly for real files. Console files weren't considered while implementing this mapping. If os.OpenFile works for you, consider yourself lucky. I prefer to use CreateFile as per MS documentation. Alex |
@alexbrainman Thanks for further explaining this. I guess you are referring to https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles. It seems like this line is relevant here:
It is not explained how access is limited when using just
So the error is thrown by |
I am not referring to anything. But I did look at https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles and https://docs.microsoft.com/en-us/windows/console/setconsolemode and https://docs.microsoft.com/en-us/windows/console/console-buffer-security-and-access-rights Perhaps other documentation.
I agree with you. I could not find explanation of why this fails. But then I found C code on Stackoverflow that worked for me (see link at #46164 (comment) ). So I stopped trying to question Microsoft documentation.
I agree. I knew that
Like I said above, I stoped wandering about this the moment I found a solution. CreateFile API have 7 parameters. Perhaps other parameters are important too. You should ask Microsoft or someone else who knows why your code does not work. Sorry I could not be more helpful. Alex |
Thanks for your response. Since we have seemingly reached the limit of what we can do with the available documentation, I'll accept the behavior of the Windows API for now. If I find the time, I may try to put together a C++ demo of the problem later and ask Microsoft about it. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
It reproduces with the latest version of
x/term
(v0.0.0-20210503060354-a79de5458b56).What operating system and processor architecture are you using (
go env
)?go env
OutputI'm cross compiling from OpenBSD for Windows.
What did you do?
I ran the following program on the Windows 10 operating system:
What did you expect to see?
I expected the program to prompt me for password input and to then print the entered password.
What did you see instead?
When running the program (either as normal user or as administator) I get this output:
The text was updated successfully, but these errors were encountered: