maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1//go:build cgo && libpam
 2// +build cgo,libpam
 3
 4/*
 5Maddy Mail Server - Composable all-in-one email server.
 6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 7
 8This program is free software: you can redistribute it and/or modify
 9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program.  If not, see <https://www.gnu.org/licenses/>.
20*/
21
22package pam
23
24/*
25#cgo LDFLAGS: -lpam
26#cgo CFLAGS: -DCGO -Wall -Wextra -Werror -Wno-unused-parameter -Wno-error=unused-parameter -Wpedantic -std=c99
27
28#include <stdlib.h>
29#include "pam.h"
30*/
31import "C"
32
33import (
34	"errors"
35	"fmt"
36	"unsafe"
37)
38
39const canCallDirectly = true
40
41var ErrInvalidCredentials = errors.New("pam: invalid credentials or unknown user")
42
43func 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 ErrInvalidCredentials
51	}
52	if errObj.status == 2 {
53		return fmt.Errorf("%s: %s", C.GoString(errObj.func_name), C.GoString(errObj.error_msg))
54	}
55	return nil
56}