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