1 1.26 andvar /* $NetBSD: recvjob.c,v 1.26 2021/07/24 21:31:39 andvar Exp $ */ 2 1.8 mikel 3 1.1 cgd /* 4 1.4 cgd * Copyright (c) 1983, 1993 5 1.4 cgd * The Regents of the University of California. All rights reserved. 6 1.4 cgd * 7 1.1 cgd * 8 1.1 cgd * Redistribution and use in source and binary forms, with or without 9 1.1 cgd * modification, are permitted provided that the following conditions 10 1.1 cgd * are met: 11 1.1 cgd * 1. Redistributions of source code must retain the above copyright 12 1.1 cgd * notice, this list of conditions and the following disclaimer. 13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 cgd * notice, this list of conditions and the following disclaimer in the 15 1.1 cgd * documentation and/or other materials provided with the distribution. 16 1.18 agc * 3. Neither the name of the University nor the names of its contributors 17 1.1 cgd * may be used to endorse or promote products derived from this software 18 1.1 cgd * without specific prior written permission. 19 1.1 cgd * 20 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.1 cgd * SUCH DAMAGE. 31 1.1 cgd */ 32 1.1 cgd 33 1.8 mikel #include <sys/cdefs.h> 34 1.8 mikel 35 1.1 cgd #ifndef lint 36 1.21 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ 37 1.21 lukem The Regents of the University of California. All rights reserved."); 38 1.4 cgd #endif /* not lint */ 39 1.4 cgd 40 1.4 cgd #ifndef lint 41 1.8 mikel #if 0 42 1.9 mrg static char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95"; 43 1.8 mikel #else 44 1.26 andvar __RCSID("$NetBSD: recvjob.c,v 1.26 2021/07/24 21:31:39 andvar Exp $"); 45 1.8 mikel #endif 46 1.1 cgd #endif /* not lint */ 47 1.1 cgd 48 1.1 cgd /* 49 1.1 cgd * Receive printer jobs from the network, queue them and 50 1.1 cgd * start the printer daemon. 51 1.1 cgd */ 52 1.4 cgd #include <sys/param.h> 53 1.4 cgd #include <sys/mount.h> 54 1.4 cgd #include <sys/stat.h> 55 1.1 cgd 56 1.4 cgd #include <unistd.h> 57 1.4 cgd #include <signal.h> 58 1.4 cgd #include <fcntl.h> 59 1.4 cgd #include <dirent.h> 60 1.4 cgd #include <syslog.h> 61 1.4 cgd #include <stdio.h> 62 1.4 cgd #include <stdlib.h> 63 1.4 cgd #include <string.h> 64 1.1 cgd #include "lp.h" 65 1.4 cgd #include "lp.local.h" 66 1.4 cgd #include "extern.h" 67 1.1 cgd #include "pathnames.h" 68 1.1 cgd 69 1.13 mjl #define ack() (void)write(STDOUT_FILENO, sp, 1); 70 1.1 cgd 71 1.10 mrg static char dfname[NAME_MAX]; /* data files */ 72 1.4 cgd static int minfree; /* keep at least minfree blocks available */ 73 1.20 christos static const char *sp = ""; 74 1.10 mrg static char tfname[NAME_MAX]; /* tmp copy of cf before linking */ 75 1.4 cgd 76 1.16 wiz static int chksize(int); 77 1.24 christos static void frecverr(const char *, ...) __dead __sysloglike(1, 2); 78 1.16 wiz static int noresponse(void); 79 1.16 wiz static void rcleanup(int); 80 1.20 christos static int read_number(const char *); 81 1.16 wiz static int readfile(char *, int); 82 1.16 wiz static int readjob(void); 83 1.1 cgd 84 1.1 cgd 85 1.4 cgd void 86 1.13 mjl recvjob(void) 87 1.1 cgd { 88 1.1 cgd struct stat stb; 89 1.20 christos int fd; 90 1.1 cgd 91 1.20 christos getprintcap(printer); 92 1.17 thorpej /* Set up the log file. */ 93 1.17 thorpej if ((fd = open(LF, O_WRONLY|O_APPEND, 0664)) < 0) { 94 1.1 cgd syslog(LOG_ERR, "%s: %m", LF); 95 1.17 thorpej fd = open(_PATH_DEVNULL, O_WRONLY); 96 1.1 cgd } 97 1.17 thorpej if (fd > 0) { 98 1.17 thorpej (void) dup2(fd, STDERR_FILENO); 99 1.17 thorpej (void) close(fd); 100 1.17 thorpej } else 101 1.17 thorpej (void) close(STDERR_FILENO); 102 1.1 cgd 103 1.1 cgd if (chdir(SD) < 0) 104 1.1 cgd frecverr("%s: %s: %m", printer, SD); 105 1.1 cgd if (stat(LO, &stb) == 0) { 106 1.1 cgd if (stb.st_mode & 010) { 107 1.1 cgd /* queue is disabled */ 108 1.1 cgd putchar('\1'); /* return error code */ 109 1.1 cgd exit(1); 110 1.1 cgd } 111 1.1 cgd } else if (stat(SD, &stb) < 0) 112 1.1 cgd frecverr("%s: %s: %m", printer, SD); 113 1.1 cgd minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */ 114 1.1 cgd signal(SIGTERM, rcleanup); 115 1.1 cgd signal(SIGPIPE, rcleanup); 116 1.1 cgd 117 1.1 cgd if (readjob()) 118 1.1 cgd printjob(); 119 1.1 cgd } 120 1.1 cgd 121 1.1 cgd /* 122 1.1 cgd * Read printer jobs sent by lpd and copy them to the spooling directory. 123 1.25 msaitoh * Return the number of jobs successfully transferred. 124 1.1 cgd */ 125 1.4 cgd static int 126 1.13 mjl readjob(void) 127 1.1 cgd { 128 1.10 mrg int size, nfiles; 129 1.10 mrg char *cp; 130 1.1 cgd 131 1.1 cgd ack(); 132 1.1 cgd nfiles = 0; 133 1.1 cgd for (;;) { 134 1.1 cgd /* 135 1.1 cgd * Read a command to tell us what to do 136 1.1 cgd */ 137 1.1 cgd cp = line; 138 1.1 cgd do { 139 1.13 mjl if ((size = read(STDOUT_FILENO, cp, 1)) != 1) { 140 1.1 cgd if (size < 0) 141 1.10 mrg frecverr("%s: Lost connection", 142 1.10 mrg printer); 143 1.1 cgd return(nfiles); 144 1.1 cgd } 145 1.22 lukem } while (*cp++ != '\n' && (size_t)(cp - line + 1) < sizeof(line)); 146 1.22 lukem if ((size_t)(cp - line + 1) >= sizeof(line)) 147 1.10 mrg frecverr("readjob overflow"); 148 1.1 cgd *--cp = '\0'; 149 1.1 cgd cp = line; 150 1.1 cgd switch (*cp++) { 151 1.1 cgd case '\1': /* cleanup because data sent was bad */ 152 1.4 cgd rcleanup(0); 153 1.1 cgd continue; 154 1.1 cgd 155 1.1 cgd case '\2': /* read cf file */ 156 1.1 cgd size = 0; 157 1.1 cgd while (*cp >= '0' && *cp <= '9') 158 1.1 cgd size = size * 10 + (*cp++ - '0'); 159 1.1 cgd if (*cp++ != ' ') 160 1.1 cgd break; 161 1.1 cgd /* 162 1.1 cgd * host name has been authenticated, we use our 163 1.1 cgd * view of the host name since we may be passed 164 1.1 cgd * something different than what gethostbyaddr() 165 1.1 cgd * returns 166 1.1 cgd */ 167 1.14 christos (void)strlcpy(cp + 6, from, 168 1.14 christos sizeof(line) + line - cp - 6); 169 1.14 christos if (strchr(cp, '/')) 170 1.14 christos frecverr("readjob: %s: illegal path name", cp); 171 1.14 christos (void)strlcpy(tfname, cp, sizeof(tfname)); 172 1.1 cgd tfname[0] = 't'; 173 1.1 cgd if (!chksize(size)) { 174 1.13 mjl (void)write(STDOUT_FILENO, "\2", 1); 175 1.1 cgd continue; 176 1.1 cgd } 177 1.1 cgd if (!readfile(tfname, size)) { 178 1.4 cgd rcleanup(0); 179 1.1 cgd continue; 180 1.1 cgd } 181 1.1 cgd if (link(tfname, cp) < 0) 182 1.1 cgd frecverr("%s: %m", tfname); 183 1.7 mrg (void)unlink(tfname); 184 1.1 cgd tfname[0] = '\0'; 185 1.1 cgd nfiles++; 186 1.1 cgd continue; 187 1.1 cgd 188 1.1 cgd case '\3': /* read df file */ 189 1.1 cgd size = 0; 190 1.1 cgd while (*cp >= '0' && *cp <= '9') 191 1.1 cgd size = size * 10 + (*cp++ - '0'); 192 1.1 cgd if (*cp++ != ' ') 193 1.1 cgd break; 194 1.1 cgd if (!chksize(size)) { 195 1.13 mjl (void)write(STDOUT_FILENO, "\2", 1); 196 1.1 cgd continue; 197 1.1 cgd } 198 1.14 christos if (strchr(cp, '/')) 199 1.14 christos frecverr("readjob: %s: illegal path name", cp); 200 1.14 christos (void)strlcpy(dfname, cp, sizeof(dfname)); 201 1.7 mrg (void)readfile(dfname, size); 202 1.1 cgd continue; 203 1.1 cgd } 204 1.4 cgd frecverr("protocol screwup: %s", line); 205 1.1 cgd } 206 1.1 cgd } 207 1.1 cgd 208 1.1 cgd /* 209 1.1 cgd * Read files send by lpd and copy them to the spooling directory. 210 1.1 cgd */ 211 1.4 cgd static int 212 1.13 mjl readfile(char *file, int size) 213 1.1 cgd { 214 1.10 mrg char *cp; 215 1.1 cgd char buf[BUFSIZ]; 216 1.10 mrg int i, j, amt; 217 1.1 cgd int fd, err; 218 1.1 cgd 219 1.1 cgd fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD); 220 1.1 cgd if (fd < 0) 221 1.1 cgd frecverr("readfile: %s: illegal path name: %m", file); 222 1.1 cgd ack(); 223 1.1 cgd err = 0; 224 1.1 cgd for (i = 0; i < size; i += BUFSIZ) { 225 1.1 cgd amt = BUFSIZ; 226 1.1 cgd cp = buf; 227 1.1 cgd if (i + amt > size) 228 1.1 cgd amt = size - i; 229 1.1 cgd do { 230 1.13 mjl j = read(STDOUT_FILENO, cp, amt); 231 1.1 cgd if (j <= 0) 232 1.1 cgd frecverr("Lost connection"); 233 1.1 cgd amt -= j; 234 1.1 cgd cp += j; 235 1.1 cgd } while (amt > 0); 236 1.1 cgd amt = BUFSIZ; 237 1.1 cgd if (i + amt > size) 238 1.1 cgd amt = size - i; 239 1.1 cgd if (write(fd, buf, amt) != amt) { 240 1.1 cgd err++; 241 1.1 cgd break; 242 1.1 cgd } 243 1.1 cgd } 244 1.7 mrg (void)close(fd); 245 1.1 cgd if (err) 246 1.1 cgd frecverr("%s: write error", file); 247 1.1 cgd if (noresponse()) { /* file sent had bad data in it */ 248 1.7 mrg (void)unlink(file); 249 1.1 cgd return(0); 250 1.1 cgd } 251 1.1 cgd ack(); 252 1.1 cgd return(1); 253 1.1 cgd } 254 1.1 cgd 255 1.4 cgd static int 256 1.13 mjl noresponse(void) 257 1.1 cgd { 258 1.1 cgd char resp; 259 1.1 cgd 260 1.13 mjl if (read(STDOUT_FILENO, &resp, 1) != 1) 261 1.1 cgd frecverr("Lost connection"); 262 1.1 cgd if (resp == '\0') 263 1.1 cgd return(0); 264 1.1 cgd return(1); 265 1.1 cgd } 266 1.1 cgd 267 1.1 cgd /* 268 1.1 cgd * Check to see if there is enough space on the disk for size bytes. 269 1.1 cgd * 1 == OK, 0 == Not OK. 270 1.1 cgd */ 271 1.4 cgd static int 272 1.13 mjl chksize(int size) 273 1.1 cgd { 274 1.1 cgd int spacefree; 275 1.19 christos struct statvfs sfb; 276 1.1 cgd 277 1.19 christos if (statvfs(".", &sfb) < 0) { 278 1.1 cgd syslog(LOG_ERR, "%s: %m", "statfs(\".\")"); 279 1.1 cgd return (1); 280 1.1 cgd } 281 1.19 christos spacefree = sfb.f_bavail * (sfb.f_frsize / 512); 282 1.1 cgd size = (size + 511) / 512; 283 1.1 cgd if (minfree + size > spacefree) 284 1.1 cgd return(0); 285 1.1 cgd return(1); 286 1.1 cgd } 287 1.1 cgd 288 1.4 cgd static int 289 1.20 christos read_number(const char *fn) 290 1.1 cgd { 291 1.1 cgd char lin[80]; 292 1.10 mrg FILE *fp; 293 1.1 cgd 294 1.1 cgd if ((fp = fopen(fn, "r")) == NULL) 295 1.1 cgd return (0); 296 1.1 cgd if (fgets(lin, 80, fp) == NULL) { 297 1.1 cgd fclose(fp); 298 1.1 cgd return (0); 299 1.1 cgd } 300 1.1 cgd fclose(fp); 301 1.1 cgd return (atoi(lin)); 302 1.1 cgd } 303 1.1 cgd 304 1.1 cgd /* 305 1.26 andvar * Remove all the files associated with the current job being transferred. 306 1.1 cgd */ 307 1.4 cgd static void 308 1.13 mjl rcleanup(int signo) 309 1.1 cgd { 310 1.1 cgd if (tfname[0]) 311 1.7 mrg (void)unlink(tfname); 312 1.1 cgd if (dfname[0]) 313 1.1 cgd do { 314 1.11 mrg do { 315 1.11 mrg if (strchr(dfname, '/') == 0) 316 1.11 mrg (void)unlink(dfname); 317 1.11 mrg } while (dfname[2]-- != 'A'); 318 1.1 cgd dfname[2] = 'z'; 319 1.1 cgd } while (dfname[0]-- != 'd'); 320 1.1 cgd dfname[0] = '\0'; 321 1.1 cgd } 322 1.1 cgd 323 1.4 cgd #include <stdarg.h> 324 1.4 cgd 325 1.4 cgd static void 326 1.4 cgd frecverr(const char *msg, ...) 327 1.1 cgd { 328 1.5 pk extern char fromb[]; 329 1.4 cgd va_list ap; 330 1.16 wiz 331 1.4 cgd va_start(ap, msg); 332 1.4 cgd rcleanup(0); 333 1.4 cgd syslog(LOG_ERR, "%s", fromb); 334 1.4 cgd vsyslog(LOG_ERR, msg, ap); 335 1.4 cgd va_end(ap); 336 1.1 cgd putchar('\1'); /* return error code */ 337 1.1 cgd exit(1); 338 1.1 cgd } 339