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

« back to all changes in this revision

Viewing changes to helpers/negotiate_auth/mswin_sspi/negotiate_auth.c

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2010-05-04 11:15:49 UTC
  • mfrom: (1.3.1 upstream)
  • mto: (20.3.1 squeeze) (21.2.1 sid)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100504111549-1apjh2g5sndki4te
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
1
2
/*
2
3
 * mswin_negotiate_auth: helper for Negotiate Authentication for Squid Cache
3
4
 *
59
60
{
60
61
    char *p = string, c;
61
62
    while ((c = *p)) {
62
 
        *p = xtoupper(c);
63
 
        p++;
 
63
        *p = xtoupper(c);
 
64
        p++;
64
65
    }
65
66
}
66
67
 
70
71
{
71
72
    char *p = string, c;
72
73
    while ((c = *p)) {
73
 
        *p = xtolower(c);
74
 
        p++;
 
74
        *p = xtolower(c);
 
75
        p++;
75
76
    }
76
77
}
77
78
 
79
80
helperfail(const char *reason)
80
81
{
81
82
#if FAIL_DEBUG
82
 
    fail_debug_enabled =1;
 
83
    fail_debug_enabled = 1;
83
84
#endif
84
85
    SEND2("BH %s", reason);
85
86
}
86
87
 
87
88
/*
88
 
  options:
89
 
  -d enable debugging.
90
 
  -v enable verbose Negotiate packet debugging.
 
89
 * options:
 
90
 * -d enable debugging.
 
91
 * -v enable verbose Negotiate packet debugging.
91
92
 */
92
93
char *my_program_name = NULL;
93
94
 
95
96
usage()
96
97
{
97
98
    fprintf(stderr,
98
 
        "Usage: %s [-d] [-v] [-h]\n"
99
 
        " -d  enable debugging.\n"
100
 
        " -v  enable verbose Negotiate packet debugging.\n"
101
 
        " -h  this message\n\n",
102
 
        my_program_name);
 
99
            "Usage: %s [-d] [-v] [-h]\n"
 
100
            " -d  enable debugging.\n"
 
101
            " -v  enable verbose Negotiate packet debugging.\n"
 
102
            " -h  this message\n\n",
 
103
            my_program_name);
103
104
}
104
105
 
105
106
 
108
109
{
109
110
    int opt, had_error = 0;
110
111
 
111
 
    opterr =0;
 
112
    opterr = 0;
112
113
    while (-1 != (opt = getopt(argc, argv, "hdv"))) {
113
 
        switch (opt) {
114
 
        case 'd':
115
 
            debug_enabled = 1;
116
 
            break;
117
 
        case 'v':
118
 
            debug_enabled = 1;
119
 
            Negotiate_packet_debug_enabled = 1;
120
 
            break;
121
 
        case 'h':
122
 
            usage();
123
 
            exit(0);
124
 
        case '?':
125
 
            opt = optopt;
126
 
            /* fall thru to default */
127
 
        default:
128
 
            fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
129
 
            usage();
130
 
            had_error = 1;
131
 
        }
 
114
        switch (opt) {
 
115
        case 'd':
 
116
            debug_enabled = 1;
 
117
            break;
 
118
        case 'v':
 
119
            debug_enabled = 1;
 
120
            Negotiate_packet_debug_enabled = 1;
 
121
            break;
 
122
        case 'h':
 
123
            usage();
 
124
            exit(0);
 
125
        case '?':
 
126
            opt = optopt;
 
127
            /* fall thru to default */
 
128
        default:
 
129
            fprintf(stderr, "unknown option: -%c. Exiting\n", opt);
 
130
            usage();
 
131
            had_error = 1;
 
132
        }
132
133
    }
133
134
    if (had_error)
134
 
        exit(1);
 
135
        exit(1);
135
136
}
136
137
 
137
138
int
142
143
    char *c, *decoded;
143
144
    int plen, status;
144
145
    int oversized = 0;
145
 
    char * ErrorMessage;
146
 
    static char cred[SSP_MAX_CRED_LEN+1];
 
146
    char *ErrorMessage;
 
147
    static char cred[SSP_MAX_CRED_LEN + 1];
147
148
    BOOL Done = FALSE;
148
149
 
149
150
try_again:
150
 
    if (fgets(buf, BUFFER_SIZE, stdin) == NULL) 
 
151
    if (fgets(buf, BUFFER_SIZE, stdin) == NULL)
151
152
        return 0;
152
153
 
153
 
    c = memchr(buf, '\n', BUFFER_SIZE); /* safer against overrun than strchr */
 
154
    c = memchr(buf, '\n', BUFFER_SIZE);         /* safer against overrun than strchr */
154
155
    if (c) {
155
 
        if (oversized) {
156
 
            helperfail("illegal request received");
157
 
            fprintf(stderr, "Illegal request received: '%s'\n", buf);
158
 
            return 1;
159
 
        }
160
 
        *c = '\0';
 
156
        if (oversized) {
 
157
            helperfail("illegal request received");
 
158
            fprintf(stderr, "Illegal request received: '%s'\n", buf);
 
159
            return 1;
 
160
        }
 
161
        *c = '\0';
161
162
    } else {
162
 
        fprintf(stderr, "No newline in '%s'\n", buf);
163
 
        oversized = 1;
164
 
        goto try_again;
 
163
        fprintf(stderr, "No newline in '%s'\n", buf);
 
164
        oversized = 1;
 
165
        goto try_again;
165
166
    }
166
167
 
167
168
    if ((strlen(buf) > 3) && Negotiate_packet_debug_enabled) {
173
174
        debug("Got '%s' from Squid\n", buf);
174
175
 
175
176
    if (memcmp(buf, "YR ", 3) == 0) {   /* refresh-request */
176
 
        /* figure out what we got */
 
177
        /* figure out what we got */
177
178
        decoded = base64_decode(buf + 3);
178
 
        /*  Note: we don't need to manage memory at this point, since
179
 
         *  base64_decode returns a pointer to static storage.
180
 
         */
181
 
        if (!decoded) {         /* decoding failure, return error */
182
 
            SEND("NA * Packet format error, couldn't base64-decode");
183
 
            return 1;
184
 
        }
185
 
        /* Obtain server blob against SSPI */
186
 
        plen = (strlen(buf) - 3) * 3 / 4;               /* we only need it here. Optimization */
 
179
        /*  Note: we don't need to manage memory at this point, since
 
180
         *  base64_decode returns a pointer to static storage.
 
181
         */
 
182
        if (!decoded) {         /* decoding failure, return error */
 
183
            SEND("NA * Packet format error, couldn't base64-decode");
 
184
            return 1;
 
185
        }
 
186
        /* Obtain server blob against SSPI */
 
187
        plen = (strlen(buf) - 3) * 3 / 4;       /* we only need it here. Optimization */
187
188
        c = (char *) SSP_MakeNegotiateBlob(decoded, plen, &Done, &status, cred);
188
189
 
189
190
        if (status == SSP_OK) {
190
191
            if (Done) {
191
 
                lc(cred);               /* let's lowercase them for our convenience */
 
192
                lc(cred);       /* let's lowercase them for our convenience */
192
193
                have_serverblob = 0;
193
194
                Done = FALSE;
194
195
                if (Negotiate_packet_debug_enabled) {
195
 
                    printf("AF %s %s\n",c,cred);
196
196
                    decoded = base64_decode(c);
197
 
                    debug("sending 'AF' %s to squid with data:\n", cred);
198
 
                    hex_dump(decoded, (strlen(c) * 3) / 4);
 
197
                    debug("sending 'AF' %s to squid with data:\n", cred);
 
198
                    if (c != NULL)
 
199
                        hex_dump(decoded, (strlen(c) * 3) / 4);
 
200
                    else
 
201
                        fprintf(stderr, "No data available.\n");
 
202
                    printf("AF %s %s\n", c, cred);
199
203
                } else
200
204
                    SEND3("AF %s %s", c, cred);
201
205
            } else {
202
206
                if (Negotiate_packet_debug_enabled) {
203
 
                    printf("TT %s\n",c);
204
207
                    decoded = base64_decode(c);
205
 
                    debug("sending 'TT' to squid with data:\n");
 
208
                    debug("sending 'TT' to squid with data:\n");
206
209
                    hex_dump(decoded, (strlen(c) * 3) / 4);
 
210
                    printf("TT %s\n", c);
207
211
                } else {
208
212
                    SEND2("TT %s", c);
209
213
                }
211
215
            }
212
216
        } else
213
217
            helperfail("can't obtain server blob");
214
 
        return 1;
 
218
        return 1;
215
219
    }
216
 
 
217
220
    if (memcmp(buf, "KK ", 3) == 0) {   /* authenticate-request */
218
221
        if (!have_serverblob) {
219
 
            helperfail("invalid server blob");
220
 
            return 1;
221
 
        }
222
 
        /* figure out what we got */
223
 
        decoded = base64_decode(buf + 3);
224
 
        /*  Note: we don't need to manage memory at this point, since
225
 
         *  base64_decode returns a pointer to static storage.
226
 
         */
227
 
        if (!decoded) {         /* decoding failure, return error */
228
 
            SEND("NA * Packet format error, couldn't base64-decode");
229
 
            return 1;
230
 
        }
231
 
 
 
222
            helperfail("invalid server blob");
 
223
            return 1;
 
224
        }
 
225
        /* figure out what we got */
 
226
        decoded = base64_decode(buf + 3);
 
227
        /*  Note: we don't need to manage memory at this point, since
 
228
         *  base64_decode returns a pointer to static storage.
 
229
         */
 
230
        if (!decoded) {         /* decoding failure, return error */
 
231
            SEND("NA * Packet format error, couldn't base64-decode");
 
232
            return 1;
 
233
        }
232
234
        /* check against SSPI */
233
 
        plen = (strlen(buf) - 3) * 3 / 4;               /* we only need it here. Optimization */
 
235
        plen = (strlen(buf) - 3) * 3 / 4;       /* we only need it here. Optimization */
234
236
        c = (char *) SSP_ValidateNegotiateCredentials(decoded, plen, &Done, &status, cred);
235
 
        
 
237
 
236
238
        if (status == SSP_ERROR) {
237
239
#if FAIL_DEBUG
238
240
            fail_debug_enabled = 1;
239
241
#endif
240
 
            FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 
 
242
            FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
241
243
                          FORMAT_MESSAGE_IGNORE_INSERTS,
242
244
                          NULL,
243
245
                          GetLastError(),
244
 
                          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   /* Default language */
245
 
                          (LPTSTR) &ErrorMessage,
 
246
                          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),    /* Default language */
 
247
                          (LPTSTR) & ErrorMessage,
246
248
                          0,
247
249
                          NULL);
248
250
            if (ErrorMessage[strlen(ErrorMessage) - 1] == '\n')
249
251
                ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
250
252
            if (ErrorMessage[strlen(ErrorMessage) - 1] == '\r')
251
253
                ErrorMessage[strlen(ErrorMessage) - 1] = '\0';
252
 
            SEND2("NA * %s", ErrorMessage);
 
254
            SEND2("NA * %s", ErrorMessage);
253
255
            LocalFree(ErrorMessage);
254
 
            return 1;
255
 
        }
256
 
 
 
256
            return 1;
 
257
        }
257
258
        if (Done) {
258
 
            lc(cred);           /* let's lowercase them for our convenience */
 
259
            lc(cred);           /* let's lowercase them for our convenience */
259
260
            have_serverblob = 0;
260
261
            Done = FALSE;
261
262
            if (Negotiate_packet_debug_enabled) {
262
 
                printf("AF %s %s\n",c,cred);
263
263
                decoded = base64_decode(c);
264
 
                debug("sending 'AF' %s to squid with data:\n", cred);
265
 
                hex_dump(decoded, (strlen(c) * 3) / 4);
 
264
                debug("sending 'AF' %s to squid with data:\n", cred);
 
265
                if (c != NULL)
 
266
                    hex_dump(decoded, (strlen(c) * 3) / 4);
 
267
                else
 
268
                    fprintf(stderr, "No data available.\n");
 
269
                printf("AF %s %s\n", c, cred);
266
270
            } else {
267
271
                SEND3("AF %s %s", c, cred);
268
272
            }
269
 
            return 1;
 
273
            return 1;
270
274
        } else {
271
275
            if (Negotiate_packet_debug_enabled) {
272
 
                printf("TT %s\n",c);
273
276
                decoded = base64_decode(c);
274
 
                debug("sending 'TT' to squid with data:\n");
 
277
                debug("sending 'TT' to squid with data:\n");
275
278
                hex_dump(decoded, (strlen(c) * 3) / 4);
 
279
                printf("TT %s\n", c);
276
280
            } else
277
 
                SEND2("TT %s", c);
278
 
            return 1;
 
281
                SEND2("TT %s", c);
 
282
            return 1;
279
283
        }
280
284
 
281
 
    } else {    /* not an auth-request */
282
 
        helperfail("illegal request received");
283
 
        fprintf(stderr, "Illegal request received: '%s'\n", buf);
284
 
        return 1;
 
285
    } else {                    /* not an auth-request */
 
286
        helperfail("illegal request received");
 
287
        fprintf(stderr, "Illegal request received: '%s'\n", buf);
 
288
        return 1;
285
289
    }
286
290
    helperfail("detected protocol error");
287
291
    return 1;
288
 
/********* END ********/
 
292
    /********* END ********/
289
293
}
290
294
 
291
295
int
296
300
    process_options(argc, argv);
297
301
 
298
302
    debug("%s build " __DATE__ ", " __TIME__ " starting up...\n", my_program_name);
299
 
    
 
303
 
300
304
    if (LoadSecurityDll(SSP_NTLM, NEGOTIATE_PACKAGE_NAME) == NULL) {
301
 
        fprintf(stderr, "FATAL, can't initialize SSPI, exiting.\n");
302
 
        exit(1);
 
305
        fprintf(stderr, "FATAL, can't initialize SSPI, exiting.\n");
 
306
        exit(1);
303
307
    }
304
308
    debug("SSPI initialized OK\n");
305
309
 
310
314
    setbuf(stderr, NULL);
311
315
 
312
316
    while (manage_request()) {
313
 
        /* everything is done within manage_request */
 
317
        /* everything is done within manage_request */
314
318
    }
315
319
    exit(0);
316
320
}