]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/basic_auth/SMB/basic_smb_auth.cc
Enable source-formatting tools to collapse multiple whitelines in the source to one.
[thirdparty/squid.git] / helpers / basic_auth / SMB / basic_smb_auth.cc
1 /*
2 * basic_smb_auth - SMB proxy authentication module
3 * Copyright (C) 1998 Richard Huveneers <richard@hekkihek.hacom.nl>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * SQUID Web Proxy Cache http://www.squid-cache.org/
18 * ----------------------------------------------------------
19 *
20 * Squid is the result of efforts by numerous individuals from
21 * the Internet community; see the CONTRIBUTORS file for full
22 * details. Many organizations have provided support for Squid's
23 * development; see the SPONSORS file for full details. Squid is
24 * Copyrighted (C) 2001 by the Regents of the University of
25 * California; see the COPYRIGHT file for full details. Squid
26 * incorporates software developed and/or copyrighted by other
27 * sources; see the CREDITS file for full details.
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
42 */
43 #include "squid.h"
44 #include "helpers/defines.h"
45 #include "rfc1738.h"
46 #include "util.h"
47
48 #if HAVE_STDIO_H
49 #include <stdio.h>
50 #endif
51 #if HAVE_STRING_H
52 #include <string.h>
53 #endif
54
55
56 #define NMB_UNICAST 1
57 #define NMB_BROADCAST 2
58
59 struct SMBDOMAIN {
60 const char *name; /* domain name */
61 const char *sname; /* match this with user input */
62 const char *passthrough; /* pass-through authentication */
63 const char *nmbaddr; /* name service address */
64 int nmbcast; /* broadcast or unicast */
65 char *authshare; /* share name of auth file */
66 const char *authfile; /* pathname of auth file */
67 struct SMBDOMAIN *next; /* linked list */
68 };
69
70 struct SMBDOMAIN *firstdom = NULL;
71 struct SMBDOMAIN *lastdom = NULL;
72
73 /*
74 * escape the backslash character, since it has a special meaning
75 * to the read command of the bourne shell.
76 */
77
78 void
79 print_esc(FILE * p, char *s)
80 {
81 char buf[HELPER_INPUT_BUFFER];
82 char *t;
83 int i = 0;
84
85 for (t = s; *t != '\0'; ++t) {
86 if (i > HELPER_INPUT_BUFFER-2) {
87 buf[i] = '\0';
88 (void) fputs(buf, p);
89 i = 0;
90 }
91 if (*t == '\\')
92 buf[i++] = '\\';
93
94 buf[i] = *t;
95 ++i;
96 }
97
98 if (i > 0) {
99 buf[i] = '\0';
100 (void) fputs(buf, p);
101 }
102 }
103
104 int
105 main(int argc, char *argv[])
106 {
107 int i;
108 char buf[HELPER_INPUT_BUFFER];
109 struct SMBDOMAIN *dom;
110 char *s;
111 char *user;
112 char *pass;
113 char *domname;
114 FILE *p;
115 const char *shcmd;
116
117 /* make standard output line buffered */
118 if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
119 return 1;
120
121 /* parse command line arguments */
122 for (i = 1; i < argc; ++i) {
123 if (strcmp(argv[i], "-d") == 0) {
124 debug_enabled = 1;
125 continue;
126 }
127 /* the next options require an argument */
128 if (i + 1 == argc)
129 break;
130
131 if (strcmp(argv[i], "-W") == 0) {
132 if ((dom = (struct SMBDOMAIN *) malloc(sizeof(struct SMBDOMAIN))) == NULL)
133 return 1;
134
135 dom->name = dom->sname = argv[++i];
136 dom->passthrough = "";
137 dom->nmbaddr = "";
138 dom->nmbcast = NMB_BROADCAST;
139 dom->authshare = (char *)"NETLOGON";
140 dom->authfile = "proxyauth";
141 dom->next = NULL;
142
143 /* append to linked list */
144 if (lastdom != NULL)
145 lastdom->next = dom;
146 else
147 firstdom = dom;
148
149 lastdom = dom;
150 continue;
151 }
152 if (strcmp(argv[i], "-w") == 0) {
153 if (lastdom != NULL)
154 lastdom->sname = argv[++i];
155 continue;
156 }
157 if (strcmp(argv[i], "-P") == 0) {
158 if (lastdom != NULL)
159 lastdom->passthrough = argv[++i];
160 continue;
161 }
162 if (strcmp(argv[i], "-B") == 0) {
163 if (lastdom != NULL) {
164 lastdom->nmbaddr = argv[++i];
165 lastdom->nmbcast = NMB_BROADCAST;
166 }
167 continue;
168 }
169 if (strcmp(argv[i], "-U") == 0) {
170 if (lastdom != NULL) {
171 lastdom->nmbaddr = argv[++i];
172 lastdom->nmbcast = NMB_UNICAST;
173 }
174 continue;
175 }
176 if (strcmp(argv[i], "-S") == 0) {
177 if (lastdom != NULL) {
178 if ((lastdom->authshare = xstrdup(argv[++i])) == NULL)
179 return 1;
180
181 /* convert backslashes to forward slashes */
182 for (s = lastdom->authshare; *s != '\0'; ++s)
183 if (*s == '\\')
184 *s = '/';
185
186 /* strip leading forward slash from share name */
187 if (*lastdom->authshare == '/')
188 ++lastdom->authshare;
189
190 if ((s = strchr(lastdom->authshare, '/')) != NULL) {
191 *s = '\0';
192 lastdom->authfile = s + 1;
193 }
194 }
195 continue;
196 }
197 }
198
199 shcmd = debug_enabled ? HELPERSCRIPT : HELPERSCRIPT " > /dev/null 2>&1";
200
201 while (fgets(buf, HELPER_INPUT_BUFFER, stdin) != NULL) {
202
203 if ((s = strchr(buf, '\n')) == NULL)
204 continue;
205 *s = '\0';
206
207 if ((s = strchr(buf, ' ')) == NULL) {
208 SEND_ERR("");
209 continue;
210 }
211 *s = '\0';
212
213 user = buf;
214 pass = s + 1;
215 domname = NULL;
216
217 rfc1738_unescape(user);
218 rfc1738_unescape(pass);
219
220 if ((s = strchr(user, '\\')) != NULL) {
221 *s = '\0';
222 domname = user;
223 user = s + 1;
224 }
225 /* match domname with linked list */
226 if (domname != NULL && strlen(domname) > 0) {
227 for (dom = firstdom; dom != NULL; dom = dom->next)
228 if (strcasecmp(dom->sname, domname) == 0)
229 break;
230 } else
231 dom = firstdom;
232
233 if (dom == NULL) {
234 SEND_ERR("");
235 continue;
236 }
237 if ((p = popen(shcmd, "w")) == NULL) {
238 SEND_ERR("");
239 continue;
240 }
241 (void) fprintf(p, "%s\n", dom->name);
242 (void) fprintf(p, "%s\n", dom->passthrough);
243 (void) fprintf(p, "%s\n", dom->nmbaddr);
244 (void) fprintf(p, "%d\n", dom->nmbcast);
245 (void) fprintf(p, "%s\n", dom->authshare);
246 (void) fprintf(p, "%s\n", dom->authfile);
247 (void) fprintf(p, "%s\n", user);
248 /* the password can contain special characters */
249 print_esc(p, pass);
250 (void) fputc('\n', p);
251 (void) fflush(p);
252
253 if (pclose(p) == 0)
254 SEND_OK("");
255 else
256 SEND_ERR("");
257 } /* while (1) */
258 return 0;
259 }