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