Check linux user and password (authenticate)

Hello,

what is the best and securest way in go to check where the password for an linux user is valid.

Best regards Jens

Does not exist (i think) a go way to check if a password is valid because this is a mater of Linux not Go language. Anyway, you could login with a password and this can be executed from Go as Linux command. You can do this using a local command like sudo -u user -i (or other similar commands) or through ssh command, in both cases having the passing password problem :wink:
Better is to use login with sshbecause is more portable and work local and remote. For this you need a ssh server active on the Linux machine. See in this project how to ssh work and pass the password.

You probably want to talk to PAM. http://godoc.org/github.com/msteinert/pam looks somewhat promising.

3 Likes

Here a solution with the PAM library:

package main

import (
	"errors"
	"fmt"

	"github.com/msteinert/pam"
)

func PAMAuth(serviceName, userName, passwd string) error {
	t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
		switch s {
		case pam.PromptEchoOff:
			return passwd, nil
		case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
			return "", nil
		}
		return "", errors.New("Unrecognized PAM message style")
	})

	if err != nil {
		return err
	}

	if err = t.Authenticate(0); err != nil {
		return err
	}

	return nil
}

func main() {
	err := PAMAuth("passwd", "user", "pass")
	if err != nil {
		fmt.Println("Error")
	} else {
		fmt.Println("Auth")
	}
}

Thank you

2 Likes

I try to build this and it bombs thusly:

…/…/github.com/msteinert/pam/transaction.go:4:31: fatal error: security/pam_appl.h: No such file or directory
//#include <security/pam_appl.h>

| Nemmind … libpam0g-dev … apt is your friend :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.