xref: /onnv-gate/usr/src/lib/librpcsvc/common/rusers_simple.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * rusers_simple.c
26*0Sstevel@tonic-gate  * These are the "easy to use" interfaces to rusers.
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
29*0Sstevel@tonic-gate  * Use is subject to license terms.
30*0Sstevel@tonic-gate  */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <rpc/rpc.h>
34*0Sstevel@tonic-gate #include <rpcsvc/rusers.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate int
rusers3(host,uap)38*0Sstevel@tonic-gate rusers3(host, uap)
39*0Sstevel@tonic-gate 	char *host;
40*0Sstevel@tonic-gate 	utmp_array *uap;
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate 	struct utmpidlearr up;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NAMES,
45*0Sstevel@tonic-gate 			xdr_void, (char *) NULL,
46*0Sstevel@tonic-gate 			xdr_utmp_array, (char *) uap, (char *) NULL) != 0) {
47*0Sstevel@tonic-gate 		/*
48*0Sstevel@tonic-gate 		 * If version 3 isn't available, try version 2.  We'll have to
49*0Sstevel@tonic-gate 		 * convert a utmpidlearr structure into a utmp_array.
50*0Sstevel@tonic-gate 		 */
51*0Sstevel@tonic-gate 		up.uia_cnt = 0;
52*0Sstevel@tonic-gate 		up.uia_arr = NULL;
53*0Sstevel@tonic-gate 		if (rusers(host, &up) != 0)
54*0Sstevel@tonic-gate 			return (-1);
55*0Sstevel@tonic-gate 		else {
56*0Sstevel@tonic-gate 			int i;
57*0Sstevel@tonic-gate 			struct ru_utmp forsize;
58*0Sstevel@tonic-gate 			rusers_utmp *rutp;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate 			uap->utmp_array_val = (rusers_utmp *)malloc(up.uia_cnt
61*0Sstevel@tonic-gate 				* sizeof (rusers_utmp));
62*0Sstevel@tonic-gate 			if (uap->utmp_array_val == NULL) {
63*0Sstevel@tonic-gate 				xdr_free(xdr_utmpidlearr, (char *)&up);
64*0Sstevel@tonic-gate 				return (-1);
65*0Sstevel@tonic-gate 			}
66*0Sstevel@tonic-gate 			uap->utmp_array_len = up.uia_cnt;
67*0Sstevel@tonic-gate 			for (rutp = uap->utmp_array_val, i = 0;
68*0Sstevel@tonic-gate 				i < up.uia_cnt; rutp++, i++) {
69*0Sstevel@tonic-gate 				rutp->ut_line = (char *)malloc(sizeof
70*0Sstevel@tonic-gate 					(forsize.ut_line)+1);
71*0Sstevel@tonic-gate 				rutp->ut_user = (char *)malloc(sizeof
72*0Sstevel@tonic-gate 					(forsize.ut_name)+1);
73*0Sstevel@tonic-gate 				rutp->ut_host = (char *)malloc(sizeof
74*0Sstevel@tonic-gate 					(forsize.ut_host)+1);
75*0Sstevel@tonic-gate 				if (rutp->ut_line == NULL ||
76*0Sstevel@tonic-gate 					rutp->ut_user == NULL ||
77*0Sstevel@tonic-gate 					rutp->ut_host == NULL) {
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate                                         while (--rutp >= uap->utmp_array_val) {
80*0Sstevel@tonic-gate 						free(rutp->ut_line);
81*0Sstevel@tonic-gate 						free(rutp->ut_user);
82*0Sstevel@tonic-gate 						free(rutp->ut_host);
83*0Sstevel@tonic-gate 					}
84*0Sstevel@tonic-gate 					free(uap->utmp_array_val);
85*0Sstevel@tonic-gate 					xdr_free(xdr_utmpidlearr, (char *)&up);
86*0Sstevel@tonic-gate 					return (-1);
87*0Sstevel@tonic-gate 				}
88*0Sstevel@tonic-gate 				strncpy(rutp->ut_line,
89*0Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_line,
90*0Sstevel@tonic-gate 					sizeof (forsize.ut_line)+1);
91*0Sstevel@tonic-gate 				strncpy(rutp->ut_user,
92*0Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_name,
93*0Sstevel@tonic-gate 					sizeof (forsize.ut_name)+1);
94*0Sstevel@tonic-gate 				strncpy(rutp->ut_host,
95*0Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_host,
96*0Sstevel@tonic-gate 					sizeof (forsize.ut_host)+1);
97*0Sstevel@tonic-gate 				rutp->ut_idle = up.uia_arr[i]->ui_idle;
98*0Sstevel@tonic-gate 				rutp->ut_time = up.uia_arr[i]->ui_utmp.ut_time;
99*0Sstevel@tonic-gate 				rutp->ut_type = RUSERS_USER_PROCESS;
100*0Sstevel@tonic-gate 							/* assume this */
101*0Sstevel@tonic-gate 			}
102*0Sstevel@tonic-gate 			xdr_free(xdr_utmpidlearr, (char *)&up);
103*0Sstevel@tonic-gate 		}
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	return (0);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate int
rnusers(host)109*0Sstevel@tonic-gate rnusers(host)
110*0Sstevel@tonic-gate 	char *host;
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate 	int nusers;
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NUM,
115*0Sstevel@tonic-gate 			xdr_void, (char *) NULL,
116*0Sstevel@tonic-gate 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0) {
117*0Sstevel@tonic-gate 		if (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NUM,
118*0Sstevel@tonic-gate 			xdr_void, (char *) NULL,
119*0Sstevel@tonic-gate 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0)
120*0Sstevel@tonic-gate 		return (-1);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	return (nusers);
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate enum clnt_stat
rusers(host,up)126*0Sstevel@tonic-gate rusers(host, up)
127*0Sstevel@tonic-gate 	char *host;
128*0Sstevel@tonic-gate 	struct utmpidlearr *up;
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	return (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NAMES,
131*0Sstevel@tonic-gate 			xdr_void, (char *) NULL,
132*0Sstevel@tonic-gate 			xdr_utmpidlearr, (char *) up, (char *) NULL));
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135