Deleted Added
full compact
common.c (97421) common.c (98152)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 27 unchanged lines hidden (view full) ---

36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40/*
41static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95";
42*/
43static const char rcsid[] =
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 27 unchanged lines hidden (view full) ---

36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40/*
41static char sccsid[] = "@(#)common.c 8.5 (Berkeley) 4/28/95";
42*/
43static const char rcsid[] =
44 "$FreeBSD: head/usr.sbin/lpr/common_source/common.c 97421 2002-05-28 19:23:47Z alfred $";
44 "$FreeBSD: head/usr.sbin/lpr/common_source/common.c 98152 2002-06-13 01:55:48Z gad $";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/types.h>
51
52#include <dirent.h>
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/types.h>
51
52#include <dirent.h>
53#include <errno.h>
53#include <fcntl.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59#include "lp.h"
60#include "lp.local.h"

--- 186 unchanged lines hidden (view full) ---

247 if (pp->status_file[0] == '/')
248 strlcpy(buf, pp->status_file, len);
249 else
250 snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
251
252 return buf;
253}
254
54#include <fcntl.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60#include "lp.h"
61#include "lp.local.h"

--- 186 unchanged lines hidden (view full) ---

248 if (pp->status_file[0] == '/')
249 strlcpy(buf, pp->status_file, len);
250 else
251 snprintf(buf, len, "%s/%s", pp->spool_dir, pp->status_file);
252
253 return buf;
254}
255
256/*
257 * Routine to change operational state of a print queue. The operational
258 ��state is indicated by the access bits on the lock file for the queue.
259 * At present, this is only called from various routines in lpc/cmds.c.
260 *
261 * XXX - Note that this works by changing access-bits on the
262 * file, and you can only do that if you are the owner of
263 * the file, or root. Thus, this won't really work for
264 * userids in the "LPR_OPER" group, unless lpc is running
265 * setuid to root (or maybe setuid to daemon).
266 * Generally lpc is installed setgid to daemon, but does
267 * not run setuid.
268 */
269int
270set_qstate(int action, const char *lfname)
271{
272 struct stat stbuf;
273 mode_t chgbits, newbits, oldmask;
274 const char *failmsg, *okmsg;
275 int chres, errsav, fd, res, statres;
276
277 /*
278 * Find what the current access-bits are.
279 */
280 memset(&stbuf, 0, sizeof(stbuf));
281 seteuid(euid);
282 statres = stat(lfname, &stbuf);
283 errsav = errno;
284 seteuid(uid);
285 if ((statres < 0) && (errsav != ENOENT)) {
286 printf("\tcannot stat() lock file\n");
287 return (SQS_STATFAIL);
288 /* NOTREACHED */
289 }
290
291 /*
292 * Determine which bit(s) should change for the requested action.
293 */
294 chgbits = stbuf.st_mode;
295 newbits = LOCK_FILE_MODE;
296 okmsg = NULL;
297 failmsg = NULL;
298 if (action & SQS_DISABLEQ) {
299 chgbits |= LFM_QUEUE_DIS;
300 newbits |= LFM_QUEUE_DIS;
301 okmsg = "queuing disabled";
302 failmsg = "disable queuing";
303 }
304 if (action & SQS_STOPP) {
305 chgbits |= LFM_PRINT_DIS;
306 newbits |= LFM_PRINT_DIS;
307 okmsg = "printing disabled";
308 failmsg = "disable printing";
309 if (action & SQS_DISABLEQ) {
310 okmsg = "printer and queuing disabled";
311 failmsg = "disable queuing and printing";
312 }
313 }
314 if (action & SQS_ENABLEQ) {
315 chgbits &= ~LFM_QUEUE_DIS;
316 newbits &= ~LFM_QUEUE_DIS;
317 okmsg = "queuing enabled";
318 failmsg = "enable queuing";
319 }
320 if (action & SQS_STARTP) {
321 chgbits &= ~LFM_PRINT_DIS;
322 newbits &= ~LFM_PRINT_DIS;
323 okmsg = "printing enabled";
324 failmsg = "enable printing";
325 }
326 if (okmsg == NULL) {
327 /* This routine was called with an invalid action. */
328 printf("\t<error in set_qstate!>\n");
329 return (SQS_PARMERR);
330 /* NOTREACHED */
331 }
332
333 res = 0;
334 if (statres >= 0) {
335 /* The file already exists, so change the access. */
336 seteuid(euid);
337 chres = chmod(lfname, chgbits);
338 errsav = errno;
339 seteuid(uid);
340 res = SQS_CHGOK;
341 if (res < 0)
342 res = SQS_CHGFAIL;
343 } else if (newbits == LOCK_FILE_MODE) {
344 /*
345 * The file does not exist, but the state requested is
346 * the same as the default state when no file exists.
347 * Thus, there is no need to create the file.
348 */
349 res = SQS_SKIPCREOK;
350 } else {
351 /*
352 * The file did not exist, so create it with the
353 * appropriate access bits for the requested action.
354 * Push a new umask around that create, to make sure
355 * all the read/write bits are set as desired.
356 */
357 oldmask = umask(S_IWOTH);
358 seteuid(euid);
359 fd = open(lfname, O_WRONLY|O_CREAT, newbits);
360 errsav = errno;
361 seteuid(uid);
362 umask(oldmask);
363 res = SQS_CREFAIL;
364 if (fd >= 0) {
365 res = SQS_CREOK;
366 close(fd);
367 }
368 }
369
370 switch (res) {
371 case SQS_CHGOK:
372 case SQS_CREOK:
373 case SQS_SKIPCREOK:
374 printf("\t%s\n", okmsg);
375 break;
376 case SQS_CREFAIL:
377 printf("\tcannot create lock file: %s\n",
378 strerror(errsav));
379 break;
380 default:
381 printf("\tcannot %s: %s\n", failmsg, strerror(errsav));
382 break;
383 }
384
385 return (res);
386}
387
255/* routine to get a current timestamp, optionally in a standard-fmt string */
256void
257lpd_gettime(struct timespec *tsp, char *strp, size_t strsize)
258{
259 struct timespec local_ts;
260 struct timeval btime;
261 char tempstr[TIMESTR_SIZE];
262#ifdef STRFTIME_WRONG_z

--- 308 unchanged lines hidden ---
388/* routine to get a current timestamp, optionally in a standard-fmt string */
389void
390lpd_gettime(struct timespec *tsp, char *strp, size_t strsize)
391{
392 struct timespec local_ts;
393 struct timeval btime;
394 char tempstr[TIMESTR_SIZE];
395#ifdef STRFTIME_WRONG_z

--- 308 unchanged lines hidden ---