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