Squid Web Cache v8/master
Loading...
Searching...
No Matches
valid.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/*
10 NT_auth - Version 2.0
11
12 Modified to act as a Squid authenticator module.
13 Removed all Pike stuff.
14 Returns OK for a successful authentication, or ERR upon error.
15
16 Guido Serassio, Torino - Italy
17
18 Uses code from -
19 Antonino Iannella 2000
20 Andrew Tridgell 1997
21 Richard Sharpe 1996
22 Bill Welliver 1999
23
24 * Distributed freely under the terms of the GNU General Public License,
25 * version 2 or later. See the file COPYING for licensing details
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, see <https://www.gnu.org/licenses/>.
34*/
35
36#include "squid.h"
38#include "util.h"
39
40#include <cwchar>
41
43const char * errormsg;
44
45const char NTV_SERVER_ERROR_MSG[] = "Internal server error";
46const char NTV_GROUP_ERROR_MSG[] = "User not allowed to use this cache";
47const char NTV_LOGON_ERROR_MSG[] = "No such user or wrong password";
48const char NTV_VALID_DOMAIN_SEPARATOR[] = "\\/";
49
50/* returns 1 on success, 0 on failure */
51static int
52Valid_Group(char *UserName, char *Group)
53{
54 int result = FALSE;
55 WCHAR wszUserName[256]; // Unicode user name
56 WCHAR wszGroup[256]; // Unicode Group
57
58 LPLOCALGROUP_USERS_INFO_0 pBuf = nullptr;
59 LPLOCALGROUP_USERS_INFO_0 pTmpBuf;
60 DWORD dwLevel = 0;
61 DWORD dwFlags = LG_INCLUDE_INDIRECT;
62 DWORD dwPrefMaxLen = -1;
63 DWORD dwEntriesRead = 0;
64 DWORD dwTotalEntries = 0;
65 NET_API_STATUS nStatus;
66 DWORD i;
67 DWORD dwTotalCount = 0;
68
69 /* Convert ANSI User Name and Group to Unicode */
70
71 MultiByteToWideChar(CP_ACP, 0, UserName,
72 strlen(UserName) + 1, wszUserName,
73 sizeof(wszUserName) / sizeof(wszUserName[0]));
74 MultiByteToWideChar(CP_ACP, 0, Group,
75 strlen(Group) + 1, wszGroup, sizeof(wszGroup) / sizeof(wszGroup[0]));
76
77 /*
78 * Call the NetUserGetLocalGroups function
79 * specifying information level 0.
80 *
81 * The LG_INCLUDE_INDIRECT flag specifies that the
82 * function should also return the names of the local
83 * groups in which the user is indirectly a member.
84 */
85 nStatus = NetUserGetLocalGroups(nullptr,
86 wszUserName,
87 dwLevel,
88 dwFlags,
89 (LPBYTE *) & pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries);
90 /*
91 * If the call succeeds,
92 */
93 if (nStatus == NERR_Success) {
94 if ((pTmpBuf = pBuf) != NULL) {
95 for (i = 0; i < dwEntriesRead; ++i) {
96 if (pTmpBuf == NULL) {
97 result = FALSE;
98 break;
99 }
100 if (wcscmp(pTmpBuf->lgrui0_name, wszGroup) == 0) {
101 result = TRUE;
102 break;
103 }
104 ++pTmpBuf;
105 ++dwTotalCount;
106 }
107 }
108 } else
109 result = FALSE;
110 /*
111 * Free the allocated memory.
112 */
113 if (pBuf != NULL)
114 NetApiBufferFree(pBuf);
115 return result;
116}
117
118int
119Valid_User(char *UserName, char *Password, char *)
120{
121 int result = NTV_SERVER_ERROR;
122 size_t i;
123 char NTDomain[256];
124 char *domain_qualify = nullptr;
125 char DomainUser[256];
126 char User[256];
127
129 xstrncpy(NTDomain, UserName, sizeof(NTDomain));
130
131 for (i=0; i < strlen(NTV_VALID_DOMAIN_SEPARATOR); ++i) {
132 if ((domain_qualify = strchr(NTDomain, NTV_VALID_DOMAIN_SEPARATOR[i])) != NULL)
133 break;
134 }
135 if (domain_qualify == NULL) {
136 strcpy(User, NTDomain);
137 strcpy(NTDomain, Default_NTDomain);
138 } else {
139 strcpy(User, domain_qualify + 1);
140 domain_qualify[0] = '\0';
141 }
142 /* Log the client on to the local computer. */
143 if (!SSP_LogonUser(User, Password, NTDomain)) {
144 result = NTV_LOGON_ERROR;
146 debug("%s\n", errormsg);
147 } else {
148 result = NTV_NO_ERROR;
149 if (strcmp(NTDomain, NTV_DEFAULT_DOMAIN) == 0)
150 strcpy(DomainUser, User);
151 else {
152 strcpy(DomainUser, NTDomain);
153 strcat(DomainUser, "\\");
154 strcat(DomainUser, User);
155 }
156 if (UseAllowedGroup) {
157 if (!Valid_Group(DomainUser, NTAllowedGroup)) {
158 result = NTV_GROUP_ERROR;
160 debug("%s\n", errormsg);
161 }
162 }
163 if (UseDisallowedGroup) {
164 if (Valid_Group(DomainUser, NTDisAllowedGroup)) {
165 result = NTV_GROUP_ERROR;
167 debug("%s\n", errormsg);
168 }
169 }
170 }
171 return result;
172}
173
char * NTAllowedGroup
int UseAllowedGroup
char * NTDisAllowedGroup
int UseDisallowedGroup
void debug(const char *format,...)
Definition debug.cc:19
#define TRUE
Definition defines.h:13
#define FALSE
Definition defines.h:16
BOOL WINAPI SSP_LogonUser(PTSTR szUser, PTSTR szPassword, PTSTR szDomain)
Definition sspwin32.cc:390
#define NULL
Definition types.h:145
const char NTV_LOGON_ERROR_MSG[]
Definition valid.cc:47
const char NTV_GROUP_ERROR_MSG[]
Definition valid.cc:46
const char NTV_VALID_DOMAIN_SEPARATOR[]
Definition valid.cc:48
int Valid_User(char *UserName, char *Password, char *)
Definition valid.cc:119
const char NTV_SERVER_ERROR_MSG[]
Definition valid.cc:45
static int Valid_Group(char *UserName, char *Group)
Definition valid.cc:52
const char * errormsg
Definition valid.cc:43
char Default_NTDomain[DNLEN+1]
Definition valid.cc:42
#define NTV_SERVER_ERROR
Definition valid.h:48
#define NTV_GROUP_ERROR
Definition valid.h:49
#define NTV_DEFAULT_DOMAIN
Definition valid.h:56
#define NTV_LOGON_ERROR
Definition valid.h:50
#define NTV_NO_ERROR
Definition valid.h:47
char * xstrncpy(char *dst, const char *src, size_t n)
Definition xstring.cc:37