mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
dmodem-login no longer reads modemmenu's default paths (the configuration, the users file, the state directory and the PAM service), and the pppd plugin no longer accepts modemmenu-socket. docs/rename.md now says what to rename on an old installation. The bbs CI job no longer rewrites the old module path, because bbs now uses github.com/D-Modem/login. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTD8BSptdpaUWGcs65
168 lines
4.3 KiB
C
168 lines
4.3 KiB
C
/*
|
|
* Copyright (C) 2026 D-Modem Authors
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*
|
|
* dmodem-login.so - a pppd plugin: the peer's PAP login checked by
|
|
* dmodem-login, on the auth socket of the call (dmodem-login's AutoPPP runs
|
|
* pppd with it in DMODEM_LOGIN_AUTH_SOCKET, or the dmodem-login-socket option
|
|
* names it). One JSON line each way:
|
|
*
|
|
* {"user": "alice", "password": "...", "service": "ppp"}
|
|
* {"ok": true, "auth": "file", "seconds_left": 3540}
|
|
*
|
|
* The time left becomes pppd's maxconnect. PAP only: CHAP would need
|
|
* the passwords kept where they can be read back.
|
|
*
|
|
* plugin dmodem-login.so require-pap refuse-chap refuse-mschap refuse-mschap-v2
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/time.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#include <pppd/pppd.h>
|
|
|
|
char pppd_version[] = VERSION;
|
|
|
|
static char *sock_path;
|
|
static int logged_in;
|
|
|
|
static option_t options[] = {
|
|
{ .name = "dmodem-login-socket", .type = o_string, .addr = &sock_path,
|
|
.description = "dmodem-login's auth socket (default: $DMODEM_LOGIN_AUTH_SOCKET)" },
|
|
{ .name = NULL }
|
|
};
|
|
|
|
/* the socket's path: the option, else dmodem-login's environment */
|
|
static const char *path(void)
|
|
{
|
|
return sock_path && *sock_path ? sock_path : getenv("DMODEM_LOGIN_AUTH_SOCKET");
|
|
}
|
|
|
|
/* PAP logins come here when there is a socket */
|
|
static int check(void)
|
|
{
|
|
return path() != NULL;
|
|
}
|
|
|
|
/* s as a JSON string into out (size n); -1 if it does not fit */
|
|
static int json_string(char *out, size_t n, const char *s)
|
|
{
|
|
size_t o = 0;
|
|
if (o + 1 >= n)
|
|
return -1;
|
|
out[o++] = '"';
|
|
for (; *s; s++) {
|
|
unsigned char c = *s;
|
|
char esc[8];
|
|
const char *add = esc;
|
|
if (c == '"' || c == '\\')
|
|
snprintf(esc, sizeof(esc), "\\%c", c);
|
|
else if (c < 0x20)
|
|
snprintf(esc, sizeof(esc), "\\u%04x", c);
|
|
else {
|
|
esc[0] = c;
|
|
esc[1] = 0;
|
|
}
|
|
if (o + strlen(add) + 2 >= n)
|
|
return -1;
|
|
memcpy(out + o, add, strlen(add));
|
|
o += strlen(add);
|
|
}
|
|
out[o++] = '"';
|
|
out[o] = 0;
|
|
return 0;
|
|
}
|
|
|
|
/* the request to the socket, its reply line into reply; -1 on failure */
|
|
static int ask(const char *user, const char *passwd, char *reply, size_t n)
|
|
{
|
|
struct sockaddr_un sun = { .sun_family = AF_UNIX };
|
|
struct timeval tv = { .tv_sec = 60 };
|
|
char u[1024], p[1024], req[2200];
|
|
size_t got = 0;
|
|
int fd, len;
|
|
|
|
if (strlen(path()) >= sizeof(sun.sun_path) ||
|
|
json_string(u, sizeof(u), user) || json_string(p, sizeof(p), passwd))
|
|
return -1;
|
|
strcpy(sun.sun_path, path());
|
|
len = snprintf(req, sizeof(req),
|
|
"{\"user\":%s,\"password\":%s,\"service\":\"ppp\"}\n", u, p);
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
if (fd < 0)
|
|
return -1;
|
|
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
|
if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) ||
|
|
write(fd, req, len) != len) {
|
|
warn("dmodem-login: %s: %m", path());
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
while (got + 1 < n) {
|
|
ssize_t r = read(fd, reply + got, n - 1 - got);
|
|
if (r <= 0)
|
|
break;
|
|
got += r;
|
|
if (memchr(reply, '\n', got))
|
|
break;
|
|
}
|
|
close(fd);
|
|
reply[got] = 0;
|
|
return got ? 0 : -1;
|
|
}
|
|
|
|
/* the PAP login: 1 right, 0 wrong (no fallback to pap-secrets) */
|
|
static int auth(char *user, char *passwd, char **msgp,
|
|
struct wordlist **paddrs, struct wordlist **popts)
|
|
{
|
|
char reply[512], *at;
|
|
|
|
(void)msgp;
|
|
(void)paddrs;
|
|
(void)popts;
|
|
if (ask(user, passwd, reply, sizeof(reply))) {
|
|
warn("dmodem-login: no answer for %s", user);
|
|
return 0;
|
|
}
|
|
if (!strstr(reply, "\"ok\":true")) {
|
|
notice("dmodem-login: login %s refused", user);
|
|
return 0;
|
|
}
|
|
at = strstr(reply, "\"seconds_left\":");
|
|
if (at) {
|
|
long secs = strtol(at + strlen("\"seconds_left\":"), NULL, 10);
|
|
/* the call's time limit: the link ends with it */
|
|
if (secs >= 0) {
|
|
maxconnect = secs > 0 ? (int)secs : 1;
|
|
info("dmodem-login: %s has %ld s", user, secs);
|
|
}
|
|
}
|
|
logged_in = 1;
|
|
return 1;
|
|
}
|
|
|
|
/* the peer's address, once logged in: as pppd's options give it (put
|
|
local:remote on pppd's command line, so that the peer cannot pick
|
|
one) */
|
|
static int address(u_int32_t addr)
|
|
{
|
|
(void)addr;
|
|
return logged_in ? 1 : -1;
|
|
}
|
|
|
|
/* pppd calls this when it loads the plugin: its option, and the PAP and
|
|
address hooks */
|
|
void plugin_init(void)
|
|
{
|
|
add_options(options);
|
|
pap_check_hook = check;
|
|
pap_auth_hook = auth;
|
|
allowed_address_hook = address;
|
|
info("dmodem-login: PAP logins checked by dmodem-login");
|
|
}
|