├── debian ├── compat ├── manpages ├── control ├── rules └── changelog ├── go └── pamserve │ ├── go.mod │ └── main.go ├── .gitignore ├── .github └── workflows │ └── c.yml ├── Makefile ├── README.md ├── pam_unixsock.8.md ├── pam_unixsock.8 ├── pam_unixsock.c └── LICENSE /debian/compat: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /debian/manpages: -------------------------------------------------------------------------------- 1 | pam_unixsock.8 2 | -------------------------------------------------------------------------------- /go/pamserve/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/miekg/pam-unixsock/go/pamserve 2 | 3 | go 1.24.0 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | 4 | # Libraries 5 | *.lib 6 | *.a 7 | 8 | # Shared objects (inc. Windows DLLs) 9 | *.dll 10 | *.so 11 | *.so.* 12 | *.dylib 13 | 14 | # Executables 15 | *.exe 16 | *.out 17 | *.app 18 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: libpam-unixsock 2 | Section: libs 3 | Maintainer: Miek Gieben 4 | Build-Depends: debhelper (>= 9), libpam0g 5 | 6 | Package: libpam-unixsock 7 | Priority: optional 8 | Architecture: amd64 9 | Description: PAM module to send credentials to a unix socket 10 | -------------------------------------------------------------------------------- /.github/workflows/c.yml: -------------------------------------------------------------------------------- 1 | name: C 2 | on: [push, pull_request] 3 | jobs: 4 | 5 | build: 6 | name: Build and Test 7 | runs-on: ubuntu-latest 8 | steps: 9 | 10 | - name: Set up environment 11 | run: sudo apt install -y libpam0g libpam0g-dev make 12 | 13 | - name: Check out code 14 | uses: actions/checkout@v3 15 | 16 | - name: Build 17 | run: make 18 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: pam_unixsock.so 2 | 3 | pam_unixsock.so: pam_unixsock.o 4 | ld -x --shared -o pam_unixsock.so pam_unixsock.o 5 | 6 | pam_unixsock.o: pam_unixsock.c 7 | gcc -fPIC -fno-stack-protector -c pam_unixsock.c -o pam_unixsock.o 8 | 9 | clean: 10 | rm -f pam_unixsock.o pam_unixsock.so 11 | 12 | pam_unixsock.8: pam_unixsock.8.md 13 | mmark -man < pam_unixsock.8.md > pam_unixsock.8 14 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | 3 | SOURCE_VERSION := $(shell dpkg-parsechangelog --show-field version | sed 's/-[0-9]cncz[0-9][0-9]*//') 4 | SOURCE := https://github.com/miekg/pam-unixsock/archive/refs/tags/v$(SOURCE_VERSION).tar.gz 5 | LIB := pam_unixsock.so 6 | DOWNLOAD := $(shell basename $(SOURCE) ) 7 | 8 | %: 9 | dh $@ 10 | 11 | override_dh_auto_configure: 12 | dh_clean 13 | if [ ! -f $(DOWNLOAD) ]; then curl -L $(SOURCE) > $(DOWNLOAD); fi 14 | rm -rf pkg; mkdir pkg 15 | tar xf $(DOWNLOAD) -C pkg --strip-components 1 16 | for i in $(LIB); do ( cd pkg/; make ); cp pkg/$$i .; done 17 | 18 | override_dh_strip: 19 | 20 | override_dh_auto_clean: 21 | 22 | override_dh_auto_install: 23 | mkdir -p debian/libpam-unixsock/usr/lib/x86_64-linux-gnu/security 24 | for i in $(LIB); do cp `basename $$i` debian/libpam-unixsock/usr/lib/x86_64-linux-gnu/security; done 25 | cp pkg/pam_unixsock.8 . 26 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | libpam-unixsock (0.5.7) unstable; urgency=medium 2 | 3 | * New upstream release 4 | 5 | -- Miek Gieben Thu, 19 Jun 2025 10:51:10 +0200 6 | 7 | libpam-unixsock (0.5.6) unstable; urgency=medium 8 | 9 | * New upstream release 10 | 11 | -- Miek Gieben Thu, 08 May 2025 14:45:58 +0200 12 | 13 | libpam-unixsock (0.5.5) unstable; urgency=medium 14 | 15 | * New upstream release 16 | 17 | -- Miek Gieben Thu, 13 Mar 2025 08:39:50 +0100 18 | 19 | libpam-unixsock (0.5.4) unstable; urgency=medium 20 | 21 | * New upstream release 22 | 23 | -- Miek Gieben Tue, 04 Mar 2025 15:10:29 +0100 24 | 25 | libpam-unixsock (0.5.3) unstable; urgency=medium 26 | 27 | * New upstream release - 28 | 29 | -- Miek Gieben Tue, 04 Mar 2025 11:49:01 +0100 30 | 31 | libpam-unixsock (0.5.2) unstable; urgency=medium 32 | 33 | * New upstream release 34 | 35 | -- Miek Gieben Tue, 04 Mar 2025 11:35:50 +0100 36 | 37 | libpam-unixsock (0.5.1) unstable; urgency=medium 38 | 39 | * New upstream release 40 | 41 | -- Miek Gieben Mon, 03 Mar 2025 11:10:35 +0100 42 | 43 | libpam-unixsock (0.5.0) stable; urgency=medium 44 | 45 | * New upstream release 46 | 47 | -- Miek Gieben Sun, 23 Feb 2025 14:21:28 +0100 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pam-unixsock 2 | 3 | pam_unixsock is PAM module to send credentials to a unix socket. See pam_unixsock.8.md for more 4 | information. 5 | 6 | In `go/pamserve` you'll find a skeleton Go server that listen on the unix socket and echos back an 7 | OK. 8 | 9 | ## Requirements 10 | 11 | - The LibPAM development headers (libpam-dev or libpam0g-dev) 12 | - A PAM-based system (currently only tested on Linux) 13 | 14 | ## Source Install 15 | 16 | make clean 17 | make all 18 | 19 | And then copy it to: 20 | 21 | cp pam_unixsock.so /lib/security 22 | 23 | Or for Ubuntu 24 | 25 | cp pam_unixsock.so /usr/lib/x86_64-linux-gnu/security 26 | 27 | ## Testing 28 | 29 | Create a fake pam service called `unixsock`: 30 | 31 | ``` 32 | % cat /etc/pam.d/unixsock 33 | #%PAM-1.0 34 | auth required pam_unixsock.so 35 | ``` 36 | 37 | Open a reader on the socket: 38 | 39 | ``` 40 | # nc -lU /var/run/pam_unix.sock 41 | ``` 42 | 43 | Use pamtester to authenticate yourself; you can just use a fake password here. 44 | 45 | ``` 46 | % sudo pamtester unixsock $USER authenticate 47 | ``` 48 | 49 | ## License 50 | 51 | Copyright (c) 2007, 2013 Jamieson Becker 52 | 53 | All rights reserved. This package is free software, licensed under the GNU 54 | General Public License (GPL), version 2 or later. 55 | 56 | No warranty is provided for this software. Please see the complete terms of 57 | the GNU General Public License for details or contact the author. 58 | 59 | Later versions Copyright (c) 2025 Miek Gieben 60 | -------------------------------------------------------------------------------- /go/pamserve/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "context" 7 | "fmt" 8 | "io" 9 | "log" 10 | "net" 11 | "os" 12 | "os/signal" 13 | "syscall" 14 | ) 15 | 16 | const socketPath = "/var/run/pam_unix.sock" 17 | 18 | func main() { 19 | if err := os.RemoveAll(socketPath); err != nil { 20 | log.Fatalf("Failed to remove existing socket file: %v", err) 21 | } 22 | 23 | listener, err := net.Listen("unix", socketPath) 24 | if err != nil { 25 | log.Fatalf("Failed to listen on Unix socket: %v", err) 26 | } 27 | defer listener.Close() 28 | os.Chmod(socketPath, 0o600) // slight race between getting the socket and setting perms... 29 | 30 | fmt.Printf("Server is listening on Unix socket: %s\n", socketPath) 31 | 32 | sigCh := make(chan os.Signal, 1) 33 | signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) 34 | ctx, cancel := context.WithCancel(context.Background()) 35 | go func() { 36 | <-sigCh 37 | fmt.Println("\nShutting down server...") 38 | cancel() 39 | listener.Close() 40 | os.Remove(socketPath) 41 | os.Exit(0) 42 | }() 43 | 44 | // Accept incoming connections 45 | for { 46 | select { 47 | case <-ctx.Done(): 48 | break 49 | default: 50 | } 51 | conn, err := listener.Accept() 52 | if err != nil { 53 | log.Printf("Failed to accept connection: %v", err) 54 | continue 55 | } 56 | go handle(conn) 57 | } 58 | } 59 | 60 | // handle reads data from the connection and processes it. 61 | func handle(conn net.Conn) { 62 | // we do a single read for the data and than "handle" it 63 | buf := make([]byte, 1024) 64 | n, err := conn.Read(buf) 65 | if err != nil { 66 | if err == io.EOF { 67 | log.Printf("Client close connection") 68 | } 69 | log.Printf("Error reading from connection: %v", err) 70 | } 71 | process(conn, buf[:n]) 72 | } 73 | 74 | // process handles the received data. 75 | func process(conn net.Conn, data []byte) { 76 | defer conn.Close() 77 | 78 | scanner := bufio.NewScanner(bytes.NewReader(data)) 79 | i := 0 80 | pam := PamUnixSock{} 81 | for scanner.Scan() { 82 | switch i { 83 | case 0: 84 | pam.username = scanner.Text() 85 | case 1: 86 | pam.service = scanner.Text() 87 | case 2: 88 | pam.prompt = scanner.Text() 89 | } 90 | i++ 91 | } 92 | 93 | log.Printf("Seen: %s\n", pam) 94 | 95 | ok := []byte("1\n") 96 | 97 | _, err := conn.Write(ok) 98 | if err != nil { 99 | log.Printf("Failed to write to connection: %v", err) 100 | } 101 | } 102 | 103 | type PamUnixSock struct { 104 | username string 105 | service string 106 | prompt string 107 | } 108 | 109 | func (p PamUnixSock) String() string { 110 | return fmt.Sprintf("user %q - service %s - prompt %q", p.username, p.service, p.prompt) 111 | } 112 | -------------------------------------------------------------------------------- /pam_unixsock.8.md: -------------------------------------------------------------------------------- 1 | %%% 2 | title="pam_unixsock 8" 3 | area="Linux-PAM Manual" 4 | date=2025-03-02 5 | [[author]] 6 | fullname="Miek Gieben" 7 | %%% 8 | 9 | # NAME 10 | 11 | pam_unixsock - PAM module to send credentials to a Unix socket 12 | 13 | # Synopsis 14 | 15 | **pam_unixsock.so** [**hidden**] [**failopen**] [**timeout**] [**debug**] [*PROMPT*] 16 | 17 | # Description 18 | 19 | This is a pluggable authentication module (PAM) that redirects the credentials to a local Unix 20 | socket. The server listening on that socket is then free to do many more complex things, because 21 | it's free from the calling process' address space. The Unix socket defaults to 22 | `/var/run/pam_unix.sock`. The protocol is described below and is fairly simplistic. If _PROMPT_ is 23 | given, the text is used to prompt the user for another (2FA) authentication token. This module only 24 | implements the _auth_ module. 25 | 26 | If the user authenticated with SSH and `SSH_AUTH_INFO_0` contains a security key (public key starts with `sk-` 27 | and ends in `@openssh.com`; everything is side stepped and the user is allowed without prompting for a second factor. 28 | 29 | # Options 30 | 31 | **debug** 32 | : log debug information with `syslog(3)`. 33 | 34 | **timeout** 35 | : set the timeout in seconds for how long to wait for a response from the server, the default is `timeout=5`. 36 | 37 | **hidden** 38 | : when prompting for another authentication token, hide the input. 39 | 40 | **failopen** 41 | : when set ignore failures to _connect_ to the Unix socket and returns PAM_SUCCESS, when a timeout is returned 42 | but an upstream server that checkcs the second factor any timeout will be ignored and PAM_SUCCESS will be returned. 43 | 44 | # Protocol 45 | 46 | **pam_unixsock** implements an extremely simple socket protocol whereby it passes an username, the 47 | PAM module and service, the second token (i.e. **PROMPT**), and the environment variable 48 | `SSH_AUTH_INFO_0` (separated by new lines) to the Unix socket and then your server simply replies with a 0, 1 or 49 | 2 when there was a timeout. 50 | 51 | [pam_unixsock] john_smith\n 52 | [pam_unixsock] \n 53 | [pam_unixsock] \n 54 | [pam_unixsock] \n 55 | [pam_unixsock] \n 56 | [your server] 1\n 57 | 58 | If your server answers within `timeout` with a `1` you are considered authenticated, `0` is not authenticated, and 59 | 2 boils down to "do not know". 60 | 61 | **pam_module** 62 | : this will be a string like "auth", or "passwd", etc. 63 | 64 | **pam_service** 65 | : name of the calling process as given to PAM, i.e. "sshd". 66 | 67 | **prompt** 68 | : the input that the user provided for **PROMPT** 69 | 70 | **env** 71 | : the contents of the environment variable `SSH_AUTH_INFO_0` (this comes from OpenSSH), or empty if 72 | it's not found. 73 | 74 | # Configuration 75 | 76 | With Ubuntu (24.04), in `/etc/pam.d/sshd`: 77 | 78 | # Standard Un*x authentication. 79 | @include common-auth 80 | 81 | # add this line 82 | auth required pam_unixsock.so debug Enter 2FA token: 83 | 84 | If you want to pace the 2fa rollout, you can use pam_succeed_if.so, to skip (success=1) 2fa module 85 | when the user is not in the 2fa group. 86 | 87 | auth [success=1] required pam_succeed_if.so user notingroup 2fa 88 | auth required pam_unixsock.so debug Enter 2FA token: 89 | 90 | ## SSH 91 | 92 | In the `sshd` configuration be sure to add: 93 | 94 | ``` 95 | KbdInteractiveAuthentication yes 96 | UsePAM yes 97 | ``` 98 | 99 | Note that with public key authentication this is bypassed, and you log in without being asked for a 100 | second token. 101 | 102 | # Notes 103 | 104 | In Fedora the socket can't be written to by sshd because selinux does not allow it. 105 | 106 | If you get the error when using `sshd`: 107 | 108 | pam_unixsock(sshd:auth): conv->conv returned error: Conversation error 109 | 110 | Be sure to have enabled `ChallengeResponseAuthentication yes` in the sshd configuration. 111 | 112 | # Author 113 | -------------------------------------------------------------------------------- /pam_unixsock.8: -------------------------------------------------------------------------------- 1 | .\" Generated by Mmark Markdown Processor - mmark.miek.nl 2 | .TH "PAM_UNIXSOCK" 8 "March 2025" "Linux-PAM Manual" "" 3 | 4 | .SH "NAME" 5 | .PP 6 | pam_unixsock \- PAM module to send credentials to a Unix socket 7 | 8 | .SH "SYNOPSIS" 9 | .PP 10 | \fBpam_unixsock.so\fP [\fBhidden\fP] [\fBfailopen\fP] [\fBtimeout\fP] [\fBdebug\fP] [\fIPROMPT\fP] 11 | 12 | .SH "DESCRIPTION" 13 | .PP 14 | This is a pluggable authentication module (PAM) that redirects the credentials to a local Unix 15 | socket. The server listening on that socket is then free to do many more complex things, because 16 | it's free from the calling process' address space. The Unix socket defaults to 17 | \fB\fC/var/run/pam_unix.sock\fR. The protocol is described below and is fairly simplistic. If \fIPROMPT\fP is 18 | given, the text is used to prompt the user for another (2FA) authentication token. This module only 19 | implements the \fIauth\fP module. 20 | 21 | .PP 22 | If the user authenticated with SSH and \fB\fCSSH_AUTH_INFO_0\fR contains a security key (public key starts with \fB\fCsk-\fR 23 | and ends in \fB\fC@openssh.com\fR; everything is side stepped and the user is allowed without prompting for a second factor. 24 | 25 | .SH "OPTIONS" 26 | .TP 27 | \fBdebug\fP 28 | log debug information with \fB\fCsyslog(3)\fR. 29 | .TP 30 | \fBtimeout\fP 31 | set the timeout in seconds for how long to wait for a response from the server, the default is \fB\fCtimeout=5\fR. 32 | .TP 33 | \fBhidden\fP 34 | when prompting for another authentication token, hide the input. 35 | .TP 36 | \fBfailopen\fP 37 | when set ignore failures to \fIconnect\fP to the Unix socket and returns PAM_SUCCESS. 38 | 39 | 40 | .SH "PROTOCOL" 41 | .PP 42 | \fBpam_unixsock\fP implements an extremely simple socket protocol whereby it passes an username, the 43 | PAM module and service, the second token (i.e. \fBPROMPT\fP), and the environment variable 44 | \fB\fCSSH_AUTH_INFO_0\fR (separated by new lines) to the Unix socket and then your server simply replies with a 0 or 1: 45 | 46 | .PP 47 | .RS 48 | 49 | .nf 50 | [pam\_unixsock] john\_smith\\n 51 | [pam\_unixsock] \\n 52 | [pam\_unixsock] \\n 53 | [pam\_unixsock] \\n 54 | [pam\_unixsock] \\n 55 | [your server] 1\\n 56 | 57 | .fi 58 | .RE 59 | 60 | .PP 61 | If your server answers within \fB\fCtimeout\fR with a \fB\fC1\fR you are considered authenticated. 62 | 63 | .TP 64 | \fBpam_module\fP 65 | this will be a string like "auth", or "passwd", etc. 66 | .TP 67 | \fBpam_service\fP 68 | name of the calling process as given to PAM, i.e. "sshd". 69 | .TP 70 | \fBprompt\fP 71 | the input that the user provided for \fBPROMPT\fP 72 | .TP 73 | \fBenv\fP 74 | the contents of the environment variable \fB\fCSSH_AUTH_INFO_0\fR (this comes from OpenSSH), or empty if 75 | it's not found. 76 | 77 | 78 | .SH "CONFIGURATION" 79 | .PP 80 | With Ubuntu (24.04), in \fB\fC/etc/pam.d/sshd\fR: 81 | 82 | .PP 83 | .RS 84 | 85 | .nf 86 | # Standard Un*x authentication. 87 | @include common\-auth 88 | 89 | # add this line 90 | auth required pam\_unixsock.so debug Enter 2FA token: 91 | 92 | .fi 93 | .RE 94 | 95 | .PP 96 | If you want to pace the 2fa rollout, you can use pam_succeed_if.so, to skip (success=1) 2fa module 97 | when the user is not in the 2fa group. 98 | 99 | .PP 100 | .RS 101 | 102 | .nf 103 | auth [success=1] required pam\_succeed\_if.so user notingroup 2fa 104 | auth required pam\_unixsock.so debug Enter 2FA token: 105 | 106 | .fi 107 | .RE 108 | 109 | .SH "SSH" 110 | .PP 111 | In the \fB\fCsshd\fR configuration be sure to add: 112 | 113 | .PP 114 | .RS 115 | 116 | .nf 117 | KbdInteractiveAuthentication yes 118 | UsePAM yes 119 | 120 | .fi 121 | .RE 122 | 123 | .PP 124 | Note that with public key authentication this is bypassed, and you log in without being asked for a 125 | second token. 126 | 127 | .SH "NOTES" 128 | .PP 129 | In Fedora the socket can't be written to by sshd because selinux does not allow it. 130 | 131 | .PP 132 | If you get the error when using \fB\fCsshd\fR: 133 | 134 | .PP 135 | .RS 136 | 137 | .nf 138 | pam\_unixsock(sshd:auth): conv\->conv returned error: Conversation error 139 | 140 | .fi 141 | .RE 142 | 143 | .PP 144 | Be sure to have enabled \fB\fCChallengeResponseAuthentication yes\fR in the sshd configuration. 145 | 146 | .SH "AUTHOR" 147 | .SH "" 148 | .PP 149 | Written by Miek Gieben. 150 | 151 | -------------------------------------------------------------------------------- /pam_unixsock.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #define DEFAULT_TIMEOUT 10 17 | #define SOCKET_PATH "/var/run/pam_unix.sock" 18 | 19 | /* 20 | * matchsk matches the string to see if it was a security key (sk). 21 | * Example test match: 22 | * "publickey sk-ssh-ed25519@openssh.com 23 | * AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIPkPxpwWvlgbEC6rEv15cULdMvfc3ai4fmskptv+WhmQAAAABHNzaDo=" 24 | */ 25 | bool matchsk(const char *s) { 26 | if (s == NULL || *s == '\0') { 27 | return false; 28 | } 29 | 30 | // Make a copy since strtok modifies the string 31 | char *copy = strdup(s); 32 | if (copy == NULL) { 33 | return false; 34 | } 35 | 36 | bool result = false; 37 | char *saveptr = NULL; 38 | 39 | // First token must be "publickey" 40 | char *token = strtok_r(copy, " ", &saveptr); 41 | if (token == NULL || strcmp(token, "publickey") != 0) { 42 | goto cleanup; 43 | } 44 | 45 | // Second token must start with "sk-" and end with "@openssh.com" 46 | token = strtok_r(NULL, " ", &saveptr); 47 | if (token == NULL) { 48 | goto cleanup; 49 | } 50 | 51 | size_t token_len = strlen(token); 52 | const char *prefix = "sk-"; 53 | const char *suffix = "@openssh.com"; 54 | size_t prefix_len = strlen(prefix); 55 | size_t suffix_len = strlen(suffix); 56 | 57 | // Check prefix and suffix 58 | if (strncmp(token, prefix, prefix_len) != 0) { 59 | goto cleanup; 60 | } 61 | 62 | if (token_len < suffix_len || 63 | strcmp(token + token_len - suffix_len, suffix) != 0) { 64 | goto cleanup; 65 | } 66 | 67 | // If we got here, all checks passed 68 | result = true; 69 | 70 | cleanup: 71 | free(copy); 72 | return result; 73 | } 74 | 75 | static int connect_to_socket(int timeout) { 76 | int sockfd; 77 | struct sockaddr_un addr; 78 | struct timeval tv; 79 | 80 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 81 | if (sockfd < 0) { 82 | return sockfd; 83 | } 84 | 85 | memset(&addr, 0, sizeof(addr)); 86 | addr.sun_family = AF_UNIX; 87 | strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1); 88 | 89 | tv.tv_sec = timeout; 90 | tv.tv_usec = 0; 91 | setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); 92 | setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); 93 | 94 | if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 95 | syslog(LOG_ERR, "pam_unixsock(:auth): connect to socket %s failed: %s", 96 | SOCKET_PATH, strerror(errno)); 97 | close(sockfd); 98 | return -1; 99 | } 100 | return sockfd; 101 | } 102 | 103 | static int send_credentials(int sockfd, bool debug, const char *username, 104 | const char *service, const char *module, 105 | const char *prompt_response, const char *env) { 106 | 107 | dprintf(sockfd, "%s\n%s\n%s\n%s\n%s\n", username, service, module, 108 | prompt_response ? prompt_response : "", env ? env : ""); 109 | char response; 110 | if (debug) { 111 | syslog(LOG_INFO, 112 | "pam_unixsock(%s:auth): wrote credentials to socket %s for %s", 113 | service, SOCKET_PATH, username); 114 | } 115 | if (read(sockfd, &response, 1) == 1) { 116 | if (response == '1') { 117 | if (debug) { 118 | syslog(LOG_INFO, 119 | "pam_unixsock(%s:auth): positive response from server seen " 120 | "for %s", 121 | service, username); 122 | } 123 | return PAM_SUCCESS; 124 | } 125 | if (response == '0') { 126 | if (debug) { 127 | syslog(LOG_INFO, 128 | "pam_unixsock(%s:auth): negative response from server seen " 129 | "for %s", 130 | service, username); 131 | } 132 | return PAM_AUTH_ERR; 133 | } 134 | } 135 | // if we got here, due to a timeout of the request, we can't really say 136 | // PAM_SUCCESS, because then everything would OK all requests... 137 | return PAM_SERVICE_ERR; 138 | } 139 | 140 | char *concat_with_space(const char *a, const char *b) { 141 | if (!a) { 142 | return (char *)b; 143 | } 144 | 145 | size_t la = strlen(a); 146 | size_t lb = strlen(b); 147 | size_t len = la + lb + 2; // +1 for space, +1 for null terminator 148 | 149 | char *result = malloc(len); 150 | if (!result) 151 | return NULL; 152 | 153 | snprintf(result, len, "%s %s", a, b); 154 | return result; 155 | } 156 | 157 | PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, 158 | const char **argv) { 159 | int retval; 160 | char *prompt = NULL; 161 | bool hidden = false; 162 | bool failopen = false; 163 | bool debug = false; 164 | int timeout = DEFAULT_TIMEOUT; 165 | 166 | for (int i = 0; i < argc; i++) { 167 | if (strcmp(argv[i], "hidden") == 0) { 168 | hidden = true; 169 | continue; 170 | } 171 | if (strcmp(argv[i], "debug") == 0) { 172 | debug = true; 173 | continue; 174 | } 175 | if (strncmp(argv[i], "timeout=", 8) == 0) { 176 | timeout = atoi(argv[i] + 8); 177 | continue; 178 | } 179 | if (strncmp(argv[i], "failopen", 8) == 0) { 180 | failopen = true; 181 | continue; 182 | } 183 | prompt = concat_with_space(prompt, argv[i]); 184 | } 185 | 186 | if (prompt) { 187 | prompt = concat_with_space(prompt, ""); // adds trailing space 188 | } 189 | 190 | const char *ssh_auth_info_0 = pam_getenv(pamh, "SSH_AUTH_INFO_0"); 191 | // if ssh_auth_info_0 contains the following we detected a security key, we 192 | // allow access if seen and shortcut the whole procedure: 193 | // publickey sk-ssh-ed25519@openssh.com \ 194 | // AAAAGnNrLXNzaC1lZDI1NTE5QG9wZW5zc2guY29tAAAAIPkPxpwWvlgbEC6rEv15cULdMvfc3ai4fmskptv+WhmQAAAABHNzaDo= 195 | if (matchsk(ssh_auth_info_0)) { 196 | syslog(LOG_INFO, "pam_unixsock(auth): security key seen %s", 197 | ssh_auth_info_0); 198 | return PAM_SUCCESS; 199 | } 200 | 201 | const char *username, *service, *prompt_response = ""; 202 | pam_get_user(pamh, &username, NULL); 203 | pam_get_item(pamh, PAM_SERVICE, (const void **)&service); 204 | 205 | if (prompt) { 206 | struct pam_message msg[1]; 207 | const struct pam_message *pmsg[1]; 208 | struct pam_response *resp = NULL; 209 | struct pam_conv *conv; 210 | 211 | retval = pam_get_item(pamh, PAM_CONV, (const void **)&conv); 212 | if (retval != PAM_SUCCESS) { 213 | syslog(LOG_ERR, "pam_unixsock(:auth): get conv returned error: %s", 214 | pam_strerror(pamh, retval)); 215 | return PAM_CONV_ERR; 216 | } 217 | if (!conv || !conv->conv) { 218 | syslog(LOG_ERR, "pam_unixsock(:auth): conv() function invalid"); 219 | return PAM_CONV_ERR; 220 | } 221 | 222 | pmsg[0] = &msg[0]; 223 | msg[0].msg = prompt; 224 | msg[0].msg_style = hidden ? PAM_PROMPT_ECHO_OFF : PAM_PROMPT_ECHO_ON; 225 | retval = conv->conv(1, pmsg, &resp, conv->appdata_ptr); 226 | if (retval != PAM_SUCCESS) { 227 | syslog(LOG_ERR, "pam_unixsock(:auth): conv->conv returned error: %s", 228 | pam_strerror(pamh, retval)); 229 | return PAM_CONV_ERR; 230 | } 231 | prompt_response = resp->resp; 232 | } 233 | free(prompt); 234 | 235 | int sockfd = connect_to_socket(timeout); 236 | if (sockfd < 0) { 237 | if (!failopen) { 238 | return PAM_AUTH_ERR; 239 | } 240 | return PAM_SUCCESS; 241 | } 242 | 243 | retval = send_credentials(sockfd, debug, username, service, "auth", 244 | prompt_response, ssh_auth_info_0); 245 | close(sockfd); 246 | if (retval == PAM_SERVICE_ERR && failopen) { 247 | return PAM_SUCCESS; 248 | } 249 | 250 | return retval; 251 | } 252 | 253 | PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, 254 | const char **argv) { 255 | return PAM_SUCCESS; 256 | } 257 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | 3 | Version 2, June 1991 4 | 5 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 6 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 7 | 8 | Everyone is permitted to copy and distribute verbatim copies 9 | of this license document, but changing it is not allowed. 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. 13 | 14 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. 15 | 16 | To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. 17 | 18 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. 19 | 20 | We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. 21 | 22 | Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. 23 | 24 | Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. 25 | 26 | The precise terms and conditions for copying, distribution and modification follow. 27 | 28 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 29 | 30 | 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". 31 | 32 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 33 | 34 | 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. 35 | 36 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 37 | 38 | 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: 39 | 40 | a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. 41 | b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. 42 | c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) 43 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. 44 | 45 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. 46 | 47 | In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 48 | 49 | 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: 50 | 51 | a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 52 | b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, 53 | c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) 54 | The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. 55 | 56 | If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 57 | 58 | 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 59 | 60 | 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 61 | 62 | 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 63 | 64 | 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. 65 | 66 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. 67 | 68 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. 69 | 70 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 71 | 72 | 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 73 | 74 | 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 75 | 76 | Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 77 | 78 | 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. 79 | 80 | NO WARRANTY 81 | 82 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 83 | 84 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 85 | 86 | END OF TERMS AND CONDITIONS 87 | --------------------------------------------------------------------------------