pid_t Start_worker( const char *name, WorkerProc *proc, struct line_list *parms, int fd ) { struct line_list args; int passfd[20]; pid_t pid; int intern_fd = 0, intern_logger = -1, intern_status = -1, intern_mail = -1, intern_lpd = -1; int passfd_count = 0; Init_line_list(&args); passfd[passfd_count++] = 0; passfd[passfd_count++] = 1; passfd[passfd_count++] = 2; if( Mail_fd > 0 ){ intern_mail = passfd_count; passfd[passfd_count++] = Mail_fd; } if( Status_fd > 0 ){ intern_status = passfd_count; passfd[passfd_count++] = Status_fd; } if( Logger_fd > 0 ){ intern_logger = passfd_count; passfd[passfd_count++] = Logger_fd; } if( Lpd_request > 0 ){ intern_lpd = passfd_count; passfd[passfd_count++] = Lpd_request; } Set_flag_value(&args,DEBUG,Debug); Set_flag_value(&args,DEBUGFV,DbgFlag); #ifdef DMALLOC { extern int dmalloc_outfile_fd; if( dmalloc_outfile_fd > 0 ){ intern_dmalloc = passfd_count; passfd[passfd_count++] = dmalloc_outfile_fd; } } #endif if(DEBUGL1){ DEBUG1("Start_worker: '%s' fd %d", name, fd ); Dump_line_list("Start_worker - parms", parms ); } Merge_line_list( &args, parms, Hash_value_sep,1,1); Free_line_list( parms ); if( fd ){ intern_fd = passfd_count; passfd[passfd_count++] = fd; } pid = Make_lpd_call( name, proc, passfd_count, passfd, &args, intern_logger, intern_status, intern_mail, intern_lpd, intern_fd ); Free_line_list( &args ); return(pid); }
void send_to_logger( int send_to_status_fd, int send_to_mail_fd, struct job *job, const char *header, char *msg_b ) { char *s, *t; char *id, *tstr; int num,pid; char out_b[4*SMALLBUFFER]; struct line_list l; if( !Is_server || Doing_cleanup ) return; Init_line_list(&l); if(DEBUGL4){ char buffer[32]; plp_snprintf(buffer,sizeof(buffer)-5, "%s", msg_b ); if( msg_b ) safestrncat( buffer,"..."); LOGDEBUG("send_to_logger: Logger_fd fd %d, send_to_status_fd %d, send_to_mail fd %d, header '%s', body '%s'", Logger_fd, send_to_status_fd, send_to_mail_fd, header, buffer ); } if( send_to_status_fd <= 0 && send_to_mail_fd <= 0 && Logger_fd <= 0 ) return; s = t = id = tstr = 0; num = 0; if( job ){ Set_str_value(&l,IDENTIFIER, (id = Find_str_value(&job->info,IDENTIFIER)) ); Set_decimal_value(&l,NUMBER, (num = Find_decimal_value(&job->info,NUMBER)) ); } Set_str_value(&l,UPDATE_TIME,(tstr=Time_str(0,0))); Set_decimal_value(&l,PROCESS,(pid=getpid())); plp_snprintf( out_b, sizeof(out_b), "%s at %s ## %s=%s %s=%d %s=%d\n", msg_b, tstr, IDENTIFIER, id, NUMBER, num, PROCESS, pid ); if( send_to_status_fd > 0 && Write_fd_str( send_to_status_fd, out_b ) < 0 ){ DEBUG4("send_to_logger: write to send_to_status_fd %d failed - %s", send_to_status_fd, Errormsg(errno) ); } if( send_to_mail_fd > 0 && Write_fd_str( send_to_mail_fd, out_b ) < 0 ){ DEBUG4("send_to_logger: write to send_to_mail_fd %d failed - %s", send_to_mail_fd, Errormsg(errno) ); } if( Logger_fd > 0 ){ Set_str_value(&l,PRINTER,Printer_DYN); Set_str_value(&l,HOST,FQDNHost_FQDN); s = Escape(msg_b,1); Set_str_value(&l,VALUE,s); if(s) free(s); s = 0; t = Join_line_list(&l,"\n"); s = Escape(t,1); if(t) free(t); t = 0; t = safestrdup4(header,"=",s,"\n",__FILE__,__LINE__); Write_fd_str( Logger_fd, t ); if( s ) free(s); s = 0; if( t ) free(t); t = 0; } Free_line_list(&l); }
int Job_status( int *sock, char *input ) { char *s, *t, *name, *hash_key; int displayformat, status_lines = 0, i, n; struct line_list l, listv; struct line_list done_list; char error[SMALLBUFFER], buffer[16]; int db, dbflag; FILE *READSTATUSFILE;//JY1120 char readbuffer[SMALLBUFFER];//JY1120 char *str_index;//JY1120 #if !defined(JYWENG20031104status) if( input && *input ) ++input;//JY1114 if(get_queue_name(input)) { printf("QueueName is not LPRServer\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } else printf("QueueName is LPRServer\n"); int prnstatus=1; char buffertosend[LARGEBUFFER]; int fdPRNPARorUSB=0;/*JYWENG20031104*/ if((check_par_usb_prn())== 1) fdPRNPARorUSB=open("/dev/usb/lp0",O_RDWR); else fdPRNPARorUSB=open("/dev/lp0",O_RDWR); if(fdPRNPARorUSB == 0) { printf("file descriptor not created\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } // ioctl(fdPRNPARorUSB, 0x060b, &prnstatus); // if(prnstatus == 0) //JY1120 if((READSTATUSFILE=fopen("/var/state/printstatus.txt", "r")) == NULL) { printf("open /var/state/printstatus.txt failed!\n"); send_ack_packet(sock, ACK_FAIL);//JY1120 return(0); } while( fgets(readbuffer, SMALLBUFFER, READSTATUSFILE) != NULL) { if((str_index = strstr(readbuffer, "PRINTER_STATUS=\""))) { str_index += 16;//moving to status strncpy(printerstatus, str_index, strlen(str_index) - 2 ); } } //JY1120 SNPRINTF(buffertosend, sizeof(buffertosend))"Status: %s\n", printerstatus); // else // SNPRINTF(buffertosend, sizeof(buffertosend))"Status: Off line\n"); // if( Write_fd_str( *sock, buffertosend ) < 0 ) cleanup(0); if( write( *sock, buffertosend, strlen(buffertosend) ) < 0 ) cleanup(0); exit(0);//JY1120 #endif #ifdef REMOVE Init_line_list(&l); Init_line_list(&listv); Init_line_list(&done_list); db = Debug; dbflag = DbgFlag; Name = "Job_status"; /* get the format */ if( (s = safestrchr(input, '\n' )) ) *s = 0; displayformat = *input++; /* * if we get a short/long request from these hosts, * reverse the sense of question */ if( Reverse_lpq_status_DYN && (displayformat == REQ_DSHORT || displayformat==REQ_DLONG) ){ Free_line_list(&l); Split(&l,Reverse_lpq_status_DYN,File_sep,0,0,0,0,0,0); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ DEBUGF(DLPQ1)("Job_status: reversing status sense"); if( displayformat == REQ_DSHORT ){ displayformat = REQ_DLONG; } else { displayformat = REQ_DSHORT; } } Free_line_list(&l); } /* * we have a list of hosts with format of the form: * Key=list; Key=list;... * key is s for short, l for long */ DEBUGF(DLPQ1)("Job_status: Force_lpq_status_DYN '%s'", Force_lpq_status_DYN); if( Force_lpq_status_DYN ){ Free_line_list(&listv); Split(&listv,Force_lpq_status_DYN,";",0,0,0,0,0,0); for(i = 0; i < listv.count; ++i ){ s = listv.list[i]; if( (t = safestrpbrk(s,File_sep)) ) *t++ = 0; Free_line_list(&l); Split(&l,t,Value_sep,0,0,0,0,0,0); DEBUGF(DLPQ1)("Job_status: Force_lpq_status '%s'='%s'", s,t); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ DEBUGF(DLPQ1)("Job_status: forcing status '%s'", s); if( safestrcasecmp(s,"s") == 0 ){ displayformat = REQ_DSHORT; } else if( safestrcasecmp(s,"l") == 0 ){ displayformat = REQ_DLONG; } status_lines = Short_status_length_DYN; break; } } Free_line_list(&l); Free_line_list(&listv); } /* * check for short status to be returned */ if( Return_short_status_DYN && displayformat == REQ_DLONG ){ Free_line_list(&l); Split(&l,Return_short_status_DYN,File_sep,0,0,0,0,0,0); if( Match_ipaddr_value( &l, &RemoteHost_IP ) == 0 ){ status_lines = Short_status_length_DYN; DEBUGF(DLPQ1)("Job_status: truncating status to %d", status_lines); } Free_line_list(&l); } DEBUGF(DLPQ1)("Job_status: doing '%s'", input ); Free_line_list(&l); Split(&l,input,Whitespace,0,0,0,0,0,0); if( l.count == 0 ){ SNPRINTF( error, sizeof(error)) "zero length command line"); goto error; }
/************************************************************************* * Receive_secure() - receive a secure transfer *************************************************************************/ int Receive_secure( int *sock, char *input ) { char *printername; char error[SMALLBUFFER]; /* error message */ char *authtype; char *cf, *s; char *jobsize = 0; char *user = 0; int tempfd = -1; int ack, status, from_server; struct line_list args, header_info, info; struct stat statb; char *tempfile = 0; const struct security *security = 0; Name = "RCVSEC"; memset( error, 0, sizeof(error)); ack = 0; status = 0; DEBUGF(DRECV1)("Receive_secure: input line '%s'", input ); Init_line_list( &args ); Init_line_list( &header_info ); Init_line_list( &info ); Split(&args,input+1,Whitespace,0,0,0,0,0,0); DEBUGFC(DRECV1)Dump_line_list("Receive_secure - input", &args); if( args.count != 5 && args.count != 4 ){ plp_snprintf( error+1, sizeof(error)-1, _("bad command line '%s'"), input ); ack = ACK_FAIL; /* no retry, don't send again */ status = JFAIL; goto error; } Check_max(&args,1); args.list[args.count] = 0; /* * \REQ_SECUREprintername C/F user authtype jobsize\n - receive a job * 0 1 2 3 4 */ printername = args.list[0]; cf = args.list[1]; user = args.list[2]; /* user is escape encoded */ Unescape(user); authtype = args.list[3]; Unescape(authtype); jobsize = args.list[4]; setproctitle( "lpd %s '%s'", Name, printername ); Perm_check.authtype = authtype; from_server = 0; if( *cf == 'F' ){ from_server = 1; } /* set up the authentication support information */ if( Is_clean_name( printername ) ){ plp_snprintf( error+1, sizeof(error)-1, _("bad printer name '%s'"), input ); ack = ACK_FAIL; /* no retry, don't send again */ status = JFAIL; goto error; } Set_DYN(&Printer_DYN,printername); if( Setup_printer( printername, error+1, sizeof(error)-1, 0 ) ){ if( jobsize ){ plp_snprintf( error+1, sizeof(error)-1, _("bad printer '%s'"), printername ); ack = ACK_FAIL; /* no retry, don't send again */ status = JFAIL; goto error; } } else { int db, dbf; db = Debug; dbf = DbgFlag; s = Find_str_value(&Spool_control,DEBUG); if(!s) s = New_debug_DYN; Parse_debug( s, 0 ); if( !(DRECVMASK & DbgFlag) ){ Debug = db; DbgFlag = dbf; } else { int tdb, tdbf; tdb = Debug; tdbf = DbgFlag; Debug = db; DbgFlag = dbf; if( Log_file_DYN ){ tempfd = Checkwrite( Log_file_DYN, &statb,0,0,0); if( tempfd > 0 && tempfd != 2 ){ dup2(tempfd,2); close(tempfd); } tempfd = -1; } Debug = tdb; DbgFlag = tdbf; LOGDEBUG("Receive_secure: socket fd %d", *sock); Dump_line_list("Receive_secure - input", &args); } DEBUGF(DRECV1)("Receive_secure: debug '%s', Debug %d, DbgFlag 0x%x", s, Debug, DbgFlag ); } if( !(security = Fix_receive_auth(authtype, &info)) ){ plp_snprintf( error+1, sizeof(error)-1, _("unsupported authentication '%s'"), authtype ); ack = ACK_FAIL; /* no retry, don't send again */ status = JFAIL; goto error; } if( !security->server_receive ){ plp_snprintf( error+1, sizeof(error)-1, _("no receive method supported for '%s'"), authtype ); ack = ACK_FAIL; /* no retry, don't send again */ status = JFAIL; goto error; } if( jobsize ){ double read_len; read_len = strtod(jobsize,0); DEBUGF(DRECV2)("Receive_secure: spooling_disabled %d", Sp_disabled(&Spool_control) ); if( Sp_disabled(&Spool_control) ){ plp_snprintf( error+1, sizeof(error)-1, _("%s: spooling disabled"), Printer_DYN ); ack = ACK_RETRY; /* retry */ status = JFAIL; goto error; } if( Max_job_size_DYN > 0 && (read_len+1023)/1024 > Max_job_size_DYN ){ plp_snprintf( error+1, sizeof(error)-1, _("%s: job size %0.0f is larger than %d K"), Printer_DYN, read_len, Max_job_size_DYN ); ack = ACK_RETRY; status = JFAIL; goto error; } else if( !Check_space( read_len, Minfree_DYN, Spool_dir_DYN ) ){ plp_snprintf( error+1, sizeof(error)-1, _("%s: insufficient file space"), Printer_DYN ); ack = ACK_RETRY; status = JFAIL; goto error; } } tempfd = Make_temp_fd(&tempfile); close(tempfd); tempfd = -1; DEBUGF(DRECV1)("Receive_secure: sock %d, user '%s', jobsize '%s'", *sock, user, jobsize ); status = security->server_receive( sock, Send_job_rw_timeout_DYN, user, jobsize, from_server, authtype, &info, error+1, sizeof(error)-1, &header_info, security, tempfile, Do_secure_work); error: DEBUGF(DRECV1)("Receive_secure: status %d, ack %d, error '%s'", status, ack, error+1 ); if( status ){ if( ack == 0 ) ack = ACK_FAIL; error[0] = ack; DEBUGF(DRECV1)("Receive_secure: sending '%s'", error ); (void)Link_send( ShortRemote_FQDN, sock, Send_query_rw_timeout_DYN, error, safestrlen(error), 0 ); Errorcode = JFAIL; } Free_line_list( &args ); Free_line_list( &header_info ); Free_line_list( &info ); close( *sock ); *sock = -1; Remove_tempfiles(); if( status == 0 && jobsize ){ /* start a new server */ DEBUGF(DRECV1)("Receive_secure: starting server"); if( Server_queue_name_DYN ){ Do_queue_jobs( Server_queue_name_DYN, 0 ); } else { Do_queue_jobs( Printer_DYN, 0 ); } } cleanup(0); }
int main(int argc, char *argv[], char *envp[]) { int i; struct line_list args; Init_line_list(&args); /* set signal handlers */ (void) plp_signal (SIGHUP, cleanup_HUP); (void) plp_signal (SIGINT, cleanup_INT); (void) plp_signal (SIGQUIT, cleanup_QUIT); (void) plp_signal (SIGTERM, cleanup_TERM); (void) signal (SIGCHLD, SIG_DFL); (void) signal (SIGPIPE, SIG_IGN); /* * set up the user state */ #ifndef NODEBUG Debug = 0; #endif Initialize(argc, argv, envp, 'D' ); Setup_configuration(); Get_parms(argc, argv); /* scan input args */ if( Auth && !getenv( "AUTH" ) ){ FPRINTF(STDERR, _("authentication requested (-A option) and AUTH environment variable not set") ); usage(); } /* now force the printer list */ if( All_printers || (Printer_DYN && safestrcasecmp(Printer_DYN,ALL) == 0 ) ){ All_printers = 1; Get_all_printcap_entries(); if(DEBUGL1)Dump_line_list("lprm - final All_line_list", &All_line_list); } DEBUG1("lprm: Printer_DYN '%s', All_printers %d, All_line_list.count %d", Printer_DYN, All_printers, All_line_list.count ); if( Username_JOB && OriginalRUID ){ struct line_list user_list; char *str, *t; struct passwd *pw; int found; uid_t uid; DEBUG2("lprm: checking '%s' for -U perms", Allow_user_setting_DYN ); Init_line_list(&user_list); Split( &user_list, Allow_user_setting_DYN,File_sep,0,0,0,0,0,0); found = 0; for( i = 0; !found && i < user_list.count; ++i ){ str = user_list.list[i]; DEBUG2("lprm: checking '%s'", str ); uid = strtol( str, &t, 10 ); if( str == t || *t ){ /* try getpasswd */ pw = getpwnam( str ); if( pw ){ uid = pw->pw_uid; } } DEBUG2( "lprm: uid '%d'", uid ); found = ( uid == OriginalRUID ); DEBUG2( "lprm: found '%d'", found ); } if( !found ){ DEBUG1( "-U (username) can only be used by ROOT or authorized users" ); Username_JOB = 0; } } if( Username_JOB ){ Set_DYN( &Logname_DYN, Username_JOB ); } Add_line_list(&args,Logname_DYN,0,0,0); for( i = Optind; argv[i]; ++i ){ Add_line_list(&args,argv[i],0,0,0); } Check_max(&args,2); args.list[args.count] = 0; if( All_printers ){ if( All_line_list.count == 0 ){ FPRINTF(STDERR,"no printers\n"); cleanup(0); } for( i = 0; i < All_line_list.count; ++i ){ Set_DYN(&Printer_DYN,All_line_list.list[i] ); Do_removal(args.list); } } else { Get_printer(); Do_removal(args.list); } Free_line_list(&args); DEBUG1("lprm: done"); Remove_tempfiles(); DEBUG1("lprm: tempfiles removed"); Errorcode = 0; DEBUG1("lprm: cleaning up"); cleanup(0); return(0); }
void Decode( char *in ) { struct line_list l, cf, info; char *s, *t, *header, *value; int i; Init_line_list(&l); Init_line_list(&cf); Init_line_list(&info); FPRINTF(STDOUT,"****\n"); if( debug )FPRINTF(STDOUT, "Decode: %s\n", in ); if((s = safestrpbrk(in,Value_sep)) ){ *s++ = 0; Unescape(s); Free_line_list(&l); Split(&l,s,Line_ends,1,Value_sep,1,1,1,0); for( i = 0; i < l.count; ++i ){ t = l.list[i]; if( debug || safestrncasecmp(t,VALUE,5) ){ FPRINTF(STDOUT,"%s\n", t ); } } s = Find_str_value(&l,VALUE,Value_sep); if( s ) Unescape(s); if( !safestrcasecmp(in,TRACE) ){ FPRINTF(STDOUT,"TRACE: '%s'\n", s ); } else if( !safestrcasecmp(in,UPDATE) ){ FPRINTF(STDOUT,"UPDATE: '%s'\n", s ); Split(&info,s,Line_ends,0,0,0,0,0,0); for( i = 0; i < info.count; ++i ){ header = info.list[i]; if( (value = safestrchr(header,'=')) ) *value++ = 0; Unescape(value); FPRINTF(STDOUT," [%d] %s='%s'\n", i, header, value ); } } else if( !safestrcasecmp(in,PRSTATUS) ){ FPRINTF(STDOUT,"PRSTATUS: '%s'\n", s ); } else if( !safestrcasecmp(in,QUEUE) ){ FPRINTF(STDOUT,"QUEUE: '%s'\n", s ); Split(&info,s,Line_ends,0,0,0,0,0,0); for( i = 0; i < info.count; ++i ){ header = info.list[i]; if( (value = safestrchr(header,'=')) ) *value++ = 0; Unescape(value); FPRINTF(STDOUT," [%d] %s='%s'\n", i, header, value ); } Free_line_list(&info); } else if( !safestrcasecmp(in,DUMP) ){ FPRINTF(STDOUT,"DUMP:\n"); Split(&info,s,Line_ends,0,0,0,0,0,0); for( i = 0; i < info.count; ++i ){ header = info.list[i]; if( (value = safestrchr(header,'=')) ) *value++ = 0; Unescape(value); if(debug) FPRINTF(STDOUT," [%d] %s='%s'\n", i, header, value ); if( !safestrcasecmp(header,QUEUE) ){ FPRINTF(STDOUT," EXTRACT QUEUE '%s'\n",value); } else if( !safestrcasecmp(header,UPDATE) ){ FPRINTF(STDOUT," EXTRACT UPDATE '%s'\n",value); } else { FPRINTF(STDOUT," EXTRACT '%s' '%s'\n",header, value); } } Free_line_list(&info); } else { FPRINTF(STDOUT,"%s: '%s'\n", in, s ); } } Free_line_list(&l); Free_line_list(&cf); Free_line_list(&info); FPRINTF(STDOUT,"\n"); }
int main(int argc, char *argv[], char *envp[]) { int i; struct line_list l, options, request_list; char msg[SMALLBUFFER], *s; Init_line_list(&l); Init_line_list(&options); Init_line_list(&request_list); /* set signal handlers */ (void) plp_signal (SIGHUP, cleanup_HUP); (void) plp_signal (SIGINT, cleanup_INT); (void) plp_signal (SIGQUIT, cleanup_QUIT); (void) plp_signal (SIGTERM, cleanup_TERM); (void) signal(SIGCHLD, SIG_DFL); (void) signal(SIGPIPE, SIG_IGN); /* * set up the user state */ Status_line_count = 1; #ifndef NODEBUG Debug = 0; #endif Displayformat = REQ_DLONG; Initialize(argc, argv, envp, 'T' ); Setup_configuration(); Get_parms(argc, argv ); /* scan input args */ if( A_flag && !getenv( "AUTH" ) ){ FPRINTF(STDERR,"lpstat: requested authenticated transfer (-A) and AUTH environment variable not set"); usage(); } /* set up configuration */ Get_printer(); Fix_Rm_Rp_info(0,0); Get_all_printcap_entries(); /* check on printing scheduler is running */ if( t_flag ){ All_printers = 1; r_flag = d_flag = p_flag = o_flag = 1; s_flag = 0; } if( s_flag ){ /* a_flag = 1; */ r_flag = 1; d_flag = 1; v_flag = 1; All_printers = 1; } if( All_printers ){ Merge_line_list( &request_list, &All_line_list,0,0,0); } Merge_line_list( &request_list, &Printer_list,0,0,0); Check_max(&options,2); if( options.count ){ for( i = options.count; i > 0 ; --i ){ options.list[i] = options.list[i-1]; } options.list[0] = safestrdup(Logname_DYN,__FILE__,__LINE__); ++options.count; } options.list[options.count] = 0; if( Found_flag == 0 ){ if( request_list.count == 0 ){ Split(&request_list,Printer_DYN,", ",1,0,1,1,0,0); } o_flag = 1; flag_count = 1; } #ifdef ORIGINAL_DEBUG//JY@1020 if(DEBUGL1)Dump_line_list("lpstat - printer request list", &request_list); if(DEBUGL1)Dump_line_list("lpstat - options", &options); #endif if( r_flag ){ Write_fd_str(1,"scheduler is running\n"); } if( d_flag ){ if( Printer_DYN == 0 ){ Write_fd_str(1,"no system default destination\n"); } else { SNPRINTF(msg,sizeof(msg)) "system default destination: %s\n", Printer_DYN); Write_fd_str(1,msg); } } if( v_flag ){ for( i = 0; i < request_list.count; ++i ){ Set_DYN(&Printer_DYN,request_list.list[i] ); Fix_Rm_Rp_info(0,0); SNPRINTF(msg,sizeof(msg)) "system for %s: %s\n", Printer_DYN, RemoteHost_DYN); Write_fd_str(1,msg); } } /* see if additional status required */ Free_line_list( &Printer_list ); for( i = 0; i < request_list.count; ++i ){ s = request_list.list[i]; Set_DYN(&Printer_DYN,s ); Show_status(options.list, 0); } Free_line_list( &Printer_list ); if( flag_count ){ for( i = 0; i < request_list.count; ++i ){ s = request_list.list[i]; Set_DYN(&Printer_DYN,s ); Show_status(options.list, 1); } } DEBUG1("lpstat: done"); Remove_tempfiles(); DEBUG1("lpstat: tempfiles removed"); Errorcode = 0; DEBUG1("lpstat: cleaning up"); return(0); }