xref: /titanic_52/usr/src/cmd/print/bsd-sysv-commands/lpmove.c (revision 96a62ada8aa6cb19b04270da282e7e21ba74b808)
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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: lpmove.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <locale.h>
35 #include <libintl.h>
36 #include <papi.h>
37 #include "common.h"
38 
39 static void
40 usage(char *program)
41 {
42 	char *name;
43 
44 	if ((name = strrchr(program, '/')) == NULL)
45 		name = program;
46 	else
47 		name++;
48 
49 	fprintf(stdout,
50 	    gettext("Usage: %s [request-id] (destination)\n"
51 	    "       %s (source) (destination)\n"), name, name);
52 	exit(1);
53 }
54 
55 static int
56 move_job(papi_service_t svc, char *src, int32_t id, char *dest)
57 {
58 	int result = 0;
59 	papi_status_t status;
60 	char *mesg = gettext("moved");
61 
62 	status = papiJobMove(svc, src, id, dest);
63 	if (status != PAPI_OK) {
64 		mesg = (char *)verbose_papi_message(svc, status);
65 		result = -1;
66 	}
67 	fprintf(stderr, gettext("%s-%d to %s: %s\n"), src, id, dest, mesg);
68 
69 	return (result);
70 }
71 
72 int
73 main(int ac, char *av[])
74 {
75 	int exit_code = 0;
76 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
77 	char *destination = NULL;
78 	int c;
79 
80 	(void) setlocale(LC_ALL, "");
81 	(void) textdomain("SUNW_OST_OSCMD");
82 
83 	while ((c = getopt(ac, av, "E:")) != EOF)
84 		switch (c) {
85 		case 'E':
86 			encryption = PAPI_ENCRYPT_REQUIRED;
87 			break;
88 		default:
89 			usage(av[0]);
90 		}
91 
92 	if (optind >= ac - 1)
93 		usage(av[0]);
94 
95 	destination = av[--ac];
96 
97 	for (c = optind; c < ac; c++) {
98 		papi_status_t status;
99 		papi_service_t svc = NULL;
100 		papi_job_t *jobs = NULL;
101 		char *printer = NULL;
102 		int32_t id = -1;
103 
104 		(void) get_printer_id(av[c], &printer, &id);
105 
106 		status = papiServiceCreate(&svc, printer, NULL, NULL,
107 		    cli_auth_callback, encryption, NULL);
108 		if (status != PAPI_OK) {
109 			fprintf(stderr, gettext(
110 			    "Failed to contact service for %s: %s\n"),
111 			    printer, verbose_papi_message(svc, status));
112 			exit(1);
113 		}
114 
115 		if (id != -1) {	/* it's a job */
116 			if (move_job(svc, printer, id, destination) < 0)
117 				exit_code = 1;
118 		} else {	/* it's a printer */
119 			char message[128];
120 			int count = 0;
121 
122 			snprintf(message, sizeof (message), "moved jobs to %s",
123 			    destination);
124 			status = papiPrinterPause(svc, printer, message);
125 			if (status != PAPI_OK) {
126 				/*
127 				 * If the user is denied the permission
128 				 * to disable then return appropriate msg
129 				 */
130 				char *result = NULL;
131 
132 				result = papiServiceGetStatusMessage(svc);
133 
134 				if (result != NULL) {
135 					/*
136 					 * Check if user is denied
137 					 * the permission
138 					 */
139 					if (strstr(result, "permission denied")
140 					    != NULL) {
141 						/*
142 						 * user is denied
143 						 * permission
144 						 */
145 						fprintf(stderr, gettext(
146 						    "UX:lpmove: ERROR:"\
147 						    " You aren't allowed"\
148 						    " to do that.\n\t"\
149 						    "  TO FIX: You must"\
150 						    " be logged in as"\
151 						    " \"lp\" or \"root\".\n"));
152 						exit_code = 1;
153 					} else {
154 						fprintf(stderr, gettext(
155 						    "Reject %s: %s\n"),
156 						    printer,
157 						    verbose_papi_message(
158 						    svc, status));
159 						exit_code = 1;
160 					}
161 				} else {
162 					fprintf(stderr, gettext(
163 					    "Reject %s: %s\n"),
164 					    printer,
165 					    verbose_papi_message(svc, status));
166 					exit_code = 1;
167 				}
168 			} else {
169 				printf(gettext(
170 				    "destination %s is not accepting"\
171 				    " requests\n"), printer);
172 
173 				status = papiPrinterListJobs(svc, printer, NULL,
174 				    0, 0, &jobs);
175 				if (status != PAPI_OK) {
176 					fprintf(stderr, gettext("Jobs %s:"\
177 					    " %s\n"),
178 					    printer,
179 					    verbose_papi_message(svc, status));
180 					exit_code = 1;
181 				}
182 
183 				printf(gettext("move in progress ...\n"));
184 				while ((jobs != NULL) && (*jobs != NULL)) {
185 					id = papiJobGetId(*jobs++);
186 					if (move_job(svc, printer,
187 					    id, destination) < 0)
188 						exit_code = 1;
189 					else
190 						count++;
191 				}
192 				printf(gettext(
193 				    "total of %d requests moved"\
194 				    " from %s to %s\n"),
195 				    count, printer, destination);
196 
197 				papiJobListFree(jobs);
198 			}
199 		}
200 
201 		papiServiceDestroy(svc);
202 	}
203 
204 	return (exit_code);
205 }
206