#include "lpsched.h"
#include "validate.h"
#include <syslog.h>
#include <errno.h>
#include <deflt.h>
#include <tsol/label.h>
#include <auth_list.h>
#define register auto
int pickfilter ( RSTATUS * , CANDIDATE * , FSTATUS * );
unsigned long chkprinter_result = 0;
char * o_cpi = 0;
char * o_lpi = 0;
char * o_width = 0;
char * o_length = 0;
static int wants_nobanner = 0;
static int wants_nolabels = 0;
static int lp_or_root = 0;
static int _chkopts ( RSTATUS *, CANDIDATE * , FSTATUS * );
static void free_candidate ( CANDIDATE * );
static int tsol_check_printer_label_range(char *, const char *);
static int tsol_lpauth(char *, char *);
static int secpolicy_chkpolicy(char *policyp);
short
_validate(RSTATUS *prs, PSTATUS *pps, PSTATUS *stop_pps, char **prefixp,
int moving)
{
register CANDIDATE *pc = 0,
*pcend,
*best_pc = 0;
register FSTATUS *pfs = 0;
register CLSTATUS *pcs = 0;
CANDIDATE *arena = 0,
single;
size_t n;
int i;
short ret;
chkprinter_result = 0;
o_cpi = o_lpi = o_width = o_length = 0;
wants_nobanner = 0;
memset (&single, 0, sizeof(single));
wants_nolabels = 0;
if ((is_system_labeled() &&
strcmp(prs->request->input_type, "postscript") == 0) &&
(secpolicy_chkpolicy("PRINT_POSTSCRIPT=") == 0)) {
if (tsol_lpauth(PRINT_POSTSCRIPT_AUTH, prs->secure->user)
== 0) {
ret = MDENYDEST;
goto Return;
}
}
lp_or_root = 0;
if (bangequ(prs->secure->user, "root") ||
bangequ(prs->secure->user, "lp"))
lp_or_root = 1;
if (prefixp)
*prefixp = prs->request->destination;
if (!pps && prs->request->destination &&
!STREQU(prs->request->destination, NAME_ANY))
if (((pps = search_pstatus(prs->request->destination)) != NULL) ||
((pcs = search_cstatus(prs->request->destination)) != NULL) &&
pcs->class->members)
;
else {
ret = MNODEST;
goto Return;
}
if (pps && pps == stop_pps) {
ret = MERRDEST;
goto Return;
}
if (prs && prs->request && prs->request->form && (pps || pcs)) {
if ((pfs = search_fstatus(prs->request->form))) {
if (lp_or_root || allowed(prs->secure->user,
pfs->users_allowed, pfs->users_denied))
;
else {
ret = MDENYMEDIA;
goto Return;
}
} else {
ret = MNOMEDIA;
goto Return;
}
}
{
register char **list,
**pl;
if (
prs->request->options
&& (list = dashos(prs->request->options))
) {
for (pl = list ; *pl; pl++)
if (STRNEQU(*pl, "cpi=", 4))
o_cpi = Strdup(*pl + 4);
else if (STRNEQU(*pl, "lpi=", 4))
o_lpi = Strdup(*pl + 4);
else if (STRNEQU(*pl, "width=", 6))
o_width = Strdup(*pl + 6);
else if (STRNEQU(*pl, "length=", 7))
o_length = Strdup(*pl + 7);
else if (STREQU(*pl, "nobanner"))
wants_nobanner = 1;
else if (STREQU(*pl, "nolabels"))
wants_nolabels = 1;
freelist (list);
}
}
#define CHKMAND(PFS) \
( \
(PFS) \
&& (PFS)->form->chset \
&& !STREQU((PFS)->form->chset, NAME_ANY) \
&& (PFS)->form->mandatory \
)
#define CHKU(PRS,PPS) \
( \
lp_or_root \
|| allowed( \
(PRS)->secure->user, \
(PPS)->users_allowed, \
(PPS)->users_denied \
) \
)
#define CHKF(PFS,PPS) \
( \
isFormMountedOnPrinter(PPS,PFS) \
|| allowed( \
(PFS)->form->name, \
(PPS)->forms_allowed, \
(PPS)->forms_denied \
) \
)
#define CHKPW(PW,PPS) \
( \
!(PPS)->printer->daisy \
|| ( \
(PPS)->pwheel_name \
&& STREQU((PPS)->pwheel_name, (PW)) \
) \
|| searchlist((PW), (PPS)->printer->char_sets) \
)
#define CHKOPTS(PRS,PC,PFS) _chkopts((PRS),(PC),(PFS))
#define CHKACCEPT(PRS,PPS) \
( \
!((PPS)->status & PS_REJECTED) \
|| (PRS)->printer == (PPS) \
|| moving \
)
if (prs->request->charset)
if (
!CHKMAND(pfs)
|| STREQU(prs->request->charset, pfs->form->chset)
)
;
else {
ret = MDENYMEDIA;
chkprinter_result |= PCK_CHARSET;
goto Return;
}
if (pps) {
(pc = &single)->pps = pps;
if (!CHKU(prs, pps)) {
ret = MDENYDEST;
goto Return;
}
if (is_system_labeled() && prs->secure->slabel != NULL) {
if (tsol_check_printer_label_range(
prs->secure->slabel,
pps->printer->name) == 0) {
ret = MDENYDEST;
goto Return;
}
}
if (pfs && !CHKF(pfs, pps)) {
ret = MNOMOUNT;
goto Return;
}
if (
prs->request->charset
&& !CHKPW(prs->request->charset, pps)
) {
ret = MNOMOUNT;
goto Return;
}
if (!CHKOPTS(prs, pc, pfs)) {
ret = MDENYDEST;
goto Return;
}
if (!CHKACCEPT(prs, pps)) {
ret = MERRDEST;
goto Return;
}
if (!pickfilter(prs, pc, pfs)) {
ret = MNOFILTER;
goto Return;
}
best_pc = pc;
ret = MOK;
goto Return;
}
if (
pcs
&& (pcs->status & CS_REJECTED)
&& !moving
&& !prs->printer
) {
ret = MERRDEST;
goto Return;
}
if (pcs)
n = lenlist(pcs->class->members);
else {
for (n = 0; PStatus != NULL && PStatus[n] != NULL; n++) ;
}
pcend = arena = (CANDIDATE *)Calloc(n, sizeof(CANDIDATE));
if (pcs) {
register char **pn;
for (pn = pcs->class->members; *pn; pn++)
if (
((pps = search_pstatus(*pn)) != NULL)
&& pps != stop_pps
)
(pcend++)->pps = pps;
} else
for (i = 0; PStatus != NULL && PStatus[i] != NULL; i++) {
pps = PStatus[i];
if (CHKACCEPT(prs, pps) && pps != stop_pps)
(pcend++)->pps = pps;
}
if (pcend == arena) {
ret = MERRDEST;
goto Return;
}
{
register CANDIDATE *pcend2;
for (pcend2 = pc = arena; pc < pcend; pc++) {
if (CHKU(prs, pc->pps) && CHKOPTS(prs, pc, pfs))
*pcend2++ = *pc;
else
free_candidate (pc);
}
if (pcend2 == arena) {
ret = MDENYDEST;
goto Return;
}
pcend = pcend2;
}
if (pfs) {
register CANDIDATE *pcend2;
for (pcend2 = pc = arena; pc < pcend; pc++)
if (CHKF(pfs, pc->pps))
*pcend2++ = *pc;
else
free_candidate (pc);
if (pcend2 == arena) {
ret = MNOMOUNT;
goto Return;
}
pcend = pcend2;
}
if (prs->request->charset) {
register CANDIDATE *pcend2;
for (pcend2 = pc = arena; pc < pcend; pc++)
if (CHKPW(prs->request->charset, pc->pps))
*pcend2++ = *pc;
else
free_candidate (pc);
if (pcend2 == arena) {
ret = MNOMOUNT;
goto Return;
}
pcend = pcend2;
}
#define CHKFMNT(PFS,PPS) (isFormUsableOnPrinter(PPS,PFS))
#define CHKPWMNT(PRS,PPS) SAME((PPS)->pwheel_name, (PRS)->request->charset)
#define CHKCHSET(PRS,PPS) \
( \
!(PRS)->request->charset \
|| !(PPS)->printer->daisy \
)
#define CHKENB(PPS) (!((PPS)->status & (PS_DISABLED|PS_FAULTED)))
#define CHKFREE(PPS) (!((PPS)->status & (PS_BUSY|PS_LATER)))
{
register CANDIDATE *pcend2;
for (pcend2 = pc = arena; pc < pcend; pc++)
if (pickfilter(prs, pc, pfs)) {
pc->weight = 0;
if (!pc->fast && !pc->slow)
pc->weight += WEIGHT_NOFILTER;
if (CHKFREE(pc->pps))
pc->weight += WEIGHT_FREE;
if (CHKENB(pc->pps))
pc->weight += WEIGHT_ENABLED;
if (CHKFMNT(pfs, pc->pps))
pc->weight += WEIGHT_MOUNTED;
if (CHKPWMNT(prs, pc->pps))
pc->weight += WEIGHT_MOUNTED;
if (CHKCHSET(prs, pc->pps))
pc->weight += WEIGHT_SELECTS;
#if defined(FILTER_EARLY_OUT)
if (pc->weight == WEIGHT_MAX) {
best_pc = pc;
ret = MOK;
goto Return;
}
#endif
*pcend2++ = *pc;
} else
free_candidate (pc);
if (pcend2 == arena) {
ret = MNOFILTER;
goto Return;
}
pcend = pcend2;
}
if (pcend - arena == 1) {
best_pc = arena;
ret = MOK;
goto Return;
}
{
register CANDIDATE *pcend2 = pcend;
if (is_system_labeled()) {
for (pcend2 = pc = arena; pc < pcend; pc++) {
if (tsol_check_printer_label_range(
prs->secure->slabel,
pps->printer->name) == 1)
*pcend2++ = *pc;
else
free_candidate(pc);
}
}
if (pcend2 == arena) {
ret = MDENYDEST;
goto Return;
}
pcend = pcend2;
}
#if defined(OTHER_FACTORS)
;
#endif
best_pc = arena;
for (pc = arena + 1; pc < pcend; pc++)
if (pc->weight > best_pc->weight)
best_pc = pc;
ret = MOK;
Return: if (ret == MOK) {
register USER *pu = Getuser(prs->secure->user);
register char *pwheel_name;
PSTATUS *oldpps = prs->printer;
prs->printer = best_pc->pps;
load_str (&(prs->printer_type), best_pc->printer_type);
if (prs->form != pfs) {
unqueue_form (prs);
queue_form (prs, pfs);
}
prs->status &= ~RSS_PWMAND;
if (CHKMAND(pfs))
pwheel_name = pfs->form->chset;
else
if ((pwheel_name = prs->request->charset) != NULL)
prs->status |= RSS_PWMAND;
if (!SAME(pwheel_name, prs->pwheel_name)) {
unqueue_pwheel (prs);
queue_pwheel (prs, pwheel_name);
}
if (prs->request->priority < 0 || 39 < prs->request->priority)
prs->request->priority = getdfltpri();
if (pu && prs->request->priority < pu->priority_limit)
prs->request->priority = pu->priority_limit;
if (
(best_pc->fast || best_pc->slow)
&& (best_pc->flags & FPARM_COPIES)
&& prs->request->copies > 1
)
prs->copies = 1;
else
prs->copies = prs->request->copies;
if (best_pc->slow) {
if (!SAME(best_pc->slow, prs->slow)) {
if (prs->request->outcome & RS_FILTERED)
prs->request->outcome &= ~RS_FILTERED;
if (
prs->request->outcome & RS_FILTERING
&& !(prs->request->outcome & RS_STOPPED)
) {
prs->request->outcome |= RS_REFILTER;
prs->request->outcome |= RS_STOPPED;
terminate (prs->exec);
} else if (
prs->request->outcome & RS_PRINTING
&& !(prs->request->outcome & RS_STOPPED)
) {
prs->request->outcome |= RS_STOPPED;
terminate (oldpps->exec);
}
}
load_str (&(prs->slow), best_pc->slow);
load_str (&(prs->output_type), best_pc->output_type);
} else
unload_str (&(prs->slow));
load_str (&(prs->fast), best_pc->fast);
if (prs->request->actions & ACT_FAST && prs->slow) {
if (prs->fast) {
prs->fast = makestr(
prs->slow,
"|",
prs->fast,
(char *)0
);
Free (prs->slow);
} else
prs->fast = prs->slow;
prs->slow = 0;
}
}
if (arena) {
for (pc = arena; pc < pcend; pc++)
free_candidate (pc);
Free ((char *)arena);
} else if (best_pc)
free_candidate (best_pc);
if (o_length)
Free (o_length);
if (o_width)
Free (o_width);
if (o_lpi)
Free (o_lpi);
if (o_cpi)
Free (o_cpi);
prs->reason = ret;
return (ret);
}
static int
_chkopts(RSTATUS *prs, CANDIDATE *pc, FSTATUS *pfs)
{
unsigned long ret = 0;
unsigned long chk = 0;
char * charset;
char * cpi = 0;
char * lpi = 0;
char * width = 0;
char * length = 0;
char * paper = NULL;
char ** pt;
int nobanner_not_allowed = 0;
if (pfs) {
cpi = pfs->cpi;
lpi = pfs->lpi;
width = pfs->pwid;
length = pfs->plen;
paper = pfs->form->paper;
} else {
cpi = o_cpi;
lpi = o_lpi;
width = o_width;
length = o_length;
}
if (pc->pps->printer->daisy ||
search_cslist(prs->request->charset, pc->pps->printer->char_sets))
charset = 0;
else
charset = prs->request->charset;
pc->printer_types = 0;
for (pt = pc->pps->printer->printer_types; *pt; pt++) {
unsigned long this;
if (paper) {
if (allowed(paper,pc->pps->paper_allowed,NULL)) {
addlist (&(pc->printer_types), *pt);
} else {
ret |= PCK_PAPER;
}
} else {
this = chkprinter(*pt, cpi, lpi, length, width,
charset);
if (this == 0)
addlist(&(pc->printer_types), *pt);
chk |= this;
}
}
if (!pc->printer_types)
ret |= chk;
if (is_system_labeled() && (wants_nolabels == 1)) {
if (!tsol_lpauth(PRINT_UNLABELED_AUTH, prs->secure->user)) {
register char **list;
if (prs->request->options &&
(list = dashos(prs->request->options))) {
dellist(&list, "nolabels");
free(prs->request->options);
prs->request->options = sprintlist(list);
}
}
}
if (pc->pps->printer->banner == BAN_ALWAYS) {
char **list;
if (is_system_labeled()) {
if (wants_nobanner == 1) {
if (tsol_lpauth(PRINT_NOBANNER_AUTH,
prs->secure->user) == 0) {
nobanner_not_allowed = 1;
}
}
}
else if ((wants_nobanner == 1) && (lp_or_root != 1)) {
nobanner_not_allowed = 1;
}
if (nobanner_not_allowed == 1) {
if (prs->request->options &&
(list = dashos(prs->request->options))) {
dellist(&list, "nobanner");
free(prs->request->options);
prs->request->options = sprintlist(list);
}
}
} else if (pc->pps->printer->banner == BAN_NEVER) {
if (wants_nobanner == 0) {
char **list = NULL;
if (prs->request->options) {
list = dashos(prs->request->options);
free(prs->request->options);
}
appendlist(&list, "nobanner");
prs->request->options = sprintlist(list);
}
} else {
}
chkprinter_result |= ret;
return (ret == 0);
}
static void
free_candidate(CANDIDATE *pc)
{
if (pc->slow)
unload_str (&(pc->slow));
if (pc->fast)
unload_str (&(pc->fast));
if (pc->printer_types) {
freelist (pc->printer_types);
pc->printer_types = 0;
}
if (pc->printer_type)
unload_str (&(pc->printer_type));
if (pc->output_type)
unload_str (&(pc->output_type));
return;
}
static int
tsol_check_printer_label_range(char *slabel, const char *printer)
{
int in_range = 0;
int err = 0;
m_range_t *range;
m_label_t *sl = NULL;
if (slabel == NULL)
return (0);
if ((err =
(str_to_label(slabel, &sl, USER_CLEAR, L_NO_CORRECTION, &in_range)))
== -1) {
return (0);
}
if ((range = getdevicerange(printer)) == NULL) {
m_label_free(sl);
return (0);
}
in_range = blinrange(sl, range);
m_label_free(sl);
m_label_free(range->lower_bound);
m_label_free(range->upper_bound);
free(range);
return (in_range);
}
static int
tsol_lpauth(char *auth, char *in_name)
{
char *cp;
int res;
if ((cp = strchr(in_name, '@')) != NULL) {
*cp = '\0';
res = chkauthattr(auth, in_name);
*cp = '@';
} else if ((cp = strchr(in_name, '!')) != NULL)
res = chkauthattr(auth, cp+1);
else
res = chkauthattr(auth, in_name);
return (res);
}
#define POLICY_FILE "/etc/default/print"
int
secpolicy_chkpolicy(char *policyp)
{
char *option;
int opt_val;
if (policyp == NULL)
return (0);
opt_val = 0;
if (defopen(POLICY_FILE) == 0) {
defcntl(DC_SETFLAGS, DC_STD & ~DC_CASE);
if ((option = defread(policyp)) != NULL)
opt_val = atoi(option);
}
(void) defopen((char *)NULL);
syslog(LOG_DEBUG, "--- Policy %s, opt_val==%d",
policyp ? policyp : "NULL", opt_val);
return (opt_val);
}