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