1//go:build cgo && libpam2// +build cgo,libpam34/*5Maddy Mail Server - Composable all-in-one email server.6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors78This program is free software: you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation, either version 3 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program. If not, see <https://www.gnu.org/licenses/>.20*/2122package pam2324/*25#cgo LDFLAGS: -lpam26#cgo CFLAGS: -DCGO -Wall -Wextra -Werror -Wno-unused-parameter -Wno-error=unused-parameter -Wpedantic -std=c992728#include <stdlib.h>29#include "pam.h"30*/31import "C"3233import (34 "errors"35 "fmt"36 "unsafe"37)3839const canCallDirectly = true4041var ErrInvalidCredentials = errors.New("pam: invalid credentials or unknown user")4243func runPAMAuth(username, password string) error {44 usernameC := C.CString(username)45 passwordC := C.CString(password)46 defer C.free(unsafe.Pointer(usernameC))47 defer C.free(unsafe.Pointer(passwordC))48 errObj := C.run_pam_auth(usernameC, passwordC)49 if errObj.status == 1 {50 return ErrInvalidCredentials51 }52 if errObj.status == 2 {53 return fmt.Errorf("%s: %s", C.GoString(errObj.func_name), C.GoString(errObj.error_msg))54 }55 return nil56}