xref: /illumos-gate/usr/src/lib/librpcsvc/common/rusers_simple.c (revision e5803b76927480e8f9b67b22201c484ccf4c2bcf)
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 /*
25  * rusers_simple.c
26  * These are the "easy to use" interfaces to rusers.
27  *
28  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
29  * Use is subject to license terms.
30  */
31 
32 #include <string.h>
33 #include <rpc/rpc.h>
34 #include <rpcsvc/rusers.h>
35 #include <stdlib.h>
36 
37 int
38 rusers3(host, uap)
39 	char *host;
40 	utmp_array *uap;
41 {
42 	struct utmpidlearr up;
43 
44 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NAMES,
45 			xdr_void, (char *) NULL,
46 			xdr_utmp_array, (char *) uap, (char *) NULL) != 0) {
47 		/*
48 		 * If version 3 isn't available, try version 2.  We'll have to
49 		 * convert a utmpidlearr structure into a utmp_array.
50 		 */
51 		up.uia_cnt = 0;
52 		up.uia_arr = NULL;
53 		if (rusers(host, &up) != 0)
54 			return (-1);
55 		else {
56 			int i;
57 			struct ru_utmp forsize;
58 			rusers_utmp *rutp;
59 
60 			uap->utmp_array_val = (rusers_utmp *)malloc(up.uia_cnt
61 				* sizeof (rusers_utmp));
62 			if (uap->utmp_array_val == NULL) {
63 				xdr_free(xdr_utmpidlearr, (char *)&up);
64 				return (-1);
65 			}
66 			uap->utmp_array_len = up.uia_cnt;
67 			for (rutp = uap->utmp_array_val, i = 0;
68 				i < up.uia_cnt; rutp++, i++) {
69 				rutp->ut_line = (char *)malloc(sizeof
70 					(forsize.ut_line)+1);
71 				rutp->ut_user = (char *)malloc(sizeof
72 					(forsize.ut_name)+1);
73 				rutp->ut_host = (char *)malloc(sizeof
74 					(forsize.ut_host)+1);
75 				if (rutp->ut_line == NULL ||
76 					rutp->ut_user == NULL ||
77 					rutp->ut_host == NULL) {
78 
79                                         while (--rutp >= uap->utmp_array_val) {
80 						free(rutp->ut_line);
81 						free(rutp->ut_user);
82 						free(rutp->ut_host);
83 					}
84 					free(uap->utmp_array_val);
85 					xdr_free(xdr_utmpidlearr, (char *)&up);
86 					return (-1);
87 				}
88 				strncpy(rutp->ut_line,
89 					up.uia_arr[i]->ui_utmp.ut_line,
90 					sizeof (forsize.ut_line)+1);
91 				strncpy(rutp->ut_user,
92 					up.uia_arr[i]->ui_utmp.ut_name,
93 					sizeof (forsize.ut_name)+1);
94 				strncpy(rutp->ut_host,
95 					up.uia_arr[i]->ui_utmp.ut_host,
96 					sizeof (forsize.ut_host)+1);
97 				rutp->ut_idle = up.uia_arr[i]->ui_idle;
98 				rutp->ut_time = up.uia_arr[i]->ui_utmp.ut_time;
99 				rutp->ut_type = RUSERS_USER_PROCESS;
100 							/* assume this */
101 			}
102 			xdr_free(xdr_utmpidlearr, (char *)&up);
103 		}
104 	}
105 	return (0);
106 }
107 
108 int
109 rnusers(host)
110 	char *host;
111 {
112 	int nusers;
113 
114 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NUM,
115 			xdr_void, (char *) NULL,
116 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0) {
117 		if (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NUM,
118 			xdr_void, (char *) NULL,
119 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0)
120 		return (-1);
121 	}
122 	return (nusers);
123 }
124 
125 enum clnt_stat
126 rusers(host, up)
127 	char *host;
128 	struct utmpidlearr *up;
129 {
130 	return (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NAMES,
131 			xdr_void, (char *) NULL,
132 			xdr_utmpidlearr, (char *) up, (char *) NULL));
133 }
134 
135