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