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