xref: /freebsd/usr.sbin/lpr/common_source/lp.h (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 
40 /*
41  * All this information used to be in global static variables shared
42  * mysteriously by various parts of the lpr/lpd suite.
43  * This structure attempts to centralize all these declarations in the
44  * hope that they can later be made more dynamic.
45  */
46 enum	lpd_filters { LPF_CIFPLOT, LPF_DVI, LPF_GRAPH, LPF_INPUT,
47 		      LPF_DITROFF, LPF_OUTPUT, LPF_FORTRAN, LPF_TROFF,
48 		      LPF_RASTER, LPF_COUNT };
49 /* NB: there is a table in common.c giving the mapping from capability names */
50 
51 struct	printer {
52 	char	*printer;	/* printer name */
53 	int	 remote;	/* true if RM points to a remote host */
54 	int	 rp_matches_local; /* true if rp has same name as us */
55 	int	 tof;		/* true if we are at top-of-form */
56 	/* ------------------------------------------------------ */
57 	char	*acct_file;	/* AF: accounting file */
58 	long	 baud_rate;	/* BR: baud rate if lp is a tty */
59 	char	*filters[LPF_COUNT]; /* CF, DF, GF, IF, NF, OF, RF, TF, VF */
60 	long	 conn_timeout;	/* CT: TCP connection timeout */
61 	long	 daemon_user;	/* DU: daemon user id -- XXX belongs ???? */
62 	char	*form_feed;	/* FF: form feed */
63 	long	 header_last;	/* HL: print header last */
64 	char	*log_file;	/* LF: log file */
65 	char	*lock_file;	/* LO: lock file */
66 	char	*lp;		/* LP: device name or network address */
67 	long	 max_copies;	/* MC: maximum number of copies allowed */
68 	long	 max_blocks;	/* MX: maximum number of blocks to copy */
69 	long	 price100;	/* PC: price per 100 units of output */
70 	long	 page_length;	/* PL: page length */
71 	long	 page_width;	/* PW: page width */
72 	long	 page_pwidth;	/* PX: page width in pixels */
73 	long	 page_plength;	/* PY: page length in pixels */
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 char	*name;		/* program name */
156 				/* host machine name */
157 extern char	host[MAXHOSTNAMELEN];
158 extern char	*from;		/* client's machine name */
159 #define MAXIPSTRLEN 32		/* maxlen of an IP-address as a string */
160 extern char	from_ip[MAXIPSTRLEN];   /* client machine's IP address */
161 
162 extern int	requ[];		/* job number of spool entries */
163 extern int	requests;	/* # of spool requests */
164 extern char	*user[];        /* users to process */
165 extern int	users;		/* # of users in user array */
166 extern char	*person;	/* name of person doing lprm */
167 
168 /*
169  * Structure used for building a sorted list of control files.
170  */
171 struct jobqueue {
172 	time_t	job_time;		/* last-mod time of cf-file */
173 	char	job_cfname[MAXNAMLEN+1];	/* control file name */
174 };
175 
176 /* lpr/lpd generates readable timestamps for logfiles, etc.  Have all those
177  * timestamps be in the same format wrt strftime().  This is ISO 8601 format,
178  * with the addition of an easy-readable day-of-the-week field.  Note that
179  * '%T' = '%H:%M:%S', and that '%z' is not available on all platforms.
180  */
181 #define LPD_TIMESTAMP_PATTERN    "%Y-%m-%dT%T%z %a"
182 
183 /*
184  * Codes to indicate which statistic records trstat_write should write.
185  */
186 typedef enum { TR_SENDING, TR_RECVING, TR_PRINTING } tr_sendrecv;
187 
188 /*
189  * Error codes for our mini printcap library.
190  */
191 #define	PCAPERR_TCLOOP		(-3)
192 #define	PCAPERR_OSERR		(-2)
193 #define	PCAPERR_NOTFOUND	(-1)
194 #define	PCAPERR_SUCCESS		0
195 #define	PCAPERR_TCOPEN		1
196 
197 /*
198  * File modes for the various status files maintained by lpd.
199  */
200 #define	LOCK_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
201 #define	LFM_PRINT_DIS	(S_IXUSR)
202 #define	LFM_QUEUE_DIS	(S_IXGRP)
203 #define	LFM_RESET_QUE	(S_IXOTH)
204 
205 #define	STAT_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
206 #define	LOG_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
207 #define	TEMP_FILE_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
208 
209 /*
210  * Command codes used in the protocol.
211  */
212 #define	CMD_CHECK_QUE	'\1'
213 #define	CMD_TAKE_THIS	'\2'
214 #define	CMD_SHOWQ_SHORT	'\3'
215 #define	CMD_SHOWQ_LONG	'\4'
216 #define	CMD_RMJOB	'\5'
217 
218 #include <sys/cdefs.h>
219 
220 __BEGIN_DECLS
221 struct	 dirent;
222 
223 void     blankfill __P((int));
224 char	*checkremote __P((struct printer *pp));
225 int      chk __P((char *));
226 void	 closeallfds __P((int start));
227 void     delay __P((int));
228 void     displayq __P((struct printer *pp, int format));
229 void     dump __P((char *, char *, int));
230 void	 fatal __P((const struct printer *pp, const char *fmp, ...));
231 int	 firstprinter __P((struct printer *pp, int *status));
232 void	 free_printer __P((struct printer *pp));
233 void	 free_request __P((struct request *rp));
234 int	 getline __P((FILE *));
235 int	 getport __P((const struct printer *pp, const char *, int));
236 int	 getprintcap __P((const char *printer, struct printer *pp));
237 int	 getq __P((const struct printer *, struct jobqueue *(*[])));
238 void     header __P((void));
239 void     inform __P((const struct printer *pp, char *cf));
240 void	 init_printer __P((struct printer *pp));
241 void	 init_request __P((struct request *rp));
242 int      inlist __P((char *, char *));
243 int      iscf __P((struct dirent *));
244 int      isowner __P((char *, char *));
245 void     ldump __P((char *, char *, int));
246 void	 lastprinter __P((void));
247 int      lockchk __P((struct printer *pp, char *));
248 char	*lock_file_name __P((const struct printer *pp, char *buf, size_t len));
249 void	 lpd_gettime __P((struct timespec *_tsp, char *_strp, int _strsize));
250 int	 nextprinter __P((struct printer *pp, int *status));
251 const
252 char	*pcaperr __P((int error));
253 void     prank __P((int));
254 void     process __P((const struct printer *pp, char *));
255 void     rmjob __P((const char *printer));
256 void     rmremote __P((const struct printer *pp));
257 void	 setprintcap __P((char *newprintcap));
258 void     show __P((char *, char *, int));
259 int      startdaemon __P((const struct printer *pp));
260 char	*status_file_name __P((const struct printer *pp, char *buf,
261 			       size_t len));
262 void	 trstat_init __P((struct printer *pp, const char *fname, int filenum));
263 void	 trstat_write __P((struct printer *pp, tr_sendrecv sendrecv,
264 			   size_t bytecnt, const char *userid,
265 			   const char *otherhost, const char *orighost));
266 ssize_t	 writel __P((int s, ...));
267 __END_DECLS
268