xref: /freebsd/usr.sbin/lpr/common_source/lp.h (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * 	From: @(#)lp.h	8.2 (Berkeley) 4/28/95
34  * $FreeBSD$
35  */
36 
37 #include <sys/queue.h>
38 #include <time.h>
39 #include <netdb.h>
40 
41 /*
42  * All this information used to be in global static variables shared
43  * mysteriously by various parts of the lpr/lpd suite.
44  * This structure attempts to centralize all these declarations in the
45  * hope that they can later be made more dynamic.
46  */
47 enum	lpd_filters { LPF_CIFPLOT, LPF_DVI, LPF_GRAPH, LPF_INPUT,
48 		      LPF_DITROFF, LPF_OUTPUT, LPF_FORTRAN, LPF_TROFF,
49 		      LPF_RASTER, LPF_COUNT };
50 /* NB: there is a table in common.c giving the mapping from capability names */
51 
52 struct	printer {
53 	char	*printer;	/* printer name */
54 	int	 remote;	/* true if RM points to a remote host */
55 	int	 rp_matches_local; /* true if rp has same name as us */
56 	int	 tof;		/* true if we are at top-of-form */
57 	/* ------------------------------------------------------ */
58 	char	*acct_file;	/* AF: accounting file */
59 	long	 baud_rate;	/* BR: baud rate if lp is a tty */
60 	char	*filters[LPF_COUNT]; /* CF, DF, GF, IF, NF, OF, RF, TF, VF */
61 	long	 conn_timeout;	/* CT: TCP connection timeout */
62 	long	 daemon_user;	/* DU: daemon user id -- XXX belongs ???? */
63 	char	*form_feed;	/* FF: form feed */
64 	long	 header_last;	/* HL: print header last */
65 	char	*log_file;	/* LF: log file */
66 	char	*lock_file;	/* LO: lock file */
67 	char	*lp;		/* LP: device name or network address */
68 	long	 max_copies;	/* MC: maximum number of copies allowed */
69 	long	 max_blocks;	/* MX: maximum number of blocks to copy */
70 	long	 price100;	/* PC: price per 100 units of output */
71 	long	 page_length;	/* PL: page length */
72 	long	 page_width;	/* PW: page width */
73 	long	 page_pwidth;	/* PX: page width in pixels */
74 	long	 page_plength;	/* PY: page length in pixels */
75 	long	 resend_copies;	/* RC: resend copies to remote host */
76 	char	*restrict_grp;	/* RG: restricted group */
77 	char	*remote_host;	/* RM: remote machine name */
78 	char	*remote_queue;	/* RP: remote printer name */
79 	long	 restricted;	/* RS: restricted to those with local accts */
80 	long	 rw;		/* RW: open LP for reading and writing */
81 	long	 short_banner;	/* SB: short banner */
82 	long	 no_copies;	/* SC: suppress multiple copies */
83 	char	*spool_dir;	/* SD: spool directory */
84 	long	 no_formfeed;	/* SF: suppress FF on each print job */
85 	long	 no_header;	/* SH: suppress header page */
86 	char	*stat_recv;	/* SR: statistics file, receiving jobs */
87 	char	*stat_send;	/* SS: statistics file, sending jobs */
88 	char	*status_file;	/* ST: status file name */
89 	char	*trailer;	/* TR: trailer string send when Q empties */
90 	char	*mode_set;	/* MS: mode set, a la stty */
91 
92 	/* variables used by trstat*() to keep statistics on file transfers */
93 #define JOBNUM_SIZE   8
94 	char 	 jobnum[JOBNUM_SIZE];
95 	long	 jobdfnum;	/* current datafile number within job */
96 	struct timespec tr_start, tr_done;
97 #define TIMESTR_SIZE 40		/* holds result from LPD_TIMESTAMP_PATTERN */
98 	char	 tr_timestr[TIMESTR_SIZE];
99 #define DIFFTIME_TS(endTS,startTS) \
100 		((double)(endTS.tv_sec - startTS.tv_sec) \
101 		+ (endTS.tv_nsec - startTS.tv_nsec) * 1.0e-9)
102 };
103 
104 /*
105  * Lists of user names and job numbers, for the benefit of the structs
106  * defined below.  We use TAILQs so that requests don't get mysteriously
107  * reversed in process.
108  */
109 struct	req_user {
110 	TAILQ_ENTRY(req_user)	ru_link; /* macro glue */
111 	char	ru_uname[1];	/* name of user */
112 };
113 TAILQ_HEAD(req_user_head, req_user);
114 
115 struct	req_file {
116 	TAILQ_ENTRY(req_file)	rf_link; /* macro glue */
117 	char	 rf_type;	/* type (lowercase cf file letter) of file */
118 	char	*rf_prettyname;	/* user-visible name of file */
119 	char	 rf_fname[1];	/* name of file */
120 };
121 TAILQ_HEAD(req_file_head, req_file);
122 
123 struct	req_jobid {
124 	TAILQ_ENTRY(req_jobid)	rj_link; /* macro glue */
125 	int	rj_job;		/* job number */
126 };
127 TAILQ_HEAD(req_jobid_head, req_jobid);
128 
129 /*
130  * Encapsulate all the information relevant to a request in the
131  * lpr/lpd protocol.
132  */
133 enum	req_type { REQ_START, REQ_RECVJOB, REQ_LIST, REQ_DELETE };
134 
135 struct	request {
136 	enum	 req_type type;	/* what sort of request is this? */
137 	struct	 printer prtr;	/* which printer is it for? */
138 	int	 remote;	/* did request arrive over network? */
139 	char	*logname;	/* login name of requesting user */
140 	char	*authname;	/* authenticated identity of requesting user */
141 	char	*prettyname;	/* ``pretty'' name of requesting user */
142 	int	 privileged;	/* was the request from a privileged user? */
143 	void	*authinfo;	/* authentication information */
144 	int	 authentic;	/* was the request securely authenticated? */
145 
146 	/* Information for queries and deletes... */
147 	int	 nusers;	/* length of following list... */
148 	struct	 req_user_head users; /* list of users to query/delete */
149 	int	 njobids;	/* length of following list... */
150 	struct	 req_jobid_head jobids;	/* list of jobids to query/delete */
151 };
152 
153 /*
154  * Global definitions for the line printer system.
155  */
156 extern char	line[BUFSIZ];
157 extern const char	*progname;	/* program name (lpr, lpq, etc) */
158 
159     /*
160      * 'local_host' is the name of the machine that lpd (lpr, whatever)
161      * is actually running on.
162      *
163      * 'from_host' will point to the 'host' variable when receiving a job
164      * from a user on the same host, or "somewhere else" when receiving a
165      * job from a remote host.  If 'from_host != local_host', then 'from_ip'
166      * is the character representation of the IP address of from_host (note
167      * that string could be an IPv6 address).
168      *
169      * Also note that when 'from_host' is not pointing at 'local_host', the
170      * string it is pointing at may be as long as NI_MAXHOST (which is very
171      * likely to be much longer than MAXHOSTNAMELEN).
172      */
173 extern char	 local_host[MAXHOSTNAMELEN];
174 extern const char	*from_host;	/* client's machine name */
175 extern const char	*from_ip;	/* client machine's IP address */
176 
177 extern int	requ[];		/* job number of spool entries */
178 extern int	requests;	/* # of spool requests */
179 extern char	*user[];        /* users to process */
180 extern int	users;		/* # of users in user array */
181 extern char	*person;	/* name of person doing lprm */
182 extern u_char	family;		/* address family */
183 
184 /*
185  * Structure used for building a sorted list of control files.
186  * The job_processed value can be used by callers of getq(), to keep
187  * track of whatever processing they are doing.
188  */
189 struct jobqueue {
190 	time_t	job_time;		/* last-mod time of cf-file */
191 	int	job_matched;		/* used by match_jobspec() */
192 	int	job_processed;		/* set to zero by getq() */
193 	char	job_cfname[MAXNAMLEN+1];	/* control file name */
194 };
195 
196 /* lpr/lpd generates readable timestamps for logfiles, etc.  Have all those
197  * timestamps be in the same format wrt strftime().  This is ISO 8601 format,
198  * with the addition of an easy-readable day-of-the-week field.  Note that
199  * '%T' = '%H:%M:%S', and that '%z' is not available on all platforms.
200  */
201 #define LPD_TIMESTAMP_PATTERN    "%Y-%m-%dT%T%z %a"
202 
203 /*
204  * Codes to indicate which statistic records trstat_write should write.
205  */
206 typedef enum { TR_SENDING, TR_RECVING, TR_PRINTING } tr_sendrecv;
207 
208 /*
209  * Error codes for our mini printcap library.
210  */
211 #define	PCAPERR_TCLOOP		(-3)
212 #define	PCAPERR_OSERR		(-2)
213 #define	PCAPERR_NOTFOUND	(-1)
214 #define	PCAPERR_SUCCESS		0
215 #define	PCAPERR_TCOPEN		1
216 
217 /*
218  * File modes for the various status files maintained by lpd.
219  */
220 #define	LOCK_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
221 #define	LFM_PRINT_DIS	(S_IXUSR)
222 #define	LFM_QUEUE_DIS	(S_IXGRP)
223 #define	LFM_RESET_QUE	(S_IXOTH)
224 
225 #define	STAT_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
226 #define	LOG_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
227 #define	TEMP_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
228 
229 /*
230  * Bit-flags for set_qstate() actions, followed by the return values.
231  */
232 #define SQS_DISABLEQ	0x01	/* Disable the queuing of new jobs */
233 #define SQS_STOPP	0x02	/* Stop the printing of jobs */
234 #define SQS_ENABLEQ	0x10	/* Enable the queuing of new jobs */
235 #define SQS_STARTP	0x20	/* Start the printing of jobs */
236 #define SQS_QCHANGED	0x80	/* The queue has changed (new jobs, etc) */
237 
238 #define SQS_PARMERR	-9	/* Invalid parameters from caller */
239 #define SQS_CREFAIL	-3	/* File did not exist, and create failed */
240 #define SQS_CHGFAIL	-2	/* File exists, but unable to change state */
241 #define SQS_STATFAIL	-1	/* Unable to stat() the lock file */
242 #define SQS_CHGOK	1	/* File existed, and the state was changed */
243 #define SQS_CREOK	2	/* File did not exist, but was created OK */
244 #define SQS_SKIPCREOK	3	/* File did not exist, and there was */
245 				/* no need to create it */
246 
247 /*
248  * Command codes used in the protocol.
249  */
250 #define	CMD_CHECK_QUE	'\1'
251 #define	CMD_TAKE_THIS	'\2'
252 #define	CMD_SHOWQ_SHORT	'\3'
253 #define	CMD_SHOWQ_LONG	'\4'
254 #define	CMD_RMJOB	'\5'
255 
256 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
257 
258 __BEGIN_DECLS
259 struct	 dirent;
260 
261 void	 blankfill(int _tocol);
262 int	 calc_jobnum(const char *_cfname, const char **_hostpp);
263 char	*checkremote(struct printer *_pp);
264 int	 chk(char *_file);
265 void	 closeallfds(int _start);
266 void	 delay(int _millisec);
267 void	 displayq(struct printer *_pp, int _format);
268 void	 dump(const char *_nfile, const char *_datafile, int _copies);
269 void	 fatal(const struct printer *_pp, const char *_msg, ...)
270 	    __printflike(2, 3);
271 int	 firstprinter(struct printer *_pp, int *_error);
272 void	 free_printer(struct printer *_pp);
273 void	 free_request(struct request *_rp);
274 int	 getline(FILE *_cfp);
275 int	 getport(const struct printer *_pp, const char *_rhost, int _rport);
276 int	 getprintcap(const char *_printer, struct printer *_pp);
277 int	 getq(const struct printer *_pp, struct jobqueue *(*_namelist[]));
278 void	 header(void);
279 void	 inform(const struct printer *_pp, char *_cf);
280 void	 init_printer(struct printer *_pp);
281 void	 init_request(struct request *_rp);
282 int	 inlist(char *_uname, char *_cfile);
283 int	 iscf(struct dirent *_d);
284 void	 ldump(const char *_nfile, const char *_datafile, int _copies);
285 void	 lastprinter(void);
286 int	 lockchk(struct printer *_pp, char *_slockf);
287 char	*lock_file_name(const struct printer *_pp, char *_buf, size_t _len);
288 void	 lpd_gettime(struct timespec *_tsp, char *_strp, size_t _strsize);
289 int	 nextprinter(struct printer *_pp, int *_error);
290 const
291 char	*pcaperr(int _error);
292 void	 prank(int _n);
293 void	 process(const struct printer *_pp, char *_file);
294 void	 rmjob(const char *_printer);
295 void	 rmremote(const struct printer *_pp);
296 void	 setprintcap(char *_newfile);
297 int	 set_qstate(int _action, const char *_lfname);
298 void	 show(const char *_nfile, const char *_datafile, int _copies);
299 int	 startdaemon(const struct printer *_pp);
300 char	*status_file_name(const struct printer *_pp, char *_buf,
301 	    size_t _len);
302 void	 trstat_init(struct printer *_pp, const char *_fname, int _filenum);
303 void	 trstat_write(struct printer *_pp, tr_sendrecv _sendrecv,
304 	    size_t _bytecnt, const char *_userid, const char *_otherhost,
305 	    const char *_orighost);
306 ssize_t	 writel(int _strm, ...);
307 __END_DECLS
308