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);
1242264Sjacobs 			status = papiPrinterDisable(svc, printer, message);
1252264Sjacobs 			if (status != PAPI_OK) {
126*9118SSonam.Gupta@Sun.COM 				/*
127*9118SSonam.Gupta@Sun.COM 				 * If the user is denied the permission
128*9118SSonam.Gupta@Sun.COM 				 * to disable then return appropriate msg
129*9118SSonam.Gupta@Sun.COM 				 */
130*9118SSonam.Gupta@Sun.COM 				char *result = NULL;
131*9118SSonam.Gupta@Sun.COM 
132*9118SSonam.Gupta@Sun.COM 				result = papiServiceGetStatusMessage(svc);
133*9118SSonam.Gupta@Sun.COM 
134*9118SSonam.Gupta@Sun.COM 				if (result != NULL) {
135*9118SSonam.Gupta@Sun.COM 					/*
136*9118SSonam.Gupta@Sun.COM 					 * Check if user is denied
137*9118SSonam.Gupta@Sun.COM 					 * the permission
138*9118SSonam.Gupta@Sun.COM 					 */
139*9118SSonam.Gupta@Sun.COM 					if (strstr(result, "permission denied")
140*9118SSonam.Gupta@Sun.COM 					    != NULL) {
141*9118SSonam.Gupta@Sun.COM 						/*
142*9118SSonam.Gupta@Sun.COM 						 * user is denied
143*9118SSonam.Gupta@Sun.COM 						 * permission
144*9118SSonam.Gupta@Sun.COM 						 */
145*9118SSonam.Gupta@Sun.COM 						fprintf(stderr, gettext(
146*9118SSonam.Gupta@Sun.COM 						    "UX:lpmove: ERROR:"\
147*9118SSonam.Gupta@Sun.COM 						    " You aren't allowed"\
148*9118SSonam.Gupta@Sun.COM 						    " to do that.\n\t"\
149*9118SSonam.Gupta@Sun.COM 						    "  TO FIX: You must"\
150*9118SSonam.Gupta@Sun.COM 						    " be logged in as"\
151*9118SSonam.Gupta@Sun.COM 						    " \"lp\" or \"root\".\n"));
152*9118SSonam.Gupta@Sun.COM 						exit_code = 1;
153*9118SSonam.Gupta@Sun.COM 					} else {
154*9118SSonam.Gupta@Sun.COM 						fprintf(stderr, gettext(
155*9118SSonam.Gupta@Sun.COM 						    "Disable %s: %s\n"),
156*9118SSonam.Gupta@Sun.COM 						    printer,
157*9118SSonam.Gupta@Sun.COM 						    verbose_papi_message(
158*9118SSonam.Gupta@Sun.COM 						    svc, status));
159*9118SSonam.Gupta@Sun.COM 						exit_code = 1;
160*9118SSonam.Gupta@Sun.COM 					}
161*9118SSonam.Gupta@Sun.COM 				} else {
162*9118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext(
163*9118SSonam.Gupta@Sun.COM 					    "Disable %s: %s\n"),
164*9118SSonam.Gupta@Sun.COM 					    printer,
165*9118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
166*9118SSonam.Gupta@Sun.COM 					exit_code = 1;
167*9118SSonam.Gupta@Sun.COM 				}
168*9118SSonam.Gupta@Sun.COM 			} else {
1692264Sjacobs 				printf(gettext(
170*9118SSonam.Gupta@Sun.COM 				    "destination %s is not accepting"\
171*9118SSonam.Gupta@Sun.COM 				    " requests\n"), printer);
172*9118SSonam.Gupta@Sun.COM 
173*9118SSonam.Gupta@Sun.COM 				status = papiPrinterListJobs(svc, printer, NULL,
174*9118SSonam.Gupta@Sun.COM 				    0, 0, &jobs);
175*9118SSonam.Gupta@Sun.COM 				if (status != PAPI_OK) {
176*9118SSonam.Gupta@Sun.COM 					fprintf(stderr, gettext("Jobs %s:"\
177*9118SSonam.Gupta@Sun.COM 					    " %s\n"),
178*9118SSonam.Gupta@Sun.COM 					    printer,
179*9118SSonam.Gupta@Sun.COM 					    verbose_papi_message(svc, status));
180*9118SSonam.Gupta@Sun.COM 					exit_code = 1;
181*9118SSonam.Gupta@Sun.COM 				}
1822264Sjacobs 
183*9118SSonam.Gupta@Sun.COM 				printf(gettext("move in progress ...\n"));
184*9118SSonam.Gupta@Sun.COM 				while ((jobs != NULL) && (*jobs != NULL)) {
185*9118SSonam.Gupta@Sun.COM 					id = papiJobGetId(*jobs++);
186*9118SSonam.Gupta@Sun.COM 					if (move_job(svc, printer,
187*9118SSonam.Gupta@Sun.COM 					    id, destination) < 0)
188*9118SSonam.Gupta@Sun.COM 						exit_code = 1;
189*9118SSonam.Gupta@Sun.COM 					else
190*9118SSonam.Gupta@Sun.COM 						count++;
191*9118SSonam.Gupta@Sun.COM 				}
192*9118SSonam.Gupta@Sun.COM 				printf(gettext(
193*9118SSonam.Gupta@Sun.COM 				    "total of %d requests moved"\
194*9118SSonam.Gupta@Sun.COM 				    " from %s to %s\n"),
195*9118SSonam.Gupta@Sun.COM 				    count, printer, destination);
196*9118SSonam.Gupta@Sun.COM 
197*9118SSonam.Gupta@Sun.COM 				papiJobListFree(jobs);
1982264Sjacobs 			}
1992264Sjacobs 		}
2002264Sjacobs 
2012264Sjacobs 		papiServiceDestroy(svc);
2022264Sjacobs 	}
2032264Sjacobs 
2042264Sjacobs 	return (exit_code);
2052264Sjacobs }
206