Пример #1
0
static int
copyPPDFile(char *ppd, char *printersPPD)

{
	int  result = 0;
	register int n = 0;
	int  fdin  = 0;
	int  fdout = 0;
	char buf[BUFSIZ];

	if ((ppd != NULL) && (printersPPD != NULL))
	{
		if ((fdin = open_locked(ppd, "r", 0)) < 0)
		{
			result = -1;
		}
		else
		{
			fdout = open_locked(printersPPD, "w", MODE_EXEC);
			if (fdout < 0)
			{
				close(fdin);
				result = -1;
			}
		}

		if (result == 0)
		{
			while ((n = read(fdin, buf, BUFSIZ)) > 0)
			{
				write(fdout, buf,  n);
			}
			close(fdout);
			close(fdin);
		}
	}
	else
	{
		result = -1;
	}

	return (result);
} /* copyPPDFile() */
Пример #2
0
int deluser ( char * user )
{
    int fd;

    if (!loaded)
    {
	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
	    return(-1);

	loaded = 1;
    }

    del_user(ppri_tbl, user);

    if ((fd = open_locked(Lp_Users, "w", LPU_MODE)) < 0)
	return(-1);

    output_tbl(fd, ppri_tbl);
    close(fd);
    return(0);
}
Пример #3
0
int
putclass(char *name, CLASS *clsbufp)
{
	char			*file;
	int fd;

	if (!name || !*name) {
		errno = EINVAL;
		return (-1);
	}

	if (STREQU(NAME_ALL, name)) {
		errno = EINVAL;
		return (-1);
	}

	/*
	 * Open the class file and write out the class members.
	 */

	if (!(file = getclassfile(name)))
		return (-1);

	if ((fd = open_locked(file, "w", MODE_READ)) < 0) {
		Free (file);
		return (-1);
	}
	Free (file);

	errno = 0;
	fdprintlist(fd, clsbufp->members);
	if (errno != 0) {
		close(fd);
		return (-1);
	}
	close(fd);

	return (0);
}
Пример #4
0
int putuser ( char * user, USER * pri_s )
{
    int fd;

    if (!loaded)
    {
	if (!(ppri_tbl = ld_priority_file(Lp_Users)))
	    return(-1);
	loaded = 1;
    }

    if (!add_user(ppri_tbl, user, pri_s->priority_limit))
    {
	return(-1);
    }

    if ((fd = open_locked(Lp_Users, "w", LPU_MODE)) < 0)
	return(-1);
    output_tbl(fd, ppri_tbl);
    close(fd);
    return(0);
}
Пример #5
0
int
main(int argc, char *argv[])
{
    int mtype, size, c,
	list = FALSE, limit = -1, deflt = -1;
    int fd;
    char *userlist = 0, *user, **users, *p;
    short status;
    struct user_priority *ppri_tbl, *ld_priority_file();
    extern char *optarg;
    extern int optind, opterr, optopt, errno;

    setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
    (void) textdomain(TEXT_DOMAIN);


    if(argc == 1) {
usage:
	(void) printf(gettext("usage: \n"));	
  	(void) printf(gettext("(assign priority limit to users)\n"));
	(void) printf(gettext("\tlpusers -q priority -u user-list\n"));

  	(void) printf(gettext(
		"(assign default priority limit for balance of users)\n"));
	(void) printf(gettext("\tlpusers -q priority\n"));

  	(void) printf(gettext("(put users back to default priority limit)\n"));
	(void) printf(gettext("\tlpusers -u user-list\n"));

  	(void) printf(gettext("(assign default priority)\n"));
	(void) printf(gettext("\tlpusers -d priority\n"));

  	(void) printf(gettext("(examine priority limits, defaults)\n"));
	(void) printf(gettext("\tlpusers -l\n"));

	exit(argc == 1);
    }

    opterr = 0; /* disable printing of errors by getopt */
    while ((c = getopt(argc, argv, "ld:q:u:")) != -1)
	switch(c) {
	case 'l':
	    if (list)
		LP_ERRMSG1(WARNING, E_LP_2MANY, 'l');
	    list = TRUE;
	    break;
	case 'd':
	    if (deflt != -1)
		LP_ERRMSG1(WARNING, E_LP_2MANY, 'd');
	    deflt = (int)strtol(optarg,&p,10);
	    if (*p || deflt<PRI_MIN || deflt>PRI_MAX) {
		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
		exit(1);
	    }
	    break;
	case 'q':
	    if (limit != -1)
		LP_ERRMSG1(WARNING, E_LP_2MANY, 'q');
	    limit = (int)strtol(optarg,&p,10);
	    if (*p || limit<PRI_MIN || limit>PRI_MAX) {
		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
		exit(1);
	    }
	    break;
	case 'u':
	    if (userlist)
		LP_ERRMSG1(WARNING, E_LP_2MANY, 'u');
	    userlist = optarg;
	    break;
	case '?':
	    if (optopt == '?') goto usage;
	    (p = "-X")[1] = optopt;
	    if (strchr("ldqu", optopt))
		LP_ERRMSG1(ERROR, E_LP_OPTARG, p);
	    else
		LP_ERRMSG1(ERROR, E_LP_OPTION, p);
	    exit(1);
	}

    if (optind < argc) {
	LP_ERRMSG1(ERROR, E_LP_EXTRA, argv[optind]);
	exit(1);
    }

    if (((list || deflt != -1) && (limit != -1 || userlist))
	|| (list && deflt != -1)) {
	LP_ERRMSG(ERROR, E_LP_OPTCOMB);
	/* invalid combination of options */
	exit(1);
    }

    PRIORITY = Lp_Users;

    /* load existing priorities from file */
    if (!(ppri_tbl = ld_priority_file(PRIORITY))) {
	switch (errno) {
	case EBADF:
	    LP_ERRMSG1(ERROR, E_LPU_BADFORM, PRIORITY);
	    break;
	default:
	    LP_ERRMSG2(ERROR, E_LPU_BADFILE, PRIORITY, errno);
	}
	exit(1);
    }

    if (list) {
	print_tbl(ppri_tbl);
	exit (0);
    } else {
	if (userlist) {
	    users = getlist(userlist, " \t", ",");
	    if (users)
		while (user = *users++) {
		    if (del_user(ppri_tbl, user) && (limit == -1))
			LP_ERRMSG1(WARNING, E_LPU_NOUSER, user);
		    if (limit != -1) {
			if (add_user(ppri_tbl, user, limit))
			    LP_ERRMSG1(WARNING, E_LPU_BADU, user);
		    }
		}
	} else if (deflt != -1)
	    ppri_tbl->deflt = deflt;
	else
	    ppri_tbl->deflt_limit = limit;

	if ((fd = open_locked(PRIORITY, "w", LPU_MODE)) < 0) {
	    LP_ERRMSG1(ERROR, E_LP_ACCESS, PRIORITY);
	    exit(1);
	}
	output_tbl(fd, ppri_tbl);
	close(fd);
    }

    if (mopen()) /* error on mopen == no spooler, exit quietly */
	exit(0);

    (void)putmessage (message, S_LOAD_USER_FILE);

    if (msend(message))
	goto Error;
    if (mrecv(reply, sizeof(reply)) == -1)
	goto Error;
    mtype = getmessage(reply, R_LOAD_USER_FILE, &status);
    if (mtype != R_LOAD_USER_FILE) {
	LP_ERRMSG1 (ERROR, E_LP_BADREPLY, mtype);
	goto NoError;
    }

    if (status == 0)
	goto NoError;

Error:	LP_ERRMSG (ERROR, E_LPU_NOLOAD);

NoError:(void)mclose ();
    return (0);
}
Пример #6
0
void
notify(register RSTATUS *prs, char *errbuf, int k, int e, int slow)
{
	register char		*cp;
	char			*file;
	int			fd;


	/*
	 * Screen out cases where no notification is needed.
	 */
	if (!(prs->request->outcome & RS_NOTIFY))
		return;
	if (
		!(prs->request->actions & (ACT_MAIL|ACT_WRITE|ACT_NOTIFY))
	     && !prs->request->alert
	     && !(prs->request->outcome & RS_CANCELLED)
	     && !e && !k && !errbuf       /* exited normally */
	)
		return;

	/*
	 * Create the notification message to the user.
	 */
	file = makereqerr(prs);
	if ((fd = open_locked(file, "w", MODE_NOREAD)) >= 0) {
		fdprintf(fd, N_Msg[0], prs->secure->req_id, prs->secure->req_id,
			prs->request->destination,
			STREQU(prs->request->destination, NAME_ANY)? " printer"
				: "");

		if (prs->request) {
			char file[BUFSIZ];
			
			GetRequestFiles(prs->request, file, sizeof(file));
			fdprintf(fd, "\nThe job title was:\t%s\n", file);
			fdprintf(fd, "     submitted by:\t%s\n",
				prs->request->user);
			fdprintf(fd, "               at:\t%s\n",
				ctime(&prs->secure->date));
		}
	
		if (prs->request->outcome & RS_PRINTED)
			fdprintf(fd, N_Msg[1], prs->printer->printer->name);

		if (prs->request->outcome & RS_CANCELLED)
			fdprintf(fd, N_Msg[2],
				(prs->request->outcome & RS_FAILED)? ", and"
					: ".");
		
	
		if (prs->request->outcome & RS_FAILED) {
			if (slow)
				fdprintf(fd, N_Msg[3]);
			else
				fdprintf(fd, N_Msg[4],
					prs->printer->printer->name);
	
			if (e > 0)
				fdprintf(fd, N_Msg[slow? 5 : 6], e);
			else if (k)
				fdprintf(fd, N_Msg[slow? 7 : 8], k);
		}
	
		if (errbuf) {
			for (cp = errbuf; *cp && *cp == '\n'; cp++)
				;
			fdprintf(fd, N_Msg[9], cp);
			if (prs->request->outcome & RS_CANCELLED)
				fdprintf(fd, "\n");
		}

		/* start fix for bugid 1100252	*/
		if (prs->request->outcome & RS_CANCELLED) {
			print_reason (fd, prs->reason);
		}

		close(fd);
		schedule (EV_NOTIFY, prs);

	}
	if (file)
		Free (file);

	return;
}
Пример #7
0
Файл: local.c Проект: micia/dma
int
deliver_local(struct qitem *it)
{
	char fn[PATH_MAX+1];
	char line[1000];
	const char *sender;
	const char *newline = "\n";
	size_t linelen;
	int tries = 0;
	int mbox;
	int error;
	int hadnl = 0;
	off_t mboxlen;
	time_t now = time(NULL);

	error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, it->addr);
	if (error < 0 || (size_t)error >= sizeof(fn)) {
		syslog(LOG_NOTICE, "local delivery deferred: %m");
		return (1);
	}

retry:
	/* wait for a maximum of 100s to get the lock to the file */
	do_timeout(100, 0);

	/* don't use O_CREAT here, because we might be running as the wrong user. */
	mbox = open_locked(fn, O_WRONLY|O_APPEND);
	if (mbox < 0) {
		int e = errno;

		do_timeout(0, 0);

		switch (e) {
		case EACCES:
		case ENOENT:
			/*
			 * The file does not exist or we can't access it.
			 * Call dma-mbox-create to create it and fix permissions.
			 */
			if (tries > 0 || create_mbox(it->addr) != 0) {
				syslog(LOG_ERR, "local delivery deferred: can not create `%s'", fn);
				return (1);
			}
			++tries;
			goto retry;

		case EINTR:
			syslog(LOG_NOTICE, "local delivery deferred: can not lock `%s'", fn);
			break;

		default:
			syslog(LOG_NOTICE, "local delivery deferred: can not open `%s': %m", fn);
			break;
		}
		return (1);
	}
	do_timeout(0, 0);

	mboxlen = lseek(mbox, 0, SEEK_END);

	/* New mails start with \nFrom ...., unless we're at the beginning of the mbox */
	if (mboxlen == 0)
		newline = "";

	/* If we're bouncing a message, claim it comes from MAILER-DAEMON */
	sender = it->sender;
	if (strcmp(sender, "") == 0)
		sender = "MAILER-DAEMON";

	if (fseek(it->mailf, 0, SEEK_SET) != 0) {
		syslog(LOG_NOTICE, "local delivery deferred: can not seek: %m");
		goto out;
	}

	error = snprintf(line, sizeof(line), "%sFrom %s\t%s", newline, sender, ctime(&now));
	if (error < 0 || (size_t)error >= sizeof(line)) {
		syslog(LOG_NOTICE, "local delivery deferred: can not write header: %m");
		goto out;
	}
	if (write(mbox, line, error) != error)
		goto wrerror;

	while (fgets(line, sizeof(line), it->mailf)) {
		linelen = strlen(line);
		if (linelen == 0 || line[linelen - 1] != '\n') {
			syslog(LOG_CRIT, "local delivery failed: corrupted queue file");
			snprintf(errmsg, sizeof(errmsg), "corrupted queue file");
			error = -1;
			goto chop;
		}

		/*
		 * mboxro processing:
		 * - escape lines that start with "From " with a > sign.
		 * - be reversable by escaping lines that contain an arbitrary
		 *   number of > signs, followed by "From ", i.e. />*From / in regexp.
		 * - strict mbox processing only requires escaping after empty lines,
		 *   yet most MUAs seem to relax this requirement and will treat any
		 *   line starting with "From " as the beginning of a new mail.
		 */
		if ((!MBOX_STRICT || hadnl) &&
		    strncmp(&line[strspn(line, ">")], "From ", 5) == 0) {
			const char *gt = ">";

			if (write(mbox, gt, 1) != 1)
				goto wrerror;
			hadnl = 0;
		} else if (strcmp(line, "\n") == 0) {
			hadnl = 1;
		} else {
			hadnl = 0;
		}
		if ((size_t)write(mbox, line, linelen) != linelen)
			goto wrerror;
	}
	
	if (ferror(it->mailf)) {
		syslog(LOG_ERR, "local delivery failed: I/O error while reading: %m");
		error = 1;
		goto chop;
	}
	
	close(mbox);
	return (0);

wrerror:
	syslog(LOG_ERR, "local delivery failed: write error: %m");
	error = 1;
chop:
	if (ftruncate(mbox, mboxlen) != 0)
		syslog(LOG_WARNING, "error recovering mbox `%s': %m", fn);
out:
	close(mbox);
	return (error);
}
Пример #8
0
int
putprinter(char *name, PRINTER *prbufp)
{
	register char *		path;
	register char *		stty;
	register char *		speed;

	int fdin, fdout;

	int			fld;

	char			buf[BUFSIZ];

	struct stat		statbuf1,
				statbuf2;


	badprinter = 0;

	if (!name || !*name) {
		errno = EINVAL;
		return (-1);
	}

	if (STREQU(NAME_ALL, name)) {
		errno = EINVAL;
		return (-1);
	}

	/*
	 * First go through the structure and see if we have
	 * anything strange.
	 */
	if (!okprinter(name, prbufp, 1)) {
		errno = EINVAL;
		return (-1);
	}

	if (!Lp_A_Printers || !Lp_A_Interfaces) {
		getadminpaths (LPUSER);
		if (!Lp_A_Printers || !Lp_A_Interfaces)
			return (0);
	}

	/*
	 * Create the parent directory for this printer
	 * if it doesn't yet exist.
	 */
	if (!(path = getprinterfile(name, (char *)0)))
		return (-1);
	if (Stat(path, &statbuf1) == 0) {
		if (!S_ISDIR(statbuf1.st_mode)) {
			Free (path);
			errno = ENOTDIR;
			return (-1);
		}
	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
		Free (path);
		return (-1);
	}
	Free (path);

	/*
	 * Create the copy of the interface program, unless
	 * that would be silly or not desired.
	 * Conversely, make sure the interface program doesn't
	 * exist for a remote printer.
	 */
	if (prbufp->remote) {
		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
			return (-1);
		(void)rmfile (path);
		Free (path);
	}
	if (prbufp->interface && (ignprinter & BAD_INTERFACE) == 0) {
		if (Stat(prbufp->interface, &statbuf1) == -1)
			return (-1);
		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
			return (-1);
		if (
			Stat(path, &statbuf2) == -1
		     || statbuf1.st_dev != statbuf2.st_dev
		     || statbuf1.st_ino != statbuf2.st_ino
		) {
			register int		n;

			if ((fdin = open_locked(prbufp->interface, "r", 0)) < 0) {
				Free (path);
				return (-1);
			}
			if ((fdout = open_locked(path, "w", MODE_EXEC)) < 0) {
				Free (path);
				close(fdin);
				return (-1);
			}
			while ((n = read(fdin, buf, BUFSIZ)) > 0)
				write (fdout, buf,  n);
			close(fdout);
			close(fdin);
		}
		Free (path);
	}

#ifdef LP_USE_PAPI_ATTR
	/*
	 * Handle PPD (Postscript Printer Definition) file for printer
	 * if this printer has been configured with one
	 */
	if ((prbufp->ppd != NULL) && (ppdopt))
	{
		if (addPrintersPPD(name, prbufp) != 0)
		{
			/* failed to added the printers PPD file */
			return (-1);
		}
	}
#endif

	/*
	 * If this printer is dialed up, remove any baud rates
	 * from the stty option list and move the last one to
	 * the ".speed" member if the ".speed" member isn't already
	 * set. Conversely, if this printer is directly connected,
	 * move any value from the ".speed" member to the stty list.
	 */

	stty = (prbufp->stty? Strdup(prbufp->stty) : 0);
	if (prbufp->speed)
		speed = Strdup(prbufp->speed);
	else
		speed = 0;

	if (prbufp->dial_info && stty) {
		register char		*newstty,
					*p,
					*q;

		register int		len;

		if (!(q = newstty = Malloc(strlen(stty) + 1))) {
			Free (stty);
			errno = ENOMEM;
			return (-1);
		}
		newstty[0] = 0;	/* start with empty copy */

		for (
			p = strtok(stty, " ");
			p;
			p = strtok((char *)0, " ")
		) {
			len = strlen(p);
			if (strspn(p, "0123456789") == len) {
				/*
				 * If "prbufp->speed" isn't set, then
				 * use the speed we just found. Don't
				 * check "speed", because if more than
				 * one speed was given in the list, we
				 * want the last one.
				 */
				if (!prbufp->speed) {
					if (speed)
						Free (speed);
					speed = Strdup(p);
				}

			} else {
				/*
				 * Not a speed, so copy it to the
				 * new stty string.
				 */
				if (q != newstty)
					*q++ = ' ';
				strcpy (q, p);
				q += len;
			}
		}

		Free (stty);
		stty = newstty;

	} else if (!prbufp->dial_info && speed) {
		register char		*newstty;

		newstty = Malloc(strlen(stty) + 1 + strlen(speed) + 1);
		if (!newstty) {
			if (stty)
				Free (stty);
			errno = ENOMEM;
			return (-1);
		}

		if (stty) {
			strcpy (newstty, stty);
			strcat (newstty, " ");
			strcat (newstty, speed);
			Free (stty);
		} else
			strcpy (newstty, speed);
		Free (speed);
		speed = 0;

		stty = newstty;

	}

	/*
	 * Open the configuration file and write out the printer
	 * configuration.
	 */

	if (!(path = getprinterfile(name, CONFIGFILE))) {
		if (stty)
			Free (stty);
		if (speed)
			Free (speed);
		return (-1);
	}
	if ((fdout = open_locked(path, "w", MODE_READ)) < 0) {
		Free (path);
		if (stty)
			Free (stty);
		if (speed)
			Free (speed);
		return (-1);
	}
	Free (path);

	errno = 0;
	for (fld = 0; fld < PR_MAX; fld++) {
		if (prbufp->remote && !prtrheadings[fld].okremote)
			continue;

		switch (fld) {

#define HEAD	prtrheadings[fld].v

		case PR_BAN:
			{
				char *ptr = NAME_ON;

				switch (prbufp->banner) {
				case BAN_ALWAYS:
					ptr = NAME_ON;
					break;
				case BAN_NEVER:
					ptr = NAME_OFF;
					break;
				case BAN_OPTIONAL:
					ptr = NAME_OPTIONAL;
					break;
				}
				(void)fdprintf(fdout, "%s %s\n", HEAD, ptr);
			}
			break;

		case PR_CPI:
			print_sdn(fdout, HEAD, prbufp->cpi);
			break;

		case PR_CS:
			if (!emptylist(prbufp->char_sets))
				print_l(fdout, HEAD, prbufp->char_sets);
			break;

		case PR_ITYPES:
			/*
			 * Put out the header even if the list is empty,
			 * to distinguish no input types from the default.
			 */
			print_l(fdout, HEAD, prbufp->input_types);
			break;

		case PR_DEV:
			print_str(fdout, HEAD, prbufp->device);
			break;

		case PR_DIAL:
			print_str(fdout, HEAD, prbufp->dial_info);
			break;

		case PR_RECOV:
			print_str(fdout, HEAD, prbufp->fault_rec);
			break;

		case PR_INTFC:
			print_str(fdout, HEAD, prbufp->interface);
			break;

		case PR_LPI:
			print_sdn(fdout, HEAD, prbufp->lpi);
			break;

		case PR_LEN:
			print_sdn(fdout, HEAD, prbufp->plen);
			break;

		case PR_LOGIN:
			if (prbufp->login & LOG_IN)
				(void)fdprintf(fdout, "%s\n", HEAD);
			break;

		case PR_PTYPE:
		{
			char			**printer_types;

			/*
			 * For backward compatibility for those who
			 * use only "->printer_type", we have to play
			 * some games here.
			 */
			if (prbufp->printer_type && !prbufp->printer_types)
				printer_types = getlist(
					prbufp->printer_type,
					LP_WS,
					LP_SEP
				);
			else
				printer_types = prbufp->printer_types;

			if (!printer_types || !*printer_types)
				print_str(fdout, HEAD, NAME_UNKNOWN);
			else
				print_l(fdout, HEAD, printer_types);

			if (printer_types != prbufp->printer_types)
				freelist (printer_types);
			break;
		}

		case PR_REMOTE:
			print_str(fdout, HEAD, prbufp->remote);
			break;

		case PR_SPEED:
			print_str(fdout, HEAD, speed);
			break;

		case PR_STTY:
			print_str(fdout, HEAD, stty);
			break;

		case PR_WIDTH:
			print_sdn(fdout, HEAD, prbufp->pwid);
			break;

#if	defined(CAN_DO_MODULES)
		case PR_MODULES:
			/*
			 * Put out the header even if the list is empty,
			 * to distinguish no modules from the default.
			 */
			print_l(fdout, HEAD, prbufp->modules);
			break;
#endif

		case PR_OPTIONS:
			print_l(fdout, HEAD, prbufp->options);
			break;

		case PR_PPD:
		{
			print_str(fdout, HEAD, prbufp->ppd);
			break;
		}
		}

	}
	if (stty)
		Free (stty);
	if (speed)
		Free (speed);
	if (errno != 0) {
		close(fdout);
		return (-1);
	}
	close(fdout);

	/*
	 * If we have a description of the printer,
	 * write it out to a separate file.
	 */
	if (prbufp->description) {

		if (!(path = getprinterfile(name, COMMENTFILE)))
			return (-1);

		if (dumpstring(path, prbufp->description) == -1) {
			Free (path);
			return (-1);
		}
		Free (path);
	
	}

	/*
	 * Now write out the alert condition.
	 */
	if (
		prbufp->fault_alert.shcmd
	     && putalert(Lp_A_Printers, name, &(prbufp->fault_alert)) == -1
	)
		return (-1);

	return (0);
}
Пример #9
0
struct user_priority * ld_priority_file ( char * path )
{
    char				line[BUFSIZ],
					*p,
					*user,
					*next_user();
    static struct user_priority		pri_tbl;
    int					line_no	= 1,
    					opri;
    int fd;

    if ((fd = open_locked(path, "r", 0)) < 0) {
	if (errno == ENOENT) {
empty:
	    pri_tbl.deflt = LEVEL_DFLT;
	    pri_tbl.deflt_limit = LIMIT_DFLT;
	    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
	    return (&pri_tbl);
	}
	return(0);
    }

    /* initialize table to empty */
    pri_tbl.deflt = -1;
    pri_tbl.deflt_limit = -1;
    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));

    /* this loop reads the line containing the default priority,
       if any, and the first priority limit.  p is left pointing
       to the colon (:) in the line with the first limit. */

    while (1)
    {
	if (!(p = fdgets(line, BUFSIZ, fd)))
	    goto empty;
	p = line;
	pri = strtol(line, &p, 10);
	if (p == line)
	    goto Error;
	if (pri < PRI_MIN || pri > PRI_MAX)
	    goto Error;
	if (line_no == 1 && *p == '\n' && !p[1])
	    pri_tbl.deflt = pri;
	else
	    if (*p == ':')
	    {
		p++;
		break;
	    }
	    else
		goto Error;
	line_no++;
    }

    do
    {
	/* search list for this priority */
	opri = pri;
	if (!(user = next_user(fd, line, &p)))
	{
	    if (pri_tbl.deflt_limit == -1)
	    {
		pri_tbl.deflt_limit = opri;
		if (pri == -1) break;
		if (!(user = next_user(fd, line, &p))) goto Error;
	    }
	    else
	    {
Error:
	        errno = EBADF;
		close(fd);
		return(0);
	    }
	}

	do
	{
	    add_user (&pri_tbl, user, pri);
	}
	while ((user = next_user(fd, line, &p)));
    }
    while (pri != -1);

    if (pri_tbl.deflt == -1)
	pri_tbl.deflt = LEVEL_DFLT;

    if (pri_tbl.deflt_limit == -1)
	pri_tbl.deflt_limit = LIMIT_DFLT;

    close(fd);
    return (&pri_tbl);
}
Пример #10
0
SECURE *
getsecure(char *file)
{
	SECURE		*secp;

	char			buf[BUFSIZ],
				*path;

	int fd;

	int			fld;


	if (*file == '/')
		path = Strdup(file);
	else
		path = makepath(Lp_Requests, file, (char *)0);
	if (!path)
		return (0);

	if ((fd = open_locked(path, "r", MODE_NOREAD)) < 0) {
		Free (path);
		return (0);
	}
	Free (path);

	secp = calloc(sizeof (*secp), 1);

	secp->user = 0;
	errno = 0;
	for (
		fld = 0;
		fld < SC_MAX && fdgets(buf, BUFSIZ, fd);
		fld++
	) {
		buf[strlen(buf) - 1] = 0;
		switch (fld) {

		case SC_REQID:
			secp->req_id = Strdup(buf);
			break;

		case SC_UID:
			secp->uid = (uid_t)atol(buf);
			break;

		case SC_USER:
			secp->user = Strdup(buf);
			break;

		case SC_GID:
			secp->gid = (gid_t)atol(buf);
			break;

		case SC_SIZE:
			secp->size = (size_t)atol(buf);
			break;

		case SC_DATE:
			secp->date = (time_t)atol(buf);
			break;

		case SC_SLABEL:
			secp->slabel = Strdup(buf);
			break;
		}
	}
	if (errno != 0 || fld != SC_MAX) {
		int			save_errno = errno;

		freesecure (secp);
		close(fd);
		errno = save_errno;
		return (0);
	}
	close(fd);

	/*
	 * Now go through the structure and see if we have
	 * anything strange.
	 */
	if (
	        secp->uid > MAXUID
	     || !secp->user
	     || secp->gid > MAXUID
	     || secp->size == 0
	     || secp->date <= 0
	) {
		freesecure (secp);
		errno = EBADF;
		return (0);
	}

	return (secp);
}
Пример #11
0
int
putsecure(char *file, SECURE *secbufp)
{
	char			*path;

	int fd;

	int			fld;

	if (*file == '/')
		path = Strdup(file);
	else
		path = makepath(Lp_Requests, file, (char *)0);
	if (!path)
		return (-1);

	if ((fd = open_locked(path, "w", MODE_NOREAD)) < 0) {
		Free (path);
		return (-1);
	}
	Free (path);

	if (
		!secbufp->req_id ||
		!secbufp->user
	)
		return (-1);

	for (fld = 0; fld < SC_MAX; fld++)

		switch (fld) {

		case SC_REQID:
			(void)fdprintf(fd, "%s\n", secbufp->req_id);
			break;

		case SC_UID:
			(void)fdprintf(fd, "%u\n", secbufp->uid);
			break;

		case SC_USER:
			(void)fdprintf(fd, "%s\n", secbufp->user);
			break;

		case SC_GID:
			(void)fdprintf(fd, "%u\n", secbufp->gid);
			break;

		case SC_SIZE:
			(void)fdprintf(fd, "%lu\n", secbufp->size);
			break;

		case SC_DATE:
			(void)fdprintf(fd, "%ld\n", secbufp->date);
			break;

		case SC_SLABEL:
			if (secbufp->slabel == NULL) {
				if (is_system_labeled()) {
					m_label_t *sl;

					sl = m_label_alloc(MAC_LABEL);
					(void) getplabel(sl);
					if (label_to_str(sl, &(secbufp->slabel),
					    M_INTERNAL, DEF_NAMES) != 0) {
						perror("label_to_str");
						secbufp->slabel =
						    strdup("bad_label");
					}
					m_label_free(sl);
					(void) fdprintf(fd, "%s\n",
					    secbufp->slabel);
				} else {
					(void) fdprintf(fd, "none\n");
				}
			} else {
				(void) fdprintf(fd, "%s\n", secbufp->slabel);
			}
			break;
		}
	close(fd);

	return (0);
}