|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
* Copyright (c) 2022 Thomas Koeller. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 7 |
* 1. Redistributions of source code must retain the above copyright |
| 8 |
* notice, this list of conditions and the following disclaimer. |
| 9 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer in the |
| 11 |
* documentation and/or other materials provided with the distribution. |
| 12 |
* |
| 13 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <errno.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <stddef.h> |
| 31 |
#include <sys/socket.h> |
| 32 |
#include <sys/un.h> |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
#include "packet.h" |
| 36 |
#include "log.h" |
| 37 |
#include "misc.h" |
| 38 |
#include "auth.h" |
| 39 |
#include "authreport.h" |
| 40 |
|
| 41 |
static int sock_l = -1; |
| 42 |
|
| 43 |
void |
| 44 |
init_auth_report(const char *skt_path) |
| 45 |
{ |
| 46 |
static const size_t sbsize = 1024; |
| 47 |
|
| 48 |
struct sockaddr_un remaddr = { |
| 49 |
.sun_family = AF_UNIX, |
| 50 |
.sun_path = { 0 } |
| 51 |
}; |
| 52 |
|
| 53 |
if (skt_path == NULL || *skt_path == 0 || |
| 54 |
strcasecmp(skt_path, "none") == 0) { |
| 55 |
verbose("Authresult processing disabled"); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
strncpy(remaddr.sun_path, skt_path, sizeof remaddr.sun_path); |
| 60 |
if (*(remaddr.sun_path + sizeof remaddr.sun_path - 1) != 0) { |
| 61 |
/* Error: socket path truncated */ |
| 62 |
fatal_f("Path '%s' too long", skt_path); |
| 63 |
} |
| 64 |
|
| 65 |
sock_l = socket(PF_LOCAL, SOCK_DGRAM, 0); |
| 66 |
|
| 67 |
if (sock_l == -1) { |
| 68 |
/* Error: failed to create local socket */ |
| 69 |
fatal_f("Failed to create local socket %s: %s", |
| 70 |
skt_path, strerror(errno)); |
| 71 |
} |
| 72 |
|
| 73 |
if (setsockopt(sock_l, SOL_SOCKET, SO_SNDBUF, &sbsize, sizeof sbsize) < 0) { |
| 74 |
fatal_f("Failed to set socket buffer size:%s", |
| 75 |
strerror(errno)); |
| 76 |
} |
| 77 |
|
| 78 |
if (set_nonblock(sock_l) < 0) { |
| 79 |
fatal_f("Failed to set socket nonblocking mode"); |
| 80 |
} |
| 81 |
|
| 82 |
if (connect(sock_l, (__CONST_SOCKADDR_ARG) &remaddr, SUN_LEN(&remaddr)) < 0) { |
| 83 |
fatal_f("Failed to connect to socket %s: %s", |
| 84 |
skt_path, strerror(errno)); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
report_auth_result(struct ssh *ssh) |
| 90 |
{ |
| 91 |
char rec[100]; |
| 92 |
int n; |
| 93 |
const Authctxt *authctxt = ssh->authctxt; |
| 94 |
ssize_t nout; |
| 95 |
|
| 96 |
if (sock_l == -1) return; |
| 97 |
|
| 98 |
n = snprintf(rec, sizeof rec, AUTHRPT_REC_FMT, |
| 99 |
authctxt->valid ? AUTHRPT_RES_ACCEPT : AUTHRPT_RES_REJECT, |
| 100 |
ssh_remote_ipaddr(ssh), |
| 101 |
ssh_remote_port(ssh), |
| 102 |
authctxt->user |
| 103 |
); |
| 104 |
|
| 105 |
if (n >= sizeof rec) { |
| 106 |
error_f("Record length %d exceeds buffer size %z", |
| 107 |
n, sizeof rec); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
nout = write(sock_l, rec, (size_t) n); |
| 112 |
if (nout != n) { |
| 113 |
if (nout < 0) |
| 114 |
error_f("Communication failure:%s", strerror(errno)); |
| 115 |
else |
| 116 |
error_f("Message tuncated"); |
| 117 |
/* Is there a better way to handle this condition? */ |
| 118 |
close(sock_l); |
| 119 |
sock_l = -1; |
| 120 |
} |
| 121 |
} |