12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
239116SNagaraj.Yedathore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs 
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
332264Sjacobs #include <string.h>
342264Sjacobs #include <locale.h>
352264Sjacobs #include <libintl.h>
362264Sjacobs #include <papi.h>
372264Sjacobs #include "common.h"
382264Sjacobs 
392264Sjacobs static void
402264Sjacobs usage(char *program)
412264Sjacobs {
422264Sjacobs 	char *name;
432264Sjacobs 
442264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
452264Sjacobs 		name = program;
462264Sjacobs 	else
472264Sjacobs 		name++;
482264Sjacobs 
492264Sjacobs 	fprintf(stdout,
509116SNagaraj.Yedathore@Sun.COM 	    gettext("Usage: %s [request-id] (destination)\n"
519116SNagaraj.Yedathore@Sun.COM 	    "       %s (source) (destination)\n"), name, name);
522264Sjacobs 	exit(1);
532264Sjacobs }
542264Sjacobs 
552264Sjacobs static int
562264Sjacobs move_job(papi_service_t svc, char *src, int32_t id, char *dest)
572264Sjacobs {
582264Sjacobs 	int result = 0;
592264Sjacobs 	papi_status_t status;
602264Sjacobs 	char *mesg = gettext("moved");
612264Sjacobs 
622264Sjacobs 	status = papiJobMove(svc, src, id, dest);
632264Sjacobs 	if (status != PAPI_OK) {
642264Sjacobs 		mesg = (char *)verbose_papi_message(svc, status);
652264Sjacobs 		result = -1;
662264Sjacobs 	}
672264Sjacobs 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
682264Sjacobs 
692264Sjacobs 	return (result);
702264Sjacobs }
712264Sjacobs 
722264Sjacobs int
732264Sjacobs main(int ac, char *av[])
742264Sjacobs {
752264Sjacobs 	int exit_code = 0;
762264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
772264Sjacobs 	char *destination = NULL;
782264Sjacobs 	int c;
792264Sjacobs 
802264Sjacobs 	(void) setlocale(LC_ALL, "");
812264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
822264Sjacobs 
832264Sjacobs 	while ((c = getopt(ac, av, "E:")) != EOF)
842264Sjacobs 		switch (c) {
852264Sjacobs 		case 'E':
862264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
872264Sjacobs 			break;
882264Sjacobs 		default:
892264Sjacobs 			usage(av[0]);
902264Sjacobs 		}
912264Sjacobs 
922264Sjacobs 	if (optind >= ac - 1)
932264Sjacobs 		usage(av[0]);
942264Sjacobs 
952264Sjacobs 	destination = av[--ac];
962264Sjacobs 
972264Sjacobs 	for (c = optind; c < ac; c++) {
982264Sjacobs 		papi_status_t status;
992264Sjacobs 		papi_service_t svc = NULL;
1002264Sjacobs 		papi_job_t *jobs = NULL;
1012264Sjacobs 		char *printer = NULL;
1022264Sjacobs 		int32_t id = -1;
1032264Sjacobs 
1042264Sjacobs 		(void) get_printer_id(av[c], &printer, &id);
1052264Sjacobs 
1062264Sjacobs 		status = papiServiceCreate(&svc, printer, NULL, NULL,
1079116SNagaraj.Yedathore@Sun.COM 		    cli_auth_callback, encryption, NULL);
1082264Sjacobs 		if (status != PAPI_OK) {
1092264Sjacobs 			fprintf(stderr, gettext(
1109116SNagaraj.Yedathore@Sun.COM 			    "Failed to contact service for %s: %s\n"),
1119116SNagaraj.Yedathore@Sun.COM 			    printer, verbose_papi_message(svc, status));
1122264Sjacobs 			exit(1);
1132264Sjacobs 		}
1142264Sjacobs 
1152264Sjacobs 		if (id != -1) {	/* it's a job */
1162264Sjacobs 			if (move_job(svc, printer, id, destination) < 0)
1172264Sjacobs 				exit_code = 1;
1182264Sjacobs 		} else {	/* it's a printer */
1192264Sjacobs 			char message[128];
1202264Sjacobs 			int count = 0;
1212264Sjacobs 
1222264Sjacobs 			snprintf(message, sizeof (message), "moved jobs to %s",
1239116SNagaraj.Yedathore@Sun.COM 			    destination);
124*9282SKeerthi.Kondaka@Sun.COM 			status = papiPrinterPause(svc, printer, message);
1252264Sjacobs 			if (status != PAPI_OK) {
1269118SSonam.Gupta@Sun.COM 				/*
1279118SSonam.Gupta@Sun.COM 				 * If the user is denied the permission
1289118SSonam.Gupta@Sun.COM 				 * to disable then return appropriate msg
1299118SSonam.Gupta@Sun.COM 				 */
1309118SSonam.Gupta@Sun.COM 				char *result = NULL;
1319118SSonam.Gupta@Sun.COM 
1329118SSonam.Gupta@Sun.COM 				result = papiServiceGetStatusMessage(svc);
1339118SSonam.Gupta@Sun.COM 
1349118SSonam.Gupta@Sun.COM 				if (result != NULL) {
1359118SSonam.Gupta@Sun.COM 					/*
1369118SSonam.Gupta@Sun.COM 					 * Check if user is denied
1379118SSonam.Gupta@Sun.COM 					 * the permission
1389118SSonam.Gupta@Sun.COM 					 */
1399118SSonam.Gupta@Sun.COM 					if (strstr(result, "permission denied")
1409118SSonam.Gupta@Sun.COM 					    != NULL) {
1419118SSonam.Gupta@Sun.COM 						/*
1429118SSonam.Gupta@Sun.COM 						 * user is denied
1439118SSonam.Gupta@Sun.COM 						 * permission
1449118SSonam.Gupta@Sun.COM 						 */
1459118SSonam.Gupta@Sun.COM 						fprintf(stderr, gettext(
1469118SSonam.Gupta@Sun.COM 						    "UX:lpmove: ERROR:"\
1479118SSonam.Gupta@Sun.COM 						    " You aren't allowed"\
1489118SSonam.Gupta@Sun.COM 						    " to do that.\n\t"\
1499118SSonam.Gupta@Sun.COM 						    "  TO FIX: You must"\
1509118SSonam.Gupta@Sun.COM 						    " be logged in as"\
1519118SSonam.Gupta@Sun.COM 						    " \"lp\" or \"root\".\n"));
1529118SSonam.Gupta@Sun.COM 						exit_code = 1;
1539118SSonam.Gupta@Sun.COM 					} else {
1549118SSonam.Gupta@Sun.COM 						fprintf(stderr, gettext(
155*9282SKeerthi.Kondaka@Sun.COM 						    "Reject %s: %s\n"),
1569118SSonam.Gupta@Sun.COM 						    printer,
1579118SSonam.Gupta@Sun.COM 						    verbose_papi_message(
1589118SSonam.Gupta@Sun.COM 						    svc, status));
1599118SSonam.Gupta@Sun.COM 						exit_code = 1;
1609118SSonam.Gupta@Sun.COM 					}
1619118SSonam.Gupta@Sun.COM 				} else {
1629118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
163*9282SKeerthi.Kondaka@Sun.COM 					    "Reject %s: %s\n"),
1649118SSonam.Gupta@Sun.COM 					    printer,
1659118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
1669118SSonam.Gupta@Sun.COM 					exit_code = 1;
1679118SSonam.Gupta@Sun.COM 				}
1689118SSonam.Gupta@Sun.COM 			} else {
1692264Sjacobs 				printf(gettext(
1709118SSonam.Gupta@Sun.COM 				    "destination %s is not accepting"\
1719118SSonam.Gupta@Sun.COM 				    " requests\n"), printer);
1729118SSonam.Gupta@Sun.COM 
1739118SSonam.Gupta@Sun.COM 				status = papiPrinterListJobs(svc, printer, NULL,
1749118SSonam.Gupta@Sun.COM 				    0, 0, &jobs);
1759118SSonam.Gupta@Sun.COM 				if (status != PAPI_OK) {
1769118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext("Jobs %s:"\
1779118SSonam.Gupta@Sun.COM 					    " %s\n"),
1789118SSonam.Gupta@Sun.COM 					    printer,
1799118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
1809118SSonam.Gupta@Sun.COM 					exit_code = 1;
1819118SSonam.Gupta@Sun.COM 				}
1822264Sjacobs 
1839118SSonam.Gupta@Sun.COM 				printf(gettext("move in progress ...\n"));
1849118SSonam.Gupta@Sun.COM 				while ((jobs != NULL) && (*jobs != NULL)) {
1859118SSonam.Gupta@Sun.COM 					id = papiJobGetId(*jobs++);
1869118SSonam.Gupta@Sun.COM 					if (move_job(svc, printer,
1879118SSonam.Gupta@Sun.COM 					    id, destination) < 0)
1889118SSonam.Gupta@Sun.COM 						exit_code = 1;
1899118SSonam.Gupta@Sun.COM 					else
1909118SSonam.Gupta@Sun.COM 						count++;
1919118SSonam.Gupta@Sun.COM 				}
1929118SSonam.Gupta@Sun.COM 				printf(gettext(
1939118SSonam.Gupta@Sun.COM 				    "total of %d requests moved"\
1949118SSonam.Gupta@Sun.COM 				    " from %s to %s\n"),
1959118SSonam.Gupta@Sun.COM 				    count, printer, destination);
1969118SSonam.Gupta@Sun.COM 
1979118SSonam.Gupta@Sun.COM 				papiJobListFree(jobs);
1982264Sjacobs 			}
1992264Sjacobs 		}
2002264Sjacobs 
2012264Sjacobs 		papiServiceDestroy(svc);
2022264Sjacobs 	}
2032264Sjacobs 
2042264Sjacobs 	return (exit_code);
2052264Sjacobs }
206