How to get environment password in script/function
6 次查看(过去 30 天)
显示 更早的评论
I know it is possible to extract environmental parameters like the username like this
getenv('username')
but is it also possible to extract the password?
2 个评论
Gareth
2018-12-14
Hi Johnny,
I am not sure if I understand your question, but usually it is considered a really bad idea to have enviroment variables with passwords... this is a major security flaw.
That being said you can alway use:
setenv('PATH', [getenv('PATH') ';D:\mypath']);
so that you create/add a particular variable to your enviroment like you would do with the path. Again just want to make sure you know if the password is on the enviroment then EVERY application can see it and it is typically a bad idea. Please further explain what you are trying to do as maybe the enviroment is not the best way to solve your problem.
回答(2 个)
Guillaume
2018-12-14
If you are talking about the password that was used by the user to log into their Windows/Mac/Linux/Whatever account, then absolutely not, for the simple reason that it's not stored at all in environment variables by any operating system. That would be a major security hole.
For that matter, the OS makes it as hard as possible for any program to even access that password. The OS should never give you the actual password, it will give you a token instead.
In any case, matlab does not have tools to access the security details of the logged in user. You could possibly do that by calling .Net functions directly from matlab (on Windows only). On other OSes, you'll certainly have to write your own mex code. As said, you still won't get the password, just a security token that you'll pass to the other application. On windows, you'll also get a UAC prompt.
How to actually do that, I don't know exactly as it's not something I have ever had a need to. If you were interested to do that with .Net, you'd have to read the System.Security documentation. It's not going to be trivial, so the answer to your is this possible is going to be: not without a lot of work.
0 个评论
Gareth
2018-12-15
The MathWorks does have a Database toolbox which does exactly what you are describing. That being said you probably would have to hard code the username/password. Indeed a common approach is to reuse credentials for database access, what I have seen people do is create a simple userinterface inside MATLAB that asks people for those credentials and then the program takes that info when pulling/fetching data.
Check out the Database Toolbox to see if this would help. I am sure that you could potentially with some effort use the .NET or java interface to access databases like this, as MATLAB can call both natively.
1 个评论
Guillaume
2018-12-15
My understanding is that the question is not about accessing databases, which can indeed be done easily with or even without the database toolbox, but about reusing the windows password to access such database.
It is not possible to retrieve the user password (windows does not even know what it is, it only stores a hash of it). While it would be possible to retrieve a security token for the logged in account and pass that to a database (if the database support it), the details how to do that would be fairly complex.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!