~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to helpers/basic_auth/SMB/smb_auth.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  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
 
 
44
#include <stdio.h>
 
45
#include <stdlib.h>
 
46
#include <string.h>
 
47
 
 
48
#include "util.h"
 
49
 
 
50
#define BUFSIZE                 256
 
51
#define NMB_UNICAST             1
 
52
#define NMB_BROADCAST   2
 
53
 
 
54
struct SMBDOMAIN {
 
55
    char *name;                 /* domain name */
 
56
    char *sname;                /* match this with user input */
 
57
    char *passthrough;          /* pass-through authentication */
 
58
    char *nmbaddr;              /* name service address */
 
59
    int nmbcast;                /* broadcast or unicast */
 
60
    char *authshare;            /* share name of auth file */
 
61
    char *authfile;             /* pathname of auth file */
 
62
    struct SMBDOMAIN *next;     /* linked list */
 
63
};
 
64
 
 
65
struct SMBDOMAIN *firstdom = NULL;
 
66
struct SMBDOMAIN *lastdom = NULL;
 
67
 
 
68
/*
 
69
 * escape the backslash character, since it has a special meaning
 
70
 * to the read command of the bourne shell.
 
71
 */
 
72
 
 
73
void
 
74
print_esc(FILE * p, char *s)
 
75
{
 
76
    char buf[256];
 
77
    char *t;
 
78
    int i = 0;
 
79
 
 
80
    for (t = s; *t != '\0'; t++) {
 
81
        if (i > 250) {
 
82
            buf[i] = '\0';
 
83
            (void) fputs(buf, p);
 
84
            i = 0;
 
85
        }
 
86
        if (*t == '\\')
 
87
            buf[i++] = '\\';
 
88
 
 
89
        buf[i++] = *t;
 
90
    }
 
91
 
 
92
    if (i > 0) {
 
93
        buf[i] = '\0';
 
94
        (void) fputs(buf, p);
 
95
    }
 
96
}
 
97
 
 
98
int
 
99
main(int argc, char *argv[])
 
100
{
 
101
    int i;
 
102
    char buf[BUFSIZE];
 
103
    struct SMBDOMAIN *dom;
 
104
    char *s;
 
105
    char *user;
 
106
    char *pass;
 
107
    char *domname;
 
108
    FILE *p;
 
109
    int debug = 0;
 
110
    char *shcmd;
 
111
 
 
112
    /* make standard output line buffered */
 
113
    if (setvbuf(stdout, NULL, _IOLBF, 0) != 0)
 
114
        return 1;
 
115
 
 
116
    /* parse command line arguments */
 
117
    for (i = 1; i < argc; i++) {
 
118
        if (strcmp(argv[i], "-d") == 0) {
 
119
            debug = 1;
 
120
            continue;
 
121
        }
 
122
        /* the next options require an argument */
 
123
        if (i + 1 == argc)
 
124
            break;
 
125
 
 
126
        if (strcmp(argv[i], "-W") == 0) {
 
127
            if ((dom = (struct SMBDOMAIN *) malloc(sizeof(struct SMBDOMAIN))) == NULL)
 
128
                          return 1;
 
129
 
 
130
            dom->name = dom->sname = argv[++i];
 
131
            dom->passthrough = "";
 
132
            dom->nmbaddr = "";
 
133
            dom->nmbcast = NMB_BROADCAST;
 
134
            dom->authshare = "NETLOGON";
 
135
            dom->authfile = "proxyauth";
 
136
            dom->next = NULL;
 
137
 
 
138
            /* append to linked list */
 
139
            if (lastdom != NULL)
 
140
                lastdom->next = dom;
 
141
            else
 
142
                firstdom = dom;
 
143
 
 
144
            lastdom = dom;
 
145
            continue;
 
146
        }
 
147
        if (strcmp(argv[i], "-w") == 0) {
 
148
            if (lastdom != NULL)
 
149
                lastdom->sname = argv[++i];
 
150
            continue;
 
151
        }
 
152
        if (strcmp(argv[i], "-P") == 0) {
 
153
            if (lastdom != NULL)
 
154
                lastdom->passthrough = argv[++i];
 
155
            continue;
 
156
        }
 
157
        if (strcmp(argv[i], "-B") == 0) {
 
158
            if (lastdom != NULL) {
 
159
                lastdom->nmbaddr = argv[++i];
 
160
                lastdom->nmbcast = NMB_BROADCAST;
 
161
            }
 
162
            continue;
 
163
        }
 
164
        if (strcmp(argv[i], "-U") == 0) {
 
165
            if (lastdom != NULL) {
 
166
                lastdom->nmbaddr = argv[++i];
 
167
                lastdom->nmbcast = NMB_UNICAST;
 
168
            }
 
169
            continue;
 
170
        }
 
171
        if (strcmp(argv[i], "-S") == 0) {
 
172
            if (lastdom != NULL) {
 
173
                if ((lastdom->authshare = strdup(argv[++i])) == NULL)
 
174
                    return 1;
 
175
 
 
176
                /* convert backslashes to forward slashes */
 
177
                for (s = lastdom->authshare; *s != '\0'; s++)
 
178
                    if (*s == '\\')
 
179
                        *s = '/';
 
180
 
 
181
                /* strip leading forward slash from share name */
 
182
                if (*lastdom->authshare == '/')
 
183
                    lastdom->authshare++;
 
184
 
 
185
                if ((s = strchr(lastdom->authshare, '/')) != NULL) {
 
186
                    *s = '\0';
 
187
                    lastdom->authfile = s + 1;
 
188
                }
 
189
            }
 
190
            continue;
 
191
        }
 
192
    }
 
193
 
 
194
    shcmd = debug ? HELPERSCRIPT : HELPERSCRIPT " > /dev/null 2>&1";
 
195
 
 
196
    /* pass to helper script */
 
197
    if (putenv("SAMBAPREFIX=" SAMBAPREFIX) != 0)
 
198
        return 1;
 
199
 
 
200
    while (1) {
 
201
        if (fgets(buf, BUFSIZE, stdin) == NULL)
 
202
            break;
 
203
 
 
204
        if ((s = strchr(buf, '\n')) == NULL)
 
205
            continue;
 
206
        *s = '\0';
 
207
 
 
208
        if ((s = strchr(buf, ' ')) == NULL) {
 
209
            (void) printf("ERR\n");
 
210
            continue;
 
211
        }
 
212
        *s = '\0';
 
213
 
 
214
        user = buf;
 
215
        pass = s + 1;
 
216
        domname = NULL;
 
217
 
 
218
        rfc1738_unescape(user);
 
219
        rfc1738_unescape(pass);
 
220
 
 
221
        if ((s = strchr(user, '\\')) != NULL) {
 
222
            *s = '\0';
 
223
            domname = user;
 
224
            user = s + 1;
 
225
        }
 
226
        /* match domname with linked list */
 
227
        if (domname != NULL && strlen(domname) > 0) {
 
228
            for (dom = firstdom; dom != NULL; dom = dom->next)
 
229
                if (strcasecmp(dom->sname, domname) == 0)
 
230
                    break;
 
231
        } else
 
232
            dom = firstdom;
 
233
 
 
234
        if (dom == NULL) {
 
235
            (void) printf("ERR\n");
 
236
            continue;
 
237
        }
 
238
        if ((p = popen(shcmd, "w")) == NULL) {
 
239
            (void) printf("ERR\n");
 
240
            continue;
 
241
        }
 
242
        (void) fprintf(p, "%s\n", dom->name);
 
243
        (void) fprintf(p, "%s\n", dom->passthrough);
 
244
        (void) fprintf(p, "%s\n", dom->nmbaddr);
 
245
        (void) fprintf(p, "%d\n", dom->nmbcast);
 
246
        (void) fprintf(p, "%s\n", dom->authshare);
 
247
        (void) fprintf(p, "%s\n", dom->authfile);
 
248
        (void) fprintf(p, "%s\n", user);
 
249
        /* the password can contain special characters */
 
250
        print_esc(p, pass);
 
251
        (void) fputc('\n', p);
 
252
        (void) fflush(p);
 
253
 
 
254
        if (pclose(p) == 0)
 
255
            (void) printf("OK\n");
 
256
        else
 
257
            (void) printf("ERR\n");
 
258
 
 
259
    }                           /* while (1) */
 
260
    return 0;
 
261
}