code.gitea.io/gitea@v1.19.3/modules/auth/pam/pam.go (about)

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build pam
     5  
     6  package pam
     7  
     8  import (
     9  	"errors"
    10  
    11  	"github.com/msteinert/pam"
    12  )
    13  
    14  // Supported is true when built with PAM
    15  var Supported = true
    16  
    17  // Auth pam auth service
    18  func Auth(serviceName, userName, passwd string) (string, error) {
    19  	t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
    20  		switch s {
    21  		case pam.PromptEchoOff:
    22  			return passwd, nil
    23  		case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
    24  			return "", nil
    25  		}
    26  		return "", errors.New("Unrecognized PAM message style")
    27  	})
    28  	if err != nil {
    29  		return "", err
    30  	}
    31  
    32  	if err = t.Authenticate(0); err != nil {
    33  		return "", err
    34  	}
    35  
    36  	if err = t.AcctMgmt(0); err != nil {
    37  		return "", err
    38  	}
    39  
    40  	// PAM login names might suffer transformations in the PAM stack.
    41  	// We should take whatever the PAM stack returns for it.
    42  	return t.GetItem(pam.User)
    43  }