1 
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2017 Gary Mills
25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  *
28  */
29 
30 /* $Id: disable.c 146 2006-03-24 00:26:54Z njacobs $ */
31 
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <libintl.h>
39 #include <papi.h>
40 #include "common.h"
41 
42 static void
usage(char * program)43 usage(char *program)
44 {
45 	char *name;
46 
47 	if ((name = strrchr(program, '/')) == NULL)
48 		name = program;
49 	else
50 		name++;
51 
52 	fprintf(stdout,
53 	    gettext("Usage: %s [-c] [-W] [-r reason] destination ...\n"),
54 	    name);
55 	exit(1);
56 }
57 
58 static void
cancel_active_job(papi_service_t svc,char * dest)59 cancel_active_job(papi_service_t svc, char *dest)
60 {
61 	papi_status_t status;
62 	papi_job_t *j = NULL;
63 	char *req_attrs[] = { "job-state", "job-id", NULL };
64 
65 	status = papiPrinterListJobs(svc, dest, req_attrs, 0, 0, &j);
66 	if ((status == PAPI_OK) && (j != NULL)) {
67 		int i;
68 
69 		for (i = 0; j[i] != NULL; j++) {
70 			papi_attribute_t **a = papiJobGetAttributeList(j[i]);
71 			int state = 0;
72 
73 			if (a == NULL)
74 				continue;
75 
76 			(void) papiAttributeListGetInteger(a, NULL,
77 			    "job-state", &state);
78 			if (state & 0x082A) { /* If state is RS_ACTIVE */
79 				int32_t id = papiJobGetId(j[i]);
80 
81 				(void) papiJobCancel(svc, dest, id);
82 			}
83 		}
84 		papiJobListFree(j);
85 	}
86 }
87 
88 int
main(int ac,char * av[])89 main(int ac, char *av[])
90 {
91 	papi_status_t status;
92 	papi_service_t svc = NULL;
93 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
94 	int exit_status = 0;
95 	int cancel = 0;
96 	char *reason = NULL;
97 	int c;
98 
99 	(void) setlocale(LC_ALL, "");
100 	(void) textdomain("SUNW_OST_OSCMD");
101 
102 	while ((c = getopt(ac, av, "EcWr:")) != EOF)
103 		switch (c) {
104 		case 'c':	/* cancel active job first */
105 			cancel = 1;
106 			break;
107 		case 'W':	/* wait for active request, not implemented */
108 			break;
109 		case 'r':	/* reason */
110 			reason = optarg;
111 			break;
112 		case 'E':
113 			encryption = PAPI_ENCRYPT_NEVER;
114 			break;
115 		default:
116 			usage(av[0]);
117 		}
118 
119 	if (ac <= optind)
120 		usage(av[0]);
121 
122 	while (optind < ac) {
123 		char *printer = av[optind++];
124 
125 		status = papiServiceCreate(&svc, printer, NULL, NULL,
126 		    cli_auth_callback, encryption, NULL);
127 		if (status != PAPI_OK) {
128 			fprintf(stderr, gettext(
129 			    "Failed to contact service for %s: %s\n"),
130 			    printer, verbose_papi_message(svc, status));
131 			exit_status = 1;
132 		}
133 
134 		status = papiPrinterDisable(svc, printer, reason);
135 		if (status == PAPI_OK) {
136 			printf(gettext("printer \"%s\" now disabled\n"),
137 			    printer);
138 		} else if (status == PAPI_NOT_ACCEPTING) {
139 			fprintf(stderr, gettext(
140 			    "Destination \"%s\" was already disabled.\n"),
141 			    printer);
142 			exit_status = 1;
143 		} else {
144 			/* The operation is not supported in lpd protocol */
145 			if (status == PAPI_OPERATION_NOT_SUPPORTED) {
146 				fprintf(stderr,
147 				    verbose_papi_message(svc, status));
148 			} else {
149 				fprintf(stderr, gettext("disable: %s: %s\n"),
150 				    printer, verbose_papi_message(svc, status));
151 			}
152 			exit_status = 1;
153 		}
154 
155 		if (cancel != 0)
156 			cancel_active_job(svc, printer);
157 
158 		papiServiceDestroy(svc);
159 	}
160 
161 	return (exit_status);
162 }
163