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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
23 /*
24  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /* LINTLIBRARY */
29 
30 #include <stdio.h>
31 #include <strings.h>
32 #include <libintl.h>
33 #include "com_sun_admin_pm_server_DoPrinterNS.h"
34 
35 jstring	glob_jstdout = NULL;
36 jstring glob_jstderr = NULL;
37 
38 extern char glob_stdout[];
39 extern char glob_stderr[];
40 
41 void set_stderr(JNIEnv *env);
42 void set_stdout(JNIEnv *env);
43 extern int _dorexec(
44 	const char *host,
45 	const char *user,
46 	const char *passwd,
47 	const char *cmd,
48 	const char *locale);
49 
50 extern int _updateoldyp(
51 	const char *action,
52 	const char *printername,
53 	const char *printserver,
54 	const char *extensions,
55 	const char *comment,
56 	const char *isdefault);
57 
58 extern int _updateldap(
59 	const char *action,
60 	const char *host,
61 	const char *user,
62 	const char *passwd,
63 	const char *printername,
64 	const char *printserver,
65 	const char *extensions,
66 	const char *comment,
67 	const char *isdefault);
68 
69 JNIEXPORT jint JNICALL
Java_com_sun_admin_pm_server_DoPrinterNS_dorexec(JNIEnv * env,jclass class,jstring jhost,jstring juser,jstring jpasswd,jstring jcmd,jstring jlocale)70 Java_com_sun_admin_pm_server_DoPrinterNS_dorexec(
71 	JNIEnv *env,
72 	jclass class,
73 	jstring jhost,
74 	jstring juser,
75 	jstring jpasswd,
76 	jstring jcmd,
77 	jstring jlocale)
78 {
79 	const char *host;
80 	const char *user;
81 	const char *passwd;
82 	const char *cmd;
83 	const char *locale;
84 
85 	int ret = 0;
86 	jstring empty = (*env)->NewStringUTF(env, "");
87 	glob_jstdout = (*env)->NewGlobalRef(env, empty);
88 	glob_jstderr = (*env)->NewGlobalRef(env, empty);
89 
90 	host = (*env)->GetStringUTFChars(env, jhost, 0);
91 	user = (*env)->GetStringUTFChars(env, juser, 0);
92 	passwd = (*env)->GetStringUTFChars(env, jpasswd, 0);
93 	cmd = (*env)->GetStringUTFChars(env, jcmd, 0);
94 	locale = (*env)->GetStringUTFChars(env, jlocale, 0);
95 
96 	ret = _dorexec(host, user, passwd, cmd, locale);
97 
98 	if (ret != 0) {
99 		(void) set_stderr(env);
100 		return (ret);
101 	}
102 	(void) set_stderr(env);
103 	(void) set_stdout(env);
104 	return (ret);
105 }
106 
107 JNIEXPORT jstring JNICALL
Java_com_sun_admin_pm_server_DoPrinterNS_getstderr(JNIEnv * env,jclass class)108 Java_com_sun_admin_pm_server_DoPrinterNS_getstderr(JNIEnv *env, jclass class)
109 {
110 	return (glob_jstderr);
111 }
112 
113 JNIEXPORT jstring JNICALL
Java_com_sun_admin_pm_server_DoPrinterNS_getstdout(JNIEnv * env,jclass class)114 Java_com_sun_admin_pm_server_DoPrinterNS_getstdout(JNIEnv *env, jclass class)
115 {
116 	return (glob_jstdout);
117 }
118 
119 void
set_stderr(JNIEnv * env)120 set_stderr(JNIEnv *env)
121 {
122 	static jstring jerrstr;
123 	static char errbuf[BUFSIZ];
124 
125 	if (*glob_stderr == '\0') {
126 		(void) strcpy(errbuf, "");
127 	} else {
128 		(void) strcpy(errbuf, glob_stderr);
129 	}
130 
131 	jerrstr = (*env)->NewStringUTF(env, "");
132 	if (glob_jstderr != NULL) {
133 		(*env)->DeleteGlobalRef(env, glob_jstderr);
134 	}
135 
136 	jerrstr = (*env)->NewStringUTF(env, errbuf);
137 	glob_jstderr = (*env)->NewGlobalRef(env, jerrstr);
138 }
139 
140 void
set_stdout(JNIEnv * env)141 set_stdout(JNIEnv *env)
142 {
143 	static jstring joutstr;
144 	static char outbuf[BUFSIZ];
145 
146 	if (*glob_stdout == '\0') {
147 		(void) strcpy(outbuf, "");
148 	} else {
149 		(void) strcpy(outbuf, glob_stdout);
150 	}
151 
152 	joutstr = (*env)->NewStringUTF(env, "");
153 	if (glob_jstdout != NULL) {
154 		(*env)->DeleteGlobalRef(env, glob_jstdout);
155 	}
156 
157 	joutstr = (*env)->NewStringUTF(env, outbuf);
158 	glob_jstdout = (*env)->NewGlobalRef(env, joutstr);
159 }
160 
161 JNIEXPORT jint JNICALL
Java_com_sun_admin_pm_server_DoPrinterNS_updateoldyp(JNIEnv * env,jclass class,jstring jaction,jstring jprintername,jstring jprintserver,jstring jextensions,jstring jcomment,jstring jisdefault)162 Java_com_sun_admin_pm_server_DoPrinterNS_updateoldyp(
163 	JNIEnv *env,
164 	jclass class,
165 	jstring jaction,
166 	jstring jprintername,
167 	jstring jprintserver,
168 	jstring jextensions,
169 	jstring jcomment,
170 	jstring jisdefault)
171 {
172 	const char *action = NULL;
173 	const char *printername = NULL;
174 	const char *printserver = NULL;
175 	const char *extensions = NULL;
176 	const char *comment = NULL;
177 	const char *isdefault = NULL;
178 
179 	int ret = 0;
180 	jstring empty = (*env)->NewStringUTF(env, "");
181 	glob_jstdout = (*env)->NewGlobalRef(env, empty);
182 	glob_jstderr = (*env)->NewGlobalRef(env, empty);
183 
184 	action = (*env)->GetStringUTFChars(env, jaction, 0);
185 	printername = (*env)->GetStringUTFChars(env, jprintername, 0);
186 	if (jprintserver != NULL) {
187 		printserver =
188 			(*env)->GetStringUTFChars(env, jprintserver, 0);
189 	}
190 	if (jextensions != NULL) {
191 		extensions =
192 			(*env)->GetStringUTFChars(env, jextensions, 0);
193 	}
194 	if (jcomment != NULL) {
195 		comment =
196 			(*env)->GetStringUTFChars(env, jcomment, 0);
197 	}
198 	isdefault = (*env)->GetStringUTFChars(env, jisdefault, 0);
199 
200 	ret = _updateoldyp(action, printername, printserver,
201 		extensions, comment, isdefault);
202 
203 	if (ret != 0) {
204 		(void) set_stderr(env);
205 		return (ret);
206 	}
207 	(void) set_stderr(env);
208 	(void) set_stdout(env);
209 	return (ret);
210 }
211 
212 JNIEXPORT jint JNICALL
Java_com_sun_admin_pm_server_DoPrinterNS_updateldap(JNIEnv * env,jclass class,jstring jaction,jstring jhost,jstring jbinddn,jstring jpasswd,jstring jprintername,jstring jprintserver,jstring jextensions,jstring jcomment,jstring jisdefault)213 Java_com_sun_admin_pm_server_DoPrinterNS_updateldap(
214 	JNIEnv *env,
215 	jclass class,
216 	jstring jaction,
217 	jstring jhost,
218 	jstring jbinddn,
219 	jstring jpasswd,
220 	jstring jprintername,
221 	jstring jprintserver,
222 	jstring jextensions,
223 	jstring jcomment,
224 	jstring jisdefault)
225 {
226 	const char *action = NULL;
227 	const char *host = NULL;
228 	const char *binddn = NULL;
229 	const char *passwd = NULL;
230 	const char *printername = NULL;
231 	const char *printserver = NULL;
232 	const char *extensions = NULL;
233 	const char *comment = NULL;
234 	const char *isdefault = NULL;
235 
236 	int ret = 0;
237 	jstring empty = (*env)->NewStringUTF(env, "");
238 	glob_jstdout = (*env)->NewGlobalRef(env, empty);
239 	glob_jstderr = (*env)->NewGlobalRef(env, empty);
240 
241 	action = (*env)->GetStringUTFChars(env, jaction, 0);
242 	printername = (*env)->GetStringUTFChars(env, jprintername, 0);
243 	if (jhost != NULL) {
244 		host = (*env)->GetStringUTFChars(env, jhost, 0);
245 	}
246 	if (jbinddn != NULL) {
247 		binddn = (*env)->GetStringUTFChars(env, jbinddn, 0);
248 	}
249 	if (jpasswd != NULL) {
250 		passwd = (*env)->GetStringUTFChars(env, jpasswd, 0);
251 	}
252 	if (jprintserver != NULL) {
253 		printserver =
254 			(*env)->GetStringUTFChars(env, jprintserver, 0);
255 	}
256 	if (jextensions != NULL) {
257 		extensions =
258 			(*env)->GetStringUTFChars(env, jextensions, 0);
259 	}
260 	if (jcomment != NULL) {
261 		comment =
262 			(*env)->GetStringUTFChars(env, jcomment, 0);
263 	}
264 	isdefault = (*env)->GetStringUTFChars(env, jisdefault, 0);
265 
266 	ret = _updateldap(action, host, binddn, passwd, printername,
267 	    printserver, extensions, comment, isdefault);
268 
269 	if (ret != 0) {
270 		(void) set_stderr(env);
271 		return (ret);
272 	}
273 	(void) set_stderr(env);
274 	(void) set_stdout(env);
275 	return (ret);
276 }
277