1 /*
2  * ------+---------+---------+---------+---------+---------+---------+---------*
3  * Copyright (c) 2001  - Garance Alistair Drosehn <gad@FreeBSD.org>.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation
28  * are those of the authors and should not be interpreted as representing
29  * official policies, either expressed or implied, of the FreeBSD Project.
30  *
31  * ------+---------+---------+---------+---------+---------+---------+---------*
32  *
33  * $FreeBSD: src/usr.sbin/lpr/common_source/ctlinfo.c,v 1.1.2.6 2002/07/14 23:47:09 gad Exp $
34  */
35 
36 /*
37  * ctlinfo - This collection of routines will know everything there is to
38  * know about the information inside a control file ('cf*') which is used
39  * to describe a print job in lpr & friends.  The eventual goal is that it
40  * will be the ONLY source file to know what's inside these control-files.
41  */
42 
43 /*
44  * Some define's useful for debuging.
45  * TRIGGERTEST_FNAME and DEBUGREADCF_FNAME, allow us to do testing on
46  * a per-spool-directory basis.
47  */
48 /* #define TRIGGERTEST_FNAME "LpdTestRenameTF" */
49 /* #define DEBUGREADCF_FNAME "LpdDebugReadCF" */
50 /* #define LEAVE_TMPCF_FILES 1 */
51 
52 #include <sys/param.h>
53 #include <sys/stat.h>
54 #include <ctype.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <limits.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <syslog.h>
63 #include <unistd.h>
64 #include "ctlinfo.h"
65 
66 struct cjprivate {
67 	struct cjobinfo pub;
68 	char	*cji_buff;		/* buffer for getline */
69 	char	*cji_eobuff;		/* last byte IN the buffer */
70 	FILE	*cji_fstream;
71 	int	 cji_buffsize;		/* # bytes in the buffer */
72 	int	 cji_dumpit;
73 };
74 
75 /*
76  * This has to be large enough to fit the maximum length of a single line
77  * in a control-file, including the leading 'command id', a trailing '\n'
78  * and ending '\0'.  The max size of an 'U'nlink line, for instance, is
79  * 1 ('U') + PATH_MAX (filename) + 2 ('\n\0').  The maximum 'H'ost line is
80  * 1 ('H') + NI_MAXHOST (remote hostname) + 2 ('\n\0').  Other lines can be
81  * even longer than those.  So, pick some nice, large, arbitrary value.
82  */
83 #define CTI_LINEMAX  PATH_MAX+NI_MAXHOST+5
84 
85 extern const char	*from_host;	/* client's machine name */
86 extern const char	*from_ip;	/* client machine's IP address */
87 
88 __BEGIN_DECLS
89 void		 ctl_dumpcji(FILE *_dbg_stream, const char *_heading,
90 		    struct cjobinfo *_cjinf);
91 static char	*ctl_getline(struct cjobinfo *_cjinf);
92 static void	 ctl_rewindcf(struct cjobinfo *_cjinf);
93 char		*ctl_rmjob(const char *_ptrname, const char *_cfname);
94 __END_DECLS
95 
96 /*
97  *	Control-files (cf*) have the following format.
98  *
99  *	Each control-file describes a single job.  It will list one or more
100  *	"datafiles" (df*) which should be copied to some printer.  Usually
101  *	there is only one datafile per job.  For the curious, RFC 1179 is an
102  *	informal and out-of-date description of lpr/lpd circa 1990.
103  *
104  *	Each line in the file gives an attribute of the job as a whole, or one
105  *	of the datafiles in the job, or a "command" indicating something to do
106  *	with one of the datafiles.  Each line starts with an 'id' that indicates
107  *	what that line is there for.  The 'id' is historically a single byte,
108  *	but may be multiple bytes (obviously it would be best if multi-byte ids
109  *	started with some letter not already used as a single-byte id!).
110  *	After the 'id', the remainder of the line will be the value of the
111  *	indicated attribute, or a name of the datafile to be operated on.
112  *
113  *	In the following lists of ids, the ids with a '!' in front of them are
114  *	NOT explicitly supported by this version of lpd, or at least "not yet
115  *	supported".  They are only listed for reference purposes, so people
116  *	won't be tempted to reuse the same id for a different purpose.
117  *
118  *	The following are attributes of the job which should not appear more
119  *	than once in a control file.  Only the 'H' and 'P' lines are required
120  *	by the RFC, but some implementations of lpr won't even get that right.
121  *
122  *	! A   - [used by lprNG]
123  *	  B   - As far as I know, this is never used as a single-byte id.
124  *		Therefore, I intend to use it for multi-byte id codes.
125  *	  C   - "class name" to display on banner page (this is sometimes
126  *		used to hold options for print filters)
127  *	! D   - [in lprNG, "timestamp" of when the job was submitted]
128  *	! E   - "environment variables" to set [some versions of linux]
129  *	  H   - "host name" of machine where the original 'lpr' was done
130  *	  I   - "indent", the amount to indent output
131  *	  J   - "job name" to display on banner page
132  *	  L   - "literal" user's name as it should be displayed on the
133  *		banner page (it is the existence of an 'L' line which
134  *		indicates that a job should have a banner page).
135  *	  M   - "mail", userid to mail to when done printing (with email
136  *		going to 'M'@'H', so to speak).
137  *	  P   - "person", the user's login name (e.g. for accounting)
138  *	! Q   - [used by lprNG for queue-name]
139  *	  R   - "resolution" in dpi, for some laser printer queues
140  *	  T   - "title" for files sent thru 'pr'
141  *	  W   - "width" to use for printing plain-text files
142  *	  Z   - In BSD, "locale" to use for datafiles sent thru 'pr'.
143  *		(this BSD usage should move to a different id...)
144  *		[in lprNG - this line holds the "Z options"]
145  *	  1   - "R font file" for files sent thru troff
146  *	  2   - "I font file" for files sent thru troff
147  *	  3   - "B font file" for files sent thru troff
148  *	  4   - "S font file" for files sent thru troff
149  *
150  *	The following are attributes attached to a datafile, and thus may
151  *	appear multiple times in a control file (once per datafile):
152  *
153  *	  N   - "name" of file (for display purposes, used by 'lpq')
154  *	  S   - "stat() info" used for symbolic link ('lpr -s')
155  *		security checks.
156  *
157  *	The following indicate actions to take on a given datafile.  The same
158  *	datafile may appear on more than one "print this file" command in the
159  *	control file.  Note that ALL ids with lowercase letters are expected
160  *	to be actions to "print this file":
161  *
162  *	  c   - "file name", cifplot file to print.  This action appears
163  *		when the user has requested 'lpr -c'.
164  *	  d   - "file name", dvi file to print, user requested 'lpr -d'
165  *	  f   - "file name", a plain-text file to print = "standard"
166  *	  g   - "file name", plot(1G) file to print, ie 'lpr -g'
167  *	  l   - "file name", text file with control chars which should
168  *		be printed literally, ie 'lpr -l'  (note: some printers
169  *		take this id as a request to print a postscript file,
170  *		and because of *that* some OS's use 'l' to indicate
171  *		that a datafile is a postscript file)
172  *	  n   - "file name", ditroff(1) file to print, ie 'lpr -n'
173  *	  o   - "file name", a postscript file to print.  This id is
174  *		described in the original RFC, but not much has been
175  *		done with it.  This 'lpr' does not generate control
176  *		lines with 'o'-actions, but lpd's printjob processing
177  *		will treat it the same as 'l'.
178  *	  p   - "file name", text file to print with pr(1), ie 'lpr -p'
179  *	  t   - "file name", troff(1) file to print, ie 'lpr -t'
180  *	  v   - "file name", plain raster file to print
181  *
182  *	  U   - "file name" of datafile to unlink (ie, remove file
183  *		from spool directory.  To be done in a 'Pass 2',
184  *		AFTER having processed all datafiles in the job).
185  *
186  */
187 
188 void
189 ctl_freeinf(struct cjobinfo *cjinf)
190 {
191 #define FREESTR(xStr) \
192 	if (xStr != NULL) { \
193 		free(xStr); \
194 		xStr = NULL;\
195 	}
196 
197 	struct cjprivate *cpriv;
198 
199 	if (cjinf == NULL)
200 		return;
201 	cpriv = cjinf->cji_priv;
202 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
203 		syslog(LOG_ERR, "in ctl_freeinf(%p): invalid cjinf (cpriv %p)",
204 		    (void *)cjinf, (void *)cpriv);
205 		return;
206 	}
207 
208 	FREESTR(cpriv->pub.cji_accthost);
209 	FREESTR(cpriv->pub.cji_acctuser);
210 	FREESTR(cpriv->pub.cji_class);
211 	FREESTR(cpriv->pub.cji_curqueue);
212 	/* [cpriv->pub.cji_fname is part of cpriv-malloced area] */
213 	FREESTR(cpriv->pub.cji_jobname);
214 	FREESTR(cpriv->pub.cji_mailto);
215 	FREESTR(cpriv->pub.cji_username);
216 
217 	if (cpriv->cji_fstream != NULL) {
218 		fclose(cpriv->cji_fstream);
219 		cpriv->cji_fstream = NULL;
220 	}
221 
222 	cjinf->cji_priv = NULL;
223 	free(cpriv);
224 #undef FREESTR
225 }
226 
227 #ifdef DEBUGREADCF_FNAME
228 static FILE *ctl_dbgfile = NULL;
229 static struct stat ctl_dbgstat;
230 #endif
231 static int ctl_dbgline = 0;
232 
233 struct cjobinfo *
234 ctl_readcf(const char *ptrname, const char *cfname)
235 {
236 	int id;
237 	char *lbuff;
238 	void *cstart;
239 	FILE *cfile;
240 	struct cjprivate *cpriv;
241 	struct cjobinfo *cjinf;
242 	size_t msize, sroom, sroom2;
243 
244 	cfile = fopen(cfname, "r");
245 	if (cfile == NULL) {
246 		syslog(LOG_ERR, "%s: ctl_readcf error fopen(%s): %s",
247 		    ptrname, cfname, strerror(errno));
248 		return NULL;
249 	}
250 
251 	sroom = roundup(sizeof(struct cjprivate), 8);
252 	sroom2 = sroom + strlen(cfname) + 1;
253 	sroom2 = roundup(sroom2, 8);
254 	msize = sroom2 + CTI_LINEMAX;
255 	msize = roundup(msize, 8);
256 	cstart = malloc(msize);
257 	if (cstart == NULL)
258 		return NULL;
259 	memset(cstart, 0, msize);
260 	cpriv = (struct cjprivate *)cstart;
261 	cpriv->pub.cji_priv = cpriv;
262 
263 	cpriv->pub.cji_fname = (char *)cstart + sroom;
264 	strcpy(cpriv->pub.cji_fname, cfname);
265 	cpriv->cji_buff = (char *)cstart + sroom2;
266 	cpriv->cji_buffsize = (int)(msize - sroom2);
267 	cpriv->cji_eobuff = (char *)cstart + msize - 1;
268 
269 	cpriv->cji_fstream = cfile;
270 	cpriv->pub.cji_curqueue = strdup(ptrname);
271 
272 	ctl_dbgline = 0;
273 #ifdef DEBUGREADCF_FNAME
274 	ctl_dbgfile = NULL;
275 	id = stat(DEBUGREADCF_FNAME, &ctl_dbgstat);
276 	if (id != -1) {
277 		/* the file exists in this spool directory, write some simple
278 		 * debugging info to it */
279 		ctl_dbgfile = fopen(DEBUGREADCF_FNAME, "a");
280 		if (ctl_dbgfile != NULL) {
281 			fprintf(ctl_dbgfile, "%s: s=%p r=%ld e=%p %p->%s\n",
282 			    ptrname, (void *)cpriv, (long)sroom,
283 			    cpriv->cji_eobuff, cpriv->pub.cji_fname,
284 			    cpriv->pub.cji_fname);
285 		}
286 	}
287 #endif
288 	/*
289 	 * Copy job-attribute values from control file to the struct of
290 	 * "public" information.  In some cases, it is invalid for the
291 	 * value to be a null-string, so that is ignored.
292 	 */
293 	cjinf = &(cpriv->pub);
294 	lbuff = ctl_getline(cjinf);
295 	while (lbuff != NULL) {
296 		id = *lbuff++;
297 		switch (id) {
298 		case 'C':
299 			cpriv->pub.cji_class = strdup(lbuff);
300 			break;
301 		case 'H':
302 			if (*lbuff == '\0')
303 				break;
304 			cpriv->pub.cji_accthost = strdup(lbuff);
305 			break;
306 		case 'J':
307 			cpriv->pub.cji_jobname = strdup(lbuff);
308 			break;
309 		case 'L':
310 			cpriv->pub.cji_username = strdup(lbuff);
311 			break;
312 		case 'M':
313 			/*
314 			 * No valid mail-to address would start with a minus.
315 			 * If this one does, it is probably some trickster who
316 			 * is trying to trigger options on sendmail.  Ignore.
317 			 */
318 			if (*lbuff == '-')
319 				break;
320 			if (*lbuff == '\0')
321 				break;
322 			cpriv->pub.cji_mailto = strdup(lbuff);
323 			break;
324 		case 'P':
325 			/* don't allow userid's with a leading minus, either */
326 			if (*lbuff == '-')
327 				break;
328 			if (*lbuff == '\0')
329 				break;
330 			cpriv->pub.cji_acctuser = strdup(lbuff);
331 			break;
332 		default:
333 			if (islower(id)) {
334 				cpriv->pub.cji_dfcount++;
335 			}
336 			break;
337 		}
338 		lbuff = ctl_getline(cjinf);
339 	}
340 
341 	/* the 'H'ost and 'P'erson fields are *always* supposed to be there */
342 	if (cpriv->pub.cji_accthost == NULL)
343 		cpriv->pub.cji_accthost = strdup(".na.");
344 	if (cpriv->pub.cji_acctuser == NULL)
345 		cpriv->pub.cji_acctuser = strdup(".na.");
346 
347 #ifdef DEBUGREADCF_FNAME
348 	if (ctl_dbgfile != NULL) {
349 		if (cpriv->cji_dumpit)
350 			ctl_dumpcji(ctl_dbgfile, "end readcf", &(cpriv->pub));
351 		fclose(ctl_dbgfile);
352 		ctl_dbgfile = NULL;
353 	}
354 #endif
355 	return &(cpriv->pub);
356 }
357 
358 /*
359  * This routine renames the temporary control file as received from some
360  * other (remote) host.  That file will almost always with `tfA*', because
361  * recvjob.c creates the file by changing `c' to `t' in the original name
362  * for the control file.  Now if you read the RFC, you would think that all
363  * control filenames start with `cfA*'.  However, it seems there are some
364  * implementations which send control filenames which start with `cf'
365  * followed by *any* letter, so this routine can not assume what the third
366  * letter will (or will not) be.  Sigh.
367  *
368  * So this will rewrite the temporary file to `rf*' (correcting any lines
369  * which need correcting), rename that `rf*' file to `cf*', and then remove
370  * the original `tf*' temporary file.
371  *
372  * The *main* purpose of this routine is to be paranoid about the contents
373  * of that control file.  It is partially meant to protect against people
374  * TRYING to cause trouble (perhaps after breaking into root of some host
375  * that this host will accept print jobs from).  The fact that we're willing
376  * to print jobs from some remote host does not mean that we should blindly
377  * do anything that host tells us to do.
378  *
379  * This is also meant to protect us from errors in other implementations of
380  * lpr, particularly since we may want to use some values from the control
381  * file as environment variables when it comes time to print, or as parameters
382  * to commands which will be exec'ed, or values in statistics records.
383  *
384  * This may also do some "conversions" between how different versions of
385  * lpr or lprNG define the contents of various lines in a control file.
386  *
387  * If there is an error, it returns a pointer to a descriptive error message.
388  * Error messages which are RETURNED (as opposed to syslog-ed) do not include
389  * the printer-queue name.  Let the caller add that if it is wanted.
390  */
391 char *
392 ctl_renametf(const char *ptrname, const char *tfname)
393 {
394 	int chk3rd, newfd, nogood, res;
395 	FILE *newcf;
396 	struct cjobinfo *cjinf;
397 	char *lbuff, *slash, *cp;
398 	char tfname2[NAME_MAX+1], cfname2[NAME_MAX+1];
399 	char errm[CTI_LINEMAX];
400 
401 #ifdef TRIGGERTEST_FNAME
402 	struct stat tstat;
403 	res = stat(TRIGGERTEST_FNAME, &tstat);
404 	if (res == -1) {
405 		/*
406 		 * if the trigger file does NOT exist in this spool directory,
407 		 * then do the exact same steps that the pre-ctlinfo code had
408 		 * been doing.  Ie, very little.
409 		 */
410 		strlcpy(cfname2, tfname, sizeof(cfname2));
411 		cfname2[0] = 'c';
412 		res = link(tfname, cfname2);
413 		if (res < 0) {
414 			snprintf(errm, sizeof(errm),
415 			    "ctl_renametf error link(%s,%s): %s", tfname,
416 			    cfname2, strerror(errno));
417 			return strdup(errm);
418 		}
419 		unlink(tfname);
420 		return NULL;
421 	}
422 #endif
423 	cjinf = NULL;		/* in case of early jump to error_ret */
424 	newcf = NULL;		/* in case of early jump to error_ret */
425 	*errm = '\0';		/* in case of early jump to error_ret */
426 
427 	chk3rd = tfname[2];
428 	if ((tfname[0] != 't') || (tfname[1] != 'f') || (!isalpha(chk3rd))) {
429 		snprintf(errm, sizeof(errm),
430 		    "ctl_renametf invalid filename: %s", tfname);
431 		goto error_ret;
432 	}
433 
434 	cjinf = ctl_readcf(ptrname, tfname);
435 	if (cjinf == NULL) {
436 		snprintf(errm, sizeof(errm),
437 		    "ctl_renametf error cti_readcf(%s)", tfname);
438 		goto error_ret;
439 	}
440 
441 	/*
442 	 * This uses open+fdopen instead of fopen because that combination
443 	 * gives us greater control over file-creation issues.
444 	 */
445 	strlcpy(tfname2, tfname, sizeof(tfname2));
446 	tfname2[0] = 'r';		/* rf<letter><job><hostname> */
447 	newfd = open(tfname2, O_WRONLY|O_CREAT|O_TRUNC, 0660);
448 	if (newfd == -1) {
449 		snprintf(errm, sizeof(errm),
450 		    "ctl_renametf error open(%s): %s", tfname2,
451 		    strerror(errno));
452 		goto error_ret;
453 	}
454 	newcf = fdopen(newfd, "w");
455 	if (newcf == NULL) {
456 		close(newfd);
457 		snprintf(errm, sizeof(errm),
458 		    "ctl_renametf error fopen(%s): %s", tfname2,
459 		    strerror(errno));
460 		goto error_ret;
461 	}
462 
463 	/*
464 	 * Do extra sanity checks on some key job-attribute fields, and
465 	 * write them out first (thus making sure they are written in the
466 	 * order we generally expect them to be in).
467 	 */
468 	/*
469 	 * Some lpr implementations on PC's set a null-string for their
470 	 * hostname.  A MacOS 10 system which has not correctly setup
471 	 * /etc/hostconfig will claim a hostname of 'localhost'.  Anything
472 	 * with blanks in it would be an invalid value for hostname.  For
473 	 * any of these invalid hostname values, replace the given value
474 	 * with the name of the host that this job is coming from.
475 	 */
476 	nogood = 0;
477 	if (cjinf->cji_accthost == NULL)
478 		nogood = 1;
479 	else if (strcmp(cjinf->cji_accthost, ".na.") == 0)
480 		nogood = 1;
481 	else if (strcmp(cjinf->cji_accthost, "localhost") == 0)
482 		nogood = 1;
483 	else {
484 		for (cp = cjinf->cji_accthost; *cp != '\0'; cp++) {
485 			if (*cp <= ' ') {
486 				nogood = 1;
487 				break;
488 			}
489 		}
490 	}
491 	if (nogood)
492 		fprintf(newcf, "H%s\n", from_host);
493 	else
494 		fprintf(newcf, "H%s\n", cjinf->cji_accthost);
495 
496 	/*
497 	 * Now do some sanity checks on the 'P' (original userid) value.  Note
498 	 * that the 'P'erson line is the second line which is ALWAYS supposed
499 	 * to be present in a control file.
500 	 *
501 	 * There is no particularly good value to use for replacements, but
502 	 * at least make sure the value is something reasonable to use in
503 	 * environment variables and statistics records.  Again, some PC
504 	 * implementations send a null-string for a value.  Various Mac
505 	 * implementations will set whatever string the user has set for
506 	 * their 'Owner Name', which usually includes blanks, etc.
507 	 */
508 	nogood = 0;
509 	if (cjinf->cji_acctuser == NULL)
510 		nogood = 1;
511 	else {
512 		for (cp = cjinf->cji_acctuser; *cp != '\0'; cp++) {
513 			if (*cp <= ' ')
514 				*cp = '_';
515 		}
516 	}
517 	if (nogood)
518 		fprintf(newcf, "P%s\n", ".na.");
519 	else
520 		fprintf(newcf, "P%s\n", cjinf->cji_acctuser);
521 
522 	/* No need for sanity checks on class, jobname, "literal" user. */
523 	if (cjinf->cji_class != NULL)
524 		fprintf(newcf, "C%s\n", cjinf->cji_class);
525 	if (cjinf->cji_jobname != NULL)
526 		fprintf(newcf, "J%s\n", cjinf->cji_jobname);
527 	if (cjinf->cji_username != NULL)
528 		fprintf(newcf, "L%s\n", cjinf->cji_username);
529 
530 	/*
531 	 * This should probably add more sanity checks on mailto value.
532 	 * Note that if the mailto value is "wrong", then there's no good
533 	 * way to know what the "correct" value would be, and we should not
534 	 * semd email to some random address.  At least for now, just ignore
535 	 * any invalid values.
536 	 */
537 	nogood = 0;
538 	if (cjinf->cji_mailto == NULL)
539 		nogood = 1;
540 	else {
541 		for (cp = cjinf->cji_acctuser; *cp != '\0'; cp++) {
542 			if (*cp <= ' ') {
543 				nogood = 1;
544 				break;
545 			}
546 		}
547 	}
548 	if (!nogood)
549 		fprintf(newcf, "M%s\n", cjinf->cji_mailto);
550 
551 	/*
552 	 * Now go thru the old control file, copying all information which
553 	 * hasn't already been written into the new file.
554 	 */
555 	ctl_rewindcf(cjinf);
556 	lbuff = ctl_getline(cjinf);
557 	while (lbuff != NULL) {
558 		switch (lbuff[0]) {
559 		case 'H':
560 		case 'P':
561 		case 'C':
562 		case 'J':
563 		case 'L':
564 		case 'M':
565 			/* already wrote values for these to the newcf */
566 			break;
567 		case 'N':
568 			/* see comments under 'U'... */
569 			if (cjinf->cji_dfcount == 0) {
570 				/* in this case, 'N's will be done in 'U' */
571 				break;
572 			}
573 			fprintf(newcf, "%s\n", lbuff);
574 			break;
575 		case 'U':
576 			/*
577 			 * check for the very common case where the remote
578 			 * host had to process 'lpr -s -r', but it did not
579 			 * remove the Unlink line from the control file.
580 			 * Such Unlink lines will legitimately have a '/' in
581 			 * them, but it is the original lpr host which would
582 			 * have done the unlink of such files, and not any
583 			 * host receiving that job.
584 			 */
585 			slash = strchr(lbuff, '/');
586 			if (slash != NULL) {
587 				break;		/* skip this line */
588 			}
589 			/*
590 			 * Okay, another kind of broken lpr implementation
591 			 * is one which send datafiles, and Unlink's those
592 			 * datafiles, but never includes any PRINT request
593 			 * for those files.  Experimentation shows that one
594 			 * copy of those datafiles should be printed with a
595 			 * format of 'f'.  If this is an example of such a
596 			 * screwed-up control file, fix it here.
597 			 */
598 			if (cjinf->cji_dfcount == 0) {
599 				lbuff++;
600 				if (strncmp(lbuff, "df", (size_t)2) == 0) {
601 					fprintf(newcf, "f%s\n", lbuff);
602 					fprintf(newcf, "U%s\n", lbuff);
603 					fprintf(newcf, "N%s\n", lbuff);
604 				}
605 				break;
606 			}
607 			fprintf(newcf, "%s\n", lbuff);
608 			break;
609 		default:
610 			fprintf(newcf, "%s\n", lbuff);
611 			break;
612 		}
613 		lbuff = ctl_getline(cjinf);
614 	}
615 
616 	ctl_freeinf(cjinf);
617 	cjinf = NULL;
618 
619 	res = fclose(newcf);
620 	newcf = NULL;
621 	if (res != 0) {
622 		snprintf(errm, sizeof(errm),
623 		    "ctl_renametf error fclose(%s): %s", tfname2,
624 		    strerror(errno));
625 		goto error_ret;
626 	}
627 
628 	strlcpy(cfname2, tfname, sizeof(cfname2));
629 	cfname2[0] = 'c';		/* rename new file to 'cfA*' */
630 	res = link(tfname2, cfname2);
631 	if (res != 0) {
632 		snprintf(errm, sizeof(errm),
633 		    "ctl_renametf error link(%s,%s): %s", tfname2, cfname2,
634 		    strerror(errno));
635 		goto error_ret;
636 	}
637 
638 	/* All the important work is done.  Now just remove temp files */
639 #ifdef LEAVE_TMPCF_FILES
640 	{
641 		struct stat tfstat;
642 		size_t size1;
643 		tfstat.st_size = 1;	/* certainly invalid value */
644 		res = stat(tfname, &tfstat);
645 		size1 = tfstat.st_size;
646 		tfstat.st_size = 2;	/* certainly invalid value */
647 		res = stat(tfname2, &tfstat);
648 		/* if the sizes do not match, or either stat call failed,
649 		 * then do not remove the temp files, but return "all OK".
650 		 * This is just so I can see what this routine had changed.
651 		 */
652 		if (size1 != tfstat.st_size)
653 			return NULL;
654 	}
655 #endif
656 	unlink(tfname);
657 	unlink(tfname2);
658 
659 	return NULL;
660 
661 error_ret:
662 	if (cjinf != NULL)
663 		ctl_freeinf(cjinf);
664 	if (newcf != NULL)
665 		fclose(newcf);
666 
667 	if (*errm != '\0')
668 		return strdup(errm);
669 	return strdup("ctl_renametf internal (missed) error");
670 }
671 
672 void
673 ctl_rewindcf(struct cjobinfo *cjinf)
674 {
675 	struct cjprivate *cpriv;
676 
677 	if (cjinf == NULL)
678 		return;
679 	cpriv = cjinf->cji_priv;
680 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
681 		syslog(LOG_ERR, "in ctl_rewindcf(%p): invalid cjinf (cpriv %p)",
682 		    (void *)cjinf, (void *)cpriv);
683 		return;
684 	}
685 
686 	rewind(cpriv->cji_fstream);		/* assume no errors... :-) */
687 }
688 
689 char *
690 ctl_rmjob(const char *ptrname, const char *cfname)
691 {
692 	struct cjobinfo	*cjinf;
693 	char *lbuff;
694 	char errm[CTI_LINEMAX];
695 
696 	cjinf = ctl_readcf(ptrname, cfname);
697 	if (cjinf == NULL) {
698 		snprintf(errm, sizeof(errm),
699 		    "ctl_renametf error cti_readcf(%s)", cfname);
700 		return strdup(errm);
701 	}
702 
703 	ctl_rewindcf(cjinf);
704 	lbuff = ctl_getline(cjinf);
705 	while (lbuff != NULL) {
706 		/* obviously we need to fill in the following... */
707 		switch (lbuff[0]) {
708 		case 'S':
709 			break;
710 		case 'U':
711 			break;
712 		default:
713 			break;
714 		}
715 		lbuff = ctl_getline(cjinf);
716 	}
717 
718 	ctl_freeinf(cjinf);
719 	cjinf = NULL;
720 
721 	return NULL;
722 }
723 
724 /*
725  * The following routine was originally written to pin down a bug.  It is
726  * no longer needed for that problem, but may be useful to keep around for
727  * other debugging.
728  */
729 void
730 ctl_dumpcji(FILE *dbg_stream, const char *heading, struct cjobinfo *cjinf)
731 {
732 #define PRINTSTR(xHdr,xStr) \
733 	astr = xStr; \
734 	ctl_dbgline++; \
735 	fprintf(dbg_stream, "%4d] %12s = ", ctl_dbgline, xHdr); \
736 	if (astr == NULL) \
737 		fprintf(dbg_stream, "NULL\n"); \
738 	else \
739 		fprintf(dbg_stream, "%p -> %s\n", astr, astr)
740 
741 	struct cjprivate *cpriv;
742 	char *astr;
743 
744 	if (cjinf == NULL) {
745 		fprintf(dbg_stream,
746 		    "ctl_dumpcji: ptr to cjobinfo for '%s' is NULL\n",
747 		    heading);
748 		return;
749 	}
750 	cpriv = cjinf->cji_priv;
751 
752 	fprintf(dbg_stream, "ctl_dumpcji: Dump '%s' of cjobinfo at %p->%p\n",
753 	    heading, (void *)cjinf, cpriv->cji_buff);
754 
755 	PRINTSTR("accthost.H", cpriv->pub.cji_accthost);
756 	PRINTSTR("acctuser.P", cpriv->pub.cji_acctuser);
757 	PRINTSTR("class.C", cpriv->pub.cji_class);
758 	PRINTSTR("cf-qname", cpriv->pub.cji_curqueue);
759 	PRINTSTR("cf-fname", cpriv->pub.cji_fname);
760 	PRINTSTR("jobname.J", cpriv->pub.cji_jobname);
761 	PRINTSTR("mailto.M", cpriv->pub.cji_mailto);
762 	PRINTSTR("hdruser.L", cpriv->pub.cji_username);
763 
764 	ctl_dbgline++;
765 	fprintf(dbg_stream, "%4d] %12s = ", ctl_dbgline, "*cjprivate");
766 	if (cpriv->pub.cji_priv == NULL)
767 		fprintf(dbg_stream, "NULL !!\n");
768 	else
769 		fprintf(dbg_stream, "%p\n", (void *)cpriv->pub.cji_priv);
770 
771 	fprintf(dbg_stream, "|- - - - --> Dump '%s' complete\n", heading);
772 
773 	/* flush output for the benefit of anyone doing a 'tail -f' */
774 	fflush(dbg_stream);
775 
776 #undef PRINTSTR
777 }
778 
779 /*
780  * This routine reads in the next line from the control-file, and removes
781  * the trailing newline character.
782  *
783  * Historical note: Earlier versions of this routine did tab-expansion for
784  * ALL lines read in, which did not make any sense for most of the lines
785  * in a control file.  For the lines where tab-expansion is useful, it will
786  * now have to be done by the calling routine.
787  */
788 static char *
789 ctl_getline(struct cjobinfo *cjinf)
790 {
791 	char *strp, *nl;
792 	struct cjprivate *cpriv;
793 
794 	if (cjinf == NULL)
795 		return NULL;
796 	cpriv = cjinf->cji_priv;
797 	if ((cpriv == NULL) || (cpriv != cpriv->pub.cji_priv)) {
798 		syslog(LOG_ERR, "in ctl_getline(%p): invalid cjinf (cpriv %p)",
799 		    (void *)cjinf, (void *)cpriv);
800 		return NULL;
801 	}
802 
803 	errno = 0;
804 	strp = fgets(cpriv->cji_buff, cpriv->cji_buffsize, cpriv->cji_fstream);
805 	if (strp == NULL) {
806 		if (errno != 0)
807 			syslog(LOG_ERR, "%s: ctl_getline error fgets(%s): %s",
808 			    cpriv->pub.cji_curqueue, cpriv->pub.cji_fname,
809 			    strerror(errno));
810 		return NULL;
811 	}
812 	nl = strchr(strp, '\n');
813 	if (nl != NULL)
814 		*nl = '\0';
815 
816 #ifdef DEBUGREADCF_FNAME
817 	/* I'd like to find out if the previous work to expand tabs was ever
818 	 * really used, and if so, on what lines and for what reason.
819 	 * Yes, all this work probably means I'm obsessed about this 'tab'
820 	 * issue, but isn't programming a matter of obsession?
821 	 */
822 	{
823 		int tabcnt;
824 		char *ch;
825 
826 		tabcnt = 0;
827 		ch = strp;
828 		for (ch = strp; *ch != '\0'; ch++) {
829 			if (*ch == '\t')
830 				tabcnt++;
831 		}
832 
833 		if (tabcnt && (ctl_dbgfile != NULL)) {
834 			cpriv->cji_dumpit++;
835 			fprintf(ctl_dbgfile, "%s: tabs=%d '%s'\n",
836 			    cpriv->pub.cji_fname, tabcnt, cpriv->cji_buff);
837 		}
838 	}
839 #endif
840 	return strp;
841 }
842