Squid Web Cache v8/master
Loading...
Searching...
No Matches
check_group.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 * This is a helper for the external ACL interface for Squid Cache
11 * Copyright (C) 2002 Rodrigo Albani de Campos (rodrigo@geekbunker.org)
12 *
13 * It reads STDIN looking for a username that matches a specified group
14 * Returns `OK' if the user belongs to the group or `ERR' otherwise, as
15 * described on http://devel.squid-cache.org/external_acl/config.html
16 * To compile this program, use:
17 *
18 * gcc -o check_group check_group.c
19 *
20 * Author: Rodrigo Albani de Campos
21 * E-Mail: rodrigo@geekbunker.org
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License along
34 * with this program; if not, see <https://www.gnu.org/licenses/>.
35 *
36 * Change Log:
37 * 2010-02-24 hno
38 * Removed group number limitation and fixed related uninitialized
39 * pointer reference (Bug #2813)
40 *
41 * Revision 1.7 2004/08/15 00:29:33 hno
42 * helper protocol changed to URL-escaped strings in Squid-3.0
43 *
44 * Revision 1.6 2002/08/12 15:48:32 hno
45 * imported strwordtok from Squid, added man page, some minor fixes
46 *
47 * Revision 1.5 2002/07/27 14:26:49 rcampos
48 * allow groups to be sent on stdin
49 *
50 * Revision 1.4 2002/04/17 01:58:48 camposr
51 * minor corrections in the getopt
52 *
53 * Revision 1.3 2002/04/17 01:43:17 camposr
54 * ready for action
55 *
56 * Revision 1.2 2002/04/17 01:32:16 camposr
57 * all main routines ready
58 *
59 * Revision 1.1 2002/04/16 05:02:32 camposr
60 * Initial revision
61 *
62 */
63#include "squid.h"
65#include "rfc1738.h"
66#include "util.h"
67
68#include <cctype>
69#include <cstring>
70#if HAVE_GRP_H
71#include <grp.h>
72#endif
73#if HAVE_UNISTD_H
74#include <unistd.h>
75#endif
76#if HAVE_PWD_H
77#include <pwd.h>
78#endif
79
80/*
81 * Verify if user's primary group matches groupname
82 * Returns 0 if user is not on the group
83 * Returns 1 otherwise
84 */
85static int
86validate_user_pw(char *username, char *groupname)
87{
88 struct passwd *p;
89 struct group *g;
90
91 if ((p = getpwnam(username)) == nullptr) {
92 /* Returns an error if user does not exist in the /etc/passwd */
93 fprintf(stderr, "ERROR: User does not exist '%s'\n", username);
94 return 0;
95 } else {
96 /* Verify if the this is the primary user group */
97 if ((g = getgrgid(p->pw_gid)) != nullptr) {
98 if ((strcmp(groupname, g->gr_name)) == 0)
99 return 1;
100 }
101 }
102
103 return 0;
104}
105
106static int
107validate_user_gr(char *username, char *groupname)
108{
109 /*
110 * Verify if the user belongs to groupname as listed in the
111 * /etc/group file
112 */
113 struct group *g;
114
115 if ((g = getgrnam(groupname)) == nullptr) {
116 fprintf(stderr, "ERROR: Group does not exist '%s'\n", groupname);
117 return 0;
118 } else {
119 while (*(g->gr_mem) != nullptr) {
120 if (strcmp(*((g->gr_mem)++), username) == 0) {
121 return 1;
122 }
123 }
124 }
125 return 0;
126}
127
128static void
129usage(char *program)
130{
131 fprintf(stderr, "Usage: %s -g group1 [-g group2 ...] [-p] [-s]\n\n",
132 program);
133 fprintf(stderr, "-g group\n");
134 fprintf(stderr,
135 " The group name or id that the user must belong in order to\n");
136 fprintf(stderr,
137 " be allowed to authenticate.\n");
138 fprintf(stderr,
139 "-p Verify primary user group as well\n");
140 fprintf(stderr,
141 "-s Strip NT domain from usernames\n");
142 fprintf(stderr,
143 "-r Strip Kerberos realm from usernames\n");
144}
145
146int
147main(int argc, char *argv[])
148{
149 char *user, *suser, *p;
150 char buf[HELPER_INPUT_BUFFER];
151 char **grents = nullptr;
152 int check_pw = 0, ch, ngroups = 0, i, j = 0, strip_dm = 0, strip_rm = 0;
153
154 /* make standard output line buffered */
155 setvbuf(stdout, nullptr, _IOLBF, 0);
156
157 /* get user options */
158 while ((ch = getopt(argc, argv, "dsrpg:")) != -1) {
159 switch (ch) {
160 case 'd':
161 debug_enabled = 1;
162 break;
163 case 's':
164 strip_dm = 1;
165 break;
166 case 'r':
167 strip_rm = 1;
168 break;
169 case 'p':
170 check_pw = 1;
171 break;
172 case 'g':
173 grents = (char**)realloc(grents, sizeof(*grents) * (ngroups+1));
174 grents[ngroups] = optarg;
175 ++ngroups;
176 break;
177 case '?':
178 if (xisprint(optopt)) {
179 fprintf(stderr, "Unknown option '-%c'.\n", optopt);
180 } else {
181 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
182 }
183 [[fallthrough]];
184 default:
185 usage(argv[0]);
186 exit(EXIT_FAILURE);
187 }
188 }
189 if (optind < argc) {
190 fprintf(stderr, "FATAL: Unknown option '%s'\n", argv[optind]);
191 usage(argv[0]);
192 exit(EXIT_FAILURE);
193 }
194 while (fgets(buf, HELPER_INPUT_BUFFER, stdin)) {
195 j = 0;
196 if ((p = strchr(buf, '\n')) == nullptr) {
197 /* too large message received.. skip and deny */
198 fprintf(stderr, "ERROR: %s: Too large: %s\n", argv[0], buf);
199 while (fgets(buf, sizeof(buf), stdin)) {
200 fprintf(stderr, "ERROR: %s: Too large..: %s\n", argv[0], buf);
201 if (strchr(buf, '\n') != nullptr)
202 break;
203 }
204 SEND_BH(HLP_MSG("Username Input too large."));
205 continue;
206 }
207 *p = '\0';
208 if ((p = strtok(buf, " ")) == nullptr) {
209 SEND_BH(HLP_MSG("No username given."));
210 continue;
211 } else {
212 user = p;
213 rfc1738_unescape(user);
214 if (strip_dm) {
215 suser = strchr(user, '\\');
216 if (!suser) suser = strchr(user, '/');
217 if (suser && suser[1]) user = suser + 1;
218 }
219 if (strip_rm) {
220 suser = strchr(user, '@');
221 if (suser) *suser = '\0';
222 }
223 /* check groups supplied by Squid */
224 while ((p = strtok(nullptr, " ")) != nullptr) {
226 if (check_pw == 1)
227 j += validate_user_pw(user, p);
228 j += validate_user_gr(user, p);
229 }
230 }
231
232 /* check groups supplied on the command line */
233 for (i = 0; i < ngroups; ++i) {
234 if (check_pw == 1) {
235 j += validate_user_pw(user, grents[i]);
236 }
237 j += validate_user_gr(user, grents[i]);
238 }
239
240 if (j > 0) {
241 SEND_OK("");
242 } else {
243 SEND_ERR("");
244 }
245 }
246 return EXIT_SUCCESS;
247}
248
#define HELPER_INPUT_BUFFER
static int validate_user_gr(char *username, char *groupname)
static int validate_user_pw(char *username, char *groupname)
int debug_enabled
Definition debug.cc:13
static void usage(void)
int optopt
Definition getopt.c:49
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition getopt.c:62
int optind
Definition getopt.c:48
char * optarg
Definition getopt.c:51
int main()
#define SEND_ERR(x)
#define SEND_OK(x)
#define HLP_MSG(text)
#define SEND_BH(x)
void rfc1738_unescape(char *url)
Definition rfc1738.cc:146
#define xisprint(x)
Definition xis.h:22