Skip to content

Commit 2626573

Browse files
authored
[TACACS] Send remote address in TACACS+ authorization message. (#12190)
Send remote address in TACACS+ authorization message. #### Why I did it TACACS+ authorization message not send remote address to server side. #### How I did it Send remote address in TACACS+ authorization message. #### How to verify it Pass all E2E test. Create new test case to validate remote address been send to server side. #### Which release branch to backport (provide reason below if selected) <!-- - Note we only backport fixes to a release branch, *not* features! - Please also provide a reason for the backporting below. - e.g. - [x] 202006 --> - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 - [ ] 202205 #### Description for the changelog Send remote address in TACACS+ authorization message. #### Ensure to add label/tag for the feature raised. example - [PR#2174](sonic-net/sonic-utilities#2174) where, Generic Config and Update feature has been labelled as GCU. #### Link to config_db schema for YANG module changes <!-- Provide a link to config_db schema for the table for which YANG model is defined Link should point to correct section on https://github.com/Azure/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md --> #### A picture of a cute animal (not mandatory but encouraged)
1 parent 2b7a3ac commit 2626573

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
From ee47eb11cbfc37600a59f06ae153da5c2c486fea Mon Sep 17 00:00:00 2001
2+
From: liuh-80 <liuh@microsoft.com>
3+
Date: Tue, 25 Oct 2022 10:34:08 +0800
4+
Subject: [PATCH] Send remote address in TACACS+ authorization message.
5+
6+
---
7+
nss_tacplus.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++-
8+
1 file changed, 76 insertions(+), 1 deletion(-)
9+
10+
diff --git a/nss_tacplus.c b/nss_tacplus.c
11+
index 2de00a6..048745a 100644
12+
--- a/nss_tacplus.c
13+
+++ b/nss_tacplus.c
14+
@@ -33,12 +33,20 @@
15+
#include <ctype.h>
16+
#include <netdb.h>
17+
#include <nss.h>
18+
+#include <limits.h>
19+
20+
#include <libtac/libtac.h>
21+
22+
#define MIN_TACACS_USER_PRIV (1)
23+
#define MAX_TACACS_USER_PRIV (15)
24+
25+
+#define GET_ENV_VARIABLE_OK 0
26+
+#define GET_ENV_VARIABLE_NOT_FOUND 1
27+
+#define GET_ENV_VARIABLE_INCORRECT_FORMAT 2
28+
+#define GET_ENV_VARIABLE_NOT_ENOUGH_BUFFER 3
29+
+#define GET_REMOTE_ADDRESS_OK 0
30+
+#define GET_REMOTE_ADDRESS_FAILED 1
31+
+
32+
static const char *nssname = "nss_tacplus"; /* for syslogs */
33+
static const char *config_file = "/etc/tacplus_nss.conf";
34+
static const char *user_conf = "/etc/tacplus_user";
35+
@@ -717,6 +725,66 @@ connect_tacacs(struct tac_attrib **attr, int srvr)
36+
return fd;
37+
}
38+
39+
+/*
40+
+ * Get environment variable first part by name and delimiters
41+
+ */
42+
+int get_environment_variable_first_part(char* dst, socklen_t size, const char* name, const char* delimiters)
43+
+{
44+
+ memset(dst, 0, size);
45+
+
46+
+ const char* variable = getenv(name);
47+
+ if (variable == NULL) {
48+
+ if (debug) {
49+
+ syslog(LOG_DEBUG, "%s: can't get environment variable %s, errno=%d", nssname, name, errno);
50+
+ }
51+
+
52+
+ return GET_ENV_VARIABLE_NOT_FOUND;
53+
+ }
54+
+
55+
+ char* context = NULL;
56+
+ char* first_part = strtok_r((char *)variable, delimiters, &context);
57+
+ if (first_part == NULL) {
58+
+ if (debug) {
59+
+ syslog(LOG_DEBUG, "%s: can't split %s by delimiters %s", nssname, variable, delimiters);
60+
+ }
61+
+
62+
+ return GET_ENV_VARIABLE_INCORRECT_FORMAT;
63+
+ }
64+
+
65+
+ int first_part_len = strlen(first_part);
66+
+ if (first_part_len >= size) {
67+
+ if (debug) {
68+
+ syslog(LOG_DEBUG, "%s: dest buffer size %d not enough for %s", nssname, size, first_part);
69+
+ }
70+
+
71+
+ return GET_ENV_VARIABLE_NOT_ENOUGH_BUFFER;
72+
+ }
73+
+
74+
+ strncpy(dst, first_part, size);
75+
+ if (debug) {
76+
+ syslog(LOG_DEBUG, "%s: remote address=%s", nssname, dst);
77+
+ }
78+
+
79+
+ return GET_ENV_VARIABLE_OK;
80+
+}
81+
+
82+
+/*
83+
+ * Get current SSH session remote address from environment variable
84+
+ */
85+
+int get_remote_address(char* dst, socklen_t size)
86+
+{
87+
+ // SSHD will create environment variable SSH_CONNECTION after user session created.
88+
+ if (get_environment_variable_first_part(dst, size, "SSH_CONNECTION", " ") == GET_ENV_VARIABLE_OK) {
89+
+ return GET_REMOTE_ADDRESS_OK;
90+
+ }
91+
+
92+
+ // Before user session created, SSHD will create environment variable SSH_CLIENT_IPADDR_PORT.
93+
+ if (get_environment_variable_first_part(dst, size, "SSH_CLIENT_IPADDR_PORT", " ") == GET_ENV_VARIABLE_OK) {
94+
+ return GET_REMOTE_ADDRESS_OK;
95+
+ }
96+
+
97+
+ return GET_REMOTE_ADDRESS_FAILED;
98+
+}
99+
100+
/*
101+
* lookup the user on a TACACS server. Returns 0 on successful lookup, else 1
102+
@@ -735,6 +803,13 @@ lookup_tacacs_user(struct pwbuf *pb)
103+
int ret = 1, done = 0;
104+
struct tac_attrib *attr;
105+
int tac_fd, srvr;
106+
+ char remote_addr[INET6_ADDRSTRLEN];
107+
+ const char* current_tty = getenv("SSH_TTY");
108+
+
109+
+ int result = get_remote_address(remote_addr, sizeof(remote_addr));
110+
+ if ((result != GET_REMOTE_ADDRESS_OK) && debug) {
111+
+ syslog(LOG_DEBUG, "%s: can't get remote address from environment variable, result=%d", nssname, result);
112+
+ }
113+
114+
for(srvr=0; srvr < tac_srv_no && !done; srvr++) {
115+
arep.msg = NULL;
116+
@@ -748,7 +823,7 @@ lookup_tacacs_user(struct pwbuf *pb)
117+
tac_ntop(tac_srv[srvr].addr->ai_addr) : "unknown", tac_fd);
118+
continue;
119+
}
120+
- ret = tac_author_send(tac_fd, pb->name, "", "", attr);
121+
+ ret = tac_author_send(tac_fd, pb->name, current_tty != NULL ? (char *)current_tty : "", remote_addr, attr);
122+
if(ret < 0) {
123+
if(debug)
124+
syslog(LOG_WARNING, "%s: TACACS+ server %s send failed (%d) for"
125+
--
126+
2.37.1.windows.1
127+

src/tacacs/nss/patch/series

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
0007-Add-support-for-TACACS-source-address.patch
88
0008-do-not-create-or-modify-local-user-if-there-is-no-pr.patch
99
0009-fix-compile-error-strncpy.patch
10+
0010-Send-remote-address-in-TACACS-authorization-message.patch

0 commit comments

Comments
 (0)