11553Srgrimes/*
21553Srgrimes * Copyright (c) 1983, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 4. Neither the name of the University nor the names of its contributors
141553Srgrimes *    may be used to endorse or promote products derived from this software
151553Srgrimes *    without specific prior written permission.
161553Srgrimes *
171553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271553Srgrimes * SUCH DAMAGE.
281553Srgrimes *
2931492Swollman * 	From: @(#)lp.h	8.2 (Berkeley) 4/28/95
3050479Speter * $FreeBSD$
311553Srgrimes */
321553Srgrimes
3331492Swollman#include <sys/queue.h>
3468253Sgad#include <time.h>
3570098Sume#include <netdb.h>
361553Srgrimes
371553Srgrimes/*
3831492Swollman * All this information used to be in global static variables shared
3931492Swollman * mysteriously by various parts of the lpr/lpd suite.
4031492Swollman * This structure attempts to centralize all these declarations in the
4131492Swollman * hope that they can later be made more dynamic.
421553Srgrimes */
4331492Swollmanenum	lpd_filters { LPF_CIFPLOT, LPF_DVI, LPF_GRAPH, LPF_INPUT,
4431492Swollman		      LPF_DITROFF, LPF_OUTPUT, LPF_FORTRAN, LPF_TROFF,
4531492Swollman		      LPF_RASTER, LPF_COUNT };
4631492Swollman/* NB: there is a table in common.c giving the mapping from capability names */
471553Srgrimes
4831492Swollmanstruct	printer {
4931492Swollman	char	*printer;	/* printer name */
5031492Swollman	int	 remote;	/* true if RM points to a remote host */
5146110Sjkh	int	 rp_matches_local; /* true if rp has same name as us */
5231492Swollman	int	 tof;		/* true if we are at top-of-form */
5331492Swollman	/* ------------------------------------------------------ */
5431492Swollman	char	*acct_file;	/* AF: accounting file */
5531492Swollman	long	 baud_rate;	/* BR: baud rate if lp is a tty */
5631492Swollman	char	*filters[LPF_COUNT]; /* CF, DF, GF, IF, NF, OF, RF, TF, VF */
5731492Swollman	long	 conn_timeout;	/* CT: TCP connection timeout */
5831492Swollman	long	 daemon_user;	/* DU: daemon user id -- XXX belongs ???? */
5931492Swollman	char	*form_feed;	/* FF: form feed */
6031492Swollman	long	 header_last;	/* HL: print header last */
6131492Swollman	char	*log_file;	/* LF: log file */
6231492Swollman	char	*lock_file;	/* LO: lock file */
6331492Swollman	char	*lp;		/* LP: device name or network address */
6431492Swollman	long	 max_copies;	/* MC: maximum number of copies allowed */
6531492Swollman	long	 max_blocks;	/* MX: maximum number of blocks to copy */
6631492Swollman	long	 price100;	/* PC: price per 100 units of output */
6731492Swollman	long	 page_length;	/* PL: page length */
6831492Swollman	long	 page_width;	/* PW: page width */
6931492Swollman	long	 page_pwidth;	/* PX: page width in pixels */
7031492Swollman	long	 page_plength;	/* PY: page length in pixels */
7195293Sgad	long	 resend_copies;	/* RC: resend copies to remote host */
7231492Swollman	char	*restrict_grp;	/* RG: restricted group */
7331492Swollman	char	*remote_host;	/* RM: remote machine name */
7431492Swollman	char	*remote_queue;	/* RP: remote printer name */
7531492Swollman	long	 restricted;	/* RS: restricted to those with local accts */
7631492Swollman	long	 rw;		/* RW: open LP for reading and writing */
7731492Swollman	long	 short_banner;	/* SB: short banner */
7831492Swollman	long	 no_copies;	/* SC: suppress multiple copies */
7931492Swollman	char	*spool_dir;	/* SD: spool directory */
8031492Swollman	long	 no_formfeed;	/* SF: suppress FF on each print job */
8131492Swollman	long	 no_header;	/* SH: suppress header page */
8268253Sgad	char	*stat_recv;	/* SR: statistics file, receiving jobs */
8368253Sgad	char	*stat_send;	/* SS: statistics file, sending jobs */
8431492Swollman	char	*status_file;	/* ST: status file name */
8531492Swollman	char	*trailer;	/* TR: trailer string send when Q empties */
8631492Swollman	char	*mode_set;	/* MS: mode set, a la stty */
8768253Sgad
8868253Sgad	/* variables used by trstat*() to keep statistics on file transfers */
8968253Sgad#define JOBNUM_SIZE   8
9068253Sgad	char 	 jobnum[JOBNUM_SIZE];
9168253Sgad	long	 jobdfnum;	/* current datafile number within job */
9268253Sgad	struct timespec tr_start, tr_done;
9368253Sgad#define TIMESTR_SIZE 40		/* holds result from LPD_TIMESTAMP_PATTERN */
9468253Sgad	char	 tr_timestr[TIMESTR_SIZE];
9568253Sgad#define DIFFTIME_TS(endTS,startTS) \
9668253Sgad		((double)(endTS.tv_sec - startTS.tv_sec) \
9768253Sgad		+ (endTS.tv_nsec - startTS.tv_nsec) * 1.0e-9)
9831492Swollman};
991553Srgrimes
10031492Swollman/*
10131492Swollman * Lists of user names and job numbers, for the benefit of the structs
10231492Swollman * defined below.  We use TAILQs so that requests don't get mysteriously
10331492Swollman * reversed in process.
10431492Swollman */
10531492Swollmanstruct	req_user {
10660938Sjake	TAILQ_ENTRY(req_user)	ru_link; /* macro glue */
10731492Swollman	char	ru_uname[1];	/* name of user */
10831492Swollman};
10960938SjakeTAILQ_HEAD(req_user_head, req_user);
11031492Swollman
11131492Swollmanstruct	req_file {
11260938Sjake	TAILQ_ENTRY(req_file)	rf_link; /* macro glue */
11331492Swollman	char	 rf_type;	/* type (lowercase cf file letter) of file */
11431492Swollman	char	*rf_prettyname;	/* user-visible name of file */
11531492Swollman	char	 rf_fname[1];	/* name of file */
11631492Swollman};
11760938SjakeTAILQ_HEAD(req_file_head, req_file);
11831492Swollman
11931492Swollmanstruct	req_jobid {
12060938Sjake	TAILQ_ENTRY(req_jobid)	rj_link; /* macro glue */
12131492Swollman	int	rj_job;		/* job number */
12231492Swollman};
12360938SjakeTAILQ_HEAD(req_jobid_head, req_jobid);
12431492Swollman
12531492Swollman/*
12631492Swollman * Encapsulate all the information relevant to a request in the
12731492Swollman * lpr/lpd protocol.
12831492Swollman */
12931492Swollmanenum	req_type { REQ_START, REQ_RECVJOB, REQ_LIST, REQ_DELETE };
13031492Swollman
13131492Swollmanstruct	request {
13231492Swollman	enum	 req_type type;	/* what sort of request is this? */
13331492Swollman	struct	 printer prtr;	/* which printer is it for? */
13431492Swollman	int	 remote;	/* did request arrive over network? */
13531492Swollman	char	*logname;	/* login name of requesting user */
13631492Swollman	char	*authname;	/* authenticated identity of requesting user */
13731492Swollman	char	*prettyname;	/* ``pretty'' name of requesting user */
13831492Swollman	int	 privileged;	/* was the request from a privileged user? */
13931492Swollman	void	*authinfo;	/* authentication information */
14031492Swollman	int	 authentic;	/* was the request securely authenticated? */
14131492Swollman
14231492Swollman	/* Information for queries and deletes... */
14331492Swollman	int	 nusers;	/* length of following list... */
14431492Swollman	struct	 req_user_head users; /* list of users to query/delete */
14531492Swollman	int	 njobids;	/* length of following list... */
14631492Swollman	struct	 req_jobid_head jobids;	/* list of jobids to query/delete */
14731492Swollman};
14831492Swollman
14931492Swollman/*
15031492Swollman * Global definitions for the line printer system.
15131492Swollman */
1521553Srgrimesextern char	line[BUFSIZ];
15378280Sgadextern const char	*progname;	/* program name (lpr, lpq, etc) */
15415703Sjoerg
15578300Sgad    /*
15678300Sgad     * 'local_host' is the name of the machine that lpd (lpr, whatever)
15778300Sgad     * is actually running on.
15878300Sgad     *
15978300Sgad     * 'from_host' will point to the 'host' variable when receiving a job
16078300Sgad     * from a user on the same host, or "somewhere else" when receiving a
16178300Sgad     * job from a remote host.  If 'from_host != local_host', then 'from_ip'
16278300Sgad     * is the character representation of the IP address of from_host (note
16378300Sgad     * that string could be an IPv6 address).
16478300Sgad     *
16578300Sgad     * Also note that when 'from_host' is not pointing at 'local_host', the
16678300Sgad     * string it is pointing at may be as long as NI_MAXHOST (which is very
16778300Sgad     * likely to be much longer than MAXHOSTNAMELEN).
16878300Sgad     */
16978300Sgadextern char	 local_host[MAXHOSTNAMELEN];
17078300Sgadextern const char	*from_host;	/* client's machine name */
17178300Sgadextern const char	*from_ip;	/* client machine's IP address */
17278300Sgad
17315703Sjoergextern int	requ[];		/* job number of spool entries */
17415703Sjoergextern int	requests;	/* # of spool requests */
17515703Sjoergextern char	*user[];        /* users to process */
17615703Sjoergextern int	users;		/* # of users in user array */
17715703Sjoergextern char	*person;	/* name of person doing lprm */
17870098Sumeextern u_char	family;		/* address family */
17915703Sjoerg
1801553Srgrimes/*
1811553Srgrimes * Structure used for building a sorted list of control files.
18299842Sgad * The job_processed value can be used by callers of getq(), to keep
18399842Sgad * track of whatever processing they are doing.
1841553Srgrimes */
18568401Sgadstruct jobqueue {
18668401Sgad	time_t	job_time;		/* last-mod time of cf-file */
18799842Sgad	int	job_matched;		/* used by match_jobspec() */
18899842Sgad	int	job_processed;		/* set to zero by getq() */
18968401Sgad	char	job_cfname[MAXNAMLEN+1];	/* control file name */
1901553Srgrimes};
1911553Srgrimes
19268253Sgad/* lpr/lpd generates readable timestamps for logfiles, etc.  Have all those
19368253Sgad * timestamps be in the same format wrt strftime().  This is ISO 8601 format,
19468253Sgad * with the addition of an easy-readable day-of-the-week field.  Note that
19568253Sgad * '%T' = '%H:%M:%S', and that '%z' is not available on all platforms.
19668253Sgad */
19768253Sgad#define LPD_TIMESTAMP_PATTERN    "%Y-%m-%dT%T%z %a"
19868253Sgad
19931492Swollman/*
20068253Sgad * Codes to indicate which statistic records trstat_write should write.
20168253Sgad */
20268253Sgadtypedef enum { TR_SENDING, TR_RECVING, TR_PRINTING } tr_sendrecv;
20368253Sgad
20468253Sgad/*
20531492Swollman * Error codes for our mini printcap library.
20631492Swollman */
20731492Swollman#define	PCAPERR_TCLOOP		(-3)
20831492Swollman#define	PCAPERR_OSERR		(-2)
20931492Swollman#define	PCAPERR_NOTFOUND	(-1)
21031492Swollman#define	PCAPERR_SUCCESS		0
21131492Swollman#define	PCAPERR_TCOPEN		1
21231492Swollman
21331492Swollman/*
21431492Swollman * File modes for the various status files maintained by lpd.
21531492Swollman */
21631492Swollman#define	LOCK_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
21731492Swollman#define	LFM_PRINT_DIS	(S_IXUSR)
21831492Swollman#define	LFM_QUEUE_DIS	(S_IXGRP)
21931492Swollman#define	LFM_RESET_QUE	(S_IXOTH)
22031492Swollman
22131492Swollman#define	STAT_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
22231492Swollman#define	LOG_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
22331492Swollman#define	TEMP_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
22431492Swollman
22531492Swollman/*
22698152Sgad * Bit-flags for set_qstate() actions, followed by the return values.
22798152Sgad */
22898152Sgad#define SQS_DISABLEQ	0x01	/* Disable the queuing of new jobs */
22998152Sgad#define SQS_STOPP	0x02	/* Stop the printing of jobs */
23098152Sgad#define SQS_ENABLEQ	0x10	/* Enable the queuing of new jobs */
23198152Sgad#define SQS_STARTP	0x20	/* Start the printing of jobs */
23299844Sgad#define SQS_QCHANGED	0x80	/* The queue has changed (new jobs, etc) */
23398152Sgad
23498152Sgad#define SQS_PARMERR	-9	/* Invalid parameters from caller */
23598152Sgad#define SQS_CREFAIL	-3	/* File did not exist, and create failed */
23698152Sgad#define SQS_CHGFAIL	-2	/* File exists, but unable to change state */
23798152Sgad#define SQS_STATFAIL	-1	/* Unable to stat() the lock file */
23898152Sgad#define SQS_CHGOK	1	/* File existed, and the state was changed */
23998152Sgad#define SQS_CREOK	2	/* File did not exist, but was created OK */
24098152Sgad#define SQS_SKIPCREOK	3	/* File did not exist, and there was */
24198152Sgad				/* no need to create it */
24298152Sgad
24398152Sgad/*
24431492Swollman * Command codes used in the protocol.
24531492Swollman */
24631492Swollman#define	CMD_CHECK_QUE	'\1'
24731492Swollman#define	CMD_TAKE_THIS	'\2'
24831492Swollman#define	CMD_SHOWQ_SHORT	'\3'
24931492Swollman#define	CMD_SHOWQ_LONG	'\4'
25031492Swollman#define	CMD_RMJOB	'\5'
25131492Swollman
252241852Seadler/*
253241852Seadler * seteuid() macros.
254241852Seadler*/
255242091Sed
256242091Sedextern uid_t	uid, euid;
257242091Sed
258241852Seadler#define PRIV_START { \
259242005Seadler    if (seteuid(euid) != 0) err(1, "seteuid failed"); \
260241852Seadler}
261241852Seadler#define PRIV_END { \
262242005Seadler    if (seteuid(uid) != 0) err(1, "seteuid failed"); \
263241852Seadler}
264241852Seadler
265241852Seadler
266117541Sgad#include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
2671553Srgrimes
2681553Srgrimes__BEGIN_DECLS
26931492Swollmanstruct	 dirent;
2701553Srgrimes
27178146Sgadvoid	 blankfill(int _tocol);
272139464Sgadint	 calc_jobnum(const char *_cfname, const char **_hostpp);
27378146Sgadchar	*checkremote(struct printer *_pp);
27478146Sgadint	 chk(char *_file);
27578146Sgadvoid	 closeallfds(int _start);
27678146Sgadvoid	 delay(int _millisec);
27778146Sgadvoid	 displayq(struct printer *_pp, int _format);
27878146Sgadvoid	 dump(const char *_nfile, const char *_datafile, int _copies);
27979739Sgadvoid	 fatal(const struct printer *_pp, const char *_msg, ...)
28079739Sgad	    __printflike(2, 3);
28178146Sgadint	 firstprinter(struct printer *_pp, int *_error);
28278146Sgadvoid	 free_printer(struct printer *_pp);
28378146Sgadvoid	 free_request(struct request *_rp);
28478146Sgadint	 getline(FILE *_cfp);
28578146Sgadint	 getport(const struct printer *_pp, const char *_rhost, int _rport);
28678146Sgadint	 getprintcap(const char *_printer, struct printer *_pp);
28778146Sgadint	 getq(const struct printer *_pp, struct jobqueue *(*_namelist[]));
28878146Sgadvoid	 header(void);
28978146Sgadvoid	 inform(const struct printer *_pp, char *_cf);
29078146Sgadvoid	 init_printer(struct printer *_pp);
29178146Sgadvoid	 init_request(struct request *_rp);
29278146Sgadint	 inlist(char *_uname, char *_cfile);
293201512Skibint	 iscf(const struct dirent *_d);
29478146Sgadvoid	 ldump(const char *_nfile, const char *_datafile, int _copies);
29578146Sgadvoid	 lastprinter(void);
29678146Sgadint	 lockchk(struct printer *_pp, char *_slockf);
29778146Sgadchar	*lock_file_name(const struct printer *_pp, char *_buf, size_t _len);
29879740Sgadvoid	 lpd_gettime(struct timespec *_tsp, char *_strp, size_t _strsize);
29978146Sgadint	 nextprinter(struct printer *_pp, int *_error);
30031492Swollmanconst
30178146Sgadchar	*pcaperr(int _error);
30278146Sgadvoid	 prank(int _n);
30378146Sgadvoid	 process(const struct printer *_pp, char *_file);
30478146Sgadvoid	 rmjob(const char *_printer);
30578146Sgadvoid	 rmremote(const struct printer *_pp);
30678146Sgadvoid	 setprintcap(char *_newfile);
30798152Sgadint	 set_qstate(int _action, const char *_lfname);
30878146Sgadvoid	 show(const char *_nfile, const char *_datafile, int _copies);
30978146Sgadint	 startdaemon(const struct printer *_pp);
31078146Sgadchar	*status_file_name(const struct printer *_pp, char *_buf,
31178146Sgad	    size_t _len);
31278146Sgadvoid	 trstat_init(struct printer *_pp, const char *_fname, int _filenum);
31378146Sgadvoid	 trstat_write(struct printer *_pp, tr_sendrecv _sendrecv,
31478146Sgad	    size_t _bytecnt, const char *_userid, const char *_otherhost,
31578146Sgad	    const char *_orighost);
31678146Sgadssize_t	 writel(int _strm, ...);
3171553Srgrimes__END_DECLS
318