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