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 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
|
/*
* Copyright (c) 1990,1993 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <netatalk/endian.h>
#include <atalk/afp.h>
#include <atalk/compat.h>
#include <atalk/util.h>
#include <limits.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#ifdef TRU64
#include <netdb.h>
#include <arpa/inet.h>
#include <sia.h>
#include <siad.h>
extern void afp_get_cmdline( int *ac, char ***av );
#endif /* TRU64 */
#include <atalk/logger.h>
#include <atalk/server_ipc.h>
#include <atalk/uuid.h>
#include <atalk/globals.h>
#include "auth.h"
#include "uam_auth.h"
#include "switch.h"
#include "status.h"
#include "fork.h"
#include "extattrs.h"
#ifdef HAVE_ACLS
#include "acls.h"
#endif
int afp_version = 11;
static int afp_version_index;
uid_t uuid;
#if defined( sun ) && !defined( __svr4__ ) || defined( ultrix )
int *groups;
#define GROUPS_SIZE sizeof(int)
#else /* sun __svr4__ ultrix */
gid_t *groups;
#define GROUPS_SIZE sizeof(gid_t)
#endif /* sun ultrix */
int ngroups;
static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login,
&uam_login};
static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw,
&uam_changepw};
static struct uam_obj *afp_uam = NULL;
void status_versions(char *data,
#ifndef NO_DDP
const ASP asp,
#endif
const DSI *dsi)
{
char *start = data;
u_int16_t status;
int len, num, i, count = 0;
memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
for ( i = 0; i < num; i++ ) {
#ifndef NO_DDP
if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
#endif /* ! NO_DDP */
if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
count++;
}
data += ntohs( status );
*data++ = count;
for ( i = 0; i < num; i++ ) {
#ifndef NO_DDP
if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
#endif /* ! NO_DDP */
if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
len = strlen( afp_versions[ i ].av_name );
*data++ = len;
memcpy( data, afp_versions[ i ].av_name , len );
data += len;
}
status = htons( data - start );
memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status));
}
void status_uams(char *data, const char *authlist)
{
char *start = data;
u_int16_t status;
struct uam_obj *uams;
int len, num = 0;
memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status));
uams = &uam_login;
while ((uams = uams->uam_prev) != &uam_login) {
if (strstr(authlist, uams->uam_path))
num++;
}
data += ntohs( status );
*data++ = num;
while ((uams = uams->uam_prev) != &uam_login) {
if (strstr(authlist, uams->uam_path)) {
LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name);
len = strlen( uams->uam_name);
*data++ = len;
memcpy( data, uams->uam_name, len );
data += len;
}
}
/* icon offset */
status = htons(data - start);
memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status));
}
/* handle errors by closing the connection. this is only needed
* by the afp_* functions. */
static int send_reply(const AFPObj *obj, const int err)
{
if ((err == AFP_OK) || (err == AFPERR_AUTHCONT))
return err;
obj->reply(obj->handle, err);
obj->exit(0);
return AFP_OK;
}
static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
char *rbuf _U_, size_t *rbuflen)
{
*rbuflen = 0;
return AFPERR_PWDEXPR;
}
static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_,
char *rbuf _U_, size_t *rbuflen)
{
*rbuflen = 0;
return( AFPERR_NOOP );
}
static int set_auth_switch(int expired)
{
int i;
if (expired) {
/*
* BF: expired password handling
* to allow the user to change his/her password we have to allow login
* but every following call except for FPChangePassword will be thrown
* away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
* for clarifying this)
*/
for (i=0; i<=0xff; i++) {
uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL);
}
uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL);
uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
}
else {
afp_switch = postauth_switch;
switch (afp_version) {
case 33:
case 32:
#ifdef HAVE_ACLS
uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
#endif /* HAVE_ACLS */
uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL);
case 31:
uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL);
uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL);
uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL);
uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL);
case 30:
uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL);
uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL);
/* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
enumerate_ext */
uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL);
uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL);
uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL);
uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL);
uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL);
case 22:
/*
* If first connection to a server is done in classic AFP2.2 version is used
* but OSX uses AFP3.x FPzzz command !
*/
uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL);
break;
}
}
return AFP_OK;
}
#define GROUPSTR_BUFSIZE 1024
static const char *print_groups(int ngroups, gid_t *groups)
{
static char groupsstr[GROUPSTR_BUFSIZE];
int i;
char *s = groupsstr;
if (ngroups == 0)
return "-";
for (i = 0; (i < ngroups) && (s < &groupsstr[GROUPSTR_BUFSIZE]); i++) {
s += snprintf(s, &groupsstr[GROUPSTR_BUFSIZE] - s, " %u", groups[i]);
}
return groupsstr;
}
static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
{
#ifdef ADMIN_GRP
int admin = 0;
#endif /* ADMIN_GRP */
if ( pwd->pw_uid == 0 ) { /* don't allow root login */
LOG(log_error, logtype_afpd, "login: root login denied!" );
return AFPERR_NOTAUTH;
}
LOG(log_note, logtype_afpd, "%s Login by %s",
afp_versions[afp_version_index].av_name, pwd->pw_name);
#ifndef NO_DDP
if (obj->proto == AFPPROTO_ASP) {
ASP asp = obj->handle;
int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
int addr_node = asp->asp_sat.sat_addr.s_node;
if (obj->options.authprintdir) {
if(addr_net && addr_node) { /* Do we have a valid Appletalk address? */
char nodename[256];
FILE *fp;
int mypid = getpid();
struct stat stat_buf;
sprintf(nodename, "%s/net%d.%dnode%d", obj->options.authprintdir,
addr_net / 256, addr_net % 256, addr_node);
LOG(log_info, logtype_afpd, "registering %s (uid %d) on %u.%u as %s",
pwd->pw_name, pwd->pw_uid, addr_net, addr_node, nodename);
if (stat(nodename, &stat_buf) == 0) { /* file exists */
if (S_ISREG(stat_buf.st_mode)) { /* normal file */
unlink(nodename);
fp = fopen(nodename, "w");
fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
fclose(fp);
chown( nodename, pwd->pw_uid, -1 );
} else { /* somebody is messing with us */
LOG(log_error, logtype_afpd, "print authfile %s is not a normal file, it will not be modified", nodename );
}
} else { /* file 'nodename' does not exist */
fp = fopen(nodename, "w");
fprintf(fp, "%s:%d\n", pwd->pw_name, mypid);
fclose(fp);
chown( nodename, pwd->pw_uid, -1 );
}
} /* if (addr_net && addr_node ) */
} /* if (options->authprintdir) */
} /* if (obj->proto == AFPPROTO_ASP) */
#endif
if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
#ifdef RUN_AS_USER
LOG(log_info, logtype_afpd, "running with uid %d", geteuid());
#else /* RUN_AS_USER */
LOG(log_error, logtype_afpd, "login: %s", strerror(errno));
return AFPERR_BADUAM;
#endif /* RUN_AS_USER */
}
/* Basically if the user is in the admin group, we stay root */
if (( ngroups = getgroups( 0, NULL )) < 0 ) {
LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
return AFPERR_BADUAM;
}
if ( NULL == (groups = calloc(ngroups, GROUPS_SIZE)) ) {
LOG(log_error, logtype_afpd, "login: %s calloc: %d", ngroups);
return AFPERR_BADUAM;
}
if (( ngroups = getgroups( ngroups, groups )) < 0 ) {
LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
return AFPERR_BADUAM;
}
#ifdef ADMIN_GRP
LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
if (obj->options.admingid != 0) {
int i;
for (i = 0; i < ngroups; i++) {
if (groups[i] == obj->options.admingid) admin = 1;
}
}
if (admin) {
ad_setfuid(0);
LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name );
}
if (!admin)
#endif /* ADMIN_GRP */
#ifdef TRU64
{
struct DSI *dsi = obj->handle;
struct hostent *hp;
char *clientname;
int argc;
char **argv;
char hostname[256];
afp_get_cmdline( &argc, &argv );
hp = gethostbyaddr( (char *) &dsi->client.sin_addr,
sizeof( struct in_addr ),
dsi->client.sin_family );
if( hp )
clientname = hp->h_name;
else
clientname = inet_ntoa( dsi->client.sin_addr );
sprintf( hostname, "%s@%s", pwd->pw_name, clientname );
if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name,
NULL, FALSE, NULL, NULL,
SIA_BEU_REALLOGIN ) != SIASUCCESS )
return AFPERR_BADUAM;
LOG(log_info, logtype_afpd, "session from %s (%s)", hostname,
inet_ntoa( dsi->client.sin_addr ) );
if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
return AFPERR_BADUAM;
}
}
#else /* TRU64 */
if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
return AFPERR_BADUAM;
}
#endif /* TRU64 */
LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(ngroups, groups));
/* There's probably a better way to do this, but for now, we just play root */
#ifdef ADMIN_GRP
if (admin)
uuid = 0;
else
#endif /* ADMIN_GRP */
uuid = pwd->pw_uid;
set_auth_switch(expired);
/* save our euid, we need it for preexec_close */
obj->uid = geteuid();
obj->logout = logout;
#ifdef FORCE_UIDGID
obj->force_uid = 1;
save_uidgid ( &obj->uidgid );
#endif
return( AFP_OK );
}
/* ---------------------- */
int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
{
uint32_t data;
DSI *dsi = (DSI *)AFPobj->handle;
*rbuflen = 0;
ibuf += 2;
ibuflen -= 2;
if (ibuflen < 4)
return AFPERR_MISC;
memcpy(&data, ibuf, 4); /* flag */
data = ntohl(data);
/*
* Possible sleeping states:
* 1) normal sleep: DSI_SLEEPING (up to 10.3)
* 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4)
*/
if (data & AFPZZZ_EXT_WAKEUP) {
/* wakeup request from exetended sleep */
if (dsi->flags & DSI_EXTSLEEP) {
LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep");
dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP);
}
} else {
/* sleep request */
dsi->flags |= DSI_SLEEPING;
if (data & AFPZZZ_EXT_SLEEP) {
LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep");
dsi->flags |= DSI_EXTSLEEP;
} else {
LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep");
}
}
/*
* According to AFP 3.3 spec we should not return anything,
* but eg 10.5.8 server still returns the numbers of hours
* the server is keeping the sessino (ie max sleeptime).
*/
data = obj->options.sleep / 120; /* hours */
if (!data) {
data = 1;
}
*rbuflen = sizeof(data);
data = htonl(data);
memcpy(rbuf, &data, sizeof(data));
rbuf += sizeof(data);
return AFP_OK;
}
/* ---------------------- */
static int create_session_token(AFPObj *obj)
{
pid_t pid;
/* use 8 bytes for token as OSX, don't know if it helps */
if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
return AFPERR_MISC;
}
if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
return AFPERR_MISC;
memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
pid = getpid();
memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
return 0;
}
static int create_session_key(AFPObj *obj)
{
/* create session key */
if (obj->sinfo.sessionkey == NULL) {
if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
return AFPERR_MISC;
uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
}
return AFP_OK;
}
/* ---------------------- */
int afp_getsession(
AFPObj *obj,
char *ibuf, size_t ibuflen,
char *rbuf, size_t *rbuflen)
{
u_int16_t type;
u_int32_t idlen = 0;
u_int32_t boottime;
u_int32_t tklen, tp;
char *token;
char *p;
*rbuflen = 0;
tklen = 0;
if (ibuflen < 2 + sizeof(type)) {
return AFPERR_PARAM;
}
ibuf += 2;
ibuflen -= 2;
memcpy(&type, ibuf, sizeof(type));
type = ntohs(type);
ibuf += sizeof(type);
ibuflen -= sizeof(type);
if ( obj->sinfo.sessiontoken == NULL ) {
if ( create_session_token( obj ) )
return AFPERR_MISC;
}
/*
*
*/
switch (type) {
case 0: /* old version ?*/
tklen = obj->sinfo.sessiontoken_len;
token = obj->sinfo.sessiontoken;
break;
case 1: /* disconnect */
case 2: /* reconnect update id */
if (ibuflen >= sizeof(idlen)) {
memcpy(&idlen, ibuf, sizeof(idlen));
idlen = ntohl(idlen);
ibuf += sizeof(idlen);
ibuflen -= sizeof(idlen);
if (ibuflen < idlen) {
return AFPERR_PARAM;
}
/* memcpy (id, ibuf, idlen) */
tklen = obj->sinfo.sessiontoken_len;
token = obj->sinfo.sessiontoken;
}
break;
case 3:
case 4:
if (ibuflen >= 8 ) {
p = ibuf;
memcpy( &idlen, ibuf, sizeof(idlen));
idlen = ntohl(idlen);
ibuf += sizeof(idlen);
ibuflen -= sizeof(idlen);
ibuf += sizeof(boottime);
ibuflen -= sizeof(boottime);
if (ibuflen < idlen || idlen > (90-10)) {
return AFPERR_PARAM;
}
if (!obj->sinfo.clientid) {
obj->sinfo.clientid = malloc(idlen + 8);
memcpy(obj->sinfo.clientid, p, idlen + 8);
obj->sinfo.clientid_len = idlen + 8;
}
if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0)
return AFPERR_MISC;
tklen = obj->sinfo.sessiontoken_len;
token = obj->sinfo.sessiontoken;
}
break;
case 8: /* Panther Kerberos Token */
tklen = obj->sinfo.cryptedkey_len;
token = obj->sinfo.cryptedkey;
break;
default:
return AFPERR_NOOP;
break;
}
if (tklen == 0)
return AFPERR_MISC;
tp = htonl(tklen);
memcpy(rbuf, &tp, sizeof(tklen));
rbuf += sizeof(tklen);
*rbuflen += sizeof(tklen);
memcpy(rbuf, token, tklen);
*rbuflen += tklen;
return AFP_OK;
}
/* ---------------------- */
int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
{
DSI *dsi = (DSI *)obj->handle;
u_int16_t type;
u_int32_t tklen;
pid_t token;
int i;
*rbuflen = 0;
ibuf += 2;
#if 0
/* check for guest user */
if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
return AFPERR_MISC;
}
#endif
memcpy(&type, ibuf, sizeof(type));
type = ntohs(type);
ibuf += sizeof(type);
memcpy(&tklen, ibuf, sizeof(tklen));
tklen = ntohl(tklen);
ibuf += sizeof(tklen);
if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
return AFPERR_MISC;
}
if (tklen != SESSIONTOKEN_LEN) {
return AFPERR_MISC;
}
tklen = sizeof(pid_t);
memcpy(&token, ibuf, tklen);
/* our stuff is pid + zero pad */
ibuf += tklen;
for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
if (*ibuf != 0) {
return AFPERR_MISC;
}
}
LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect");
dsi->flags |= DSI_RECONINPROG;
/* Deactivate tickle timer */
const struct itimerval none = {{0, 0}, {0, 0}};
setitimer(ITIMER_REAL, &none, NULL);
/* check for old session, possibly transfering session from here to there */
if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0)
goto exit;
/* write uint16_t DSI request ID */
if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) {
LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID");
goto exit;
}
/* now send our connected AFP client socket */
if (send_fd(obj->ipc_fd, dsi->socket) != 0)
goto exit;
/* Now see what happens: either afpd master sends us SIGTERM because our session */
/* has been transfered to a old disconnected session, or we continue */
sleep(5);
if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */
/* Reconnect succeeded, we exit now after sleeping some more */
sleep(2); /* sleep some more to give the recon. session time */
LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded");
exit(0);
}
exit:
/* Reinstall tickle timer */
setitimer(ITIMER_REAL, &dsi->timer, NULL);
LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed");
return AFPERR_MISC;
}
/* ---------------------- */
static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
{
int num,i;
if (!len || len > ibuflen)
return AFPERR_BADVERS;
num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]);
for ( i = 0; i < num; i++ ) {
if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) {
afp_version = afp_versions[ i ].av_number;
afp_version_index = i;
break;
}
}
if ( i == num ) /* An inappropo version */
return AFPERR_BADVERS ;
if (afp_version >= 30 && obj->proto != AFPPROTO_DSI)
return AFPERR_BADVERS ;
/* FIXME Hack */
if (afp_version >= 30 && sizeof(off_t) != 8) {
LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" );
return AFPERR_BADVERS ;
}
return 0;
}
/* ---------------------- */
int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
{
struct passwd *pwd = NULL;
size_t len;
int i;
*rbuflen = 0;
if ( nologin & 1)
return send_reply(obj, AFPERR_SHUTDOWN );
if (ibuflen < 2)
return send_reply(obj, AFPERR_BADVERS );
ibuf++;
len = (unsigned char) *ibuf++;
ibuflen -= 2;
i = get_version(obj, ibuf, ibuflen, len);
if (i)
return send_reply(obj, i );
if (ibuflen <= len)
return send_reply(obj, AFPERR_BADUAM);
ibuf += len;
ibuflen -= len;
len = (unsigned char) *ibuf++;
ibuflen--;
if (!len || len > ibuflen)
return send_reply(obj, AFPERR_BADUAM);
if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) )
return send_reply(obj, AFPERR_BADUAM);
ibuf += len;
ibuflen -= len;
if (AFP_OK != (i = create_session_key(obj)) )
return send_reply(obj, i);
i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
return send_reply(obj, i);
return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
}
/* ---------------------- */
int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
{
struct passwd *pwd = NULL;
size_t len;
int i;
char type;
u_int16_t len16;
char *username;
*rbuflen = 0;
if ( nologin & 1)
return send_reply(obj, AFPERR_SHUTDOWN );
if (ibuflen < 5)
return send_reply(obj, AFPERR_BADVERS );
ibuf++;
ibuf++; /* pad */
ibuf +=2; /* flag */
len = (unsigned char) *ibuf;
ibuf++;
ibuflen -= 5;
i = get_version(obj, ibuf, ibuflen, len);
if (i)
return send_reply(obj, i );
if (ibuflen <= len)
return send_reply(obj, AFPERR_BADUAM);
ibuf += len;
ibuflen -= len;
len = (unsigned char) *ibuf;
ibuf++;
ibuflen--;
if (!len || len > ibuflen)
return send_reply(obj, AFPERR_BADUAM);
if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL)
return send_reply(obj, AFPERR_BADUAM);
ibuf += len;
ibuflen -= len;
if (!afp_uam->u.uam_login.login_ext) {
LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name );
return send_reply(obj, AFPERR_BADUAM);
}
/* user name */
if (ibuflen <= 1 +sizeof(len16))
return send_reply(obj, AFPERR_PARAM);
type = *ibuf;
username = ibuf;
ibuf++;
ibuflen--;
if (type != 3)
return send_reply(obj, AFPERR_PARAM);
memcpy(&len16, ibuf, sizeof(len16));
ibuf += sizeof(len16);
ibuflen -= sizeof(len16);
len = ntohs(len16);
if (len > ibuflen)
return send_reply(obj, AFPERR_PARAM);
ibuf += len;
ibuflen -= len;
/* directory service name */
if (!ibuflen)
return send_reply(obj, AFPERR_PARAM);
type = *ibuf;
ibuf++;
ibuflen--;
switch(type) {
case 1:
case 2:
if (!ibuflen)
return send_reply(obj, AFPERR_PARAM);
len = (unsigned char) *ibuf;
ibuf++;
ibuflen--;
break;
case 3:
/* With "No User Authen" it is equal */
if (ibuflen < sizeof(len16))
return send_reply(obj, AFPERR_PARAM);
memcpy(&len16, ibuf, sizeof(len16));
ibuf += sizeof(len16);
ibuflen -= sizeof(len16);
len = ntohs(len16);
break;
default:
return send_reply(obj, AFPERR_PARAM);
}
#if 0
if (len != 0) {
LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" );
return send_reply(obj, AFPERR_PARAM);
}
#endif
ibuf += len;
ibuflen -= len;
/* Pad */
if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */
ibuf++;
ibuflen--;
}
if (AFP_OK != (i = create_session_key(obj)) ) {
return send_reply(obj, i);
}
/* FIXME user name are in UTF8 */
i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
return send_reply(obj, i);
return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
}
/* ---------------------- */
int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
{
struct passwd *pwd = NULL;
int err;
if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
*rbuflen = 0;
return send_reply(obj, AFPERR_NOTAUTH );
}
ibuf += 2; ibuflen -= 2;
err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
rbuf, rbuflen);
if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
return send_reply(obj, err);
return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
}
int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
{
DSI *dsi = (DSI *)(obj->handle);
LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
of_close_all_forks();
close_all_vol();
dsi->flags = DSI_AFP_LOGGED_OUT;
*rbuflen = 0;
return AFP_OK;
}
/* change password --
* NOTE: an FPLogin must already have completed successfully for this
* to work. this also does a little pre-processing before it hands
* it off to the uam.
*/
int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
{
char username[MACFILELEN + 1], *start = ibuf;
struct uam_obj *uam;
struct passwd *pwd;
size_t len;
int ret;
*rbuflen = 0;
ibuf += 2;
/* check if password change is allowed, OS-X ignores the flag.
* we shouldn't trust the client on this anyway.
* not sure about the "right" error code, NOOP for now */
if (!(obj->options.passwdbits & PASSWD_SET))
return AFPERR_NOOP;
/* make sure we can deal w/ this uam */
len = (unsigned char) *ibuf++;
if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
return AFPERR_BADUAM;
ibuf += len;
if ((len + 1) & 1) /* pad byte */
ibuf++;
if ( afp_version < 30) {
len = (unsigned char) *ibuf++;
if ( len > sizeof(username) - 1) {
return AFPERR_PARAM;
}
memcpy(username, ibuf, len);
username[ len ] = '\0';
ibuf += len;
if ((len + 1) & 1) /* pad byte */
ibuf++;
} else {
/* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */
if ( ibuf[0] != '\0' || ibuf[1] != '\0')
return AFPERR_PARAM;
ibuf += 2;
len = MIN(sizeof(username), strlen(obj->username));
memcpy(username, obj->username, len);
username[ len ] = '\0';
}
LOG(log_info, logtype_afpd, "changing password for <%s>", username);
if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL )
return AFPERR_PARAM;
/* send it off to the uam. we really don't use ibuflen right now. */
if (ibuflen < (size_t)(ibuf - start))
return AFPERR_PARAM;
ibuflen -= (ibuf - start);
ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
rbuf, rbuflen);
LOG(log_info, logtype_afpd, "password change %s.",
(ret == AFPERR_AUTHCONT) ? "continued" :
(ret ? "failed" : "succeeded"));
if ( ret == AFP_OK )
set_auth_switch(0);
return ret;
}
/* FPGetUserInfo */
int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
{
u_int8_t thisuser;
u_int32_t id;
u_int16_t bitmap;
char *bitmapp;
LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:");
*rbuflen = 0;
ibuf++;
thisuser = *ibuf++;
ibuf += sizeof(id); /* userid is not used in AFP 2.0 */
memcpy(&bitmap, ibuf, sizeof(bitmap));
bitmap = ntohs(bitmap);
/* deal with error cases. we don't have to worry about
* AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always
* succeed. */
if (!thisuser)
return AFPERR_PARAM;
if ((bitmap & USERIBIT_ALL) != bitmap)
return AFPERR_BITMAP;
/* remember place where we store the possibly modified bitmap later */
memcpy(rbuf, ibuf, sizeof(bitmap));
bitmapp = rbuf;
rbuf += sizeof(bitmap);
*rbuflen = sizeof(bitmap);
/* copy the user/group info */
if (bitmap & USERIBIT_USER) {
id = htonl(geteuid());
memcpy(rbuf, &id, sizeof(id));
rbuf += sizeof(id);
*rbuflen += sizeof(id);
}
if (bitmap & USERIBIT_GROUP) {
id = htonl(getegid());
memcpy(rbuf, &id, sizeof(id));
rbuf += sizeof(id);
*rbuflen += sizeof(id);
}
if (bitmap & USERIBIT_UUID) {
if ( ! (obj->options.flags & OPTION_UUID)) {
bitmap &= ~USERIBIT_UUID;
bitmap = htons(bitmap);
memcpy(bitmapp, &bitmap, sizeof(bitmap));
} else {
LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username);
int ret;
atalk_uuid_t uuid;
ret = getuuidfromname( obj->username, UUID_USER, uuid);
if (ret != 0) {
LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !");
return AFPERR_NOITEM;
}
LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid));
memcpy(rbuf, uuid, UUID_BINSIZE);
rbuf += UUID_BINSIZE;
*rbuflen += UUID_BINSIZE;
}
}
LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
return AFP_OK;
}
#define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \
(((type) == UAM_SERVER_CHANGEPW) ? \
&uam_changepw : NULL))
/* just do a linked list search. this could be sped up with a hashed
* list, but i doubt anyone's going to have enough uams to matter. */
struct uam_obj *auth_uamfind(const int type, const char *name,
const int len)
{
struct uam_obj *prev, *start;
if (!name || !(start = UAM_LIST(type)))
return NULL;
prev = start;
while ((prev = prev->uam_prev) != start)
if (strndiacasecmp(prev->uam_name, name, len) == 0)
return prev;
return NULL;
}
int auth_register(const int type, struct uam_obj *uam)
{
struct uam_obj *start;
if (!uam || !uam->uam_name || (*uam->uam_name == '\0'))
return -1;
if (!(start = UAM_LIST(type)))
return 1; /* we don't know what to do with it, caller must free it */
uam_attach(start, uam);
return 0;
}
/* load all of the modules */
int auth_load(const char *path, const char *list)
{
char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p;
struct uam_mod *mod;
struct stat st;
size_t len;
if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
return -1;
strlcpy(buf, list, sizeof(buf));
if ((p = strtok(buf, ",")) == NULL)
return -1;
strcpy(name, path);
if (name[len - 1] != '/') {
strcat(name, "/");
len++;
}
while (p) {
strlcpy(name + len, p, sizeof(name) - len);
LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
/*
if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {
*/
if (stat(name, &st) == 0) {
if ((mod = uam_load(name, p))) {
uam_attach(&uam_modules, mod);
LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
} else {
LOG(log_error, logtype_afpd, "uam: %s load failure",p);
}
} else {
LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));
}
p = strtok(NULL, ",");
}
return 0;
}
/* get rid of all of the uams */
void auth_unload(void)
{
struct uam_mod *mod, *prev, *start = &uam_modules;
prev = start->uam_prev;
while ((mod = prev) != start) {
prev = prev->uam_prev;
uam_detach(mod);
uam_unload(mod);
}
}
|