17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * rusers_simple.c
257c478bd9Sstevel@tonic-gate  * These are the "easy to use" interfaces to rusers.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
31*3df5395bSJohn Levon /*
32*3df5395bSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
33*3df5395bSJohn Levon  */
34*3df5395bSJohn Levon 
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
377c478bd9Sstevel@tonic-gate #include <rpcsvc/rusers.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int
rusers3(host,uap)417c478bd9Sstevel@tonic-gate rusers3(host, uap)
427c478bd9Sstevel@tonic-gate 	char *host;
437c478bd9Sstevel@tonic-gate 	utmp_array *uap;
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	struct utmpidlearr up;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NAMES,
487c478bd9Sstevel@tonic-gate 			xdr_void, (char *) NULL,
497c478bd9Sstevel@tonic-gate 			xdr_utmp_array, (char *) uap, (char *) NULL) != 0) {
507c478bd9Sstevel@tonic-gate 		/*
517c478bd9Sstevel@tonic-gate 		 * If version 3 isn't available, try version 2.  We'll have to
527c478bd9Sstevel@tonic-gate 		 * convert a utmpidlearr structure into a utmp_array.
537c478bd9Sstevel@tonic-gate 		 */
547c478bd9Sstevel@tonic-gate 		up.uia_cnt = 0;
557c478bd9Sstevel@tonic-gate 		up.uia_arr = NULL;
567c478bd9Sstevel@tonic-gate 		if (rusers(host, &up) != 0)
577c478bd9Sstevel@tonic-gate 			return (-1);
587c478bd9Sstevel@tonic-gate 		else {
597c478bd9Sstevel@tonic-gate 			int i;
607c478bd9Sstevel@tonic-gate 			struct ru_utmp forsize;
617c478bd9Sstevel@tonic-gate 			rusers_utmp *rutp;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 			uap->utmp_array_val = (rusers_utmp *)malloc(up.uia_cnt
647c478bd9Sstevel@tonic-gate 				* sizeof (rusers_utmp));
657c478bd9Sstevel@tonic-gate 			if (uap->utmp_array_val == NULL) {
667c478bd9Sstevel@tonic-gate 				xdr_free(xdr_utmpidlearr, (char *)&up);
677c478bd9Sstevel@tonic-gate 				return (-1);
687c478bd9Sstevel@tonic-gate 			}
697c478bd9Sstevel@tonic-gate 			uap->utmp_array_len = up.uia_cnt;
707c478bd9Sstevel@tonic-gate 			for (rutp = uap->utmp_array_val, i = 0;
717c478bd9Sstevel@tonic-gate 				i < up.uia_cnt; rutp++, i++) {
727c478bd9Sstevel@tonic-gate 				rutp->ut_line = (char *)malloc(sizeof
737c478bd9Sstevel@tonic-gate 					(forsize.ut_line)+1);
747c478bd9Sstevel@tonic-gate 				rutp->ut_user = (char *)malloc(sizeof
757c478bd9Sstevel@tonic-gate 					(forsize.ut_name)+1);
767c478bd9Sstevel@tonic-gate 				rutp->ut_host = (char *)malloc(sizeof
777c478bd9Sstevel@tonic-gate 					(forsize.ut_host)+1);
787c478bd9Sstevel@tonic-gate 				if (rutp->ut_line == NULL ||
797c478bd9Sstevel@tonic-gate 					rutp->ut_user == NULL ||
807c478bd9Sstevel@tonic-gate 					rutp->ut_host == NULL) {
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate                                         while (--rutp >= uap->utmp_array_val) {
837c478bd9Sstevel@tonic-gate 						free(rutp->ut_line);
847c478bd9Sstevel@tonic-gate 						free(rutp->ut_user);
857c478bd9Sstevel@tonic-gate 						free(rutp->ut_host);
867c478bd9Sstevel@tonic-gate 					}
877c478bd9Sstevel@tonic-gate 					free(uap->utmp_array_val);
887c478bd9Sstevel@tonic-gate 					xdr_free(xdr_utmpidlearr, (char *)&up);
897c478bd9Sstevel@tonic-gate 					return (-1);
907c478bd9Sstevel@tonic-gate 				}
91*3df5395bSJohn Levon 				(void) strncpy(rutp->ut_line,
927c478bd9Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_line,
937c478bd9Sstevel@tonic-gate 					sizeof (forsize.ut_line)+1);
94*3df5395bSJohn Levon 				(void) strncpy(rutp->ut_user,
957c478bd9Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_name,
967c478bd9Sstevel@tonic-gate 					sizeof (forsize.ut_name)+1);
97*3df5395bSJohn Levon 				(void) strncpy(rutp->ut_host,
987c478bd9Sstevel@tonic-gate 					up.uia_arr[i]->ui_utmp.ut_host,
997c478bd9Sstevel@tonic-gate 					sizeof (forsize.ut_host)+1);
1007c478bd9Sstevel@tonic-gate 				rutp->ut_idle = up.uia_arr[i]->ui_idle;
1017c478bd9Sstevel@tonic-gate 				rutp->ut_time = up.uia_arr[i]->ui_utmp.ut_time;
1027c478bd9Sstevel@tonic-gate 				rutp->ut_type = RUSERS_USER_PROCESS;
1037c478bd9Sstevel@tonic-gate 							/* assume this */
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 			xdr_free(xdr_utmpidlearr, (char *)&up);
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	return (0);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate int
rnusers(host)1127c478bd9Sstevel@tonic-gate rnusers(host)
1137c478bd9Sstevel@tonic-gate 	char *host;
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	int nusers;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (rpc_call(host, RUSERSPROG, RUSERSVERS_3, RUSERSPROC_NUM,
1187c478bd9Sstevel@tonic-gate 			xdr_void, (char *) NULL,
1197c478bd9Sstevel@tonic-gate 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0) {
1207c478bd9Sstevel@tonic-gate 		if (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NUM,
1217c478bd9Sstevel@tonic-gate 			xdr_void, (char *) NULL,
1227c478bd9Sstevel@tonic-gate 			xdr_u_int, (char *) &nusers, (char *) NULL) != 0)
123*3df5395bSJohn Levon 			return (-1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return (nusers);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate enum clnt_stat
rusers(host,up)1297c478bd9Sstevel@tonic-gate rusers(host, up)
1307c478bd9Sstevel@tonic-gate 	char *host;
1317c478bd9Sstevel@tonic-gate 	struct utmpidlearr *up;
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	return (rpc_call(host, RUSERSPROG, RUSERSVERS_IDLE, RUSERSPROC_NAMES,
1347c478bd9Sstevel@tonic-gate 			xdr_void, (char *) NULL,
1357c478bd9Sstevel@tonic-gate 			xdr_utmpidlearr, (char *) up, (char *) NULL));
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
138