int Job_control( int *sock, char *input ) { #ifdef REMOVE struct line_list tokens; char error[LINEBUFFER]; int tokencount; int i, action, permission; char *name, *user = 0, *s, *lpc_command; /* get the format */ Init_line_list(&tokens); error[0] = 0; Name = "Job_control"; ++input; if( (s = safestrchr(input, '\n' )) ) *s = 0; DEBUGF(DCTRL1)("Job_control: socket %d, doing '%s'", *sock, input ); /* check printername for characters, underscore, digits */ Split(&tokens,input,Whitespace,0,0,0,0,0,0); #ifdef ORIGINAL_DEBUG//JY@1020 DEBUGFC(DCTRL2)Dump_line_list("Job_control - input", &tokens); #endif tokencount = tokens.count; if( tokencount < 3 ){ SNPRINTF( error, sizeof(error)) _("bad control command '%s'"), input ); goto error; }
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; }
static int Do_secure_work( char *jobsize, int from_server, char *tempfile, struct line_list *header_info ) { int n, len, linecount = 0, done = 0, fd, status = 0; char *s, *t; char buffer[SMALLBUFFER]; char error[SMALLBUFFER]; struct stat statb; error[0] = 0; if( (fd = Checkread(tempfile,&statb)) < 0 ){ status = JFAIL; plp_snprintf( error, sizeof(error), "Do_secure_work: reopen of '%s' failed - %s", tempfile, Errormsg(errno)); goto error; } buffer[0] = 0; n = 0; done = 0; linecount = 0; while( !done && n < (int)sizeof(buffer)-1 && (len = Read_fd_len_timeout( Send_query_rw_timeout_DYN, fd, buffer+n, sizeof(buffer)-1-n )) > 0 ){ buffer[n+len] = 0; DEBUGF(DRECV1)("Do_secure_work: read %d - '%s'", len, buffer ); while( !done && (s = safestrchr(buffer,'\n')) ){ *s++ = 0; if( safestrlen(buffer) == 0 ){ done = 1; break; } DEBUGF(DRECV1)("Do_secure_work: line [%d] '%s'", linecount, buffer ); if( (t = strchr(buffer,'=')) ){ *t++ = 0; Unescape(t); Set_str_value(header_info, buffer, t ); } else { switch( linecount ){ case 0: if( jobsize ){ if( from_server ){ Set_str_value(header_info,CLIENT,buffer); } done = 1; } else { Set_str_value(header_info,INPUT,buffer); break; } break; case 1: Set_str_value(header_info,CLIENT,buffer); done = 1; break; } } ++linecount; memmove(buffer,s,safestrlen(s)+1); n = safestrlen(buffer); } } if( fd >= 0 ) close(fd); fd = -1; DEBUGFC(DRECV1)Dump_line_list("Do_secure_work - header", header_info ); if( (status = Check_secure_perms( header_info, from_server, error, sizeof(error))) ){ goto error; } DEBUGFC(DRECV1)Dump_line_list("Do_secure_work - header after check", header_info ); buffer[0] = 0; if( jobsize ){ if( (fd = Checkread(tempfile, &statb) ) < 0 ){ status = JFAIL; plp_snprintf( error, sizeof(error), "Do_secure_work: reopen of '%s' for read failed - %s", tempfile, Errormsg(errno)); goto error; } status = Scan_block_file( fd, error, sizeof(error), header_info ); if( (fd = Checkwrite(tempfile,&statb,O_WRONLY|O_TRUNC,1,0)) < 0 ){ status = JFAIL; plp_snprintf( error, sizeof(error), "Do_secure_work: reopen of '%s' for write failed - %s", tempfile, Errormsg(errno)); goto error; } } else { if( (fd = Checkwrite(tempfile,&statb,O_WRONLY|O_TRUNC,1,0)) < 0 ){ status = JFAIL; plp_snprintf( error, sizeof(error), "Do_secure_work: reopen of '%s' for write failed - %s", tempfile, Errormsg(errno)); goto error; } if( (s = Find_str_value(header_info,INPUT)) ){ Dispatch_input( &fd, s, "from secure link" ); } } error: if( fd >= 0 ) close(fd); fd = -1; DEBUGF(DRECV1)("Do_secure_work: status %d, tempfile '%s', error '%s'", status, tempfile, error ); if( error[0] ){ DEBUGF(DRECV1)("Do_secure_work: updating tempfile '%s', error '%s'", tempfile, error ); if( (fd = Checkwrite(tempfile,&statb,O_WRONLY|O_TRUNC,1,0)) < 0 ){ Errorcode = JFAIL; logerr_die(LOG_INFO, "Do_secure_work: reopen of '%s' for write failed", tempfile ); } Write_fd_str(fd,error); close(fd); } DEBUGF(DRECV1)("Do_secure_work: returning %d", status ); return( status ); }
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[] ) { int n, i, c, err, max_port = 0; char *portname, *s; struct servent *servent; struct info *in; prog = argv[0]; Opterr = 1; while( (n = Getopt(argc, argv, "dut")) != EOF ){ switch(n){ default: usage(); break; case 'u': use_udp = !use_udp; break; case 't': use_tcp = !use_tcp; break; case 'd': debug = 1; break; } } i = argc - Optind; if( i > 1 ) usage(); if( i == 1 ){ portname = argv[Optind]; n = atoi( portname ); if( n <= 0 ){ servent = getservbyname( portname, "udp" ); if( servent ){ n = ntohs( servent->s_port ); } } if( n <= 0 ){ FPRINTF( STDERR, "udp_open: bad port number '%s'\n",portname ); usage(); } port_num = n; } if( !use_tcp && !use_udp ) use_udp = 1; if( debug ){ FPRINTF(STDERR,"monitor: udp %d, tcp %d, port %d\n", use_udp, use_tcp, port_num ); } max_port = 0; FD_ZERO( &readfds ); if( use_udp && (udp_fd = udp_open( port_num )) >= 0){ if( debug ) FPRINTF(STDERR,"monitor: udp port %d\n", udp_fd ); FD_SET(udp_fd, &readfds); if( udp_fd >= max_port ) max_port = udp_fd+1; } if( use_tcp && (tcp_fd = tcp_open( port_num )) >= 0){ if( debug ) FPRINTF(STDERR,"monitor: tcp port %d\n", tcp_fd ); FD_SET(tcp_fd, &readfds); if( tcp_fd >= max_port ) max_port = tcp_fd+1; } if( debug ){ FPRINTF(STDERR,"monitor: max_port %d\n", max_port ); for( i = 0; i < max_port; ++i ){ if( FD_ISSET(i, &readfds) ){ FPRINTF(STDERR,"monitor: initial on %d\n", i ); } } } while(1){ FD_ZERO( &testfds ); for( i = 0; i < max_port; ++i ){ if( FD_ISSET(i, &readfds) ){ if( debug ) FPRINTF(STDERR,"monitor: waiting on %d\n", i ); FD_SET(i, &testfds); } } if( debug ) FPRINTF(STDERR,"monitor: starting wait, max %d\n", i ); n = select( i, FD_SET_FIX((fd_set *))&testfds, FD_SET_FIX((fd_set *))0, FD_SET_FIX((fd_set *))0, (struct timeval *)0 ); err = errno; if( debug ) FPRINTF(STDERR,"monitor: select returned %d\n", n ); if( n < 0 ){ FPRINTF( STDERR, "select error - %s\n", Errormsg(errno) ); if( err != EINTR ) break; } if( n > 0 ) for( i = 0; i < max_port; ++i ){ if( FD_ISSET(i, &testfds) ){ if( debug ) FPRINTF(STDERR,"monitor: input on %d\n", i ); if( i == tcp_fd ){ #if defined(HAVE_SOCKLEN_T) socklen_t len; #else int len; #endif struct sockaddr_in sinaddr; len = sizeof( sinaddr ); i = accept( tcp_fd, (struct sockaddr *)&sinaddr, &len ); if( i < 0 ){ FPRINTF( STDERR, "accept error - %s\n", Errormsg(errno) ); continue; } FPRINTF( STDOUT, "connection from %s\n", inet_ntoa( sinaddr.sin_addr ) ); if( i >= max_port ) max_port = i+1; FD_SET(i, &readfds); } else { c = read( i, buffer, sizeof(buffer)-1 ); if( c == 0 ){ /* closed connection */ FPRINTF(STDOUT, "closed connection %d\n", i ); close( i ); FD_CLR(i, &readfds ); Clear_buffer(i); } else if( c > 0 ){ buffer[c] = 0; if(debug)FPRINTF( STDOUT, "recv port %d: %s\n", i, buffer ); Add_buffer(i); in = Save_outbuf_len( i, buffer, c ); while( (s = safestrchr(in->buffer,'\n')) ){ *s++ = 0; Decode(in->buffer); memmove(in->buffer,s, safestrlen(s)+1 ); in->len = safestrlen(in->buffer); } } else { FPRINTF( STDERR, "read error - %s\n", Errormsg(errno) ); close( i ); FD_CLR(i, &readfds ); } } } } } return(0); }