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

« back to all changes in this revision

Viewing changes to helpers/external_acl/mswin_ad_group/mswin_check_ad_group.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:
36
36
 *              First release, based on mswin_check_lm_group.
37
37
 *
38
38
 * This is a helper for the external ACL interface for Squid Cache
39
 
 * 
 
39
 *
40
40
 * It reads from the standard input the domain username and a list of
41
41
 * groups and tries to match it against the groups membership of the
42
42
 * specified username.
93
93
    DWORD netret;
94
94
 
95
95
    if ((netret = DsRoleGetPrimaryDomainInformation(NULL, DsRolePrimaryDomainInfoBasic, (PBYTE *) & pDSRoleInfo) == ERROR_SUCCESS)) {
96
 
        /* 
97
 
         * Check the machine role.
98
 
         */
99
 
 
100
 
        if ((pDSRoleInfo->MachineRole == DsRole_RoleMemberWorkstation) ||
101
 
            (pDSRoleInfo->MachineRole == DsRole_RoleMemberServer) ||
102
 
            (pDSRoleInfo->MachineRole == DsRole_RoleBackupDomainController) ||
103
 
            (pDSRoleInfo->MachineRole == DsRole_RolePrimaryDomainController)) {
104
 
 
105
 
            size_t len = wcslen(pDSRoleInfo->DomainNameFlat);
106
 
 
107
 
            /* allocate buffer for str + null termination */
108
 
            safe_free(DomainName);
109
 
            DomainName = (char *) xmalloc(len + 1);
110
 
            if (DomainName == NULL)
111
 
                return NULL;
112
 
 
113
 
            /* copy unicode buffer */
114
 
            WideCharToMultiByte(CP_ACP, 0, pDSRoleInfo->DomainNameFlat, -1, DomainName, len, NULL, NULL);
115
 
 
116
 
            /* add null termination */
117
 
            DomainName[len] = '\0';
118
 
 
119
 
            /* 
120
 
             * Member of a domain. Display it in debug mode.
121
 
             */
122
 
            debug("Member of Domain %s\n", DomainName);
123
 
            debug("Into forest %S\n", pDSRoleInfo->DomainForestName);
124
 
 
125
 
        } else {
126
 
            debug("Not a Domain member\n");
127
 
        }
 
96
        /*
 
97
         * Check the machine role.
 
98
         */
 
99
 
 
100
        if ((pDSRoleInfo->MachineRole == DsRole_RoleMemberWorkstation) ||
 
101
                (pDSRoleInfo->MachineRole == DsRole_RoleMemberServer) ||
 
102
                (pDSRoleInfo->MachineRole == DsRole_RoleBackupDomainController) ||
 
103
                (pDSRoleInfo->MachineRole == DsRole_RolePrimaryDomainController)) {
 
104
 
 
105
            size_t len = wcslen(pDSRoleInfo->DomainNameFlat);
 
106
 
 
107
            /* allocate buffer for str + null termination */
 
108
            safe_free(DomainName);
 
109
            DomainName = (char *) xmalloc(len + 1);
 
110
            if (DomainName == NULL)
 
111
                return NULL;
 
112
 
 
113
            /* copy unicode buffer */
 
114
            WideCharToMultiByte(CP_ACP, 0, pDSRoleInfo->DomainNameFlat, -1, DomainName, len, NULL, NULL);
 
115
 
 
116
            /* add null termination */
 
117
            DomainName[len] = '\0';
 
118
 
 
119
            /*
 
120
             * Member of a domain. Display it in debug mode.
 
121
             */
 
122
            debug("Member of Domain %s\n", DomainName);
 
123
            debug("Into forest %S\n", pDSRoleInfo->DomainForestName);
 
124
 
 
125
        } else {
 
126
            debug("Not a Domain member\n");
 
127
        }
128
128
    } else
129
 
        debug("DsRoleGetPrimaryDomainInformation Error: %ld\n", netret);
 
129
        debug("DsRoleGetPrimaryDomainInformation Error: %ld\n", netret);
130
130
 
131
131
    /*
132
132
     * Free the allocated memory.
133
133
     */
134
134
    if (pDSRoleInfo != NULL)
135
 
        DsRoleFreeMemory(pDSRoleInfo);
 
135
        DsRoleFreeMemory(pDSRoleInfo);
136
136
 
137
137
    return DomainName;
138
138
}
144
144
    WCHAR wszGroup[GNLEN + 1];  // Unicode Group
145
145
 
146
146
    while (*array) {
147
 
        MultiByteToWideChar(CP_ACP, 0, *array,
148
 
            strlen(*array) + 1, wszGroup, sizeof(wszGroup) / sizeof(wszGroup[0]));
149
 
        debug("Windows group: %S, Squid group: %S\n", str, wszGroup);
150
 
        if ((use_case_insensitive_compare ? _wcsicmp(str, wszGroup) : wcscmp(str, wszGroup)) == 0)
151
 
            return 0;
152
 
        array++;
 
147
        MultiByteToWideChar(CP_ACP, 0, *array,
 
148
                            strlen(*array) + 1, wszGroup, sizeof(wszGroup) / sizeof(wszGroup[0]));
 
149
        debug("Windows group: %S, Squid group: %S\n", str, wszGroup);
 
150
        if ((use_case_insensitive_compare ? _wcsicmp(str, wszGroup) : wcscmp(str, wszGroup)) == 0)
 
151
            return 0;
 
152
        array++;
153
153
    }
154
154
    return -1;
155
155
}
175
175
    LPBYTE pBufTmp = NULL;
176
176
 
177
177
    if ((Domain_Separator = strchr(UserName, '/')) != NULL)
178
 
        *Domain_Separator = '\\';
 
178
        *Domain_Separator = '\\';
179
179
 
180
180
    debug("Valid_Local_Groups: checking group membership of '%s'.\n", UserName);
181
181
 
182
 
/* Convert ANSI User Name and Group to Unicode */
 
182
    /* Convert ANSI User Name and Group to Unicode */
183
183
 
184
184
    MultiByteToWideChar(CP_ACP, 0, UserName,
185
 
        strlen(UserName) + 1, wszUserName, sizeof(wszUserName) / sizeof(wszUserName[0]));
 
185
                        strlen(UserName) + 1, wszUserName, sizeof(wszUserName) / sizeof(wszUserName[0]));
186
186
 
187
187
    /*
188
 
     * Call the NetUserGetLocalGroups function 
 
188
     * Call the NetUserGetLocalGroups function
189
189
     * specifying information level 0.
190
 
     * 
191
 
     * The LG_INCLUDE_INDIRECT flag specifies that the 
192
 
     * function should also return the names of the local 
 
190
     *
 
191
     * The LG_INCLUDE_INDIRECT flag specifies that the
 
192
     * function should also return the names of the local
193
193
     * groups in which the user is indirectly a member.
194
194
     */
195
195
    nStatus = NetUserGetLocalGroups(NULL,
196
 
        wszUserName,
197
 
        dwLevel,
198
 
        dwFlags,
199
 
        &pBufTmp,
200
 
        dwPrefMaxLen,
201
 
        &dwEntriesRead,
202
 
        &dwTotalEntries);
 
196
                                    wszUserName,
 
197
                                    dwLevel,
 
198
                                    dwFlags,
 
199
                                    &pBufTmp,
 
200
                                    dwPrefMaxLen,
 
201
                                    &dwEntriesRead,
 
202
                                    &dwTotalEntries);
203
203
    pBuf = (LPLOCALGROUP_USERS_INFO_0) pBufTmp;
204
204
    /*
205
205
     * If the call succeeds,
206
206
     */
207
207
    if (nStatus == NERR_Success) {
208
 
        if ((pTmpBuf = pBuf) != NULL) {
209
 
            for (i = 0; i < dwEntriesRead; i++) {
210
 
                assert(pTmpBuf != NULL);
211
 
                if (pTmpBuf == NULL) {
212
 
                    result = 0;
213
 
                    break;
214
 
                }
215
 
                if (wcstrcmparray(pTmpBuf->lgrui0_name, Groups) == 0) {
216
 
                    result = 1;
217
 
                    break;
218
 
                }
219
 
                pTmpBuf++;
220
 
                dwTotalCount++;
221
 
            }
222
 
        }
 
208
        if ((pTmpBuf = pBuf) != NULL) {
 
209
            for (i = 0; i < dwEntriesRead; i++) {
 
210
                assert(pTmpBuf != NULL);
 
211
                if (pTmpBuf == NULL) {
 
212
                    result = 0;
 
213
                    break;
 
214
                }
 
215
                if (wcstrcmparray(pTmpBuf->lgrui0_name, Groups) == 0) {
 
216
                    result = 1;
 
217
                    break;
 
218
                }
 
219
                pTmpBuf++;
 
220
                dwTotalCount++;
 
221
            }
 
222
        }
223
223
    } else
224
 
        result = 0;
225
 
/*
226
 
 * Free the allocated memory.
227
 
 */
 
224
        result = 0;
 
225
    /*
 
226
     * Free the allocated memory.
 
227
     */
228
228
    if (pBuf != NULL)
229
 
        NetApiBufferFree(pBuf);
 
229
        NetApiBufferFree(pBuf);
230
230
    return result;
231
231
}
232
232
 
260
260
    strncpy(NTDomain, UserName, sizeof(NTDomain));
261
261
 
262
262
    for (j = 0; j < strlen(NTV_VALID_DOMAIN_SEPARATOR); j++) {
263
 
        if ((domain_qualify = strchr(NTDomain, NTV_VALID_DOMAIN_SEPARATOR[j])) != NULL)
264
 
            break;
 
263
        if ((domain_qualify = strchr(NTDomain, NTV_VALID_DOMAIN_SEPARATOR[j])) != NULL)
 
264
            break;
265
265
    }
266
266
    if (domain_qualify == NULL) {
267
 
        strcpy(User, NTDomain);
268
 
        strcpy(NTDomain, DefaultDomain);
 
267
        strcpy(User, NTDomain);
 
268
        strcpy(NTDomain, DefaultDomain);
269
269
    } else {
270
 
        strcpy(User, domain_qualify + 1);
271
 
        domain_qualify[0] = '\0';
272
 
        strlwr(NTDomain);
 
270
        strcpy(User, domain_qualify + 1);
 
271
        domain_qualify[0] = '\0';
 
272
        strlwr(NTDomain);
273
273
    }
274
274
 
275
275
    debug("Valid_Global_Groups: checking group membership of '%s\\%s'.\n", NTDomain, User);
277
277
    /* Convert ANSI User Name to Unicode */
278
278
 
279
279
    MultiByteToWideChar(CP_ACP, 0, User,
280
 
        strlen(User) + 1, wszUserName,
281
 
        sizeof(wszUserName) / sizeof(wszUserName[0]));
 
280
                        strlen(User) + 1, wszUserName,
 
281
                        sizeof(wszUserName) / sizeof(wszUserName[0]));
282
282
 
283
283
    /* Query AD for a DC */
284
284
 
285
285
    if (DsGetDcName(NULL, NTDomain, NULL, NULL, DS_IS_FLAT_NAME | DS_RETURN_FLAT_NAME, &pDCInfo) != NO_ERROR) {
286
 
        fprintf(stderr, "%s DsGetDcName() failed.'\n", myname);
287
 
        if (pDCInfo != NULL)
288
 
            NetApiBufferFree(pDCInfo);
289
 
        return result;
 
286
        fprintf(stderr, "%s DsGetDcName() failed.'\n", myname);
 
287
        if (pDCInfo != NULL)
 
288
            NetApiBufferFree(pDCInfo);
 
289
        return result;
290
290
    }
291
291
    /* Convert ANSI Domain Controller Name to Unicode */
292
292
 
293
293
    MultiByteToWideChar(CP_ACP, 0, pDCInfo->DomainControllerName,
294
 
        strlen(pDCInfo->DomainControllerName) + 1, wszDomainControllerName,
295
 
        sizeof(wszDomainControllerName) / sizeof(wszDomainControllerName[0]));
 
294
                        strlen(pDCInfo->DomainControllerName) + 1, wszDomainControllerName,
 
295
                        sizeof(wszDomainControllerName) / sizeof(wszDomainControllerName[0]));
296
296
 
297
297
    debug("Using '%S' as DC for '%s' user's domain.\n", wszDomainControllerName, NTDomain);
298
298
    debug("DC Active Directory Site is %s\n", pDCInfo->DcSiteName);
299
299
    debug("Machine Active Directory Site is %s\n", pDCInfo->ClientSiteName);
300
300
 
301
301
    /*
302
 
     * Call the NetUserGetGroups function 
 
302
     * Call the NetUserGetGroups function
303
303
     * specifying information level 0.
304
304
     */
305
305
    dwLevel = 0;
306
306
    pBufTmp = NULL;
307
307
    nStatus = NetUserGetGroups(wszDomainControllerName,
308
 
        wszUserName,
309
 
        dwLevel,
310
 
        &pBufTmp,
311
 
        dwPrefMaxLen,
312
 
        &dwEntriesRead,
313
 
        &dwTotalEntries);
 
308
                               wszUserName,
 
309
                               dwLevel,
 
310
                               &pBufTmp,
 
311
                               dwPrefMaxLen,
 
312
                               &dwEntriesRead,
 
313
                               &dwTotalEntries);
314
314
    pUsrBuf = (LPGROUP_USERS_INFO_0) pBufTmp;
315
315
    /*
316
316
     * If the call succeeds,
317
317
     */
318
318
    if (nStatus == NERR_Success) {
319
 
        if ((pTmpBuf = pUsrBuf) != NULL) {
320
 
            for (i = 0; i < dwEntriesRead; i++) {
321
 
                assert(pTmpBuf != NULL);
322
 
                if (pTmpBuf == NULL) {
323
 
                    result = 0;
324
 
                    break;
325
 
                }
326
 
                if (wcstrcmparray(pTmpBuf->grui0_name, Groups) == 0) {
327
 
                    result = 1;
328
 
                    break;
329
 
                }
330
 
                pTmpBuf++;
331
 
                dwTotalCount++;
332
 
            }
333
 
        }
 
319
        if ((pTmpBuf = pUsrBuf) != NULL) {
 
320
            for (i = 0; i < dwEntriesRead; i++) {
 
321
                assert(pTmpBuf != NULL);
 
322
                if (pTmpBuf == NULL) {
 
323
                    result = 0;
 
324
                    break;
 
325
                }
 
326
                if (wcstrcmparray(pTmpBuf->grui0_name, Groups) == 0) {
 
327
                    result = 1;
 
328
                    break;
 
329
                }
 
330
                pTmpBuf++;
 
331
                dwTotalCount++;
 
332
            }
 
333
        }
334
334
    } else {
335
 
        result = 0;
336
 
        fprintf(stderr, "%s NetUserGetGroups() failed.'\n", myname);
 
335
        result = 0;
 
336
        fprintf(stderr, "%s NetUserGetGroups() failed.'\n", myname);
337
337
    }
338
338
    /*
339
339
     * Free the allocated memory.
340
340
     */
341
341
    if (pUsrBuf != NULL)
342
 
        NetApiBufferFree(pUsrBuf);
 
342
        NetApiBufferFree(pUsrBuf);
343
343
    if (pDCInfo != NULL)
344
 
        NetApiBufferFree((LPVOID) pDCInfo);
 
344
        NetApiBufferFree((LPVOID) pDCInfo);
345
345
    return result;
346
346
}
347
347
 
349
349
usage(char *program)
350
350
{
351
351
    fprintf(stderr, "Usage: %s [-D domain][-G][-P][-c][-d][-h]\n"
352
 
        " -D    default user Domain\n"
353
 
        " -G    enable Domain Global group mode\n"
354
 
        " -c    use case insensitive compare\n"
355
 
        " -d    enable debugging\n"
356
 
        " -h    this message\n",
357
 
        program);
 
352
            " -D    default user Domain\n"
 
353
            " -G    enable Domain Global group mode\n"
 
354
            " -c    use case insensitive compare\n"
 
355
            " -d    enable debugging\n"
 
356
            " -h    this message\n",
 
357
            program);
358
358
}
359
359
 
360
360
void
364
364
 
365
365
    opterr = 0;
366
366
    while (-1 != (opt = getopt(argc, argv, "D:Gcdh"))) {
367
 
        switch (opt) {
368
 
        case 'D':
369
 
            DefaultDomain = xstrndup(optarg, DNLEN + 1);
370
 
            strlwr(DefaultDomain);
371
 
            break;
372
 
        case 'G':
373
 
            use_global = 1;
374
 
            break;
375
 
        case 'c':
376
 
            use_case_insensitive_compare = 1;
377
 
            break;
378
 
        case 'd':
379
 
            debug_enabled = 1;
380
 
            break;
381
 
        case 'h':
382
 
            usage(argv[0]);
383
 
            exit(0);
384
 
        case '?':
385
 
            opt = optopt;
386
 
            /* fall thru to default */
387
 
        default:
388
 
            fprintf(stderr, "%s Unknown option: -%c. Exiting\n", myname, opt);
389
 
            usage(argv[0]);
390
 
            exit(1);
391
 
            break;              /* not reached */
392
 
        }
 
367
        switch (opt) {
 
368
        case 'D':
 
369
            DefaultDomain = xstrndup(optarg, DNLEN + 1);
 
370
            strlwr(DefaultDomain);
 
371
            break;
 
372
        case 'G':
 
373
            use_global = 1;
 
374
            break;
 
375
        case 'c':
 
376
            use_case_insensitive_compare = 1;
 
377
            break;
 
378
        case 'd':
 
379
            debug_enabled = 1;
 
380
            break;
 
381
        case 'h':
 
382
            usage(argv[0]);
 
383
            exit(0);
 
384
        case '?':
 
385
            opt = optopt;
 
386
            /* fall thru to default */
 
387
        default:
 
388
            fprintf(stderr, "%s Unknown option: -%c. Exiting\n", myname, opt);
 
389
            usage(argv[0]);
 
390
            exit(1);
 
391
            break;              /* not reached */
 
392
        }
393
393
    }
394
394
    return;
395
395
}
407
407
    int n;
408
408
 
409
409
    if (argc > 0) {             /* should always be true */
410
 
        myname = strrchr(argv[0], '/');
411
 
        if (myname == NULL)
412
 
            myname = argv[0];
 
410
        myname = strrchr(argv[0], '/');
 
411
        if (myname == NULL)
 
412
            myname = argv[0];
413
413
    } else {
414
 
        myname = "(unknown)";
 
414
        myname = "(unknown)";
415
415
    }
416
416
    mypid = getpid();
417
417
 
422
422
    process_options(argc, argv);
423
423
 
424
424
    if (use_global) {
425
 
        if ((machinedomain = GetDomainName()) == NULL) {
426
 
            fprintf(stderr, "%s Can't read machine domain\n", myname);
427
 
            exit(1);
428
 
        }
429
 
        strlwr(machinedomain);
430
 
        if (!DefaultDomain)
431
 
            DefaultDomain = xstrdup(machinedomain);
 
425
        if ((machinedomain = GetDomainName()) == NULL) {
 
426
            fprintf(stderr, "%s Can't read machine domain\n", myname);
 
427
            exit(1);
 
428
        }
 
429
        strlwr(machinedomain);
 
430
        if (!DefaultDomain)
 
431
            DefaultDomain = xstrdup(machinedomain);
432
432
    }
433
433
    debug("External ACL win32 group helper build " __DATE__ ", " __TIME__
434
 
        " starting up...\n");
 
434
          " starting up...\n");
435
435
    if (use_global)
436
 
        debug("Domain Global group mode enabled using '%s' as default domain.\n", DefaultDomain);
 
436
        debug("Domain Global group mode enabled using '%s' as default domain.\n", DefaultDomain);
437
437
    if (use_case_insensitive_compare)
438
 
        debug("Warning: running in case insensitive mode !!!\n");
 
438
        debug("Warning: running in case insensitive mode !!!\n");
439
439
 
440
440
    /* Main Loop */
441
441
    while (fgets(buf, sizeof(buf), stdin)) {
442
 
        if (NULL == strchr(buf, '\n')) {
443
 
            /* too large message received.. skip and deny */
444
 
            fprintf(stderr, "%s: ERROR: Too large: %s\n", argv[0], buf);
445
 
            while (fgets(buf, sizeof(buf), stdin)) {
446
 
                fprintf(stderr, "%s: ERROR: Too large..: %s\n", argv[0], buf);
447
 
                if (strchr(buf, '\n') != NULL)
448
 
                    break;
449
 
            }
450
 
            goto error;
451
 
        }
452
 
        if ((p = strchr(buf, '\n')) != NULL)
453
 
            *p = '\0';          /* strip \n */
454
 
        if ((p = strchr(buf, '\r')) != NULL)
455
 
            *p = '\0';          /* strip \r */
456
 
 
457
 
        debug("Got '%s' from Squid (length: %d).\n", buf, strlen(buf));
458
 
 
459
 
        if (buf[0] == '\0') {
460
 
            fprintf(stderr, "Invalid Request\n");
461
 
            goto error;
462
 
        }
463
 
        username = strtok(buf, " ");
464
 
        for (n = 0; (group = strtok(NULL, " ")) != NULL; n++) {
465
 
            rfc1738_unescape(group);
466
 
            groups[n] = group;
467
 
        }
468
 
        groups[n] = NULL;
469
 
 
470
 
        if (NULL == username) {
471
 
            fprintf(stderr, "Invalid Request\n");
472
 
            goto error;
473
 
        }
474
 
        rfc1738_unescape(username);
475
 
 
476
 
        if ((use_global ? Valid_Global_Groups(username, groups) : Valid_Local_Groups(username, groups))) {
477
 
            printf("OK\n");
478
 
        } else {
479
 
          error:
480
 
            printf("ERR\n");
481
 
        }
482
 
        err = 0;
 
442
        if (NULL == strchr(buf, '\n')) {
 
443
            /* too large message received.. skip and deny */
 
444
            fprintf(stderr, "%s: ERROR: Too large: %s\n", argv[0], buf);
 
445
            while (fgets(buf, sizeof(buf), stdin)) {
 
446
                fprintf(stderr, "%s: ERROR: Too large..: %s\n", argv[0], buf);
 
447
                if (strchr(buf, '\n') != NULL)
 
448
                    break;
 
449
            }
 
450
            goto error;
 
451
        }
 
452
        if ((p = strchr(buf, '\n')) != NULL)
 
453
            *p = '\0';          /* strip \n */
 
454
        if ((p = strchr(buf, '\r')) != NULL)
 
455
            *p = '\0';          /* strip \r */
 
456
 
 
457
        debug("Got '%s' from Squid (length: %d).\n", buf, strlen(buf));
 
458
 
 
459
        if (buf[0] == '\0') {
 
460
            fprintf(stderr, "Invalid Request\n");
 
461
            goto error;
 
462
        }
 
463
        username = strtok(buf, " ");
 
464
        for (n = 0; (group = strtok(NULL, " ")) != NULL; n++) {
 
465
            rfc1738_unescape(group);
 
466
            groups[n] = group;
 
467
        }
 
468
        groups[n] = NULL;
 
469
 
 
470
        if (NULL == username) {
 
471
            fprintf(stderr, "Invalid Request\n");
 
472
            goto error;
 
473
        }
 
474
        rfc1738_unescape(username);
 
475
 
 
476
        if ((use_global ? Valid_Global_Groups(username, groups) : Valid_Local_Groups(username, groups))) {
 
477
            printf("OK\n");
 
478
        } else {
 
479
error:
 
480
            printf("ERR\n");
 
481
        }
 
482
        err = 0;
483
483
    }
484
484
    return 0;
485
485
}