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