ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
Revision: 1.18
Committed: Mon Jan 5 17:20:29 2004 UTC (20 years, 4 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_8
Changes since 1.17: +8 -0 lines
Log Message:
Replace duplicated code for dropping privileges with a library
function that always drops setuid/setgid privileges.

File Contents

# User Rev Content
1 tdb 1.5 /*
2     * i-scream central monitoring system
3 tdb 1.6 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 tdb 1.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.
10     *
11     * This program 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.
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.
19     */
20    
21     #ifdef HAVE_CONFIG_H
22     #include "config.h"
23     #endif
24    
25 pajs 1.1 #include <stdio.h>
26     #include <string.h>
27 pajs 1.3 #include <stdlib.h>
28 ats 1.18 #include <unistd.h>
29 pajs 1.3 #include <sys/types.h>
30     #include <regex.h>
31 ats 1.9 #ifdef ALLBSD
32 ats 1.8 #include <fcntl.h>
33     #include <kvm.h>
34     #endif
35 ats 1.9 #ifdef NETBSD
36     #include <uvm/uvm_extern.h>
37     #endif
38 pajs 1.1
39 ats 1.7 #include "tools.h"
40    
41 pajs 1.14 #ifdef SOLARIS
42     #include <libdevinfo.h>
43     #include <kstat.h>
44     #include <unistd.h>
45     #include <ctype.h>
46     #include <sys/types.h>
47     #include <sys/dkio.h>
48     #include <sys/stat.h>
49     #include <fcntl.h>
50     #include <sys/fcntl.h>
51     #include <dirent.h>
52     #endif
53    
54     #ifdef SOLARIS
55     struct map{
56     char *bsd;
57     char *svr;
58    
59     struct map *next;
60     };
61     typedef struct map mapping_t;
62    
63     static mapping_t *mapping = NULL;
64    
65     char *get_svr_from_bsd(char *bsd){
66     mapping_t *map_ptr;
67     for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
68     if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
69    
70     return bsd;
71     }
72    
73     void add_mapping(char *bsd, char *svr){
74     mapping_t *map_ptr;
75     mapping_t *map_end_ptr;
76    
77     bsd = strdup(bsd);
78     svr = strdup(svr);
79    
80     if (mapping == NULL){
81     mapping = malloc(sizeof(mapping_t));
82     if (mapping == NULL) return;
83     map_ptr = mapping;
84     }else{
85     /* See if its already been added */
86     for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){
87     if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){
88     return;
89     }
90     map_end_ptr = map_ptr;
91     }
92    
93     /* We've reached end of list and not found the entry.. So we need to malloc
94     * new mapping_t
95     */
96     map_end_ptr->next = malloc(sizeof(mapping_t));
97     if (map_end_ptr->next == NULL) return;
98     map_ptr = map_end_ptr->next;
99     }
100    
101     map_ptr->next = NULL;
102     map_ptr->bsd = bsd;
103     map_ptr->svr = svr;
104    
105     return;
106     }
107    
108     char *read_dir(char *disk_path){
109     DIR *dirp;
110 tdb 1.15 struct dirent *dp;
111     struct stat stbuf;
112 pajs 1.14 char *svr_name;
113 tdb 1.15 char current_dir[MAXPATHLEN];
114     char file_name[MAXPATHLEN];
115     char temp_name[MAXPATHLEN];
116     char dir_dname[MAXPATHLEN];
117 pajs 1.14 char *dsk_dir;
118     int x;
119    
120     dsk_dir = "/dev/osa/dev/dsk";
121 tdb 1.15 strncpy(current_dir, dsk_dir, sizeof current_dir);
122     if ((dirp = opendir(current_dir)) == NULL){
123 pajs 1.14 dsk_dir = "/dev/dsk";
124     snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
125     if ((dirp = opendir(current_dir)) == NULL){
126     return NULL;
127     }
128     }
129    
130 tdb 1.15 while ((dp = readdir(dirp)) != NULL){
131 pajs 1.14 snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
132     snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
133     stat(dir_dname,&stbuf);
134    
135     if (S_ISBLK(stbuf.st_mode)){
136     x = readlink(dir_dname, file_name, sizeof(file_name));
137     file_name[x] = '\0';
138     if (strcmp(file_name, temp_name) == 0) {
139     svr_name = strdup(dp->d_name);
140     closedir(dirp);
141     return svr_name;
142     }
143     }
144     }
145 tdb 1.16 closedir(dirp);
146 pajs 1.14 return NULL;
147     }
148    
149    
150    
151     int get_alias(char *alias){
152     char file[MAXPATHLEN];
153     di_node_t root_node;
154     di_node_t node;
155     di_minor_t minor = DI_MINOR_NIL;
156     char tmpnode[MAXPATHLEN];
157     char *phys_path;
158     char *minor_name;
159     char *value;
160     int instance;
161     if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
162 pajs 1.17 return 1;
163 pajs 1.14 }
164     node = di_drv_first_node(alias, root_node);
165     while (node != DI_NODE_NIL) {
166     if ((minor = di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
167     instance = di_instance(node);
168     phys_path = di_devfs_path(node);
169     minor_name = di_minor_name(minor);
170     strcpy(tmpnode, alias);
171     sprintf(tmpnode, "%s%d", tmpnode, instance);
172 pajs 1.17 strlcpy(file, "/devices", sizeof file);
173     strlcat(file, phys_path, sizeof file);
174     strlcat(file, ":", sizeof file);
175     strlcat(file, minor_name, sizeof file);
176 pajs 1.14 value = read_dir(file);
177     if (value != NULL){
178     add_mapping(tmpnode, value);
179     }
180     di_devfs_path_free(phys_path);
181     node = di_drv_next_node(node);
182     }else{
183     node = di_drv_next_node(node);
184     }
185 tdb 1.15 }
186 pajs 1.14 di_fini(root_node);
187 pajs 1.17 return 0;
188 pajs 1.14 }
189    
190 pajs 1.17 int build_mapping(){
191 pajs 1.14 char device_name[512];
192     int x;
193 tdb 1.15 kstat_ctl_t *kc;
194     kstat_t *ksp;
195     kstat_io_t kios;
196 pajs 1.14
197     if ((kc = kstat_open()) == NULL) {
198 tdb 1.16 return;
199 tdb 1.15 }
200 pajs 1.14
201 tdb 1.15 for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
202     if (!strcmp(ksp->ks_class, "disk")) {
203     if(ksp->ks_type != KSTAT_TYPE_IO) continue;
204     /* We dont want metadevices appearing as num_diskio */
205     if(strcmp(ksp->ks_module, "md")==0) continue;
206     if((kstat_read(kc, ksp, &kios))==-1) continue;
207 pajs 1.14 strncpy(device_name, ksp->ks_name, sizeof device_name);
208     for(x=0;x<(sizeof device_name);x++){
209     if( isdigit((int)device_name[x]) ) break;
210     }
211     if(x == sizeof device_name) x--;
212     device_name[x] = '\0';
213 pajs 1.17 if((get_alias(device_name)) != 0){
214     return 1;
215     }
216 tdb 1.15 }
217 pajs 1.14 }
218 tdb 1.15
219 pajs 1.17 return 0;
220 pajs 1.14 }
221    
222     #endif
223    
224    
225    
226 pajs 1.1 char *f_read_line(FILE *f, const char *string){
227     /* Max line length. 8k should be more than enough */
228     static char line[8192];
229    
230 pajs 1.2 while((fgets(line, sizeof(line), f))!=NULL){
231 pajs 1.1 if(strncmp(string, line, strlen(string))==0){
232     return line;
233     }
234     }
235    
236     return NULL;
237 pajs 1.3 }
238    
239     char *get_string_match(char *line, regmatch_t *match){
240     int len=match->rm_eo - match->rm_so;
241     char *match_string=malloc(len+1);
242    
243     match_string=strncpy(match_string, line+match->rm_so, len);
244     match_string[len]='\0';
245    
246     return match_string;
247     }
248 ats 1.7
249 pajs 1.14
250    
251 ats 1.13 #ifndef HAVE_ATOLL
252     static long long atoll(const char *s) {
253     long long value = 0;
254     int isneg = 0;
255    
256     while (*s == ' ' || *s == '\t') {
257     s++;
258     }
259     if (*s == '-') {
260     isneg = 1;
261     s++;
262     }
263     while (*s >= '0' && *s <= '9') {
264     value = (10 * value) + (*s - '0');
265     s++;
266     }
267     return (isneg ? -value : value);
268     }
269     #endif
270    
271 pajs 1.3 long long get_ll_match(char *line, regmatch_t *match){
272     char *ptr;
273     long long num;
274    
275     ptr=line+match->rm_so;
276     num=atoll(ptr);
277    
278     return num;
279 ats 1.8 }
280    
281 ats 1.9 #ifdef ALLBSD
282 ats 1.8 kvm_t *get_kvm() {
283     static kvm_t *kvmd = NULL;
284    
285     if (kvmd != NULL) {
286     return kvmd;
287     }
288    
289     kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
290     return kvmd;
291 pajs 1.1 }
292 pajs 1.4 #endif
293 ats 1.9
294     #ifdef NETBSD
295     struct uvmexp *get_uvmexp() {
296     static u_long addr = 0;
297     static struct uvmexp uvm;
298     kvm_t *kvmd = get_kvm();
299    
300     if (kvmd == NULL) {
301     return NULL;
302     }
303    
304     if (addr == 0) {
305     struct nlist symbols[] = {
306     { "_uvmexp" },
307     { NULL }
308     };
309    
310     if (kvm_nlist(kvmd, symbols) != 0) {
311     return NULL;
312     }
313     addr = symbols[0].n_value;
314     }
315    
316     if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
317     return NULL;
318     }
319     return &uvm;
320     }
321     #endif
322    
323 pajs 1.10 int statgrab_init(){
324 pajs 1.11 #ifdef ALLBSD
325     {
326     kvm_t *kvmd = get_kvm();
327 tdb 1.15 if (kvmd == NULL) return 1;
328 pajs 1.11 }
329 pajs 1.10 #endif
330     #ifdef NETBSD
331 pajs 1.11 {
332     struct uvmexp *uvm = get_uvmexp();
333     if (uvm == NULL) return 1;
334     }
335 pajs 1.14 #endif
336     #ifdef SOLARIS
337 pajs 1.17 if((build_mapping()) != 0) return 1;
338 pajs 1.10 #endif
339     return 0;
340     }
341 ats 1.18
342     int statgrab_drop_privileges() {
343     if (setegid(getgid()) != 0) return -1;
344     if (seteuid(getuid()) != 0) return -1;
345     return 0;
346     }
347