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