1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef WINDOWS
# define _WIN32_WINNT 0x0500
# include <windows.h>
# include <Sddl.h>
# include <lm.h>
# include <Dsgetdc.h>
/*
* for some reason we cannot include basistypes.h when building authuser.
* only include it ifndef WINDOWS in msg_juti.h,
* and do special handling here.
* TODO: should be understood and fixed (in basis_types.h?)
*/
# define SFN "%-.100s"
# define _(x) (x)
# define _MESSAGE(x,y) (y)
#else
# include <pwd.h>
# if !(defined(DARWIN) || defined(FREEBSD) || defined(NETBSD))
# include <crypt.h>
# endif
# include <unistd.h>
#include <grp.h>
#endif
#if defined(IRIX65) || defined(AIX43) || defined(HP1164) || defined(INTERIX) || defined(ALPHA5) || defined(WINDOWS)
#define JUTI_NO_PAM
#elif defined(DARWIN)
#include <pam/pam_appl.h>
#else
#include <security/pam_appl.h>
#endif
#if defined(DARWIN) || defined(AIX51) || defined(AIX43) || defined(INTERIX) || defined(FREEBSD) || defined(ALPHA5) || defined(WINDOWS) || defined(NETBSD)
#define JUTI_NO_SHADOW
#else
#include <shadow.h>
#endif
#if defined(AIX51) || defined(AIX43)
#include <userpw.h>
#endif
#include "msg_utilbin.h"
#include "juti.h"
#if 0
#define DEBUG 1
#endif
#define BUF_SIZE 1024
#ifdef WINDOWS
typedef int gid_t;
#endif
typedef struct error_handler_str error_handler_t;
struct error_handler_str {
void (*error)(const char* fmt, ...);
};
typedef enum {
JUTI_AUTH_SUCCESS = 0, /* authentication success */
JUTI_AUTH_FAILED = 1, /* invalid username or password */
JUTI_AUTH_ERROR = 2 /* authentication is not proper configured */
} auth_result_t;
static void usage(void);
static void print_error(const char* format, ...);
static int mygetline(char s[], int lim);
static auth_result_t do_system_authentication(const char *username,
const char *password,
int *uid,
int *gid,
error_handler_t *error_handler);
#ifdef WINDOWS
static auth_result_t do_windows_system_authentication(const char *username,
const char *password,
char ***pppszUID,
char ***pppszGID,
char ***pppszGroupNames,
int *nGIDs,
error_handler_t *error_handler);
#else
static auth_result_t do_pam_authenticate(const char* service, const char *username, const char *password,
error_handler_t *error_handler);
static auth_result_t get_crypted_password(const char* username, char* buffer, size_t size,
error_handler_t *error_handler);
static int juti_getgrouplist(const char *uname, gid_t agroup, gid_t **groups_res, char*** group_names, int *grpcnt);
static int add_group(struct group *grp, gid_t **groups, char*** group_names, int* ngroups, int* maxgroups);
#endif
static void usage(void) {
fprintf(stderr, "authuser <method> <params>\n");
fprintf(stderr, "\n");
fprintf(stderr, " <method> ");
#ifndef JUTI_NO_PAM
fprintf(stderr, "\"pam\" or ");
#endif
fprintf(stderr, "\"system\"\n");
fprintf(stderr, "\n");
#ifndef JUTI_NO_PAM
fprintf(stderr, "Params for PAM authentification\n");
fprintf(stderr, " -s <servicename> name of the pam service\n");
#endif
fprintf(stderr, "\n");
fprintf(stderr, "<username> and <password> are read from stdin\n");
exit(1);
}
static void print_error(const char* format, ...) {
va_list ap;
va_start(ap, format);
fprintf(stderr, MSG_AUTHUSER_ERROR);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
}
int main(int argc, char** argv) {
const char *auth_method = NULL;
const char *service = NULL;
char username[BUF_SIZE];
char password[BUF_SIZE];
int i = 0;
int ret = 0;
error_handler_t error_handler;
int uid = 0;
int gid = 0;
#ifdef WINDOWS
char **ppszUID = NULL;
char **ppszGID = NULL;
char **ppszGroupNames = NULL;
int nGIDs;
#endif
error_handler.error = print_error;
if(argc < 2 ) {
print_error(MSG_AUTUSER_INVAILD_ARG_COUNT);
usage();
}
auth_method = argv[1];
#ifndef WINDOWS
#define SGE_SUPERUSER_UID 0
/* only root can successfull execute this */
if(geteuid() != SGE_SUPERUSER_UID && geteuid() != getuid()) {
print_error(MSG_AUTHUSER_ONLY_ROOT_S, argv[0]);
return 1;
}
#endif
if(strcmp(auth_method, "pam") == 0 ) {
for(i=2; i < argc; i++) {
if( strcmp(argv[i], "-s") == 0 ) {
i++;
if(i >= argc) {
print_error(MSG_AUTUSER_MISSING_PAM_SERVICE);
usage();
}
service = argv[i];
} else {
print_error(MSG_AUTUSER_UNKNOWN_PARAM_S, argv[i]);
usage();
}
}
if (service == NULL) {
print_error(MSG_AUTUSER_MISSING_PAM_SERVICE);
usage();
}
} else if (strcmp(auth_method, "system" ) == 0 ) {
} else {
print_error(MSG_AUTUSER_UNKNOWN_AUTH_METHOD_S, auth_method);
usage();
}
fprintf(stdout, "username: ");
fflush(stdout);
if(mygetline(username, BUF_SIZE-1) <= 0) {
usage();
return 1;
}
fprintf(stdout, "password: ");
fflush(stdout);
setEcho(0);
if(mygetline(password, BUF_SIZE-1) < 0) {
setEcho(1);
usage();
return 1;
}
fprintf(stdout,"\n");
setEcho(1);
if (strcmp(auth_method, "pam") == 0 ) {
#ifndef WINDOWS
ret = do_pam_authenticate(service, username, password, &error_handler);
if(ret == JUTI_AUTH_SUCCESS) {
struct passwd *pw = getpwnam(username);
if(pw == NULL) {
print_error(MSG_AUTHUSER_NO_PW_ENTRY_SS,
username, strerror(errno));
return -1;
}
uid = pw->pw_uid;
gid = pw->pw_gid;
}
#else
ret = JUTI_AUTH_ERROR;
print_error(MSG_AUTHUSER_PAM_NOT_AVAILABLE);
#endif
} else if(strcmp(auth_method, "system") == 0 ) {
#ifndef WINDOWS
ret = do_system_authentication(username, password, &uid, &gid, &error_handler);
#else
ret = do_windows_system_authentication(username, password,
&ppszUID, &ppszGID, &ppszGroupNames,
&nGIDs, &error_handler);
#endif
} else {
ret = -1;
}
if (ret==JUTI_AUTH_SUCCESS) {
int group_count = 0;
gid_t *groups = NULL;
char **group_names = NULL;
#ifndef WINDOWS
fprintf(stdout, "uid %d\n", uid);
fprintf(stdout, "gid ");
if(juti_getgrouplist(username, gid, &groups, &group_names, &group_count) == 0) {
for(i = 0; i < group_count; i++) {
if(i>0) {
fprintf(stdout, ",%s("gid_t_fmt")", group_names[i], groups[i]);
} else {
fprintf(stdout, "%s("gid_t_fmt")", group_names[i], groups[i]);
}
free(group_names[i]);
}
free(groups);
free(group_names);
}
#else
fprintf(stdout, "uid %s\n", *ppszUID);
LocalFree(ppszUID[0]);
free(ppszUID);
fprintf(stdout, "gid ");
for(i=0; i<nGIDs; i++) {
if(i>0) {
fprintf(stdout, ",%s(%s)", ppszGroupNames[i], ppszGID[i]);
} else {
fprintf(stdout, "%s(%s)", ppszGroupNames[i], ppszGID[i]);
}
LocalFree(ppszGID[i]);
free(ppszGroupNames[i]);
}
free(ppszGID);
free(ppszGroupNames);
#endif
fprintf(stdout, "\n");
}
return ret;
}
static int mygetline(char s[], int lim) { /* get a line into s, return length */
/*
User data is sent one field to a line, with the data
set terminated when a line containing only a semi-colon is received.
*/
int c, i;
for (i = 0; i < lim-1 && ((c = getchar()) != EOF) && c != '\n'; i++) {
s[i] = c;
}
s[i] = '\0';
return (i);
}
#ifndef WINDOWS
static auth_result_t do_system_authentication(const char *username,
const char *password,
int *uid,
int *gid,
error_handler_t *error_handler)
{
auth_result_t ret = JUTI_AUTH_SUCCESS;
char crypted_password[BUF_SIZE];
char *new_crypted_password = NULL;
struct passwd *pwres = getpwnam(username);
if(pwres == NULL) {
ret = JUTI_AUTH_FAILED;
error_handler->error(MSG_AUTHUSER_USER_UNKNOWN_S, username);
goto error;
}
#ifdef DEBUG
printf("user found in passwd\n");
#endif
ret = get_crypted_password(username, crypted_password, BUF_SIZE, error_handler);
if(ret != JUTI_AUTH_SUCCESS) {
goto error;
}
#ifdef DEBUG
printf(" crypted password: %s\n", crypted_password);
#endif
#if !defined(INTERIX)
new_crypted_password = crypt(password, crypted_password);
#endif
if (new_crypted_password == NULL) {
error_handler->error(MSG_AUTHUSER_CRYPT_FAILED_S, strerror(errno));
ret = JUTI_AUTH_ERROR;
} else {
#ifdef DEBUG
printf("new crypted password: %s\n", new_crypted_password);
#endif
if(strcmp(crypted_password, new_crypted_password) == 0) {
ret = JUTI_AUTH_SUCCESS;
} else {
error_handler->error(MSG_AUTHUSER_INVALID_PASSWORD);
ret = JUTI_AUTH_FAILED;
}
}
*uid = pwres->pw_uid;
*gid = pwres->pw_gid;
error:
return ret;
}
#ifndef JUTI_NO_PAM
/*
* For application data, like password. Passes through the
* pam framework to the conversation function.
*/
struct app_pam_data {
const char *password;
};
/* pam conversation function */
#if defined(SOLARIS) || defined(AIX) || defined(HP11) || defined(HP1164)
static int login_conv(int num_msg, struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr);
#else
static int login_conv(int num_msg, const struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr);
#endif
#endif
static auth_result_t do_pam_authenticate(const char* service, const char *username,
const char *password,
error_handler_t *error_handler)
{
#ifdef JUTI_NO_PAM
error_handler->error(MSG_AUTHUSER_PAM_NOT_AVAILABLE);
return JUTI_AUTH_ERROR;
#else
auth_result_t ret;
int status;
pam_handle_t *pamh; /* pam handle */
struct pam_conv pamconv; /* pam init structure */
struct app_pam_data app_data; /* pam application data */
pamh = NULL;
/*
* app_data gets passed through the framework
* to "login_conv". Set the password for use in login_conv
* and set up the pam_conv data with the conversation
* function and the application data pointer.
*/
app_data.password = password;
pamconv.conv = login_conv;
pamconv.appdata_ptr = &app_data;
/* pam start session */
status = pam_start(service, username, &pamconv, &pamh);
if(status != PAM_SUCCESS) {
ret = JUTI_AUTH_ERROR;
goto error;
}
status = pam_authenticate(pamh, PAM_SILENT);
if(status != PAM_SUCCESS) {
ret = JUTI_AUTH_FAILED;
goto error;
}
/* check if the authenicated user is allowed to use machine */
status = pam_acct_mgmt(pamh, PAM_SILENT);
if (status != PAM_SUCCESS) {
ret = JUTI_AUTH_FAILED;
goto error;
}
ret = JUTI_AUTH_SUCCESS;
error:
if (status != PAM_SUCCESS) {
const char *pam_err_msg = pam_strerror(pamh, status);
error_handler->error(MSG_AUTHUSER_PAM_ERROR_S, pam_err_msg);
}
/* end pam session */
if (pamh != NULL) {
pam_end(pamh, status == PAM_SUCCESS ? PAM_SUCCESS : PAM_ABORT);
}
return ret;
#endif
}
/*
* login_conv:
* this is the conversation function called from a PAM authentication module
* to print erro messagae or get user information
*/
#ifndef JUTI_NO_PAM
#if defined(SOLARIS) || defined(AIX) || defined(HP11) || defined(HP1164)
static int login_conv(int num_msg, struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr)
#else
static int login_conv(int num_msg, const struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr)
#endif
{
int count = 0;
int reply_used = 0;
struct pam_response *reply;
if (num_msg <= 0) {
#ifdef DEBUG
printf("no message returned from pam module\n");
#endif
return (PAM_CONV_ERR);
}
reply = (struct pam_response *)calloc(num_msg,
sizeof (struct pam_response));
if (reply == NULL) {
#ifdef DEBUG
printf("calloc memory error\n");
#endif
return (PAM_BUF_ERR);
}
for (count = 0; count < num_msg; ++count) {
#ifdef DEBUG
printf("\n login_conv: prompt_style: %d" , msgm[count]->msg_style);
printf("\n login_conv: prompt_message: %s" , msgm[count]->msg);
printf("\n PAM_PROMPT_ECHO_OFF %d" , PAM_PROMPT_ECHO_OFF);
printf("\n PAM_PROMPT_ECHO_ON %d" , PAM_PROMPT_ECHO_ON);
printf("\n PAM_ERROR_MSG %d" , PAM_ERROR_MSG);
printf("\n PAM_TEXT_INFO %d" , PAM_TEXT_INFO);
#endif
switch (msgm[count]->msg_style) {
case PAM_PROMPT_ECHO_OFF:
if (((struct app_pam_data *)appdata_ptr)->password != NULL) {
(reply+count)->resp = strdup(((struct app_pam_data *)
appdata_ptr)->password);
if (reply[count].resp == NULL) {
/*
* It may be the case that some modules won't free
* the pam_response memory if the return is not
* PAM_SUCCESS. We should not have had
* multiple PAM_PROMPT_ECHO_OFF in a single
* login_conv call but just in case see if
* reply was modified anyway.
*/
if (reply_used) {
int i;
for (i = 0; i < num_msg; ++i) {
if (reply[i].resp != NULL) {
free(reply[i].resp);
}
}
}
free(reply);
*response = NULL;
return (PAM_BUF_ERR);
}
}
/*
* Always set reply_used even if the password is NULL
* A NULL response cannot be returned with PAM_SUCCESS.
* Could return PAN_CONV_ERR but let later code determine
* the effect of a NULL password.
*/
reply_used = 1;
break;
/* For empty user name, shouldn't happen */
case PAM_PROMPT_ECHO_ON:
/*
* It may be the case that some modules won't free
* the pam_response memory if the return is not
* PAM_SUCCESS. We should not have had
* multiple PAM_PROMPT_ECHO_OFF in a single
* login_conv call but just in case see if
* reply was modified anyway.
*/
if (reply_used) {
int i;
for (i = 0; i < num_msg; ++i) {
if (reply[i].resp != NULL) {
free(reply[i].resp);
}
}
}
free(reply);
*response = NULL;
/*
* Have to return error here since PAM can loop
* when no username is provided
*/
break;
case PAM_ERROR_MSG:
case PAM_TEXT_INFO:
break;
default:
break;
}
}
/*
* The response may not always be freed by
* modules for PAM_ERROR_MSG, and PAM_TEXT_INFO
* So make sure it is freed if we didn't use it.
*/
if (!reply_used) {
free(reply);
reply = NULL;
}
*response = reply;
return (PAM_SUCCESS);
}
#endif
static auth_result_t get_crypted_password(const char* username, char* buffer, size_t size,
error_handler_t *error_handler) {
#if defined(AIX43) || defined(AIX51)
#define BUFSIZE 1024
char buf[BUFSIZE];
struct userpw *pw = NULL;
strncpy(buf, username, BUFSIZE);
pw = getuserpw(buf);
if(pw == NULL) {
error_handler->error(MSG_AUTHUSER_USER_UNKNOWN_S, username);
return JUTI_AUTH_FAILED;
} else {
strncpy(pw->upw_passwd, buffer, size);
return JUTI_AUTH_SUCCESS;
}
#else
struct passwd *pw = getpwnam(username);
if(pw == NULL ) {
error_handler->error(MSG_AUTHUSER_USER_UNKNOWN_S, username);
return JUTI_AUTH_FAILED;
#if !defined(JUTI_NO_SHADOW)
/* On linux the getpwnam returns the password "x" if the user has a shadow
entry and authuser is not started as user root */
} else if (strcmp("x", pw->pw_passwd) == 0) {
struct spwd *pres = NULL;
#ifdef DEBUG
printf("getpwnam did not return the crypted passwd, try getspnam\n");
#endif
pres = getspnam(username);
if(pres == NULL) {
error_handler->error(MSG_AUTHUSER_NO_SHADOW_ENTRY_S, username);
return JUTI_AUTH_FAILED;
}
strncpy(buffer, pres->sp_pwdp, size);
#endif
} else {
strncpy(buffer, pw->pw_passwd, size);
}
#endif
return JUTI_AUTH_SUCCESS;
}
static int add_group(struct group *grp, gid_t **groups_res, char*** group_names_res, int* ngroups_res, int* maxgroups_res) {
int ngroups = *ngroups_res;
int maxgroups = *maxgroups_res;
gid_t *groups = *groups_res;
char** group_names = *group_names_res;
if (ngroups >= maxgroups) {
gid_t *new_groups;
char **new_group_names;
maxgroups +=10;
new_groups = (gid_t*)realloc(groups, sizeof(gid_t) * maxgroups);
if (new_groups == NULL) {
return -1;
}
groups = new_groups;
new_group_names = (char**)realloc(group_names, sizeof(char*) * maxgroups);
if(new_group_names == NULL) {
return -1;
}
group_names = new_group_names;
}
groups[ngroups] = grp->gr_gid;
group_names[ngroups++] = strdup(grp->gr_name);
*ngroups_res = ngroups;
*maxgroups_res = maxgroups;
*groups_res = groups;
*group_names_res = group_names;
return 0;
}
static int juti_getgrouplist(const char *uname, gid_t agroup, gid_t **groups_res, char*** group_names_res, int *grpcnt)
{
struct group *grp;
int bail;
int ret = 0;
int maxgroups = 0;
gid_t *groups = NULL;
char **group_names = NULL;
int ngroups = 0;
int i = 0;
setgrent();
while ((grp = getgrent())) {
for (bail = 0, i = 0; bail == 0 && i < ngroups; i++) {
if (groups[i] == grp->gr_gid) {
bail = 1;
}
}
if (bail) {
continue;
}
if (grp->gr_gid == agroup) {
if(add_group(grp, &groups, &group_names, &ngroups, &maxgroups) != 0) {
ret = -1;
goto error;
}
continue;
}
for (i = 0; grp->gr_mem[i]; i++) {
if (strcmp(grp->gr_mem[i], uname) == 0) {
if(add_group(grp, &groups, &group_names, &ngroups, &maxgroups) != 0) {
ret = -1;
goto error;
}
}
}
}
endgrent();
error:
if (ret != 0) {
if (groups != NULL) {
free(groups);
}
for (i=0; i < ngroups; i++) {
free(group_names[i]);
}
if (group_names != NULL) {
free(group_names);
}
} else {
*groups_res = groups;
*group_names_res = group_names;
*grpcnt = ngroups;
}
return ret;
}
#else
/* WINDOWS is defined */
static int GetSidStrings(HANDLE hToken,
TOKEN_INFORMATION_CLASS TokenInfo,
char ***pppszSid,
char ***pppszGroupNames,
int *nStrings)
{
#define MAX_DOMAIN_NAME 1000
#define MAX_GROUP_NAME 1000
LPVOID pTokenInfo = NULL;
DWORD dwLength;
DWORD i;
int ret = 0;
SID_NAME_USE SidNameUse;
char szDomainName[MAX_DOMAIN_NAME];
char szGroupName[MAX_GROUP_NAME];
DWORD dwMaxDomainName = MAX_DOMAIN_NAME;
DWORD dwMaxGroupName = MAX_GROUP_NAME;
GetTokenInformation(hToken, TokenInfo,
(LPVOID)pTokenInfo, 0, &dwLength);
if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
ret = 1;
goto error;
}
pTokenInfo = (PTOKEN_OWNER)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, dwLength);
if(pTokenInfo == NULL) {
ret = 2;
goto error;
}
if(!GetTokenInformation(hToken, TokenInfo,
(LPVOID)pTokenInfo, dwLength, &dwLength)) {
ret = 3;
goto error;
}
if(TokenInfo == TokenOwner) {
TOKEN_OWNER *pTokenOwner = (TOKEN_OWNER*)pTokenInfo;
*pppszSid = (char**)malloc(sizeof(char*));
ConvertSidToStringSid(pTokenOwner->Owner, *pppszSid);
} else if(TokenInfo == TokenGroups) {
TOKEN_GROUPS *pTokenGroups = (TOKEN_GROUPS*)pTokenInfo;
*pppszSid = (char**)malloc(sizeof(char*) * pTokenGroups->GroupCount);
*pppszGroupNames = (char**)malloc(sizeof(char*) * pTokenGroups->GroupCount);
*nStrings = pTokenGroups->GroupCount;
for(i=0; i<pTokenGroups->GroupCount; i++) {
ConvertSidToStringSid(pTokenGroups->Groups[i].Sid, &((*pppszSid)[i]));
LookupAccountSid(NULL, pTokenGroups->Groups[i].Sid,
szGroupName, &dwMaxGroupName,
szDomainName, &dwMaxDomainName,
&SidNameUse);
(*pppszGroupNames)[i] = (char*)malloc(sizeof(char) * (strlen(szDomainName) + strlen(szGroupName) + 2));
sprintf((*pppszGroupNames)[i], "%s\\%s", szDomainName, szGroupName);
}
}
error:
HeapFree(GetProcessHeap(), 0, pTokenInfo);
return ret;
}
static auth_result_t do_windows_system_authentication(const char *username,
const char *password,
char ***pppszUID,
char ***pppszGID,
char ***pppszGroupNames,
int *nGIDs,
error_handler_t *error_handler)
{
auth_result_t ret = JUTI_AUTH_SUCCESS;
HANDLE hToken = INVALID_HANDLE_VALUE;
int nUIDs = 0;
char *domain;
char *userbuf;
char *user;
char *backslash;
char *buf = NULL;
DWORD buf_size = 0;
DOMAIN_CONTROLLER_INFO *pbuf;
DWORD dwRes;
/*
* username can be in format "domain\username", split it up.
*/
userbuf = strdup(username);
user=userbuf;
backslash = strchr(user, '\\');
if(backslash != NULL) {
domain = user;
user = backslash+1;
*backslash = '\0';
} else {
/*
* if no domain was provided, use primary domain by default
*/
dwRes = DsGetDcName(NULL, NULL, NULL, NULL,
DS_RETURN_FLAT_NAME|DS_PDC_REQUIRED, &pbuf);
if(dwRes == ERROR_SUCCESS && pbuf->DomainName != NULL) {
domain = pbuf->DomainName;
} else {
/*
* no primary domain, use local host as domain
*/
domain = ".";
}
}
if(!LogonUser(
user,
domain,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
&hToken)) {
ret = JUTI_AUTH_ERROR;
error_handler->error(MSG_AUTHUSER_WRONG_USER_OR_PASSWORD);
goto error;
}
GetSidStrings(hToken, TokenOwner, pppszUID, NULL, &nUIDs);
GetSidStrings(hToken, TokenGroups, pppszGID, pppszGroupNames, nGIDs);
CloseHandle(hToken);
error:
free(userbuf);
if(pbuf != NULL) {
NetApiBufferFree(pbuf);
}
return ret;
}
#endif
|