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