PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/lpr/common_source/ctlinfo.c

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