1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /*LINTLIBRARY*/ 27 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 #include <libintl.h> 32 #include <pwd.h> 33 34 /* lpsched include files */ 35 #include "lp.h" 36 #include "requests.h" 37 #include "printers.h" 38 39 #include <papi_impl.h> 40 41 papi_status_t 42 job_attributes_to_lpsched_request(papi_service_t svc, REQUEST *r, 43 papi_attribute_t **attributes) 44 { 45 papi_status_t status = PAPI_OK; 46 papi_attribute_t *attr; 47 papi_attribute_t **unmapped = NULL; 48 papi_attribute_t *tmp[2]; 49 int i; 50 char *s; 51 52 char **options = NULL; 53 char **modes = NULL; 54 55 char pr_filter = 0; 56 char *pr_title = NULL; 57 int pr_width = -1; 58 int pr_indent = -1; 59 int numberUp = 0; 60 int orientation = 0; 61 int lower = 0; 62 int upper = 0; 63 char buf[256]; 64 void *iterator = NULL; 65 char *mapped_keys[] = { "copies", "document-format", "form", 66 "job-class", "job-hold-until", "job-host", "job-name", 67 "job-originating-user-name", "job-printer", 68 "job-sheets", "lp-charset", "lp-modes", "number-up", 69 "orienttation-requested", "page-ranges", "pr-filter", 70 "pr-indent", "pr-title", "pr-width", "job-priority", 71 "requesting-user-name", "job-originating-host-name", 72 NULL }; 73 74 if (attributes == NULL) 75 return (PAPI_BAD_ARGUMENT); 76 77 /* replace the current destination */ 78 papiAttributeListGetLPString(attributes, 79 "job-printer", &r->destination); 80 81 /* set the copies. We need at least 1 */ 82 i = r->copies; 83 papiAttributeListGetInteger(attributes, NULL, "copies", &i); 84 if (i <= 0) 85 i = 1; 86 r->copies = i; 87 88 /* 89 * set the priority. PAPI/IPP uses 1-100, lpsched use 0-39, so we 90 * have to convert it. 91 */ 92 if (papiAttributeListGetInteger(attributes, NULL, "job-priority", &i) 93 == PAPI_OK) { 94 if ((i < 1) || (i > 100)) 95 i = 50; 96 i = 40 - (i / 2.5); 97 r->priority = i; 98 } 99 if ((r->priority < 0) || (r->priority > 39)) 100 r->priority = 20; 101 102 /* set the requested form to print on */ 103 papiAttributeListGetLPString(attributes, "form", &r->form); 104 105 /* set the page range */ 106 memset(tmp, NULL, sizeof (tmp)); 107 tmp[0] = papiAttributeListFind(attributes, "page-ranges"); 108 if (tmp[0] != NULL) { 109 char buf[BUFSIZ]; 110 111 papiAttributeListToString(tmp, " ", buf, sizeof (buf)); 112 if ((s = strchr(buf, '=')) != NULL) 113 r->pages = (char *)strdup(++s); 114 } 115 116 /* 117 * set the document format, converting to old format names as 118 * as needed. 119 */ 120 s = NULL; 121 papiAttributeListGetString(attributes, NULL, "document-format", &s); 122 if (s != NULL) 123 r->input_type = strdup(mime_type_to_lp_type(s)); 124 125 126 /* 127 * If we don't have an owner, set one. 128 */ 129 if (r->user == NULL) { 130 uid_t uid = getuid(); 131 struct passwd *pw; 132 char *user = "intruder"; 133 char *host = NULL; 134 char buf[256]; 135 136 if ((pw = getpwuid(uid)) != NULL) 137 user = pw->pw_name; /* default to the process owner */ 138 139 papiAttributeListGetString(attributes, NULL, 140 "job-originating-host-name", &host); 141 papiAttributeListGetString(attributes, NULL, 142 "job-host", &host); 143 papiAttributeListGetString(attributes, NULL, 144 "job-originating-user-name", &user); 145 papiAttributeListGetString(attributes, NULL, 146 "requesting-user-name", &user); 147 148 snprintf(buf, sizeof (buf), "%s%s%s", user, 149 (host ? "@" : ""), (host ? host : "")); 150 user = buf; 151 152 r->user = strdup(user); 153 } 154 155 /* set any held state */ 156 s = NULL; 157 papiAttributeListGetString(attributes, NULL, "job-hold-until", &s); 158 if (s != NULL) { 159 r->actions &= ~(ACT_SPECIAL); /* strip immediate/hold/resume */ 160 if (strcmp(s, "resume") == 0) 161 r->actions |= ACT_RESUME; 162 else if ((strcmp(s, "immediate") == 0) || 163 (strcmp(s, "no-hold") == 0)) 164 r->actions |= ACT_IMMEDIATE; 165 else if ((strcmp(s, "indefinite") == 0) || 166 (strcmp(s, "hold") == 0)) 167 r->actions |= ACT_HOLD; 168 } 169 170 /* set lp charset/printwheel */ 171 papiAttributeListGetLPString(attributes, "lp-charset", &r->charset); 172 173 /* legacy pr(1) filter related garbage "lpr -p" */ 174 papiAttributeListGetBoolean(attributes, NULL, "pr-filter", &pr_filter); 175 papiAttributeListGetString(attributes, NULL, "pr-title", &pr_title); 176 papiAttributeListGetInteger(attributes, NULL, "pr-width", &pr_width); 177 papiAttributeListGetInteger(attributes, NULL, "pr-indent", &pr_indent); 178 179 if (pr_filter != 0) { 180 char buf[128]; 181 182 if (pr_title != NULL) { 183 snprintf(buf, sizeof (buf), "prtitle='%s'", pr_title); 184 appendlist(&modes, buf); 185 } 186 187 if (pr_width > 0) { 188 snprintf(buf, sizeof (buf), "prwidth=%d", pr_width); 189 appendlist(&modes, buf); 190 } 191 192 if (pr_indent > 0) { 193 snprintf(buf, sizeof (buf), "indent=%d", pr_indent); 194 appendlist(&modes, buf); 195 } 196 } else if ((pr_title != NULL) || (pr_width >= 0) || (pr_indent >= 0)) 197 detailed_error(svc, gettext( 198 "pr(1) filter options specified without enabling pr(1) filter")); 199 200 /* add burst page information */ 201 s = NULL; 202 papiAttributeListGetString(attributes, NULL, "job-sheets", &s); 203 if ((s != NULL) && (strcasecmp(s, "none") != 0)) { 204 char buf[128]; 205 char *class = NULL; 206 char *job_name = NULL; 207 208 papiAttributeListGetLPString(attributes, "job-class", &class); 209 papiAttributeListGetLPString(attributes, "job-name", &job_name); 210 211 /* burst page is enabled by default, add the title */ 212 snprintf(buf, sizeof (buf), "%s%s%s", 213 (job_name ? job_name : ""), 214 (job_name && class ? "\\n#####\\n#####\\t\\t " : ""), 215 (class ? class : "")); 216 if (buf[0] != '\0') { 217 if (r->title != NULL) 218 free(r->title); 219 r->title = strdup(buf); 220 } 221 } else if ((s != NULL) && (strcasecmp(s, "none") == 0)) { 222 /* burst page is disabled via lp "option" */ 223 appendlist(&options, "nobanner"); 224 } 225 226 /* Convert attribute "number-up" to mode group=n */ 227 papiAttributeListGetInteger(attributes, NULL, "number-up", &numberUp); 228 if ((numberUp >= 2) && ((numberUp % 2) == 0)) { 229 snprintf(buf, sizeof (buf), "group=%d", numberUp); 230 appendlist(&modes, buf); 231 } 232 233 /* 234 * Convert attribute "orientation-requested" to modes 235 * 'landscape', 'portrait', etc. 236 */ 237 papiAttributeListGetInteger(attributes, NULL, 238 "orientation-requested", &orientation); 239 if ((orientation >= 3) && (orientation <= 6)) { 240 switch (orientation) { 241 case 4: /* landscape */ 242 case 5: /* reverse-landscape, use landscape instead */ 243 appendlist(&modes, "landscape"); 244 break; 245 case 3: /* portrait */ 246 case 6: /* reverse-portrait, use portrait instead */ 247 default: 248 appendlist(&modes, "portrait"); 249 break; 250 } 251 } 252 253 /* add "lp -y" modes */ 254 attr = papiAttributeListFind(attributes, "lp-modes"); 255 if ((attr != NULL) && (attr->type == PAPI_STRING) && 256 (attr->values != NULL)) { 257 int i; 258 259 for (i = 0; attr->values[i] != NULL; i++) 260 appendlist(&modes, attr->values[i]->string); 261 } 262 263 if (modes != NULL) { 264 if (r->modes == NULL) 265 free(r->modes); 266 r->modes = sprintlist(modes); 267 freelist(modes); 268 } 269 270 /* add any unconsumed attributes to the "options" list */ 271 split_and_copy_attributes(mapped_keys, attributes, NULL, &unmapped); 272 if (unmapped != NULL) { /* convert them to lp options */ 273 char *buf = malloc(1024); 274 ssize_t size = 1024; 275 276 while (papiAttributeListToString(unmapped, " ", buf, size) 277 != PAPI_OK) { 278 size += 1024; 279 buf = realloc(buf, size); 280 } 281 appendlist(&options, buf); 282 free(buf); 283 papiAttributeListFree(unmapped); 284 } 285 286 if (options != NULL) { 287 if (r->options != NULL) 288 free(r->options); 289 r->options = sprintlist(options); 290 freelist(options); 291 } 292 293 return (PAPI_OK); 294 } 295 296 /* 297 * Convert REQUEST->outcome (or R_REQUEST_* state) to the equivalent 298 * PAPI attribute representation. 299 */ 300 static void 301 lpsched_request_outcome_to_attributes(papi_attribute_t ***attributes, 302 unsigned short state) 303 { 304 if (attributes == NULL) 305 return; 306 307 if (state & (RS_HELD|RS_ADMINHELD)) { 308 papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 309 "job-state", 0x04); /* held */ 310 papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE, 311 "job-state-reasons", "job-hold-until-specified"); 312 } else if (state & RS_ACTIVE) { 313 papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 314 "job-state", 0x05); 315 if (state & RS_FILTERING) 316 papiAttributeListAddString(attributes, 317 PAPI_ATTR_REPLACE, 318 "job-state-reasons", "job-transforming"); 319 else if (state & RS_PRINTING) 320 papiAttributeListAddString(attributes, 321 PAPI_ATTR_REPLACE, 322 "job-state-reasons", "job-printing"); 323 else 324 papiAttributeListAddString(attributes, 325 PAPI_ATTR_REPLACE, 326 "job-state-reasons", "job-processing"); 327 } else if (state & RS_CANCELLED) { 328 papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 329 "job-state", 0x07); 330 papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE, 331 "job-state-reasons", "job-canceled-by-user"); 332 } else if (state & RS_PRINTED) { 333 papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 334 "job-state", 0x09); 335 papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE, 336 "job-state-reasons", "job-complete"); 337 } else { 338 papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE, 339 "job-state", 0x03); 340 papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE, 341 "job-state-reasons", "job-queued"); 342 } 343 papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE, 344 "job-hold-until", 345 ((state & RS_HELD) ? "indefinite" : "no-hold")); 346 } 347 348 /* 349 * convert user[@host] to papi attributes 350 */ 351 static void 352 lpsched_user_to_job_attributes(papi_attribute_t ***list, char *user) 353 { 354 if ((list != NULL) && (user != NULL) && (user[0] != NULL)) { 355 char *host = strrchr(user, '@'); 356 357 if (host != NULL) { 358 *host = NULL; 359 papiAttributeListAddString(list, PAPI_ATTR_REPLACE, 360 "job-originating-user-name", user); 361 papiAttributeListAddString(list, PAPI_ATTR_REPLACE, 362 "job-originating-host-name", host + 1); 363 *host = '@'; 364 } else 365 papiAttributeListAddString(list, PAPI_ATTR_REPLACE, 366 "job-originating-user-name", user); 367 } 368 } 369 370 /* 371 * Convert REQUEST structure to the equivalent PAPI attribute representation. 372 */ 373 void 374 lpsched_request_to_job_attributes(REQUEST *r, job_t *j) 375 { 376 char *tmp; 377 int i; 378 379 /* copies */ 380 papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE, 381 "copies", r->copies); 382 383 /* destination */ 384 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 385 "printer-name", r->destination); 386 387 /* form */ 388 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 389 "form", r->form); 390 391 /* options */ 392 papiAttributeListFromString(&j->attributes, PAPI_ATTR_APPEND, 393 r->options); 394 395 tmp = (((r->options != NULL) && (strstr(r->options, "nobanner") 396 != NULL)) ? "none" : "standard"); 397 papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE, 398 "job-sheets", tmp); 399 400 tmp = (((r->options != NULL) && (strstr(r->options, "duplex") 401 != NULL)) ? "two-sized" : "one-sided"); 402 papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE, 403 "sides", tmp); 404 405 i = (((r->options != NULL) && (strstr(r->options, "landscape") 406 != NULL)) ? 4 : 3); 407 papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE, 408 "orientation-requested", i); 409 410 /* priority (map 0-39 to 1-100) */ 411 papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE, 412 "job-priority", 413 (int)(100 - (r->priority * 2.5))); 414 415 /* pages */ 416 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 417 "page-ranges", r->pages); 418 419 /* charset */ 420 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 421 "lp-charset", r->charset); 422 423 /* modes */ 424 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 425 "lp-modes", r->modes); 426 427 /* title */ 428 papiAttributeListAddLPString(&j->attributes, PAPI_ATTR_REPLACE, 429 "job-name", r->title); 430 431 /* input_type */ 432 433 /* user */ 434 lpsched_user_to_job_attributes(&j->attributes, r->user); 435 436 /* outcome */ 437 lpsched_request_outcome_to_attributes(&j->attributes, r->outcome); 438 439 /* constants, (should be derived from options) */ 440 papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE, 441 "number-up", 1); 442 443 papiAttributeListAddString(&j->attributes, PAPI_ATTR_REPLACE, 444 "multiple-document-handling", 445 "seperate-documents-collated-copies"); 446 } 447 448 /* 449 * Convert R_REQUEST_* results to the equivalent PAPI attribute representation. 450 */ 451 void 452 job_status_to_attributes(job_t *job, char *req_id, char *user, char *slabel, 453 size_t size, time_t date, short state, char *destination, 454 char *form, char *charset, short rank, char *file) 455 { 456 char buf[BUFSIZ]; 457 char *p; 458 459 lpsched_user_to_job_attributes(&job->attributes, user); 460 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 461 "job-k-octets", size/1024); 462 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 463 "job-octets", size); 464 if ((p = strrchr(req_id, '-')) != NULL) { 465 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 466 "job-id", atoi(++p)); 467 } 468 snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s/%d", 469 destination, atoi(p)); 470 papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE, 471 "job-uri", buf); 472 snprintf(buf, sizeof (buf), "lpsched://localhost/printers/%s", 473 destination); 474 papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE, 475 "job-printer-uri", buf); 476 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 477 "job-printer-up-time", time(NULL)); 478 papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE, 479 "output-device-assigned", destination); 480 papiAttributeListAddString(&job->attributes, PAPI_ATTR_REPLACE, 481 "printer-name", destination); 482 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE, 483 "form", form); 484 485 lpsched_request_outcome_to_attributes(&job->attributes, state); 486 487 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 488 "time-at-creation", date); 489 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE, 490 "lpsched-request-id", req_id); 491 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE, 492 "lp-charset", charset); 493 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 494 "lpsched-job-state", state); 495 papiAttributeListAddInteger(&job->attributes, PAPI_ATTR_REPLACE, 496 "number-of-intervening-jobs", rank - 1); 497 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_REPLACE, 498 "lpsched-file", file); 499 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL, 500 "job-name", file); 501 papiAttributeListAddLPString(&job->attributes, PAPI_ATTR_EXCL, 502 "tsol-sensitivity-label", slabel); 503 } 504 505 void 506 lpsched_read_job_configuration(service_t *svc, job_t *j, char *file) 507 { 508 REQUEST *r; 509 510 if ((r = getrequest(file)) == NULL) { 511 detailed_error(svc, gettext("unable to read job data: %s"), 512 file); 513 return; 514 } 515 516 lpsched_request_to_job_attributes(r, j); 517 } 518