1/* 2 * $Id: afp_asp.c,v 1.29 2010-03-09 06:55:12 franklahm Exp $ 3 * 4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu) 5 * Copyright (c) 1990,1993 Regents of The University of Michigan. 6 * All Rights Reserved. See COPYRIGHT. 7 * 8 * modified from main.c. this handles afp over asp. 9 */ 10 11#ifdef HAVE_CONFIG_H 12#include "config.h" 13#endif /* HAVE_CONFIG_H */ 14 15#ifndef NO_DDP 16 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <signal.h> 21#include <atalk/logger.h> 22#include <errno.h> 23#ifdef HAVE_SYS_TIME_H 24#include <sys/time.h> 25#endif /* HAVE_SYS_TIME_H */ 26#ifdef HAVE_SYS_STAT_H 27#include <sys/stat.h> 28#endif /* HAVE_SYS_STAT_H */ 29 30#include <netatalk/endian.h> 31#include <atalk/atp.h> 32#include <atalk/asp.h> 33#include <atalk/compat.h> 34#include <atalk/util.h> 35 36#include "globals.h" 37#include "switch.h" 38#include "auth.h" 39#include "fork.h" 40 41#ifdef FORCE_UIDGID 42#warning UIDGID 43#include "uid.h" 44#endif /* FORCE_UIDGID */ 45 46static AFPObj *child; 47 48static void afp_authprint_remove(AFPObj *); 49 50static void afp_asp_close(AFPObj *obj) 51{ 52 ASP asp = obj->handle; 53 54 if (seteuid( obj->uid ) < 0) { 55 LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno)); 56 exit(EXITERR_SYS); 57 } 58 close_all_vol(); 59 if (obj->options.authprintdir) afp_authprint_remove(obj); 60 61 if (obj->logout) 62 (*obj->logout)(); 63 64 LOG(log_info, logtype_afpd, "%.2fKB read, %.2fKB written", 65 asp->read_count / 1024.0, asp->write_count / 1024.0); 66 asp_close( asp ); 67} 68 69/* removes the authprint trailing when appropriate */ 70static void afp_authprint_remove(AFPObj *obj) 71{ 72 ASP asp = obj->handle; 73 char addr_filename[256]; 74 char addr_filename_buff[256]; 75 struct stat cap_st; 76 77 sprintf(addr_filename, "%s/net%d.%dnode%d", obj->options.authprintdir, 78 ntohs( asp->asp_sat.sat_addr.s_net )/256, 79 ntohs( asp->asp_sat.sat_addr.s_net )%256, 80 asp->asp_sat.sat_addr.s_node ); 81 82 memset( addr_filename_buff, 0, 256 ); 83 84 if(lstat(addr_filename, &cap_st) == 0) { 85 if( S_ISREG(cap_st.st_mode) ) { 86 int len; 87 int capfd = open( addr_filename, O_RDONLY ); 88 if ((len = read( capfd, addr_filename_buff, 256 )) > 0) { 89 int file_pid; 90 char *p_filepid; 91 close(capfd); 92 addr_filename_buff[len] = 0; 93 if ( (p_filepid = strrchr(addr_filename_buff, ':')) != NULL) { 94 *p_filepid = '\0'; 95 p_filepid++; 96 file_pid = atoi(p_filepid); 97 if (file_pid == (int)getpid()) { 98 if(unlink(addr_filename) == 0) { 99 LOG(log_info, logtype_afpd, "removed %s", addr_filename); 100 } else { 101 LOG(log_info, logtype_afpd, "error removing %s: %s", 102 addr_filename, strerror(errno)); 103 } 104 } else { 105 LOG(log_info, logtype_afpd, "%s belongs to another pid %d", 106 addr_filename, file_pid ); 107 } 108 } else { /* no pid info */ 109 if (unlink(addr_filename) == 0) { 110 LOG(log_info, logtype_afpd, "removed %s", addr_filename ); 111 } else { 112 LOG(log_info, logtype_afpd, "error removing %s: %s", 113 addr_filename, strerror(errno)); 114 } 115 } 116 } else { 117 LOG(log_info, logtype_afpd, "couldn't read data from %s", addr_filename ); 118 } 119 } else { 120 LOG(log_info, logtype_afpd, "%s is not a regular file", addr_filename ); 121 } 122 } else { 123 LOG(log_info, logtype_afpd, "error stat'ing %s: %s", 124 addr_filename, strerror(errno)); 125 } 126} 127 128/* ------------------------ 129 * SIGTERM 130*/ 131static void afp_asp_die(const int sig) 132{ 133 ASP asp = child->handle; 134 135 asp_attention(asp, AFPATTN_SHUTDOWN); 136 if ( asp_shutdown( asp ) < 0 ) { 137 LOG(log_error, logtype_afpd, "afp_die: asp_shutdown: %s", strerror(errno) ); 138 } 139 140 afp_asp_close(child); 141 if (sig == SIGTERM || sig == SIGALRM) 142 exit( 0 ); 143 else 144 exit(sig); 145} 146 147/* ----------------------------- 148 * SIGUSR1 149 */ 150static void afp_asp_timedown(int sig _U_) 151{ 152 struct sigaction sv; 153 struct itimerval it; 154 155 /* shutdown and don't reconnect. server going down in 5 minutes. */ 156 asp_attention(child->handle, AFPATTN_SHUTDOWN | AFPATTN_NORECONNECT | 157 AFPATTN_TIME(5)); 158 159 it.it_interval.tv_sec = 0; 160 it.it_interval.tv_usec = 0; 161 it.it_value.tv_sec = 300; 162 it.it_value.tv_usec = 0; 163 if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) { 164 LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) ); 165 afp_asp_die(EXITERR_SYS); 166 } 167 168 memset(&sv, 0, sizeof(sv)); 169 sv.sa_handler = afp_asp_die; 170 sigemptyset( &sv.sa_mask ); 171 sigaddset(&sv.sa_mask, SIGHUP); 172 sigaddset(&sv.sa_mask, SIGTERM); 173 sv.sa_flags = SA_RESTART; 174 if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) { 175 LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) ); 176 afp_asp_die(EXITERR_SYS); 177 } 178 179 /* ignore myself */ 180 sv.sa_handler = SIG_IGN; 181 sigemptyset( &sv.sa_mask ); 182 sv.sa_flags = SA_RESTART; 183 if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) { 184 LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) ); 185 afp_asp_die(EXITERR_SYS); 186 } 187} 188 189/* --------------------------------- 190 * SIGHUP reload configuration file 191*/ 192extern volatile int reload_request; 193 194static void afp_asp_reload(int sig _U_) 195{ 196 reload_request = 1; 197} 198 199/* ---------------------- */ 200#ifdef SERVERTEXT 201static void afp_asp_getmesg (int sig _U_) 202{ 203 readmessage(child); 204 asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5)); 205} 206#endif /* SERVERTEXT */ 207 208/* ---------------------- */ 209void afp_over_asp(AFPObj *obj) 210{ 211 ASP asp; 212 struct sigaction action; 213 int func, reply = 0; 214#ifdef DEBUG1 215 int ccnt = 0; 216#endif 217 218 AFPobj = obj; 219 obj->exit = afp_asp_die; 220 obj->reply = (int (*)()) asp_cmdreply; 221 obj->attention = (int (*)(void *, AFPUserBytes)) asp_attention; 222 child = obj; 223 asp = (ASP) obj->handle; 224 225 /* install signal handlers 226 * With ASP tickle handler is done in the parent process 227 */ 228 memset(&action, 0, sizeof(action)); 229 230 /* install SIGHUP */ 231 action.sa_handler = afp_asp_reload; 232 sigemptyset( &action.sa_mask ); 233 sigaddset(&action.sa_mask, SIGTERM); 234 sigaddset(&action.sa_mask, SIGUSR1); 235#ifdef SERVERTEXT 236 sigaddset(&action.sa_mask, SIGUSR2); 237#endif 238 action.sa_flags = SA_RESTART; 239 if ( sigaction( SIGHUP, &action, NULL ) < 0 ) { 240 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) ); 241 afp_asp_die(EXITERR_SYS); 242 } 243 244 /* install SIGTERM */ 245 action.sa_handler = afp_asp_die; 246 sigemptyset( &action.sa_mask ); 247 sigaddset(&action.sa_mask, SIGHUP); 248 sigaddset(&action.sa_mask, SIGUSR1); 249#ifdef SERVERTEXT 250 sigaddset(&action.sa_mask, SIGUSR2); 251#endif 252 action.sa_flags = SA_RESTART; 253 if ( sigaction( SIGTERM, &action, NULL ) < 0 ) { 254 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) ); 255 afp_asp_die(EXITERR_SYS); 256 } 257 258#ifdef SERVERTEXT 259 /* Added for server message support */ 260 action.sa_handler = afp_asp_getmesg; 261 sigemptyset( &action.sa_mask ); 262 sigaddset(&action.sa_mask, SIGTERM); 263 sigaddset(&action.sa_mask, SIGUSR1); 264 sigaddset(&action.sa_mask, SIGHUP); 265 action.sa_flags = SA_RESTART; 266 if ( sigaction( SIGUSR2, &action, NULL) < 0 ) { 267 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) ); 268 afp_asp_die(EXITERR_SYS); 269 } 270#endif /* SERVERTEXT */ 271 272 /* SIGUSR1 - set down in 5 minutes */ 273 action.sa_handler = afp_asp_timedown; 274 sigemptyset( &action.sa_mask ); 275 sigaddset(&action.sa_mask, SIGHUP); 276 sigaddset(&action.sa_mask, SIGTERM); 277#ifdef SERVERTEXT 278 sigaddset(&action.sa_mask, SIGUSR2); 279#endif 280 action.sa_flags = SA_RESTART; 281 if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) { 282 LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) ); 283 afp_asp_die(EXITERR_SYS); 284 } 285 286 LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u", 287 ntohs( asp->asp_sat.sat_addr.s_net ), 288 asp->asp_sat.sat_addr.s_node, asp->asp_sat.sat_port, 289 ntohs( atp_sockaddr( asp->asp_atp )->sat_addr.s_net ), 290 atp_sockaddr( asp->asp_atp )->sat_addr.s_node, 291 atp_sockaddr( asp->asp_atp )->sat_port ); 292 293 while ((reply = asp_getrequest(asp))) { 294 if (reload_request) { 295 reload_request = 0; 296 load_volumes(child); 297 } 298 switch (reply) { 299 case ASPFUNC_CLOSE : 300 afp_asp_close(obj); 301 LOG(log_info, logtype_afpd, "done" ); 302 303#ifdef DEBUG1 304 if ( obj->options.flags & OPTION_DEBUG ) { 305 printf( "done\n" ); 306 } 307#endif 308 return; 309 break; 310 311 case ASPFUNC_CMD : 312#ifdef AFS 313 if ( writtenfork ) { 314 if ( flushfork( writtenfork ) < 0 ) { 315 LOG(log_error, logtype_afpd, "main flushfork: %s", 316 strerror(errno)); 317 } 318 writtenfork = NULL; 319 } 320#endif /* AFS */ 321 func = (u_char) asp->commands[0]; 322#ifdef DEBUG1 323 if ( obj->options.flags & OPTION_DEBUG ) { 324 printf("command: %d (%s)\n", func, AfpNum2name(func)); 325 bprint( asp->commands, asp->cmdlen ); 326 } 327#endif 328 if ( afp_switch[ func ] != NULL ) { 329 /* 330 * The function called from afp_switch is expected to 331 * read its parameters out of buf, put its 332 * results in replybuf (updating rbuflen), and 333 * return an error code. 334 */ 335 asp->datalen = ASP_DATASIZ; 336 reply = (*afp_switch[ func ])(obj, 337 asp->commands, asp->cmdlen, 338 asp->data, &asp->datalen); 339#ifdef FORCE_UIDGID 340 /* bring everything back to old euid, egid */ 341 if (obj->force_uid) 342 restore_uidgid ( &obj->uidgid ); 343#endif /* FORCE_UIDGID */ 344 } else { 345 LOG(log_error, logtype_afpd, "bad function %X", func ); 346 asp->datalen = 0; 347 reply = AFPERR_NOOP; 348 } 349#ifdef DEBUG1 350 if ( obj->options.flags & OPTION_DEBUG ) { 351 printf( "reply: %d, %d\n", reply, ccnt++ ); 352 bprint( asp->data, asp->datalen ); 353 } 354#endif 355 if ( asp_cmdreply( asp, reply ) < 0 ) { 356 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) ); 357 afp_asp_die(EXITERR_CLNT); 358 } 359 break; 360 361 case ASPFUNC_WRITE : 362 func = (u_char) asp->commands[0]; 363#ifdef DEBUG1 364 if ( obj->options.flags & OPTION_DEBUG ) { 365 printf( "(write) command: %d\n", func ); 366 bprint( asp->commands, asp->cmdlen ); 367 } 368#endif 369 if ( afp_switch[ func ] != NULL ) { 370 asp->datalen = ASP_DATASIZ; 371 reply = (*afp_switch[ func ])(obj, 372 asp->commands, asp->cmdlen, 373 asp->data, &asp->datalen); 374#ifdef FORCE_UIDGID 375 /* bring everything back to old euid, egid */ 376 if (obj->force_uid) 377 restore_uidgid ( &obj->uidgid ); 378#endif /* FORCE_UIDGID */ 379 } else { 380 LOG(log_error, logtype_afpd, "(write) bad function %X", func ); 381 asp->datalen = 0; 382 reply = AFPERR_NOOP; 383 } 384#ifdef DEBUG1 385 if ( obj->options.flags & OPTION_DEBUG ) { 386 printf( "(write) reply code: %d, %d\n", reply, ccnt++ ); 387 bprint( asp->data, asp->datalen ); 388 } 389#endif 390 if ( asp_wrtreply( asp, reply ) < 0 ) { 391 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) ); 392 afp_asp_die(EXITERR_CLNT); 393 } 394 break; 395 default: 396 /* 397 * Bad asp packet. Probably should have asp filter them, 398 * since they are typically things like out-of-order packet. 399 */ 400 LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply ); 401 break; 402 } 403#ifdef DEBUG1 404 if ( obj->options.flags & OPTION_DEBUG ) { 405#ifdef notdef 406 pdesc( stdout ); 407#endif /* notdef */ 408 of_pforkdesc( stdout ); 409 fflush( stdout ); 410 } 411#endif 412 } 413} 414 415#endif 416