xref: /freebsd/usr.sbin/lpr/common_source/ctlinfo.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
11de7b4b8SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4442e0eafSGarance A Drosehn  * ------+---------+---------+---------+---------+---------+---------+---------*
5ad1f7851SGarance A Drosehn  * Copyright (c) 2001,2011  - Garance Alistair Drosehn <gad@FreeBSD.org>.
6442e0eafSGarance A Drosehn  * All rights reserved.
7442e0eafSGarance A Drosehn  *
8442e0eafSGarance A Drosehn  * Redistribution and use in source and binary forms, with or without
9442e0eafSGarance A Drosehn  * modification, are permitted provided that the following conditions
10442e0eafSGarance A Drosehn  * are met:
11442e0eafSGarance A Drosehn  *   1. Redistributions of source code must retain the above copyright
12442e0eafSGarance A Drosehn  *      notice, this list of conditions and the following disclaimer.
13442e0eafSGarance A Drosehn  *   2. Redistributions in binary form must reproduce the above copyright
14442e0eafSGarance A Drosehn  *      notice, this list of conditions and the following disclaimer in the
15442e0eafSGarance A Drosehn  *      documentation and/or other materials provided with the distribution.
16442e0eafSGarance A Drosehn  *
17442e0eafSGarance A Drosehn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18442e0eafSGarance A Drosehn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19442e0eafSGarance A Drosehn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20442e0eafSGarance A Drosehn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21442e0eafSGarance A Drosehn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22442e0eafSGarance A Drosehn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23442e0eafSGarance A Drosehn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24442e0eafSGarance A Drosehn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25442e0eafSGarance A Drosehn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26442e0eafSGarance A Drosehn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27442e0eafSGarance A Drosehn  * SUCH DAMAGE.
28442e0eafSGarance A Drosehn  *
29442e0eafSGarance A Drosehn  * The views and conclusions contained in the software and documentation
30442e0eafSGarance A Drosehn  * are those of the authors and should not be interpreted as representing
31442e0eafSGarance A Drosehn  * official policies, either expressed or implied, of the FreeBSD Project.
32442e0eafSGarance A Drosehn  *
33442e0eafSGarance A Drosehn  * ------+---------+---------+---------+---------+---------+---------+---------*
34442e0eafSGarance A Drosehn  */
35442e0eafSGarance A Drosehn 
361f589b47SGarance A Drosehn #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
37442e0eafSGarance A Drosehn /*
38442e0eafSGarance A Drosehn  * ctlinfo - This collection of routines will know everything there is to
39442e0eafSGarance A Drosehn  * know about the information inside a control file ('cf*') which is used
40442e0eafSGarance A Drosehn  * to describe a print job in lpr & friends.  The eventual goal is that it
41442e0eafSGarance A Drosehn  * will be the ONLY source file to know what's inside these control-files.
42442e0eafSGarance A Drosehn  */
43442e0eafSGarance A Drosehn 
44442e0eafSGarance A Drosehn /*
45840d7237SGordon Bergling  * Some define's useful for debugging.
46442e0eafSGarance A Drosehn  * TRIGGERTEST_FNAME and DEBUGREADCF_FNAME, allow us to do testing on
47442e0eafSGarance A Drosehn  * a per-spool-directory basis.
48442e0eafSGarance A Drosehn  */
49442e0eafSGarance A Drosehn /* #define TRIGGERTEST_FNAME "LpdTestRenameTF" */
50442e0eafSGarance A Drosehn /* #define DEBUGREADCF_FNAME "LpdDebugReadCF" */
51442e0eafSGarance A Drosehn /* #define LEAVE_TMPCF_FILES 1 */
52442e0eafSGarance A Drosehn 
53442e0eafSGarance A Drosehn #include <sys/types.h>
54442e0eafSGarance A Drosehn #include <sys/stat.h>
55442e0eafSGarance A Drosehn #include <ctype.h>
56442e0eafSGarance A Drosehn #include <errno.h>
57442e0eafSGarance A Drosehn #include <fcntl.h>
58442e0eafSGarance A Drosehn #include <limits.h>
59442e0eafSGarance A Drosehn #include <netdb.h>
60e357c6ecSGarance A Drosehn #include <pwd.h>
61442e0eafSGarance A Drosehn #include <stdio.h>
62442e0eafSGarance A Drosehn #include <stdlib.h>
63442e0eafSGarance A Drosehn #include <string.h>
64442e0eafSGarance A Drosehn #include <syslog.h>
65442e0eafSGarance A Drosehn #include <unistd.h>
66442e0eafSGarance A Drosehn #include "ctlinfo.h"
67442e0eafSGarance A Drosehn 
68442e0eafSGarance A Drosehn struct cjprivate {
69442e0eafSGarance A Drosehn 	struct cjobinfo pub;
70442e0eafSGarance A Drosehn 	char	*cji_buff;		/* buffer for getline */
71442e0eafSGarance A Drosehn 	char	*cji_eobuff;		/* last byte IN the buffer */
72442e0eafSGarance A Drosehn 	FILE	*cji_fstream;
73442e0eafSGarance A Drosehn 	int	 cji_buffsize;		/* # bytes in the buffer */
74442e0eafSGarance A Drosehn 	int	 cji_dumpit;
75442e0eafSGarance A Drosehn };
76442e0eafSGarance A Drosehn 
77e357c6ecSGarance A Drosehn /*
78e357c6ecSGarance A Drosehn  * All the following take a parameter of 'int', but expect values in the
79e357c6ecSGarance A Drosehn  * range of unsigned char.  Define wrappers which take values of type 'char',
80e357c6ecSGarance A Drosehn  * whether signed or unsigned, and ensure they end up in the right range.
81e357c6ecSGarance A Drosehn  */
82e357c6ecSGarance A Drosehn #define	isdigitch(Anychar) isdigit((u_char)(Anychar))
83e357c6ecSGarance A Drosehn #define	islowerch(Anychar) islower((u_char)(Anychar))
84e357c6ecSGarance A Drosehn #define	isupperch(Anychar) isupper((u_char)(Anychar))
85e357c6ecSGarance A Drosehn #define	tolowerch(Anychar) tolower((u_char)(Anychar))
86e357c6ecSGarance A Drosehn 
87e357c6ecSGarance A Drosehn #define	OTHER_USERID_CHARS  "-_"	/* special chars valid in a userid */
88e357c6ecSGarance A Drosehn 
89442e0eafSGarance A Drosehn #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
90442e0eafSGarance A Drosehn 
91442e0eafSGarance A Drosehn /*
92442e0eafSGarance A Drosehn  * This has to be large enough to fit the maximum length of a single line
93442e0eafSGarance A Drosehn  * in a control-file, including the leading 'command id', a trailing '\n'
94442e0eafSGarance A Drosehn  * and ending '\0'.  The max size of an 'U'nlink line, for instance, is
95442e0eafSGarance A Drosehn  * 1 ('U') + PATH_MAX (filename) + 2 ('\n\0').  The maximum 'H'ost line is
96442e0eafSGarance A Drosehn  * 1 ('H') + NI_MAXHOST (remote hostname) + 2 ('\n\0').  Other lines can be
97442e0eafSGarance A Drosehn  * even longer than those.  So, pick some nice, large, arbitrary value.
98442e0eafSGarance A Drosehn  */
99442e0eafSGarance A Drosehn #define CTI_LINEMAX  PATH_MAX+NI_MAXHOST+5
100442e0eafSGarance A Drosehn 
101442e0eafSGarance A Drosehn extern const char	*from_host;	/* client's machine name */
102442e0eafSGarance A Drosehn extern const char	*from_ip;	/* client machine's IP address */
103442e0eafSGarance A Drosehn 
104442e0eafSGarance A Drosehn __BEGIN_DECLS
105442e0eafSGarance A Drosehn void		 ctl_dumpcji(FILE *_dbg_stream, const char *_heading,
106442e0eafSGarance A Drosehn 		    struct cjobinfo *_cjinf);
107442e0eafSGarance A Drosehn static char	*ctl_getline(struct cjobinfo *_cjinf);
108442e0eafSGarance A Drosehn static void	 ctl_rewindcf(struct cjobinfo *_cjinf);
109442e0eafSGarance A Drosehn char		*ctl_rmjob(const char *_ptrname, const char *_cfname);
110442e0eafSGarance A Drosehn __END_DECLS
111442e0eafSGarance A Drosehn 
112442e0eafSGarance A Drosehn /*
113442e0eafSGarance A Drosehn  * Here are some things which might be needed when compiling this under
114442e0eafSGarance A Drosehn  * platforms other than FreeBSD.
115442e0eafSGarance A Drosehn  */
116442e0eafSGarance A Drosehn #ifndef __FreeBSD__
117442e0eafSGarance A Drosehn #   ifndef NAME_MAX
118442e0eafSGarance A Drosehn #	define NAME_MAX	255
119442e0eafSGarance A Drosehn #   endif
120442e0eafSGarance A Drosehn #   ifndef NI_MAXHOST
121442e0eafSGarance A Drosehn #	define NI_MAXHOST	1025
122442e0eafSGarance A Drosehn #   endif
123442e0eafSGarance A Drosehn #   ifndef PATH_MAX
124442e0eafSGarance A Drosehn #	define PATH_MAX	1024
125442e0eafSGarance A Drosehn #   endif
126442e0eafSGarance A Drosehn __BEGIN_DECLS
127442e0eafSGarance A Drosehn char		*strdup(const char *_src);
128442e0eafSGarance A Drosehn size_t		 strlcpy(char *_dst, const char *_src, size_t _siz);
129442e0eafSGarance A Drosehn __END_DECLS
130442e0eafSGarance A Drosehn #endif
131442e0eafSGarance A Drosehn 
132442e0eafSGarance A Drosehn /*
133442e0eafSGarance A Drosehn  *	Control-files (cf*) have the following format.
134442e0eafSGarance A Drosehn  *
135442e0eafSGarance A Drosehn  *	Each control-file describes a single job.  It will list one or more
136442e0eafSGarance A Drosehn  *	"datafiles" (df*) which should be copied to some printer.  Usually
137442e0eafSGarance A Drosehn  *	there is only one datafile per job.  For the curious, RFC 1179 is an
138442e0eafSGarance A Drosehn  *	informal and out-of-date description of lpr/lpd circa 1990.
139442e0eafSGarance A Drosehn  *
140442e0eafSGarance A Drosehn  *	Each line in the file gives an attribute of the job as a whole, or one
141442e0eafSGarance A Drosehn  *	of the datafiles in the job, or a "command" indicating something to do
142442e0eafSGarance A Drosehn  *	with one of the datafiles.  Each line starts with an 'id' that indicates
143442e0eafSGarance A Drosehn  *	what that line is there for.  The 'id' is historically a single byte,
144442e0eafSGarance A Drosehn  *	but may be multiple bytes (obviously it would be best if multi-byte ids
145442e0eafSGarance A Drosehn  *	started with some letter not already used as a single-byte id!).
146442e0eafSGarance A Drosehn  *	After the 'id', the remainder of the line will be the value of the
147442e0eafSGarance A Drosehn  *	indicated attribute, or a name of the datafile to be operated on.
148442e0eafSGarance A Drosehn  *
149442e0eafSGarance A Drosehn  *	In the following lists of ids, the ids with a '!' in front of them are
150442e0eafSGarance A Drosehn  *	NOT explicitly supported by this version of lpd, or at least "not yet
151442e0eafSGarance A Drosehn  *	supported".  They are only listed for reference purposes, so people
152442e0eafSGarance A Drosehn  *	won't be tempted to reuse the same id for a different purpose.
153442e0eafSGarance A Drosehn  *
154442e0eafSGarance A Drosehn  *	The following are attributes of the job which should not appear more
155442e0eafSGarance A Drosehn  *	than once in a control file.  Only the 'H' and 'P' lines are required
156442e0eafSGarance A Drosehn  *	by the RFC, but some implementations of lpr won't even get that right.
157442e0eafSGarance A Drosehn  *
158442e0eafSGarance A Drosehn  *	! A   - [used by lprNG]
159442e0eafSGarance A Drosehn  *	  B   - As far as I know, this is never used as a single-byte id.
160442e0eafSGarance A Drosehn  *		Therefore, I intend to use it for multi-byte id codes.
161442e0eafSGarance A Drosehn  *	  C   - "class name" to display on banner page (this is sometimes
162442e0eafSGarance A Drosehn  *		used to hold options for print filters)
163442e0eafSGarance A Drosehn  *	! D   - [in lprNG, "timestamp" of when the job was submitted]
164442e0eafSGarance A Drosehn  *	! E   - "environment variables" to set [some versions of linux]
165442e0eafSGarance A Drosehn  *	  H   - "host name" of machine where the original 'lpr' was done
166442e0eafSGarance A Drosehn  *	  I   - "indent", the amount to indent output
167442e0eafSGarance A Drosehn  *	  J   - "job name" to display on banner page
168442e0eafSGarance A Drosehn  *	  L   - "literal" user's name as it should be displayed on the
169442e0eafSGarance A Drosehn  *		banner page (it is the existence of an 'L' line which
170442e0eafSGarance A Drosehn  *		indicates that a job should have a banner page).
171442e0eafSGarance A Drosehn  *	  M   - "mail", userid to mail to when done printing (with email
172442e0eafSGarance A Drosehn  *		going to 'M'@'H', so to speak).
173442e0eafSGarance A Drosehn  *	  P   - "person", the user's login name (e.g. for accounting)
174442e0eafSGarance A Drosehn  *	! Q   - [used by lprNG for queue-name]
175442e0eafSGarance A Drosehn  *	  R   - "resolution" in dpi, for some laser printer queues
176442e0eafSGarance A Drosehn  *	  T   - "title" for files sent thru 'pr'
177442e0eafSGarance A Drosehn  *	  W   - "width" to use for printing plain-text files
178442e0eafSGarance A Drosehn  *	  Z   - In BSD, "locale" to use for datafiles sent thru 'pr'.
179442e0eafSGarance A Drosehn  *		(this BSD usage should move to a different id...)
180442e0eafSGarance A Drosehn  *		[in lprNG - this line holds the "Z options"]
181442e0eafSGarance A Drosehn  *	  1   - "R font file" for files sent thru troff
182442e0eafSGarance A Drosehn  *	  2   - "I font file" for files sent thru troff
183442e0eafSGarance A Drosehn  *	  3   - "B font file" for files sent thru troff
184442e0eafSGarance A Drosehn  *	  4   - "S font file" for files sent thru troff
185442e0eafSGarance A Drosehn  *
186442e0eafSGarance A Drosehn  *	The following are attributes attached to a datafile, and thus may
187442e0eafSGarance A Drosehn  *	appear multiple times in a control file (once per datafile):
188442e0eafSGarance A Drosehn  *
189442e0eafSGarance A Drosehn  *	  N   - "name" of file (for display purposes, used by 'lpq')
190442e0eafSGarance A Drosehn  *	  S   - "stat() info" used for symbolic link ('lpr -s')
191442e0eafSGarance A Drosehn  *		security checks.
192442e0eafSGarance A Drosehn  *
193442e0eafSGarance A Drosehn  *	The following indicate actions to take on a given datafile.  The same
194442e0eafSGarance A Drosehn  *	datafile may appear on more than one "print this file" command in the
195626fb2a6SGarance A Drosehn  *	control file.  Note that ALL ids with lowercase letters are expected
196626fb2a6SGarance A Drosehn  *	to be actions to "print this file":
197442e0eafSGarance A Drosehn  *
198626fb2a6SGarance A Drosehn  *	  c   - "file name", cifplot file to print.  This action appears
199626fb2a6SGarance A Drosehn  *		when the user has requested 'lpr -c'.
200626fb2a6SGarance A Drosehn  *	  d   - "file name", dvi file to print, user requested 'lpr -d'
201626fb2a6SGarance A Drosehn  *	  f   - "file name", a plain-text file to print = "standard"
202626fb2a6SGarance A Drosehn  *	  g   - "file name", plot(1G) file to print, ie 'lpr -g'
203626fb2a6SGarance A Drosehn  *	  l   - "file name", text file with control chars which should
204626fb2a6SGarance A Drosehn  *		be printed literally, ie 'lpr -l'  (note: some printers
20521ecfd4aSGarance A Drosehn  *		take this id as a request to print a postscript file,
20621ecfd4aSGarance A Drosehn  *		and because of *that* some OS's use 'l' to indicate
20721ecfd4aSGarance A Drosehn  *		that a datafile is a postscript file)
208626fb2a6SGarance A Drosehn  *	  n   - "file name", ditroff(1) file to print, ie 'lpr -n'
209626fb2a6SGarance A Drosehn  *	  o   - "file name", a postscript file to print.  This id is
210626fb2a6SGarance A Drosehn  *		described in the original RFC, but not much has been
211626fb2a6SGarance A Drosehn  *		done with it.  This 'lpr' does not generate control
212626fb2a6SGarance A Drosehn  *		lines with 'o'-actions, but lpd's printjob processing
21321ecfd4aSGarance A Drosehn  *		will treat it the same as 'l'.
214626fb2a6SGarance A Drosehn  *	  p   - "file name", text file to print with pr(1), ie 'lpr -p'
215626fb2a6SGarance A Drosehn  *	  t   - "file name", troff(1) file to print, ie 'lpr -t'
216442e0eafSGarance A Drosehn  *	  v   - "file name", plain raster file to print
217442e0eafSGarance A Drosehn  *
218442e0eafSGarance A Drosehn  *	  U   - "file name" of datafile to unlink (ie, remove file
219442e0eafSGarance A Drosehn  *		from spool directory.  To be done in a 'Pass 2',
220442e0eafSGarance A Drosehn  *		AFTER having processed all datafiles in the job).
221442e0eafSGarance A Drosehn  *
222442e0eafSGarance A Drosehn  */
223442e0eafSGarance A Drosehn 
224442e0eafSGarance A Drosehn void
ctl_freeinf(struct cjobinfo * cjinf)225442e0eafSGarance A Drosehn ctl_freeinf(struct cjobinfo *cjinf)
226442e0eafSGarance A Drosehn {
227442e0eafSGarance A Drosehn #define FREESTR(xStr) \
228442e0eafSGarance A Drosehn 	if (xStr != NULL) { \
229442e0eafSGarance A Drosehn 		free(xStr); \
230442e0eafSGarance A Drosehn 		xStr = NULL;\
231442e0eafSGarance A Drosehn 	}
232442e0eafSGarance A Drosehn 
233442e0eafSGarance A Drosehn 	struct cjprivate *cpriv;
234442e0eafSGarance A Drosehn 
235442e0eafSGarance A Drosehn 	if (cjinf == NULL)
236442e0eafSGarance A Drosehn 		return;
237442e0eafSGarance A Drosehn 	cpriv = cjinf->cji_priv;
238442e0eafSGarance A Drosehn 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
239442e0eafSGarance A Drosehn 		syslog(LOG_ERR, "in ctl_freeinf(%p): invalid cjinf (cpriv %p)",
24032e56cd1SGarance A Drosehn 		    (void *)cjinf, (void *)cpriv);
241442e0eafSGarance A Drosehn 		return;
242442e0eafSGarance A Drosehn 	}
243442e0eafSGarance A Drosehn 
244442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_accthost);
245442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_acctuser);
246442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_class);
247442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_curqueue);
248442e0eafSGarance A Drosehn 	/* [cpriv->pub.cji_fname is part of cpriv-malloced area] */
249442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_jobname);
250442e0eafSGarance A Drosehn 	FREESTR(cpriv->pub.cji_mailto);
251ad1f7851SGarance A Drosehn 	FREESTR(cpriv->pub.cji_headruser);
252442e0eafSGarance A Drosehn 
253442e0eafSGarance A Drosehn 	if (cpriv->cji_fstream != NULL) {
254442e0eafSGarance A Drosehn 		fclose(cpriv->cji_fstream);
255442e0eafSGarance A Drosehn 		cpriv->cji_fstream = NULL;
256442e0eafSGarance A Drosehn 	}
257442e0eafSGarance A Drosehn 
258442e0eafSGarance A Drosehn 	cjinf->cji_priv = NULL;
259442e0eafSGarance A Drosehn 	free(cpriv);
260442e0eafSGarance A Drosehn #undef FREESTR
261442e0eafSGarance A Drosehn }
262442e0eafSGarance A Drosehn 
263442e0eafSGarance A Drosehn #ifdef DEBUGREADCF_FNAME
264442e0eafSGarance A Drosehn static FILE *ctl_dbgfile = NULL;
265442e0eafSGarance A Drosehn static struct stat ctl_dbgstat;
266442e0eafSGarance A Drosehn #endif
267442e0eafSGarance A Drosehn static int ctl_dbgline = 0;
268442e0eafSGarance A Drosehn 
269442e0eafSGarance A Drosehn struct cjobinfo *
ctl_readcf(const char * ptrname,const char * cfname)270442e0eafSGarance A Drosehn ctl_readcf(const char *ptrname, const char *cfname)
271442e0eafSGarance A Drosehn {
272442e0eafSGarance A Drosehn 	int id;
273442e0eafSGarance A Drosehn 	char *lbuff;
274442e0eafSGarance A Drosehn 	void *cstart;
275442e0eafSGarance A Drosehn 	FILE *cfile;
276442e0eafSGarance A Drosehn 	struct cjprivate *cpriv;
277442e0eafSGarance A Drosehn 	struct cjobinfo *cjinf;
278442e0eafSGarance A Drosehn 	size_t msize, sroom, sroom2;
279442e0eafSGarance A Drosehn 
280442e0eafSGarance A Drosehn 	cfile = fopen(cfname, "r");
281442e0eafSGarance A Drosehn 	if (cfile == NULL) {
282442e0eafSGarance A Drosehn 		syslog(LOG_ERR, "%s: ctl_readcf error fopen(%s): %s",
283442e0eafSGarance A Drosehn 		    ptrname, cfname, strerror(errno));
284442e0eafSGarance A Drosehn 		return NULL;
285442e0eafSGarance A Drosehn 	}
286442e0eafSGarance A Drosehn 
287442e0eafSGarance A Drosehn 	sroom = roundup(sizeof(struct cjprivate), 8);
288442e0eafSGarance A Drosehn 	sroom2 = sroom + strlen(cfname) + 1;
289442e0eafSGarance A Drosehn 	sroom2 = roundup(sroom2, 8);
290442e0eafSGarance A Drosehn 	msize = sroom2 + CTI_LINEMAX;
291442e0eafSGarance A Drosehn 	msize = roundup(msize, 8);
292442e0eafSGarance A Drosehn 	cstart = malloc(msize);
29302e8b7b2SGarance A Drosehn 	if (cstart == NULL) {
29402e8b7b2SGarance A Drosehn 		fclose(cfile);
295442e0eafSGarance A Drosehn 		return NULL;
29602e8b7b2SGarance A Drosehn 	}
297442e0eafSGarance A Drosehn 	memset(cstart, 0, msize);
298442e0eafSGarance A Drosehn 	cpriv = (struct cjprivate *)cstart;
299442e0eafSGarance A Drosehn 	cpriv->pub.cji_priv = cpriv;
300442e0eafSGarance A Drosehn 
301442e0eafSGarance A Drosehn 	cpriv->pub.cji_fname = (char *)cstart + sroom;
302442e0eafSGarance A Drosehn 	strcpy(cpriv->pub.cji_fname, cfname);
303442e0eafSGarance A Drosehn 	cpriv->cji_buff = (char *)cstart + sroom2;
304442e0eafSGarance A Drosehn 	cpriv->cji_buffsize = (int)(msize - sroom2);
305442e0eafSGarance A Drosehn 	cpriv->cji_eobuff = (char *)cstart + msize - 1;
306442e0eafSGarance A Drosehn 
307442e0eafSGarance A Drosehn 	cpriv->cji_fstream = cfile;
308442e0eafSGarance A Drosehn 	cpriv->pub.cji_curqueue = strdup(ptrname);
309442e0eafSGarance A Drosehn 
310442e0eafSGarance A Drosehn 	ctl_dbgline = 0;
311442e0eafSGarance A Drosehn #ifdef DEBUGREADCF_FNAME
312442e0eafSGarance A Drosehn 	ctl_dbgfile = NULL;
313442e0eafSGarance A Drosehn 	id = stat(DEBUGREADCF_FNAME, &ctl_dbgstat);
314442e0eafSGarance A Drosehn 	if (id != -1) {
315442e0eafSGarance A Drosehn 		/* the file exists in this spool directory, write some simple
316442e0eafSGarance A Drosehn 		 * debugging info to it */
317442e0eafSGarance A Drosehn 		ctl_dbgfile = fopen(DEBUGREADCF_FNAME, "a");
318442e0eafSGarance A Drosehn 		if (ctl_dbgfile != NULL) {
319442e0eafSGarance A Drosehn 			fprintf(ctl_dbgfile, "%s: s=%p r=%ld e=%p %p->%s\n",
32032e56cd1SGarance A Drosehn 			    ptrname, (void *)cpriv, (long)sroom,
32132e56cd1SGarance A Drosehn 			    cpriv->cji_eobuff, cpriv->pub.cji_fname,
32232e56cd1SGarance A Drosehn 			    cpriv->pub.cji_fname);
323442e0eafSGarance A Drosehn 		}
324442e0eafSGarance A Drosehn 	}
325442e0eafSGarance A Drosehn #endif
326442e0eafSGarance A Drosehn 	/*
327442e0eafSGarance A Drosehn 	 * Copy job-attribute values from control file to the struct of
328442e0eafSGarance A Drosehn 	 * "public" information.  In some cases, it is invalid for the
329442e0eafSGarance A Drosehn 	 * value to be a null-string, so that is ignored.
330442e0eafSGarance A Drosehn 	 */
331442e0eafSGarance A Drosehn 	cjinf = &(cpriv->pub);
332442e0eafSGarance A Drosehn 	lbuff = ctl_getline(cjinf);
333442e0eafSGarance A Drosehn 	while (lbuff != NULL) {
334442e0eafSGarance A Drosehn 		id = *lbuff++;
335442e0eafSGarance A Drosehn 		switch (id) {
336442e0eafSGarance A Drosehn 		case 'C':
337442e0eafSGarance A Drosehn 			cpriv->pub.cji_class = strdup(lbuff);
338442e0eafSGarance A Drosehn 			break;
339442e0eafSGarance A Drosehn 		case 'H':
340442e0eafSGarance A Drosehn 			if (*lbuff == '\0')
341442e0eafSGarance A Drosehn 				break;
342442e0eafSGarance A Drosehn 			cpriv->pub.cji_accthost = strdup(lbuff);
343442e0eafSGarance A Drosehn 			break;
344442e0eafSGarance A Drosehn 		case 'J':
345442e0eafSGarance A Drosehn 			cpriv->pub.cji_jobname = strdup(lbuff);
346442e0eafSGarance A Drosehn 			break;
347442e0eafSGarance A Drosehn 		case 'L':
348ad1f7851SGarance A Drosehn 			cpriv->pub.cji_headruser = strdup(lbuff);
349442e0eafSGarance A Drosehn 			break;
350442e0eafSGarance A Drosehn 		case 'M':
351442e0eafSGarance A Drosehn 			/*
352442e0eafSGarance A Drosehn 			 * No valid mail-to address would start with a minus.
353442e0eafSGarance A Drosehn 			 * If this one does, it is probably some trickster who
354442e0eafSGarance A Drosehn 			 * is trying to trigger options on sendmail.  Ignore.
355442e0eafSGarance A Drosehn 			 */
356442e0eafSGarance A Drosehn 			if (*lbuff == '-')
357442e0eafSGarance A Drosehn 				break;
358442e0eafSGarance A Drosehn 			if (*lbuff == '\0')
359442e0eafSGarance A Drosehn 				break;
360442e0eafSGarance A Drosehn 			cpriv->pub.cji_mailto = strdup(lbuff);
361442e0eafSGarance A Drosehn 			break;
362442e0eafSGarance A Drosehn 		case 'P':
363442e0eafSGarance A Drosehn 			if (*lbuff == '\0')
364442e0eafSGarance A Drosehn 				break;
365e357c6ecSGarance A Drosehn 			/* The userid must not start with a minus sign */
366e357c6ecSGarance A Drosehn 			if (*lbuff == '-')
367e357c6ecSGarance A Drosehn 				*lbuff = '_';
368442e0eafSGarance A Drosehn 			cpriv->pub.cji_acctuser = strdup(lbuff);
369442e0eafSGarance A Drosehn 			break;
370442e0eafSGarance A Drosehn 		default:
371442e0eafSGarance A Drosehn 			if (islower(id)) {
372442e0eafSGarance A Drosehn 				cpriv->pub.cji_dfcount++;
373442e0eafSGarance A Drosehn 			}
374442e0eafSGarance A Drosehn 			break;
375442e0eafSGarance A Drosehn 		}
376442e0eafSGarance A Drosehn 		lbuff = ctl_getline(cjinf);
377442e0eafSGarance A Drosehn 	}
378442e0eafSGarance A Drosehn 
379442e0eafSGarance A Drosehn 	/* the 'H'ost and 'P'erson fields are *always* supposed to be there */
380442e0eafSGarance A Drosehn 	if (cpriv->pub.cji_accthost == NULL)
381442e0eafSGarance A Drosehn 		cpriv->pub.cji_accthost = strdup(".na.");
382442e0eafSGarance A Drosehn 	if (cpriv->pub.cji_acctuser == NULL)
383442e0eafSGarance A Drosehn 		cpriv->pub.cji_acctuser = strdup(".na.");
384442e0eafSGarance A Drosehn 
385442e0eafSGarance A Drosehn #ifdef DEBUGREADCF_FNAME
386442e0eafSGarance A Drosehn 	if (ctl_dbgfile != NULL) {
387442e0eafSGarance A Drosehn 		if (cpriv->cji_dumpit)
388442e0eafSGarance A Drosehn 			ctl_dumpcji(ctl_dbgfile, "end readcf", &(cpriv->pub));
389442e0eafSGarance A Drosehn 		fclose(ctl_dbgfile);
390442e0eafSGarance A Drosehn 		ctl_dbgfile = NULL;
391442e0eafSGarance A Drosehn 	}
392442e0eafSGarance A Drosehn #endif
393442e0eafSGarance A Drosehn 	return &(cpriv->pub);
394442e0eafSGarance A Drosehn }
395442e0eafSGarance A Drosehn 
396442e0eafSGarance A Drosehn /*
397442e0eafSGarance A Drosehn  * This routine renames the temporary control file as received from some
3981fd731faSGarance A Drosehn  * other (remote) host.  That file will almost always with `tfA*', because
3991fd731faSGarance A Drosehn  * recvjob.c creates the file by changing `c' to `t' in the original name
4001fd731faSGarance A Drosehn  * for the control file.  Now if you read the RFC, you would think that all
4011fd731faSGarance A Drosehn  * control filenames start with `cfA*'.  However, it seems there are some
4021fd731faSGarance A Drosehn  * implementations which send control filenames which start with `cf'
4031fd731faSGarance A Drosehn  * followed by *any* letter, so this routine can not assume what the third
4041fd731faSGarance A Drosehn  * letter will (or will not) be.  Sigh.
405442e0eafSGarance A Drosehn  *
4061fd731faSGarance A Drosehn  * So this will rewrite the temporary file to `rf*' (correcting any lines
4071fd731faSGarance A Drosehn  * which need correcting), rename that `rf*' file to `cf*', and then remove
4081fd731faSGarance A Drosehn  * the original `tf*' temporary file.
4091fd731faSGarance A Drosehn  *
4101fd731faSGarance A Drosehn  * The *main* purpose of this routine is to be paranoid about the contents
411442e0eafSGarance A Drosehn  * of that control file.  It is partially meant to protect against people
412442e0eafSGarance A Drosehn  * TRYING to cause trouble (perhaps after breaking into root of some host
413442e0eafSGarance A Drosehn  * that this host will accept print jobs from).  The fact that we're willing
414442e0eafSGarance A Drosehn  * to print jobs from some remote host does not mean that we should blindly
415442e0eafSGarance A Drosehn  * do anything that host tells us to do.
416442e0eafSGarance A Drosehn  *
417442e0eafSGarance A Drosehn  * This is also meant to protect us from errors in other implementations of
418442e0eafSGarance A Drosehn  * lpr, particularly since we may want to use some values from the control
419442e0eafSGarance A Drosehn  * file as environment variables when it comes time to print, or as parameters
420442e0eafSGarance A Drosehn  * to commands which will be exec'ed, or values in statistics records.
421442e0eafSGarance A Drosehn  *
422442e0eafSGarance A Drosehn  * This may also do some "conversions" between how different versions of
423442e0eafSGarance A Drosehn  * lpr or lprNG define the contents of various lines in a control file.
424442e0eafSGarance A Drosehn  *
425442e0eafSGarance A Drosehn  * If there is an error, it returns a pointer to a descriptive error message.
426442e0eafSGarance A Drosehn  * Error messages which are RETURNED (as opposed to syslog-ed) do not include
427442e0eafSGarance A Drosehn  * the printer-queue name.  Let the caller add that if it is wanted.
428442e0eafSGarance A Drosehn  */
429442e0eafSGarance A Drosehn char *
ctl_renametf(const char * ptrname,const char * tfname)430442e0eafSGarance A Drosehn ctl_renametf(const char *ptrname, const char *tfname)
431442e0eafSGarance A Drosehn {
432e357c6ecSGarance A Drosehn 	int chk3rd, has_uc, newfd, nogood, res;
433442e0eafSGarance A Drosehn 	FILE *newcf;
434442e0eafSGarance A Drosehn 	struct cjobinfo *cjinf;
435442e0eafSGarance A Drosehn 	char *lbuff, *slash, *cp;
436442e0eafSGarance A Drosehn 	char tfname2[NAME_MAX+1], cfname2[NAME_MAX+1];
437442e0eafSGarance A Drosehn 	char errm[CTI_LINEMAX];
438442e0eafSGarance A Drosehn 
439442e0eafSGarance A Drosehn #ifdef TRIGGERTEST_FNAME
440442e0eafSGarance A Drosehn 	struct stat tstat;
441442e0eafSGarance A Drosehn 	res = stat(TRIGGERTEST_FNAME, &tstat);
442442e0eafSGarance A Drosehn 	if (res == -1) {
443442e0eafSGarance A Drosehn 		/*
444442e0eafSGarance A Drosehn 		 * if the trigger file does NOT exist in this spool directory,
445442e0eafSGarance A Drosehn 		 * then do the exact same steps that the pre-ctlinfo code had
446442e0eafSGarance A Drosehn 		 * been doing.  Ie, very little.
447442e0eafSGarance A Drosehn 		 */
448442e0eafSGarance A Drosehn 		strlcpy(cfname2, tfname, sizeof(cfname2));
449442e0eafSGarance A Drosehn 		cfname2[0] = 'c';
450442e0eafSGarance A Drosehn 		res = link(tfname, cfname2);
451442e0eafSGarance A Drosehn 		if (res < 0) {
452442e0eafSGarance A Drosehn 			snprintf(errm, sizeof(errm),
453442e0eafSGarance A Drosehn 			    "ctl_renametf error link(%s,%s): %s", tfname,
454442e0eafSGarance A Drosehn 			    cfname2, strerror(errno));
455442e0eafSGarance A Drosehn 			return strdup(errm);
456442e0eafSGarance A Drosehn 		}
457442e0eafSGarance A Drosehn 		unlink(tfname);
458442e0eafSGarance A Drosehn 		return NULL;
459442e0eafSGarance A Drosehn 	}
460442e0eafSGarance A Drosehn #endif
461442e0eafSGarance A Drosehn 	cjinf = NULL;		/* in case of early jump to error_ret */
462442e0eafSGarance A Drosehn 	newcf = NULL;		/* in case of early jump to error_ret */
463442e0eafSGarance A Drosehn 	*errm = '\0';		/* in case of early jump to error_ret */
464442e0eafSGarance A Drosehn 
4651fd731faSGarance A Drosehn 	chk3rd = tfname[2];
4661fd731faSGarance A Drosehn 	if ((tfname[0] != 't') || (tfname[1] != 'f') || (!isalpha(chk3rd))) {
467442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
468442e0eafSGarance A Drosehn 		    "ctl_renametf invalid filename: %s", tfname);
469442e0eafSGarance A Drosehn 		goto error_ret;
470442e0eafSGarance A Drosehn 	}
471442e0eafSGarance A Drosehn 
472442e0eafSGarance A Drosehn 	cjinf = ctl_readcf(ptrname, tfname);
473442e0eafSGarance A Drosehn 	if (cjinf == NULL) {
474442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
475442e0eafSGarance A Drosehn 		    "ctl_renametf error cti_readcf(%s)", tfname);
476442e0eafSGarance A Drosehn 		goto error_ret;
477442e0eafSGarance A Drosehn 	}
478442e0eafSGarance A Drosehn 
479442e0eafSGarance A Drosehn 	/*
480442e0eafSGarance A Drosehn 	 * This uses open+fdopen instead of fopen because that combination
481442e0eafSGarance A Drosehn 	 * gives us greater control over file-creation issues.
482442e0eafSGarance A Drosehn 	 */
483442e0eafSGarance A Drosehn 	strlcpy(tfname2, tfname, sizeof(tfname2));
4841fd731faSGarance A Drosehn 	tfname2[0] = 'r';		/* rf<letter><job><hostname> */
485442e0eafSGarance A Drosehn 	newfd = open(tfname2, O_WRONLY|O_CREAT|O_TRUNC, 0660);
486442e0eafSGarance A Drosehn 	if (newfd == -1) {
487442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
488442e0eafSGarance A Drosehn 		    "ctl_renametf error open(%s): %s", tfname2,
489442e0eafSGarance A Drosehn 		    strerror(errno));
490442e0eafSGarance A Drosehn 		goto error_ret;
491442e0eafSGarance A Drosehn 	}
492442e0eafSGarance A Drosehn 	newcf = fdopen(newfd, "w");
493442e0eafSGarance A Drosehn 	if (newcf == NULL) {
494442e0eafSGarance A Drosehn 		close(newfd);
495442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
496442e0eafSGarance A Drosehn 		    "ctl_renametf error fopen(%s): %s", tfname2,
497442e0eafSGarance A Drosehn 		    strerror(errno));
498442e0eafSGarance A Drosehn 		goto error_ret;
499442e0eafSGarance A Drosehn 	}
500442e0eafSGarance A Drosehn 
501442e0eafSGarance A Drosehn 	/*
502442e0eafSGarance A Drosehn 	 * Do extra sanity checks on some key job-attribute fields, and
503442e0eafSGarance A Drosehn 	 * write them out first (thus making sure they are written in the
504442e0eafSGarance A Drosehn 	 * order we generally expect them to be in).
505442e0eafSGarance A Drosehn 	 */
506442e0eafSGarance A Drosehn 	/*
507442e0eafSGarance A Drosehn 	 * Some lpr implementations on PC's set a null-string for their
508442e0eafSGarance A Drosehn 	 * hostname.  A MacOS 10 system which has not correctly setup
509442e0eafSGarance A Drosehn 	 * /etc/hostconfig will claim a hostname of 'localhost'.  Anything
510442e0eafSGarance A Drosehn 	 * with blanks in it would be an invalid value for hostname.  For
511442e0eafSGarance A Drosehn 	 * any of these invalid hostname values, replace the given value
512442e0eafSGarance A Drosehn 	 * with the name of the host that this job is coming from.
513442e0eafSGarance A Drosehn 	 */
514442e0eafSGarance A Drosehn 	nogood = 0;
515442e0eafSGarance A Drosehn 	if (cjinf->cji_accthost == NULL)
516442e0eafSGarance A Drosehn 		nogood = 1;
517442e0eafSGarance A Drosehn 	else if (strcmp(cjinf->cji_accthost, ".na.") == 0)
518442e0eafSGarance A Drosehn 		nogood = 1;
519442e0eafSGarance A Drosehn 	else if (strcmp(cjinf->cji_accthost, "localhost") == 0)
520442e0eafSGarance A Drosehn 		nogood = 1;
521442e0eafSGarance A Drosehn 	else {
522442e0eafSGarance A Drosehn 		for (cp = cjinf->cji_accthost; *cp != '\0'; cp++) {
523442e0eafSGarance A Drosehn 			if (*cp <= ' ') {
524442e0eafSGarance A Drosehn 				nogood = 1;
525442e0eafSGarance A Drosehn 				break;
526442e0eafSGarance A Drosehn 			}
527442e0eafSGarance A Drosehn 		}
528442e0eafSGarance A Drosehn 	}
529442e0eafSGarance A Drosehn 	if (nogood)
530442e0eafSGarance A Drosehn 		fprintf(newcf, "H%s\n", from_host);
531442e0eafSGarance A Drosehn 	else
532442e0eafSGarance A Drosehn 		fprintf(newcf, "H%s\n", cjinf->cji_accthost);
533442e0eafSGarance A Drosehn 
534442e0eafSGarance A Drosehn 	/*
535442e0eafSGarance A Drosehn 	 * Now do some sanity checks on the 'P' (original userid) value.  Note
536442e0eafSGarance A Drosehn 	 * that the 'P'erson line is the second line which is ALWAYS supposed
537442e0eafSGarance A Drosehn 	 * to be present in a control file.
538442e0eafSGarance A Drosehn 	 *
539442e0eafSGarance A Drosehn 	 * There is no particularly good value to use for replacements, but
540442e0eafSGarance A Drosehn 	 * at least make sure the value is something reasonable to use in
541442e0eafSGarance A Drosehn 	 * environment variables and statistics records.  Again, some PC
542442e0eafSGarance A Drosehn 	 * implementations send a null-string for a value.  Various Mac
543442e0eafSGarance A Drosehn 	 * implementations will set whatever string the user has set for
544442e0eafSGarance A Drosehn 	 * their 'Owner Name', which usually includes blanks, etc.
545442e0eafSGarance A Drosehn 	 */
546442e0eafSGarance A Drosehn 	nogood = 0;
547442e0eafSGarance A Drosehn 	if (cjinf->cji_acctuser == NULL)
548442e0eafSGarance A Drosehn 		nogood = 1;
549e357c6ecSGarance A Drosehn 	else if (strcmp(cjinf->cji_acctuser, ".na.") == 0)
550e357c6ecSGarance A Drosehn 		;			/* No further checks needed... */
551442e0eafSGarance A Drosehn 	else {
552e357c6ecSGarance A Drosehn 		has_uc = 0;
553e357c6ecSGarance A Drosehn 		cp = cjinf->cji_acctuser;
554e357c6ecSGarance A Drosehn 		if (*cp == '-')
555e357c6ecSGarance A Drosehn 			*cp++ = '_';
556e357c6ecSGarance A Drosehn 		for (; *cp != '\0'; cp++) {
557e357c6ecSGarance A Drosehn 			if (islowerch(*cp) || isdigitch(*cp))
558e357c6ecSGarance A Drosehn 				continue;	/* Standard valid characters */
559e357c6ecSGarance A Drosehn 			if (strchr(OTHER_USERID_CHARS, *cp) != NULL)
560e357c6ecSGarance A Drosehn 				continue;	/* Some more valid characters */
561e357c6ecSGarance A Drosehn 			if (isupperch(*cp)) {
562e357c6ecSGarance A Drosehn 				has_uc = 1;	/* These may be valid... */
563e357c6ecSGarance A Drosehn 				continue;
564e357c6ecSGarance A Drosehn 			}
565442e0eafSGarance A Drosehn 			*cp = '_';
566442e0eafSGarance A Drosehn 		}
567e357c6ecSGarance A Drosehn 		/*
568e357c6ecSGarance A Drosehn 		 * Some Windows hosts send print jobs where the correct userid
569e357c6ecSGarance A Drosehn 		 * has been converted to uppercase, and that can cause trouble
570e357c6ecSGarance A Drosehn 		 * for sites that expect the correct value (for something like
571e357c6ecSGarance A Drosehn 		 * accounting).  On the other hand, some sites do use uppercase
572e357c6ecSGarance A Drosehn 		 * in their userids, so we can't blindly convert to lowercase.
573e357c6ecSGarance A Drosehn 		 */
574e357c6ecSGarance A Drosehn 		if (has_uc && (getpwnam(cjinf->cji_acctuser) == NULL)) {
575e357c6ecSGarance A Drosehn 			for (cp = cjinf->cji_acctuser; *cp != '\0'; cp++) {
576e357c6ecSGarance A Drosehn 				if (isupperch(*cp))
577e357c6ecSGarance A Drosehn 					*cp = tolowerch(*cp);
578e357c6ecSGarance A Drosehn 			}
579e357c6ecSGarance A Drosehn 		}
580442e0eafSGarance A Drosehn 	}
581442e0eafSGarance A Drosehn 	if (nogood)
582442e0eafSGarance A Drosehn 		fprintf(newcf, "P%s\n", ".na.");
583442e0eafSGarance A Drosehn 	else
584442e0eafSGarance A Drosehn 		fprintf(newcf, "P%s\n", cjinf->cji_acctuser);
585442e0eafSGarance A Drosehn 
586442e0eafSGarance A Drosehn 	/* No need for sanity checks on class, jobname, "literal" user. */
587442e0eafSGarance A Drosehn 	if (cjinf->cji_class != NULL)
588442e0eafSGarance A Drosehn 		fprintf(newcf, "C%s\n", cjinf->cji_class);
589442e0eafSGarance A Drosehn 	if (cjinf->cji_jobname != NULL)
590442e0eafSGarance A Drosehn 		fprintf(newcf, "J%s\n", cjinf->cji_jobname);
591ad1f7851SGarance A Drosehn 	if (cjinf->cji_headruser != NULL)
592ad1f7851SGarance A Drosehn 		fprintf(newcf, "L%s\n", cjinf->cji_headruser);
593442e0eafSGarance A Drosehn 
594442e0eafSGarance A Drosehn 	/*
595442e0eafSGarance A Drosehn 	 * This should probably add more sanity checks on mailto value.
596442e0eafSGarance A Drosehn 	 * Note that if the mailto value is "wrong", then there's no good
597442e0eafSGarance A Drosehn 	 * way to know what the "correct" value would be, and we should not
598442e0eafSGarance A Drosehn 	 * semd email to some random address.  At least for now, just ignore
599442e0eafSGarance A Drosehn 	 * any invalid values.
600442e0eafSGarance A Drosehn 	 */
601442e0eafSGarance A Drosehn 	nogood = 0;
602442e0eafSGarance A Drosehn 	if (cjinf->cji_mailto == NULL)
603442e0eafSGarance A Drosehn 		nogood = 1;
604442e0eafSGarance A Drosehn 	else {
605da464120SGarance A Drosehn 		for (cp = cjinf->cji_mailto; *cp != '\0'; cp++) {
606442e0eafSGarance A Drosehn 			if (*cp <= ' ') {
607442e0eafSGarance A Drosehn 				nogood = 1;
608442e0eafSGarance A Drosehn 				break;
609442e0eafSGarance A Drosehn 			}
610442e0eafSGarance A Drosehn 		}
611442e0eafSGarance A Drosehn 	}
612442e0eafSGarance A Drosehn 	if (!nogood)
613442e0eafSGarance A Drosehn 		fprintf(newcf, "M%s\n", cjinf->cji_mailto);
614442e0eafSGarance A Drosehn 
615442e0eafSGarance A Drosehn 	/*
616442e0eafSGarance A Drosehn 	 * Now go thru the old control file, copying all information which
617442e0eafSGarance A Drosehn 	 * hasn't already been written into the new file.
618442e0eafSGarance A Drosehn 	 */
619442e0eafSGarance A Drosehn 	ctl_rewindcf(cjinf);
620442e0eafSGarance A Drosehn 	lbuff = ctl_getline(cjinf);
621442e0eafSGarance A Drosehn 	while (lbuff != NULL) {
622442e0eafSGarance A Drosehn 		switch (lbuff[0]) {
623442e0eafSGarance A Drosehn 		case 'H':
624442e0eafSGarance A Drosehn 		case 'P':
625442e0eafSGarance A Drosehn 		case 'C':
626442e0eafSGarance A Drosehn 		case 'J':
627442e0eafSGarance A Drosehn 		case 'L':
628442e0eafSGarance A Drosehn 		case 'M':
629442e0eafSGarance A Drosehn 			/* already wrote values for these to the newcf */
630442e0eafSGarance A Drosehn 			break;
631442e0eafSGarance A Drosehn 		case 'N':
632442e0eafSGarance A Drosehn 			/* see comments under 'U'... */
633442e0eafSGarance A Drosehn 			if (cjinf->cji_dfcount == 0) {
634442e0eafSGarance A Drosehn 				/* in this case, 'N's will be done in 'U' */
635442e0eafSGarance A Drosehn 				break;
636442e0eafSGarance A Drosehn 			}
637442e0eafSGarance A Drosehn 			fprintf(newcf, "%s\n", lbuff);
638442e0eafSGarance A Drosehn 			break;
639442e0eafSGarance A Drosehn 		case 'U':
640442e0eafSGarance A Drosehn 			/*
641442e0eafSGarance A Drosehn 			 * check for the very common case where the remote
642442e0eafSGarance A Drosehn 			 * host had to process 'lpr -s -r', but it did not
643442e0eafSGarance A Drosehn 			 * remove the Unlink line from the control file.
644442e0eafSGarance A Drosehn 			 * Such Unlink lines will legitimately have a '/' in
645442e0eafSGarance A Drosehn 			 * them, but it is the original lpr host which would
646442e0eafSGarance A Drosehn 			 * have done the unlink of such files, and not any
647442e0eafSGarance A Drosehn 			 * host receiving that job.
648442e0eafSGarance A Drosehn 			 */
649442e0eafSGarance A Drosehn 			slash = strchr(lbuff, '/');
650442e0eafSGarance A Drosehn 			if (slash != NULL) {
651442e0eafSGarance A Drosehn 				break;		/* skip this line */
652442e0eafSGarance A Drosehn 			}
653442e0eafSGarance A Drosehn 			/*
654442e0eafSGarance A Drosehn 			 * Okay, another kind of broken lpr implementation
655442e0eafSGarance A Drosehn 			 * is one which send datafiles, and Unlink's those
656442e0eafSGarance A Drosehn 			 * datafiles, but never includes any PRINT request
657442e0eafSGarance A Drosehn 			 * for those files.  Experimentation shows that one
658442e0eafSGarance A Drosehn 			 * copy of those datafiles should be printed with a
659442e0eafSGarance A Drosehn 			 * format of 'f'.  If this is an example of such a
660442e0eafSGarance A Drosehn 			 * screwed-up control file, fix it here.
661442e0eafSGarance A Drosehn 			 */
662442e0eafSGarance A Drosehn 			if (cjinf->cji_dfcount == 0) {
663442e0eafSGarance A Drosehn 				lbuff++;
664442e0eafSGarance A Drosehn 				if (strncmp(lbuff, "df", (size_t)2) == 0) {
665442e0eafSGarance A Drosehn 					fprintf(newcf, "f%s\n", lbuff);
666442e0eafSGarance A Drosehn 					fprintf(newcf, "U%s\n", lbuff);
667442e0eafSGarance A Drosehn 					fprintf(newcf, "N%s\n", lbuff);
668442e0eafSGarance A Drosehn 				}
669442e0eafSGarance A Drosehn 				break;
670442e0eafSGarance A Drosehn 			}
671442e0eafSGarance A Drosehn 			fprintf(newcf, "%s\n", lbuff);
672442e0eafSGarance A Drosehn 			break;
673442e0eafSGarance A Drosehn 		default:
674442e0eafSGarance A Drosehn 			fprintf(newcf, "%s\n", lbuff);
675442e0eafSGarance A Drosehn 			break;
676442e0eafSGarance A Drosehn 		}
677442e0eafSGarance A Drosehn 		lbuff = ctl_getline(cjinf);
678442e0eafSGarance A Drosehn 	}
679442e0eafSGarance A Drosehn 
680442e0eafSGarance A Drosehn 	ctl_freeinf(cjinf);
681442e0eafSGarance A Drosehn 	cjinf = NULL;
682442e0eafSGarance A Drosehn 
683442e0eafSGarance A Drosehn 	res = fclose(newcf);
684442e0eafSGarance A Drosehn 	newcf = NULL;
685442e0eafSGarance A Drosehn 	if (res != 0) {
686442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
687442e0eafSGarance A Drosehn 		    "ctl_renametf error fclose(%s): %s", tfname2,
688442e0eafSGarance A Drosehn 		    strerror(errno));
689442e0eafSGarance A Drosehn 		goto error_ret;
690442e0eafSGarance A Drosehn 	}
691442e0eafSGarance A Drosehn 
692442e0eafSGarance A Drosehn 	strlcpy(cfname2, tfname, sizeof(cfname2));
693442e0eafSGarance A Drosehn 	cfname2[0] = 'c';		/* rename new file to 'cfA*' */
694442e0eafSGarance A Drosehn 	res = link(tfname2, cfname2);
695442e0eafSGarance A Drosehn 	if (res != 0) {
696442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
697442e0eafSGarance A Drosehn 		    "ctl_renametf error link(%s,%s): %s", tfname2, cfname2,
698442e0eafSGarance A Drosehn 		    strerror(errno));
699442e0eafSGarance A Drosehn 		goto error_ret;
700442e0eafSGarance A Drosehn 	}
701442e0eafSGarance A Drosehn 
702442e0eafSGarance A Drosehn 	/* All the important work is done.  Now just remove temp files */
703442e0eafSGarance A Drosehn #ifdef LEAVE_TMPCF_FILES
704442e0eafSGarance A Drosehn 	{
705442e0eafSGarance A Drosehn 		struct stat tfstat;
706442e0eafSGarance A Drosehn 		size_t size1;
707442e0eafSGarance A Drosehn 		tfstat.st_size = 1;	/* certainly invalid value */
708442e0eafSGarance A Drosehn 		res = stat(tfname, &tfstat);
709442e0eafSGarance A Drosehn 		size1 = tfstat.st_size;
710442e0eafSGarance A Drosehn 		tfstat.st_size = 2;	/* certainly invalid value */
711442e0eafSGarance A Drosehn 		res = stat(tfname2, &tfstat);
712394fd40fSGarance A Drosehn 		/*
713394fd40fSGarance A Drosehn 		 * If the sizes do not match, or either stat call failed,
714394fd40fSGarance A Drosehn 		 * then do not remove the temp files, but just move them
715394fd40fSGarance A Drosehn 		 * out of the way.  This is so I can see what this routine
716394fd40fSGarance A Drosehn 		 * had changed (and the files won't interfere with some
717394fd40fSGarance A Drosehn 		 * later job coming in from the same host).  In this case,
718394fd40fSGarance A Drosehn 		 * we don't care if we clobber some previous file.
719442e0eafSGarance A Drosehn 		 */
720394fd40fSGarance A Drosehn 		if (size1 != tfstat.st_size) {
721394fd40fSGarance A Drosehn 			strlcpy(cfname2, tfname, sizeof(cfname2));
722394fd40fSGarance A Drosehn 			strlcat(cfname2, "._T", sizeof(cfname2));
723394fd40fSGarance A Drosehn 			rename(tfname, cfname2);
724394fd40fSGarance A Drosehn 			strlcpy(cfname2, tfname2, sizeof(cfname2));
725394fd40fSGarance A Drosehn 			strlcat(cfname2, "._T", sizeof(cfname2));
726394fd40fSGarance A Drosehn 			rename(tfname2, cfname2);
727442e0eafSGarance A Drosehn 			return NULL;
728442e0eafSGarance A Drosehn 		}
729394fd40fSGarance A Drosehn 	}
730442e0eafSGarance A Drosehn #endif
731442e0eafSGarance A Drosehn 	unlink(tfname);
732442e0eafSGarance A Drosehn 	unlink(tfname2);
733442e0eafSGarance A Drosehn 
734442e0eafSGarance A Drosehn 	return NULL;
735442e0eafSGarance A Drosehn 
736442e0eafSGarance A Drosehn error_ret:
737442e0eafSGarance A Drosehn 	if (cjinf != NULL)
738442e0eafSGarance A Drosehn 		ctl_freeinf(cjinf);
739442e0eafSGarance A Drosehn 	if (newcf != NULL)
740442e0eafSGarance A Drosehn 		fclose(newcf);
741442e0eafSGarance A Drosehn 
742442e0eafSGarance A Drosehn 	if (*errm != '\0')
743442e0eafSGarance A Drosehn 		return strdup(errm);
744442e0eafSGarance A Drosehn 	return strdup("ctl_renametf internal (missed) error");
745442e0eafSGarance A Drosehn }
746442e0eafSGarance A Drosehn 
747442e0eafSGarance A Drosehn void
ctl_rewindcf(struct cjobinfo * cjinf)748442e0eafSGarance A Drosehn ctl_rewindcf(struct cjobinfo *cjinf)
749442e0eafSGarance A Drosehn {
750442e0eafSGarance A Drosehn 	struct cjprivate *cpriv;
751442e0eafSGarance A Drosehn 
752442e0eafSGarance A Drosehn 	if (cjinf == NULL)
753442e0eafSGarance A Drosehn 		return;
754442e0eafSGarance A Drosehn 	cpriv = cjinf->cji_priv;
755442e0eafSGarance A Drosehn 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
756442e0eafSGarance A Drosehn 		syslog(LOG_ERR, "in ctl_rewindcf(%p): invalid cjinf (cpriv %p)",
75732e56cd1SGarance A Drosehn 		    (void *)cjinf, (void *)cpriv);
758442e0eafSGarance A Drosehn 		return;
759442e0eafSGarance A Drosehn 	}
760442e0eafSGarance A Drosehn 
761442e0eafSGarance A Drosehn 	rewind(cpriv->cji_fstream);		/* assume no errors... :-) */
762442e0eafSGarance A Drosehn }
763442e0eafSGarance A Drosehn 
764442e0eafSGarance A Drosehn char *
ctl_rmjob(const char * ptrname,const char * cfname)765442e0eafSGarance A Drosehn ctl_rmjob(const char *ptrname, const char *cfname)
766442e0eafSGarance A Drosehn {
767442e0eafSGarance A Drosehn 	struct cjobinfo	*cjinf;
768442e0eafSGarance A Drosehn 	char *lbuff;
769442e0eafSGarance A Drosehn 	char errm[CTI_LINEMAX];
770442e0eafSGarance A Drosehn 
771442e0eafSGarance A Drosehn 	cjinf = ctl_readcf(ptrname, cfname);
772442e0eafSGarance A Drosehn 	if (cjinf == NULL) {
773442e0eafSGarance A Drosehn 		snprintf(errm, sizeof(errm),
774442e0eafSGarance A Drosehn 		    "ctl_renametf error cti_readcf(%s)", cfname);
775442e0eafSGarance A Drosehn 		return strdup(errm);
776442e0eafSGarance A Drosehn 	}
777442e0eafSGarance A Drosehn 
778442e0eafSGarance A Drosehn 	ctl_rewindcf(cjinf);
779442e0eafSGarance A Drosehn 	lbuff = ctl_getline(cjinf);
780442e0eafSGarance A Drosehn 	while (lbuff != NULL) {
781442e0eafSGarance A Drosehn 		/* obviously we need to fill in the following... */
782442e0eafSGarance A Drosehn 		switch (lbuff[0]) {
783442e0eafSGarance A Drosehn 		case 'S':
784442e0eafSGarance A Drosehn 			break;
785442e0eafSGarance A Drosehn 		case 'U':
786442e0eafSGarance A Drosehn 			break;
787442e0eafSGarance A Drosehn 		default:
788442e0eafSGarance A Drosehn 			break;
789442e0eafSGarance A Drosehn 		}
790442e0eafSGarance A Drosehn 		lbuff = ctl_getline(cjinf);
791442e0eafSGarance A Drosehn 	}
792442e0eafSGarance A Drosehn 
793442e0eafSGarance A Drosehn 	ctl_freeinf(cjinf);
794442e0eafSGarance A Drosehn 	cjinf = NULL;
795442e0eafSGarance A Drosehn 
796442e0eafSGarance A Drosehn 	return NULL;
797442e0eafSGarance A Drosehn }
798442e0eafSGarance A Drosehn 
799442e0eafSGarance A Drosehn /*
800442e0eafSGarance A Drosehn  * The following routine was originally written to pin down a bug.  It is
801442e0eafSGarance A Drosehn  * no longer needed for that problem, but may be useful to keep around for
802442e0eafSGarance A Drosehn  * other debugging.
803442e0eafSGarance A Drosehn  */
804442e0eafSGarance A Drosehn void
ctl_dumpcji(FILE * dbg_stream,const char * heading,struct cjobinfo * cjinf)805442e0eafSGarance A Drosehn ctl_dumpcji(FILE *dbg_stream, const char *heading, struct cjobinfo *cjinf)
806442e0eafSGarance A Drosehn {
807442e0eafSGarance A Drosehn #define PRINTSTR(xHdr,xStr) \
808442e0eafSGarance A Drosehn 	astr = xStr; \
809442e0eafSGarance A Drosehn 	ctl_dbgline++; \
810442e0eafSGarance A Drosehn 	fprintf(dbg_stream, "%4d] %12s = ", ctl_dbgline, xHdr); \
811442e0eafSGarance A Drosehn 	if (astr == NULL) \
812442e0eafSGarance A Drosehn 		fprintf(dbg_stream, "NULL\n"); \
813442e0eafSGarance A Drosehn 	else \
814442e0eafSGarance A Drosehn 		fprintf(dbg_stream, "%p -> %s\n", astr, astr)
815442e0eafSGarance A Drosehn 
816442e0eafSGarance A Drosehn 	struct cjprivate *cpriv;
817442e0eafSGarance A Drosehn 	char *astr;
818442e0eafSGarance A Drosehn 
819442e0eafSGarance A Drosehn 	if (cjinf == NULL) {
820442e0eafSGarance A Drosehn 		fprintf(dbg_stream,
821442e0eafSGarance A Drosehn 		    "ctl_dumpcji: ptr to cjobinfo for '%s' is NULL\n",
822442e0eafSGarance A Drosehn 		    heading);
823442e0eafSGarance A Drosehn 		return;
824442e0eafSGarance A Drosehn 	}
825442e0eafSGarance A Drosehn 	cpriv = cjinf->cji_priv;
826442e0eafSGarance A Drosehn 
827442e0eafSGarance A Drosehn 	fprintf(dbg_stream, "ctl_dumpcji: Dump '%s' of cjobinfo at %p->%p\n",
82832e56cd1SGarance A Drosehn 	    heading, (void *)cjinf, cpriv->cji_buff);
829442e0eafSGarance A Drosehn 
830442e0eafSGarance A Drosehn 	PRINTSTR("accthost.H", cpriv->pub.cji_accthost);
831442e0eafSGarance A Drosehn 	PRINTSTR("acctuser.P", cpriv->pub.cji_acctuser);
832442e0eafSGarance A Drosehn 	PRINTSTR("class.C", cpriv->pub.cji_class);
833442e0eafSGarance A Drosehn 	PRINTSTR("cf-qname", cpriv->pub.cji_curqueue);
834442e0eafSGarance A Drosehn 	PRINTSTR("cf-fname", cpriv->pub.cji_fname);
835442e0eafSGarance A Drosehn 	PRINTSTR("jobname.J", cpriv->pub.cji_jobname);
836442e0eafSGarance A Drosehn 	PRINTSTR("mailto.M", cpriv->pub.cji_mailto);
837ad1f7851SGarance A Drosehn 	PRINTSTR("headruser.L", cpriv->pub.cji_headruser);
838442e0eafSGarance A Drosehn 
839442e0eafSGarance A Drosehn 	ctl_dbgline++;
840442e0eafSGarance A Drosehn 	fprintf(dbg_stream, "%4d] %12s = ", ctl_dbgline, "*cjprivate");
841442e0eafSGarance A Drosehn 	if (cpriv->pub.cji_priv == NULL)
842442e0eafSGarance A Drosehn 		fprintf(dbg_stream, "NULL !!\n");
843442e0eafSGarance A Drosehn 	else
84432e56cd1SGarance A Drosehn 		fprintf(dbg_stream, "%p\n", (void *)cpriv->pub.cji_priv);
845442e0eafSGarance A Drosehn 
846442e0eafSGarance A Drosehn 	fprintf(dbg_stream, "|- - - - --> Dump '%s' complete\n", heading);
847442e0eafSGarance A Drosehn 
848442e0eafSGarance A Drosehn 	/* flush output for the benefit of anyone doing a 'tail -f' */
849442e0eafSGarance A Drosehn 	fflush(dbg_stream);
850442e0eafSGarance A Drosehn 
851442e0eafSGarance A Drosehn #undef PRINTSTR
852442e0eafSGarance A Drosehn }
853442e0eafSGarance A Drosehn 
854442e0eafSGarance A Drosehn /*
855442e0eafSGarance A Drosehn  * This routine reads in the next line from the control-file, and removes
856442e0eafSGarance A Drosehn  * the trailing newline character.
857442e0eafSGarance A Drosehn  *
858442e0eafSGarance A Drosehn  * Historical note: Earlier versions of this routine did tab-expansion for
859442e0eafSGarance A Drosehn  * ALL lines read in, which did not make any sense for most of the lines
860442e0eafSGarance A Drosehn  * in a control file.  For the lines where tab-expansion is useful, it will
861442e0eafSGarance A Drosehn  * now have to be done by the calling routine.
862442e0eafSGarance A Drosehn  */
863442e0eafSGarance A Drosehn static char *
ctl_getline(struct cjobinfo * cjinf)864442e0eafSGarance A Drosehn ctl_getline(struct cjobinfo *cjinf)
865442e0eafSGarance A Drosehn {
866442e0eafSGarance A Drosehn 	char *strp, *nl;
867442e0eafSGarance A Drosehn 	struct cjprivate *cpriv;
868442e0eafSGarance A Drosehn 
869442e0eafSGarance A Drosehn 	if (cjinf == NULL)
870442e0eafSGarance A Drosehn 		return NULL;
871442e0eafSGarance A Drosehn 	cpriv = cjinf->cji_priv;
872442e0eafSGarance A Drosehn 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
873442e0eafSGarance A Drosehn 		syslog(LOG_ERR, "in ctl_getline(%p): invalid cjinf (cpriv %p)",
87432e56cd1SGarance A Drosehn 		    (void *)cjinf, (void *)cpriv);
875442e0eafSGarance A Drosehn 		return NULL;
876442e0eafSGarance A Drosehn 	}
877442e0eafSGarance A Drosehn 
878442e0eafSGarance A Drosehn 	errno = 0;
879442e0eafSGarance A Drosehn 	strp = fgets(cpriv->cji_buff, cpriv->cji_buffsize, cpriv->cji_fstream);
880442e0eafSGarance A Drosehn 	if (strp == NULL) {
881442e0eafSGarance A Drosehn 		if (errno != 0)
882442e0eafSGarance A Drosehn 			syslog(LOG_ERR, "%s: ctl_getline error fgets(%s): %s",
883442e0eafSGarance A Drosehn 			    cpriv->pub.cji_curqueue, cpriv->pub.cji_fname,
884442e0eafSGarance A Drosehn 			    strerror(errno));
885442e0eafSGarance A Drosehn 		return NULL;
886442e0eafSGarance A Drosehn 	}
887442e0eafSGarance A Drosehn 	nl = strchr(strp, '\n');
888442e0eafSGarance A Drosehn 	if (nl != NULL)
889442e0eafSGarance A Drosehn 		*nl = '\0';
890442e0eafSGarance A Drosehn 
891442e0eafSGarance A Drosehn #ifdef DEBUGREADCF_FNAME
892442e0eafSGarance A Drosehn 	/* I'd like to find out if the previous work to expand tabs was ever
893442e0eafSGarance A Drosehn 	 * really used, and if so, on what lines and for what reason.
894442e0eafSGarance A Drosehn 	 * Yes, all this work probably means I'm obsessed about this 'tab'
895442e0eafSGarance A Drosehn 	 * issue, but isn't programming a matter of obsession?
896442e0eafSGarance A Drosehn 	 */
897442e0eafSGarance A Drosehn 	{
898442e0eafSGarance A Drosehn 		int tabcnt;
899442e0eafSGarance A Drosehn 		char *ch;
900442e0eafSGarance A Drosehn 
901442e0eafSGarance A Drosehn 		tabcnt = 0;
902442e0eafSGarance A Drosehn 		ch = strp;
903442e0eafSGarance A Drosehn 		for (ch = strp; *ch != '\0'; ch++) {
904442e0eafSGarance A Drosehn 			if (*ch == '\t')
905442e0eafSGarance A Drosehn 				tabcnt++;
906442e0eafSGarance A Drosehn 		}
907442e0eafSGarance A Drosehn 
908442e0eafSGarance A Drosehn 		if (tabcnt && (ctl_dbgfile != NULL)) {
909442e0eafSGarance A Drosehn 			cpriv->cji_dumpit++;
910442e0eafSGarance A Drosehn 			fprintf(ctl_dbgfile, "%s: tabs=%d '%s'\n",
911442e0eafSGarance A Drosehn 			    cpriv->pub.cji_fname, tabcnt, cpriv->cji_buff);
912442e0eafSGarance A Drosehn 		}
913442e0eafSGarance A Drosehn 	}
914442e0eafSGarance A Drosehn #endif
915442e0eafSGarance A Drosehn 	return strp;
916442e0eafSGarance A Drosehn }
917