1 |
< |
/* |
2 |
< |
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
1 |
> |
/* |
2 |
> |
* i-scream libstatgrab |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
6 |
< |
* This program is free software; you can redistribute it and/or |
7 |
< |
* modify it under the terms of the GNU General Public License |
8 |
< |
* as published by the Free Software Foundation; either version 2 |
9 |
< |
* of the License, or (at your option) any later version. |
6 |
> |
* This library is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU Lesser General Public |
8 |
> |
* License as published by the Free Software Foundation; either |
9 |
> |
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library is distributed in the hope that it will be useful, |
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
< |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
< |
* GNU General Public License for more details. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser General Public License for more details. |
15 |
|
* |
16 |
< |
* You should have received a copy of the GNU General Public License |
17 |
< |
* along with this program; if not, write to the Free Software |
18 |
< |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 |
> |
* You should have received a copy of the GNU Lesser General Public |
17 |
> |
* License along with this library; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
> |
* 02111-1307 USA |
20 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
25 |
|
#include "config.h" |
26 |
|
#endif |
27 |
< |
#include "statgrab.h" |
27 |
> |
|
28 |
|
#include <stdlib.h> |
29 |
|
#include <stdio.h> |
30 |
|
#include <string.h> |
31 |
< |
#ifdef FREEBSD |
31 |
> |
#include "statgrab.h" |
32 |
> |
#include "vector.h" |
33 |
> |
#ifdef ALLBSD |
34 |
|
#include <sys/types.h> |
35 |
|
#endif |
36 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
37 |
+ |
#include <limits.h> |
38 |
+ |
#endif |
39 |
+ |
#ifdef OPENBSD |
40 |
+ |
#include <sys/param.h> |
41 |
+ |
#endif |
42 |
+ |
#ifndef WIN32 |
43 |
|
#include <utmp.h> |
44 |
+ |
#endif |
45 |
+ |
#ifdef CYGWIN |
46 |
+ |
#include <sys/unistd.h> |
47 |
+ |
#endif |
48 |
+ |
#ifdef HPUX |
49 |
+ |
#include <utmp.h> |
50 |
+ |
#endif |
51 |
+ |
#ifdef WIN32 |
52 |
+ |
#include <windows.h> |
53 |
+ |
#include <lm.h> |
54 |
+ |
#endif |
55 |
|
|
56 |
< |
#define START_VAL 5 |
56 |
> |
sg_user_stats *sg_get_user_stats(){ |
57 |
> |
int num_users = 0, pos = 0, new_pos; |
58 |
> |
VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL); |
59 |
> |
static sg_user_stats user_stats; |
60 |
> |
#ifdef ALLBSD |
61 |
> |
struct utmp entry; |
62 |
> |
FILE *f; |
63 |
|
|
64 |
< |
user_stat_t *get_user_stats(){ |
65 |
< |
int num_users=0; |
64 |
> |
if ((f=fopen(_PATH_UTMP, "r")) == NULL){ |
65 |
> |
sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP); |
66 |
> |
return NULL; |
67 |
> |
} |
68 |
> |
while((fread(&entry, sizeof(entry),1,f)) != 0){ |
69 |
> |
if (entry.ut_name[0] == '\0') continue; |
70 |
|
|
71 |
< |
static user_stat_t user_stat; |
72 |
< |
static int watermark=-1; |
73 |
< |
struct utmp *entry; |
71 |
> |
new_pos = pos + strlen(entry.ut_name) + 1; |
72 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
73 |
> |
return NULL; |
74 |
> |
} |
75 |
|
|
76 |
< |
name *name_ptr; |
76 |
> |
strcpy(name_list + pos, entry.ut_name); |
77 |
> |
name_list[new_pos - 1] = ' '; |
78 |
> |
pos = new_pos; |
79 |
> |
num_users++; |
80 |
> |
} |
81 |
> |
fclose(f); |
82 |
> |
#elif defined (WIN32) |
83 |
> |
LPWKSTA_USER_INFO_0 buf = NULL; |
84 |
> |
LPWKSTA_USER_INFO_0 tmp_buf; |
85 |
> |
unsigned long entries_read = 0; |
86 |
> |
unsigned long entries_tot = 0; |
87 |
> |
unsigned long resumehandle = 0; |
88 |
> |
NET_API_STATUS nStatus; |
89 |
> |
int i; |
90 |
> |
char name[256]; |
91 |
|
|
92 |
< |
/* First case call */ |
93 |
< |
if (watermark==-1){ |
94 |
< |
user_stat.name_list=malloc(START_VAL * sizeof *user_stat.name_list); |
95 |
< |
if(user_stat.name_list==NULL){ |
92 |
> |
do { |
93 |
> |
nStatus = NetWkstaUserEnum(NULL, 0, (LPBYTE*)&buf, |
94 |
> |
MAX_PREFERRED_LENGTH, &entries_read, |
95 |
> |
&entries_tot, &resumehandle); |
96 |
> |
if((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) { |
97 |
> |
if((tmp_buf = buf) == NULL) { |
98 |
> |
continue; |
99 |
> |
} |
100 |
> |
for (i=0; i<entries_read; i++) { |
101 |
> |
//assert(tmp_buf != NULL); |
102 |
> |
if (tmp_buf == NULL) { |
103 |
> |
sg_set_error(SG_ERROR_PERMISSION, "User list"); |
104 |
> |
break; |
105 |
> |
} |
106 |
> |
/* It's in unicode. We are not. Convert */ |
107 |
> |
WideCharToMultiByte(CP_ACP, 0, tmp_buf->wkui0_username, -1, name, sizeof(name), NULL, NULL); |
108 |
> |
|
109 |
> |
new_pos = pos + strlen(name) + 1; |
110 |
> |
if(VECTOR_RESIZE(name_list, new_pos) < 0) { |
111 |
> |
NetApiBufferFree(buf); |
112 |
> |
return NULL; |
113 |
> |
} |
114 |
> |
strcpy(name_list + pos, name); |
115 |
> |
name_list[new_pos - 1] = ' '; |
116 |
> |
pos = new_pos; |
117 |
> |
|
118 |
> |
tmp_buf++; |
119 |
> |
num_users++; |
120 |
> |
} |
121 |
> |
} else { |
122 |
> |
sg_set_error(SG_ERROR_PERMISSION, "User enum"); |
123 |
|
return NULL; |
124 |
|
} |
125 |
< |
watermark=START_VAL; |
126 |
< |
} |
125 |
> |
if (buf != NULL) { |
126 |
> |
NetApiBufferFree(buf); |
127 |
> |
buf=NULL; |
128 |
> |
} |
129 |
> |
} while (nStatus == ERROR_MORE_DATA); |
130 |
> |
if (buf != NULL) { |
131 |
> |
NetApiBufferFree(buf); |
132 |
> |
} |
133 |
> |
#else |
134 |
> |
/* This works on everything else. */ |
135 |
> |
struct utmp *entry; |
136 |
|
|
137 |
|
setutent(); |
138 |
|
while((entry=getutent()) != NULL) { |
139 |
< |
if(entry->ut_type==USER_PROCESS) { |
56 |
< |
if(num_users>watermark-1){ |
57 |
< |
name_ptr=user_stat.name_list; |
58 |
< |
if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){ |
59 |
< |
user_stat.name_list=name_ptr; |
60 |
< |
return NULL; |
61 |
< |
} |
62 |
< |
watermark=watermark*2; |
63 |
< |
} |
139 |
> |
if (entry->ut_type != USER_PROCESS) continue; |
140 |
|
|
141 |
< |
strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE); |
142 |
< |
/* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */ |
143 |
< |
|
68 |
< |
user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0'; |
69 |
< |
|
70 |
< |
num_users++; |
141 |
> |
new_pos = pos + strlen(entry->ut_user) + 1; |
142 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
143 |
> |
return NULL; |
144 |
|
} |
145 |
+ |
|
146 |
+ |
strcpy(name_list + pos, entry->ut_user); |
147 |
+ |
name_list[new_pos - 1] = ' '; |
148 |
+ |
pos = new_pos; |
149 |
+ |
num_users++; |
150 |
|
} |
151 |
|
endutent(); |
152 |
< |
user_stat.num_entries=num_users; |
152 |
> |
#endif |
153 |
|
|
154 |
< |
return &user_stat; |
154 |
> |
/* Remove the extra space at the end, and append a \0. */ |
155 |
> |
if (num_users != 0) { |
156 |
> |
pos--; |
157 |
> |
} |
158 |
> |
if (VECTOR_RESIZE(name_list, pos + 1) < 0) { |
159 |
> |
return NULL; |
160 |
> |
} |
161 |
> |
name_list[pos] = '\0'; |
162 |
|
|
163 |
+ |
user_stats.num_entries = num_users; |
164 |
+ |
user_stats.name_list = name_list; |
165 |
+ |
return &user_stats; |
166 |
|
} |