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