简体   繁体   English

Linux 通过 node.js 登录

[英]Linux login via node.js

I'm currently building an (experimental) webapp to login and control a (linux) server (especially for monitoring) in node.js. For authentification i would like to use the linux users.我目前正在构建一个(实验性)webapp 来登录和控制 node.js 中的(linux)服务器(特别是用于监控)。为了进行身份验证,我想使用 linux 用户。

Either via /etc/passwd and /etc/shadow , reading information works quite good, but i can not authenticate ( crypto , bcrypt won't work), via child_process.exec i can execute openssl passwd , this only generates md5 ( 1 ), but the passwords stored in /etc/shadow are encrypted with SHA512 ( 6 ).通过/etc/passwd/etc/shadow ,读取信息效果很好,但我无法验证( cryptobcrypt不起作用),通过child_process.exec我可以执行openssl passwd ,这只会生成 md5 ( 1 ) ,但存储在/etc/shadow中的密码是使用 SHA512 ( 6 ) 加密的。 Is there any way I'm missing out?有什么办法让我错过了吗?

The other way i tried, is using the login command (via child_process.spawn ).我尝试的另一种方法是使用login命令(通过child_process.spawn )。 but this always exits with code 1 .但这总是以代码1退出。 All other commands (eg apt-get upgrade ) work as expected.所有其他命令(例如apt-get upgrade )都按预期工作。

PS: my server is running on root so there should be no problem with permissions. PS:我的服务器是root运行的所以权限应该没有问题。

Thanks for your help谢谢你的帮助

EDIT : As i already pointed out, this is an EXPERIMENTAL project, so I'm not planning to use it in production, i was just curious about it.编辑:正如我已经指出的,这是一个实验项目,所以我不打算在生产中使用它,我只是对此感到好奇。 It's a bit similar to PCMonitor有点类似于PCMonitor

I know running it as root is not a good idea, and thats not what i would use in production apps.我知道以root身份运行它不是一个好主意,这不是我在生产应用程序中使用的。

To clarify : My question was more about, why every command (i have tried so far) works good with child_process.spawn except login .澄清一下:我的问题更多是关于为什么除了login之外的每个命令(到目前为止我已经尝试过)都适用于child_process.spawn And how i could generate a SHA512 password with a salt (exactly like the ones in /etc/shadow以及我如何生成带有盐的SHA512密码(与/etc/shadow中的密码完全一样)

Well this question is rather old, but I stumbled upon a similar problem verifying the entered password against the systems user db.好吧,这个问题很老了,但是我偶然发现了一个类似的问题,即针对系统用户 db 验证输入的密码。 Maybe this can help others having the same task.也许这可以帮助其他人完成同样的任务。

I finally did it via the expect tool.我终于通过expect工具做到了。
So I came up with this expect script:所以我想出了这个期望脚本:

#!/usr/bin/expect

set username [lindex $argv 0];
set password [lindex $argv 1];
set timeout 5

puts "$username $password"

if {[file exists /usr/bin/su]} {
    spawn /usr/bin/su $username
}
if {[file exists /bin/su]} {
    spawn /bin/su $username
}

expect -exact "Password:"
send "$password\n" 

expect { 
    -ex "su: " {
        puts "login failed"
        exit 1
    }
    -ex "@" {
        puts "login successful"
        send "exit\n"
        exit 0
    }
    timeout {
        puts "login failed (timeout)"
        exit 1
    }

    default { 
        puts "login failed (unknown reason)"
        exit 1
    }
}

If you save this script for example as pw_check.sh you then can call it like this in nodejs例如,如果将此脚本保存为 pw_check.sh,则可以在 nodejs 中这样调用它

var child = execFile(
        '/path/to/pw_check.sh',
        [username,password],
        {stdio: ['pipe', 'pipe', 'pipe']});

        child.on('exit', function(code, signal) {
            if (code === 0) {
                //pw check was successful
            } else {
                console.log("Authentication error.")
                //pw check failed
            }
        });

I must admit that this is not a very secure way to do this, but I found no better one yet.我必须承认,这不是一种非常安全的方法,但我还没有找到更好的方法。 And creating a separate user db was no option for me.创建一个单独的用户数据库对我来说不是一个选择。

You can use node.js to connect via SSH to the localhost server, check out this module https://github.com/mscdex/ssh2 . 您可以使用node.js通过SSH连接到localhost服务器,查看此模块https://github.com/mscdex/ssh2 You can forward the commands sent via the web api to the ssh connection. 您可以将通过Web api发送的命令转发到ssh连接。

This way it would be a lot safer than exec-ing commands, and you would be able to use any users allowed to ssh. 这样,它比执行命令安全得多,并且您将能够使用任何允许ssh的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM