1/* 2 * Copyright (c) 1990,1993 Regents of The University of Michigan. 3 * All Rights Reserved. See COPYRIGHT. 4 */ 5 6#ifdef HAVE_CONFIG_H 7#include "config.h" 8#endif /* HAVE_CONFIG_H */ 9 10#include <stdio.h> 11#include <stdlib.h> 12#include <errno.h> 13#include <unistd.h> 14#include <sys/types.h> 15#include <sys/param.h> 16#include <sys/stat.h> 17#include <arpa/inet.h> 18 19#include <atalk/afp.h> 20#include <atalk/compat.h> 21#include <atalk/util.h> 22#include <limits.h> 23#include <string.h> 24#include <ctype.h> 25#include <time.h> 26#include <pwd.h> 27#include <grp.h> 28 29#ifdef TRU64 30#include <netdb.h> 31#include <arpa/inet.h> 32#include <sia.h> 33#include <siad.h> 34 35extern void afp_get_cmdline( int *ac, char ***av ); 36#endif /* TRU64 */ 37 38#include <atalk/logger.h> 39#include <atalk/server_ipc.h> 40#include <atalk/uuid.h> 41#include <atalk/globals.h> 42#include <atalk/unix.h> 43 44#include "auth.h" 45#include "uam_auth.h" 46#include "switch.h" 47#include "status.h" 48#include "fork.h" 49#include "extattrs.h" 50#ifdef HAVE_ACLS 51#include "acls.h" 52#endif 53 54static int afp_version_index; 55static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules}; 56static struct uam_obj uam_login = {"", "", 0, {{NULL, NULL, NULL, NULL }}, &uam_login, 57 &uam_login}; 58static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &uam_changepw, 59 &uam_changepw}; 60 61static struct uam_obj *afp_uam = NULL; 62 63 64void status_versions( char *data, const DSI *dsi) 65{ 66 char *start = data; 67 uint16_t status; 68 int len, num, i, count = 0; 69 70 memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status)); 71 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] ); 72 73 for ( i = 0; i < num; i++ ) { 74 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue; 75 count++; 76 } 77 data += ntohs( status ); 78 *data++ = count; 79 80 for ( i = 0; i < num; i++ ) { 81 if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue; 82 len = strlen( afp_versions[ i ].av_name ); 83 *data++ = len; 84 memcpy( data, afp_versions[ i ].av_name , len ); 85 data += len; 86 } 87 status = htons( data - start ); 88 memcpy(start + AFPSTATUS_UAMSOFF, &status, sizeof(status)); 89} 90 91void status_uams(char *data, const char *authlist) 92{ 93 char *start = data; 94 uint16_t status; 95 struct uam_obj *uams; 96 int len, num = 0; 97 98 memcpy(&status, start + AFPSTATUS_UAMSOFF, sizeof(status)); 99 uams = &uam_login; 100 while ((uams = uams->uam_prev) != &uam_login) { 101 if (strstr(authlist, uams->uam_path)) 102 num++; 103 } 104 105 data += ntohs( status ); 106 *data++ = num; 107 while ((uams = uams->uam_prev) != &uam_login) { 108 if (strstr(authlist, uams->uam_path)) { 109 LOG(log_info, logtype_afpd, "uam: \"%s\" available", uams->uam_name); 110 len = strlen( uams->uam_name); 111 *data++ = len; 112 memcpy( data, uams->uam_name, len ); 113 data += len; 114 } 115 } 116 117 /* icon offset */ 118 status = htons(data - start); 119 memcpy(start + AFPSTATUS_ICONOFF, &status, sizeof(status)); 120} 121 122/* handle errors by closing the connection. this is only needed 123 * by the afp_* functions. */ 124static int send_reply(const AFPObj *obj, const int err) 125{ 126 if ((err == AFP_OK) || (err == AFPERR_AUTHCONT)) 127 return err; 128 129 obj->reply(obj->dsi, err); 130 obj->exit(0); 131 132 return AFP_OK; 133} 134 135static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 136 char *rbuf _U_, size_t *rbuflen) 137{ 138 *rbuflen = 0; 139 return AFPERR_PWDEXPR; 140} 141 142static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 143 char *rbuf _U_, size_t *rbuflen) 144{ 145 *rbuflen = 0; 146 return( AFPERR_NOOP ); 147} 148 149static int set_auth_switch(const AFPObj *obj, int expired) 150{ 151 int i; 152 153 if (expired) { 154 /* 155 * BF: expired password handling 156 * to allow the user to change his/her password we have to allow login 157 * but every following call except for FPChangePassword will be thrown 158 * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple 159 * for clarifying this) 160 */ 161 162 for (i=0; i<=0xff; i++) { 163 uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL); 164 } 165 uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL); 166 uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL); 167 } 168 else { 169 afp_switch = postauth_switch; 170 switch (obj->afp_version) { 171 172 case 33: 173 case 32: 174#ifdef HAVE_ACLS 175 uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL); 176 uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL); 177 uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL); 178#endif /* HAVE_ACLS */ 179 uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL); 180 uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL); 181 uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL); 182 uam_afpserver_action(AFP_LISTEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_listextattr, NULL); 183 184 case 31: 185 uam_afpserver_action(AFP_SYNCDIR, UAM_AFPSERVER_POSTAUTH, afp_syncdir, NULL); 186 uam_afpserver_action(AFP_SYNCFORK, UAM_AFPSERVER_POSTAUTH, afp_syncfork, NULL); 187 uam_afpserver_action(AFP_SPOTLIGHT_PRIVATE, UAM_AFPSERVER_POSTAUTH, afp_null_nolog, NULL); 188 uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL); 189 190 case 30: 191 uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL); 192 uam_afpserver_action(AFP_BYTELOCK_EXT, UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL); 193 /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue 194 enumerate_ext */ 195 uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL); 196 uam_afpserver_action(AFP_GETSESSTOKEN, UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL); 197 uam_afpserver_action(AFP_READ_EXT, UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL); 198 uam_afpserver_action(AFP_WRITE_EXT, UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL); 199 uam_afpserver_action(AFP_DISCTOLDSESS, UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL); 200 201 case 22: 202 /* 203 * If first connection to a server is done in classic AFP2.2 version is used 204 * but OSX uses AFP3.x FPzzz command ! 205 */ 206 uam_afpserver_action(AFP_ZZZ, UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL); 207 break; 208 } 209 } 210 211 return AFP_OK; 212} 213 214static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired) 215{ 216#ifdef ADMIN_GRP 217 int admin = 0; 218#endif /* ADMIN_GRP */ 219#if 0 //alow root login Edison 20130520 220 if ( pwd->pw_uid == 0 ) { /* don't allow root login */ 221 LOG(log_error, logtype_afpd, "login: root login denied!" ); 222 return AFPERR_NOTAUTH; 223 } 224#endif 225 226 LOG(log_note, logtype_afpd, "%s Login by %s", 227 afp_versions[afp_version_index].av_name, pwd->pw_name); 228 229 if (set_groups(obj, pwd) != 0) 230 return AFPERR_BADUAM; 231 232#ifdef ADMIN_GRP 233 LOG(log_debug, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid); 234 235 if (obj->options.admingid != 0) { 236 int i; 237 for (i = 0; i < obj->ngroups; i++) { 238 if (obj->groups[i] == obj->options.admingid) admin = 1; 239 } 240 } 241 if (admin) { 242 ad_setfuid(0); 243 LOG(log_info, logtype_afpd, "admin login -- %s", pwd->pw_name ); 244 } 245 if (!admin) 246#endif /* ADMIN_GRP */ 247#ifdef TRU64 248 { 249 struct DSI *dsi = obj->handle; 250 struct hostent *hp; 251 char *clientname; 252 int argc; 253 char **argv; 254 char hostname[256]; 255 256 afp_get_cmdline( &argc, &argv ); 257 258 hp = gethostbyaddr( (char *) &dsi->client.sin_addr, 259 sizeof( struct in_addr ), 260 dsi->client.sin_family ); 261 262 if( hp ) 263 clientname = hp->h_name; 264 else 265 clientname = inet_ntoa( dsi->client.sin_addr ); 266 267 sprintf( hostname, "%s@%s", pwd->pw_name, clientname ); 268 269 if( sia_become_user( NULL, argc, argv, hostname, pwd->pw_name, 270 NULL, FALSE, NULL, NULL, 271 SIA_BEU_REALLOGIN ) != SIASUCCESS ) 272 return AFPERR_BADUAM; 273 274 LOG(log_info, logtype_afpd, "session from %s (%s)", hostname, 275 inet_ntoa( dsi->client.sin_addr ) ); 276 277 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) { 278 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) ); 279 return AFPERR_BADUAM; 280 } 281 } 282#else /* TRU64 */ 283 if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) { 284 LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) ); 285 return AFPERR_BADUAM; 286 } 287#endif /* TRU64 */ 288 289 LOG(log_debug, logtype_afpd, "login: supplementary groups: %s", print_groups(obj->ngroups, obj->groups)); 290 291 /* There's probably a better way to do this, but for now, we just play root */ 292#ifdef ADMIN_GRP 293 if (admin) 294 obj->uid = 0; 295 else 296#endif /* ADMIN_GRP */ 297 obj->uid = geteuid(); 298 299 set_auth_switch(obj, expired); 300 /* save our euid, we need it for preexec_close */ 301 obj->uid = geteuid(); 302 obj->logout = logout; 303 304 /* pam_umask or similar might have changed our umask */ 305 (void)umask(obj->options.umask); 306 307 /* Some PAM module might have reset our signal handlers and timer, so we need to reestablish them */ 308 afp_over_dsi_sighandlers(obj); 309 310 return( AFP_OK ); 311} 312 313/* ---------------------- */ 314int afp_zzz(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) 315{ 316 uint32_t data; 317 DSI *dsi = (DSI *)AFPobj->dsi; 318 319 *rbuflen = 0; 320 ibuf += 2; 321 ibuflen -= 2; 322 323 if (ibuflen < 4) 324 return AFPERR_MISC; 325 memcpy(&data, ibuf, 4); /* flag */ 326 data = ntohl(data); 327 328 /* 329 * Possible sleeping states: 330 * 1) normal sleep: DSI_SLEEPING (up to 10.3) 331 * 2) extended sleep: DSI_SLEEPING | DSI_EXTSLEEP (starting with 10.4) 332 */ 333 334 if (data & AFPZZZ_EXT_WAKEUP) { 335 /* wakeup request from exetended sleep */ 336 if (dsi->flags & DSI_EXTSLEEP) { 337 LOG(log_note, logtype_afpd, "afp_zzz: waking up from extended sleep"); 338 dsi->flags &= ~(DSI_SLEEPING | DSI_EXTSLEEP); 339 ipc_child_state(obj, DSI_RUNNING); 340 } 341 } else { 342 /* sleep request */ 343 dsi->flags |= DSI_SLEEPING; 344 if (data & AFPZZZ_EXT_SLEEP) { 345 LOG(log_note, logtype_afpd, "afp_zzz: entering extended sleep"); 346 dsi->flags |= DSI_EXTSLEEP; 347 ipc_child_state(obj, DSI_EXTSLEEP); 348 } else { 349 LOG(log_note, logtype_afpd, "afp_zzz: entering normal sleep"); 350 ipc_child_state(obj, DSI_SLEEPING); 351 } 352 } 353 354 /* 355 * According to AFP 3.3 spec we should not return anything, 356 * but eg 10.5.8 server still returns the numbers of hours 357 * the server is keeping the sessino (ie max sleeptime). 358 */ 359 data = obj->options.sleep / 120; /* hours */ 360 if (!data) { 361 data = 1; 362 } 363 *rbuflen = sizeof(data); 364 data = htonl(data); 365 memcpy(rbuf, &data, sizeof(data)); 366 rbuf += sizeof(data); 367 368 return AFP_OK; 369} 370 371/* ---------------------- */ 372static int create_session_token(AFPObj *obj) 373{ 374 pid_t pid; 375 376 /* use 8 bytes for token as OSX, don't know if it helps */ 377 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) { 378 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN ); 379 return AFPERR_MISC; 380 } 381 382 if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) ) 383 return AFPERR_MISC; 384 385 memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN); 386 obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN; 387 pid = getpid(); 388 memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t)); 389 390 return 0; 391} 392 393static int create_session_key(AFPObj *obj) 394{ 395 /* create session key */ 396 if (obj->sinfo.sessionkey == NULL) { 397 if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) ) 398 return AFPERR_MISC; 399 uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN); 400 obj->sinfo.sessionkey_len = SESSIONKEY_LEN; 401 } 402 return AFP_OK; 403} 404 405 406/* ---------------------- */ 407int afp_getsession( 408 AFPObj *obj, 409 char *ibuf, size_t ibuflen, 410 char *rbuf, size_t *rbuflen) 411{ 412 uint16_t type; 413 uint32_t idlen = 0; 414 uint32_t boottime; 415 uint32_t tklen, tp; 416 char *token; 417 char *p; 418 419 *rbuflen = 0; 420 tklen = 0; 421 422 if (ibuflen < 2 + sizeof(type)) { 423 return AFPERR_PARAM; 424 } 425 426 ibuf += 2; 427 ibuflen -= 2; 428 429 memcpy(&type, ibuf, sizeof(type)); 430 type = ntohs(type); 431 ibuf += sizeof(type); 432 ibuflen -= sizeof(type); 433 434 if ( obj->sinfo.sessiontoken == NULL ) { 435 if ( create_session_token( obj ) ) 436 return AFPERR_MISC; 437 } 438 439 /* 440 * 441 */ 442 switch (type) { 443 case 0: /* old version ?*/ 444 tklen = obj->sinfo.sessiontoken_len; 445 token = obj->sinfo.sessiontoken; 446 break; 447 case 1: /* disconnect */ 448 case 2: /* reconnect update id */ 449 if (ibuflen >= sizeof(idlen)) { 450 memcpy(&idlen, ibuf, sizeof(idlen)); 451 idlen = ntohl(idlen); 452 ibuf += sizeof(idlen); 453 ibuflen -= sizeof(idlen); 454 if (ibuflen < idlen) { 455 return AFPERR_PARAM; 456 } 457 /* memcpy (id, ibuf, idlen) */ 458 tklen = obj->sinfo.sessiontoken_len; 459 token = obj->sinfo.sessiontoken; 460 } 461 break; 462 case 3: 463 case 4: 464 if (ibuflen >= 8 ) { 465 p = ibuf; 466 memcpy( &idlen, ibuf, sizeof(idlen)); 467 idlen = ntohl(idlen); 468 ibuf += sizeof(idlen); 469 ibuflen -= sizeof(idlen); 470 ibuf += sizeof(boottime); 471 ibuflen -= sizeof(boottime); 472 if (ibuflen < idlen || idlen > (90-10)) { 473 return AFPERR_PARAM; 474 } 475 if (!obj->sinfo.clientid) { 476 obj->sinfo.clientid = malloc(idlen + 8); 477 memcpy(obj->sinfo.clientid, p, idlen + 8); 478 obj->sinfo.clientid_len = idlen + 8; 479 } 480 if (ipc_child_write(obj->ipc_fd, IPC_GETSESSION, idlen+8, p) != 0) 481 return AFPERR_MISC; 482 tklen = obj->sinfo.sessiontoken_len; 483 token = obj->sinfo.sessiontoken; 484 } 485 break; 486 case 8: /* Panther Kerberos Token */ 487 tklen = obj->sinfo.cryptedkey_len; 488 token = obj->sinfo.cryptedkey; 489 break; 490 default: 491 return AFPERR_NOOP; 492 break; 493 494 } 495 496 if (tklen == 0) 497 return AFPERR_MISC; 498 499 tp = htonl(tklen); 500 memcpy(rbuf, &tp, sizeof(tklen)); 501 rbuf += sizeof(tklen); 502 *rbuflen += sizeof(tklen); 503 504 memcpy(rbuf, token, tklen); 505 *rbuflen += tklen; 506 507 return AFP_OK; 508} 509 510/* ---------------------- */ 511int afp_disconnect(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen) 512{ 513 DSI *dsi = (DSI *)obj->dsi; 514 uint16_t type; 515 uint32_t tklen; 516 pid_t token; 517 int i; 518 519 *rbuflen = 0; 520 ibuf += 2; 521 522#if 0 523 /* check for guest user */ 524 if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) { 525 return AFPERR_MISC; 526 } 527#endif 528 529 memcpy(&type, ibuf, sizeof(type)); 530 type = ntohs(type); 531 ibuf += sizeof(type); 532 533 memcpy(&tklen, ibuf, sizeof(tklen)); 534 tklen = ntohl(tklen); 535 ibuf += sizeof(tklen); 536 537 if ( sizeof(pid_t) > SESSIONTOKEN_LEN) { 538 LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN ); 539 return AFPERR_MISC; 540 } 541 if (tklen != SESSIONTOKEN_LEN) { 542 return AFPERR_MISC; 543 } 544 tklen = sizeof(pid_t); 545 memcpy(&token, ibuf, tklen); 546 547 /* our stuff is pid + zero pad */ 548 ibuf += tklen; 549 for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) { 550 if (*ibuf != 0) { 551 return AFPERR_MISC; 552 } 553 } 554 555 LOG(log_note, logtype_afpd, "afp_disconnect: trying primary reconnect"); 556 dsi->flags |= DSI_RECONINPROG; 557 558 /* Deactivate tickle timer */ 559 const struct itimerval none = {{0, 0}, {0, 0}}; 560 setitimer(ITIMER_REAL, &none, NULL); 561 562 /* check for old session, possibly transfering session from here to there */ 563 if (ipc_child_write(obj->ipc_fd, IPC_DISCOLDSESSION, tklen, &token) != 0) 564 goto exit; 565 /* write uint16_t DSI request ID */ 566 if (writet(obj->ipc_fd, &dsi->header.dsi_requestID, 2, 0, 2) != 2) { 567 LOG(log_error, logtype_afpd, "afp_disconnect: couldn't send DSI request ID"); 568 goto exit; 569 } 570 /* now send our connected AFP client socket */ 571 if (send_fd(obj->ipc_fd, dsi->socket) != 0) 572 goto exit; 573 /* Now see what happens: either afpd master sends us SIGTERM because our session */ 574 /* has been transfered to a old disconnected session, or we continue */ 575 sleep(5); 576 577 if (!(dsi->flags & DSI_RECONINPROG)) { /* deleted in SIGTERM handler */ 578 /* Reconnect succeeded, we exit now after sleeping some more */ 579 sleep(2); /* sleep some more to give the recon. session time */ 580 LOG(log_note, logtype_afpd, "afp_disconnect: primary reconnect succeeded"); 581 exit(0); 582 } 583 584exit: 585 /* Reinstall tickle timer */ 586 setitimer(ITIMER_REAL, &dsi->timer, NULL); 587 588 LOG(log_error, logtype_afpd, "afp_disconnect: primary reconnect failed"); 589 return AFPERR_MISC; 590} 591 592/* ---------------------- */ 593static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len) 594{ 595 int num,i; 596 597 if (!len || len > ibuflen) 598 return AFPERR_BADVERS; 599 600 num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ]); 601 for ( i = 0; i < num; i++ ) { 602 if ( strncmp( ibuf, afp_versions[ i ].av_name , len ) == 0 ) { 603 obj->afp_version = afp_versions[ i ].av_number; 604 afp_version_index = i; 605 break; 606 } 607 } 608 if ( i == num ) /* An inappropo version */ 609 return AFPERR_BADVERS ; 610 611 /* FIXME Hack */ 612 if (obj->afp_version >= 30 && sizeof(off_t) != 8) { 613 LOG(log_error, logtype_afpd, "get_version: no LARGE_FILE support recompile!" ); 614 return AFPERR_BADVERS ; 615 } 616 617 return 0; 618} 619 620/* ---------------------- */ 621int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) 622{ 623 struct passwd *pwd = NULL; 624 size_t len; 625 int i; 626 627 *rbuflen = 0; 628 629 if ( nologin & 1) 630 return send_reply(obj, AFPERR_SHUTDOWN ); 631 632 if (ibuflen < 2) 633 return send_reply(obj, AFPERR_BADVERS ); 634 635 ibuf++; 636 len = (unsigned char) *ibuf++; 637 ibuflen -= 2; 638 639 i = get_version(obj, ibuf, ibuflen, len); 640 if (i) 641 return send_reply(obj, i ); 642 643 if (ibuflen <= len) 644 return send_reply(obj, AFPERR_BADUAM); 645 646 ibuf += len; 647 ibuflen -= len; 648 649 len = (unsigned char) *ibuf++; 650 ibuflen--; 651 652 if (!len || len > ibuflen) 653 return send_reply(obj, AFPERR_BADUAM); 654 655 if (NULL == (afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) ) 656 return send_reply(obj, AFPERR_BADUAM); 657 ibuf += len; 658 ibuflen -= len; 659 660 if (AFP_OK != (i = create_session_key(obj)) ) 661 return send_reply(obj, i); 662 663 i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen); 664 665 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR)) 666 return send_reply(obj, i); 667 668 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0))); 669} 670 671/* ---------------------- */ 672int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) 673{ 674 struct passwd *pwd = NULL; 675 size_t len; 676 int i; 677 char type; 678 uint16_t len16; 679 char *username; 680 681 *rbuflen = 0; 682 683 if ( nologin & 1) 684 return send_reply(obj, AFPERR_SHUTDOWN ); 685 686 if (ibuflen < 5) 687 return send_reply(obj, AFPERR_BADVERS ); 688 689 ibuf++; 690 ibuf++; /* pad */ 691 ibuf +=2; /* flag */ 692 693 len = (unsigned char) *ibuf; 694 ibuf++; 695 ibuflen -= 5; 696 697 i = get_version(obj, ibuf, ibuflen, len); 698 if (i) 699 return send_reply(obj, i ); 700 701 if (ibuflen <= len) 702 return send_reply(obj, AFPERR_BADUAM); 703 704 ibuf += len; 705 ibuflen -= len; 706 707 len = (unsigned char) *ibuf; 708 ibuf++; 709 ibuflen--; 710 711 if (!len || len > ibuflen) 712 return send_reply(obj, AFPERR_BADUAM); 713 714 if ((afp_uam = auth_uamfind(UAM_SERVER_LOGIN, ibuf, len)) == NULL) 715 return send_reply(obj, AFPERR_BADUAM); 716 ibuf += len; 717 ibuflen -= len; 718 719 if (!afp_uam->u.uam_login.login_ext) { 720 LOG(log_error, logtype_afpd, "login_ext: uam %s not AFP 3 ready!", afp_uam->uam_name ); 721 return send_reply(obj, AFPERR_BADUAM); 722 } 723 /* user name */ 724 if (ibuflen <= 1 +sizeof(len16)) 725 return send_reply(obj, AFPERR_PARAM); 726 type = *ibuf; 727 username = ibuf; 728 ibuf++; 729 ibuflen--; 730 if (type != 3) 731 return send_reply(obj, AFPERR_PARAM); 732 733 memcpy(&len16, ibuf, sizeof(len16)); 734 ibuf += sizeof(len16); 735 ibuflen -= sizeof(len16); 736 len = ntohs(len16); 737 if (len > ibuflen) 738 return send_reply(obj, AFPERR_PARAM); 739 ibuf += len; 740 ibuflen -= len; 741 742 /* directory service name */ 743 if (!ibuflen) 744 return send_reply(obj, AFPERR_PARAM); 745 type = *ibuf; 746 ibuf++; 747 ibuflen--; 748 749 switch(type) { 750 case 1: 751 case 2: 752 if (!ibuflen) 753 return send_reply(obj, AFPERR_PARAM); 754 len = (unsigned char) *ibuf; 755 ibuf++; 756 ibuflen--; 757 break; 758 case 3: 759 /* With "No User Authen" it is equal */ 760 if (ibuflen < sizeof(len16)) 761 return send_reply(obj, AFPERR_PARAM); 762 memcpy(&len16, ibuf, sizeof(len16)); 763 ibuf += sizeof(len16); 764 ibuflen -= sizeof(len16); 765 len = ntohs(len16); 766 break; 767 default: 768 return send_reply(obj, AFPERR_PARAM); 769 } 770#if 0 771 if (len != 0) { 772 LOG(log_error, logtype_afpd, "login_ext: directory service path not null!" ); 773 return send_reply(obj, AFPERR_PARAM); 774 } 775#endif 776 ibuf += len; 777 ibuflen -= len; 778 779 /* Pad */ 780 if (ibuflen && ((unsigned long) ibuf & 1)) { /* pad character */ 781 ibuf++; 782 ibuflen--; 783 } 784 785 if (AFP_OK != (i = create_session_key(obj)) ) { 786 return send_reply(obj, i); 787 } 788 789 /* FIXME user name are in UTF8 */ 790 i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen); 791 792 if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR)) 793 return send_reply(obj, i); 794 795 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0))); 796} 797 798/* ---------------------- */ 799int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) 800{ 801 struct passwd *pwd = NULL; 802 int err; 803 804 if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) { 805 *rbuflen = 0; 806 return send_reply(obj, AFPERR_NOTAUTH ); 807 } 808 809 ibuf += 2; ibuflen -= 2; 810 err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen, 811 rbuf, rbuflen); 812 if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR)) 813 return send_reply(obj, err); 814 815 return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0))); 816} 817 818 819int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen) 820{ 821 DSI *dsi = (DSI *)(obj->dsi); 822 823 LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username); 824 of_close_all_forks(obj); 825 close_all_vol(obj); 826 dsi->flags = DSI_AFP_LOGGED_OUT; 827 *rbuflen = 0; 828 return AFP_OK; 829} 830 831 832 833/* change password -- 834 * NOTE: an FPLogin must already have completed successfully for this 835 * to work. this also does a little pre-processing before it hands 836 * it off to the uam. 837 */ 838int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen) 839{ 840 char username[MACFILELEN + 1], *start = ibuf; 841 struct uam_obj *uam; 842 struct passwd *pwd; 843 size_t len; 844 int ret; 845 846 *rbuflen = 0; 847 ibuf += 2; 848 849 /* check if password change is allowed, OS-X ignores the flag. 850 * we shouldn't trust the client on this anyway. 851 * not sure about the "right" error code, NOOP for now */ 852 if (!(obj->options.passwdbits & PASSWD_SET)) 853 return AFPERR_NOOP; 854 855 /* make sure we can deal w/ this uam */ 856 len = (unsigned char) *ibuf++; 857 if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL) 858 return AFPERR_BADUAM; 859 860 ibuf += len; 861 if ((len + 1) & 1) /* pad byte */ 862 ibuf++; 863 864 if (obj->afp_version < 30) { 865 len = (unsigned char) *ibuf++; 866 if ( len > sizeof(username) - 1) { 867 return AFPERR_PARAM; 868 } 869 memcpy(username, ibuf, len); 870 username[ len ] = '\0'; 871 ibuf += len; 872 if ((len + 1) & 1) /* pad byte */ 873 ibuf++; 874 } else { 875 /* AFP > 3.0 doesn't pass the username, APF 3.1 specs page 124 */ 876 if ( ibuf[0] != '\0' || ibuf[1] != '\0') 877 return AFPERR_PARAM; 878 ibuf += 2; 879 len = MIN(sizeof(username) - 1, strlen(obj->username)); 880 memcpy(username, obj->username, len); 881 username[ len ] = '\0'; 882 } 883 884 885 LOG(log_info, logtype_afpd, "changing password for <%s>", username); 886 887 if (( pwd = uam_getname( obj, username, sizeof(username))) == NULL ) 888 return AFPERR_PARAM; 889 890 /* send it off to the uam. we really don't use ibuflen right now. */ 891 if (ibuflen < (size_t)(ibuf - start)) 892 return AFPERR_PARAM; 893 894 ibuflen -= (ibuf - start); 895 ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen, 896 rbuf, rbuflen); 897 LOG(log_info, logtype_afpd, "password change %s.", 898 (ret == AFPERR_AUTHCONT) ? "continued" : 899 (ret ? "failed" : "succeeded")); 900 if ( ret == AFP_OK ) 901 set_auth_switch(obj, 0); 902 903 return ret; 904} 905 906 907/* FPGetUserInfo */ 908int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen) 909{ 910 uint8_t thisuser; 911 uint32_t id; 912 uint16_t bitmap; 913 char *bitmapp; 914 915 LOG(log_debug, logtype_afpd, "begin afp_getuserinfo:"); 916 917 *rbuflen = 0; 918 ibuf++; 919 thisuser = *ibuf++; 920 ibuf += sizeof(id); /* userid is not used in AFP 2.0 */ 921 memcpy(&bitmap, ibuf, sizeof(bitmap)); 922 bitmap = ntohs(bitmap); 923 924 /* deal with error cases. we don't have to worry about 925 * AFPERR_ACCESS or AFPERR_NOITEM as geteuid and getegid always 926 * succeed. */ 927 if (!thisuser) 928 return AFPERR_PARAM; 929 if ((bitmap & USERIBIT_ALL) != bitmap) 930 return AFPERR_BITMAP; 931 932 /* remember place where we store the possibly modified bitmap later */ 933 memcpy(rbuf, ibuf, sizeof(bitmap)); 934 bitmapp = rbuf; 935 rbuf += sizeof(bitmap); 936 *rbuflen = sizeof(bitmap); 937 938 /* copy the user/group info */ 939 if (bitmap & USERIBIT_USER) { 940 id = htonl(geteuid()); 941 memcpy(rbuf, &id, sizeof(id)); 942 rbuf += sizeof(id); 943 *rbuflen += sizeof(id); 944 } 945 946 if (bitmap & USERIBIT_GROUP) { 947 id = htonl(getegid()); 948 memcpy(rbuf, &id, sizeof(id)); 949 rbuf += sizeof(id); 950 *rbuflen += sizeof(id); 951 } 952 953 if (bitmap & USERIBIT_UUID) { 954 if ( ! (obj->options.flags & OPTION_UUID)) { 955 bitmap &= ~USERIBIT_UUID; 956 bitmap = htons(bitmap); 957 memcpy(bitmapp, &bitmap, sizeof(bitmap)); 958 } else { 959 LOG(log_debug, logtype_afpd, "afp_getuserinfo: get UUID for \'%s\'", obj->username); 960 int ret; 961 atalk_uuid_t uuid; 962 ret = getuuidfromname( obj->username, UUID_USER, uuid); 963 if (ret != 0) { 964 LOG(log_info, logtype_afpd, "afp_getuserinfo: error getting UUID !"); 965 return AFPERR_NOITEM; 966 } 967 LOG(log_debug, logtype_afpd, "afp_getuserinfo: got UUID: %s", uuid_bin2string(uuid)); 968 969 memcpy(rbuf, uuid, UUID_BINSIZE); 970 rbuf += UUID_BINSIZE; 971 *rbuflen += UUID_BINSIZE; 972 } 973 } 974 975 LOG(log_debug, logtype_afpd, "END afp_getuserinfo:"); 976 return AFP_OK; 977} 978 979#define UAM_LIST(type) (((type) == UAM_SERVER_LOGIN || (type) == UAM_SERVER_LOGIN_EXT) ? &uam_login : \ 980 (((type) == UAM_SERVER_CHANGEPW) ? \ 981 &uam_changepw : NULL)) 982 983/* just do a linked list search. this could be sped up with a hashed 984 * list, but i doubt anyone's going to have enough uams to matter. */ 985struct uam_obj *auth_uamfind(const int type, const char *name, 986 const int len) 987{ 988 struct uam_obj *prev, *start; 989 990 if (!name || !(start = UAM_LIST(type))) 991 return NULL; 992 993 prev = start; 994 while ((prev = prev->uam_prev) != start) 995 if (strndiacasecmp(prev->uam_name, name, len) == 0) 996 return prev; 997 998 return NULL; 999} 1000 1001int auth_register(const int type, struct uam_obj *uam) 1002{ 1003 struct uam_obj *start; 1004 1005 if (!uam || !uam->uam_name || (*uam->uam_name == '\0')) 1006 return -1; 1007 1008 if (!(start = UAM_LIST(type))) 1009 return 1; /* we don't know what to do with it, caller must free it */ 1010 1011 uam_attach(start, uam); 1012 return 0; 1013} 1014 1015/* load all of the modules */ 1016int auth_load(const char *path, const char *list) 1017{ 1018 char name[MAXPATHLEN + 1], buf[MAXPATHLEN + 1], *p; 1019 struct uam_mod *mod; 1020 struct stat st; 1021 size_t len; 1022 1023 if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2) 1024 return -1; 1025 1026 strlcpy(buf, list, sizeof(buf)); 1027 if ((p = strtok(buf, ", ")) == NULL) 1028 return -1; 1029 1030 strcpy(name, path); 1031 if (name[len - 1] != '/') { 1032 strcat(name, "/"); 1033 len++; 1034 } 1035 1036 while (p) { 1037 strlcpy(name + len, p, sizeof(name) - len); 1038 LOG(log_debug, logtype_afpd, "uam: loading (%s)", name); 1039 /* 1040 if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) { 1041 */ 1042 if (stat(name, &st) == 0) { 1043 if ((mod = uam_load(name, p))) { 1044 uam_attach(&uam_modules, mod); 1045 LOG(log_debug, logtype_afpd, "uam: %s loaded", p); 1046 } else { 1047 LOG(log_error, logtype_afpd, "uam: %s load failure",p); 1048 } 1049 } else { 1050 LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st)); 1051 } 1052 p = strtok(NULL, ", "); 1053 } 1054 1055 return 0; 1056} 1057 1058/* get rid of all of the uams */ 1059void auth_unload(void) 1060{ 1061 struct uam_mod *mod, *prev, *start = &uam_modules; 1062 1063 prev = start->uam_prev; 1064 while ((mod = prev) != start) { 1065 prev = prev->uam_prev; 1066 uam_detach(mod); 1067 uam_unload(mod); 1068 } 1069} 1070