| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* This program is free software; you can redistribute it and/or
|
| 3 |
|
|
* modify it under the terms of the GNU General Public License
|
| 4 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 5 |
|
|
* of the License, or (at your option) any later version.
|
| 6 |
|
|
*
|
| 7 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 8 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 9 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 10 |
|
|
* GNU General Public License for more details.
|
| 11 |
|
|
*
|
| 12 |
|
|
* You should have received a copy of the GNU General Public License
|
| 13 |
|
|
* along with this program; if not, write to the Free Software
|
| 14 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 15 |
|
|
*/
|
| 16 |
|
|
#define COMMAND_GROUPS_INTERNAL_ACCESS
|
| 17 |
|
|
#include "common/setup_before.h"
|
| 18 |
|
|
#include <math.h>
|
| 19 |
|
|
#include <stdio.h>
|
| 20 |
|
|
#ifdef HAVE_STDDEF_H
|
| 21 |
|
|
# include <stddef.h>
|
| 22 |
|
|
#else
|
| 23 |
|
|
# ifndef NULL
|
| 24 |
|
|
# define NULL ((void *)0)
|
| 25 |
|
|
# endif
|
| 26 |
|
|
#endif
|
| 27 |
|
|
#ifdef STDC_HEADERS
|
| 28 |
|
|
# include <stdlib.h>
|
| 29 |
|
|
#else
|
| 30 |
|
|
# ifdef HAVE_MALLOC_H
|
| 31 |
|
|
# include <malloc.h>
|
| 32 |
|
|
# endif
|
| 33 |
|
|
#endif
|
| 34 |
|
|
#ifdef HAVE_STRING_H
|
| 35 |
|
|
# include <string.h>
|
| 36 |
|
|
#else
|
| 37 |
|
|
# ifdef HAVE_STRINGS_H
|
| 38 |
|
|
# include <strings.h>
|
| 39 |
|
|
# endif
|
| 40 |
|
|
#endif
|
| 41 |
|
|
#include <errno.h>
|
| 42 |
|
|
#include "common/eventlog.h"
|
| 43 |
|
|
#include "common/list.h"
|
| 44 |
|
|
#include "compat/strerror.h"
|
| 45 |
|
|
#include "prefs.h"
|
| 46 |
|
|
#include "common/util.h"
|
| 47 |
|
|
#include "common/xalloc.h"
|
| 48 |
|
|
#include "command_groups.h"
|
| 49 |
|
|
#include "common/setup_after.h"
|
| 50 |
|
|
|
| 51 |
|
|
//#define COMMANDGROUPSDEBUG 1
|
| 52 |
|
|
|
| 53 |
|
|
static t_list * command_groups_head=NULL;
|
| 54 |
|
|
static FILE * fp = NULL;
|
| 55 |
|
|
|
| 56 |
|
|
extern int command_groups_load(char const * filename)
|
| 57 |
|
|
{
|
| 58 |
|
|
unsigned int line;
|
| 59 |
|
|
unsigned int pos;
|
| 60 |
|
|
char * buff;
|
| 61 |
|
|
char * temp;
|
| 62 |
|
|
char const * command;
|
| 63 |
|
|
unsigned int group;
|
| 64 |
|
|
t_command_groups * entry;
|
| 65 |
|
|
|
| 66 |
|
|
if (!filename) {
|
| 67 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got NULL filename");
|
| 68 |
|
|
return -1;
|
| 69 |
|
|
}
|
| 70 |
|
|
if (!(fp = fopen(filename,"r"))) {
|
| 71 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for reading (fopen: %s)",filename,pstrerror(errno));
|
| 72 |
|
|
return -1;
|
| 73 |
|
|
}
|
| 74 |
|
|
|
| 75 |
|
|
command_groups_head = list_create();
|
| 76 |
|
|
|
| 77 |
|
|
for (line=1; (buff = file_get_line(fp)); line++) {
|
| 78 |
|
|
for (pos=0; buff[pos]=='\t' || buff[pos]==' '; pos++);
|
| 79 |
|
|
if (buff[pos]=='\0' || buff[pos]=='#') {
|
| 80 |
|
|
continue;
|
| 81 |
|
|
}
|
| 82 |
|
|
if ((temp = strrchr(buff,'#'))) {
|
| 83 |
|
|
unsigned int len;
|
| 84 |
|
|
unsigned int endpos;
|
| 85 |
|
|
|
| 86 |
|
|
*temp = '\0';
|
| 87 |
|
|
len = strlen(buff)+1;
|
| 88 |
|
|
for (endpos=len-1; buff[endpos]=='\t' || buff[endpos]==' '; endpos--);
|
| 89 |
|
|
buff[endpos+1] = '\0';
|
| 90 |
|
|
}
|
| 91 |
|
|
if (!(temp = strtok(buff," \t"))) { /* strtok modifies the string it is passed */
|
| 92 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"missing group on line %u of file \"%s\"",line,filename);
|
| 93 |
|
|
continue;
|
| 94 |
|
|
}
|
| 95 |
|
|
if (str_to_uint(temp,&group)<0) {
|
| 96 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"group '%s' not a valid group (1-8)",temp);
|
| 97 |
|
|
continue;
|
| 98 |
|
|
}
|
| 99 |
|
|
if (group == 0 || group > 8) {
|
| 100 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"group '%u' not within groups limits (1-8)",group);
|
| 101 |
|
|
continue;
|
| 102 |
|
|
}
|
| 103 |
|
|
while ((command = strtok(NULL," \t"))) {
|
| 104 |
|
|
entry = xmalloc(sizeof(t_command_groups));
|
| 105 |
|
|
entry->group = 1 << (group-1);
|
| 106 |
|
|
entry->command = xstrdup(command);
|
| 107 |
|
|
list_append_data(command_groups_head,entry);
|
| 108 |
|
|
#ifdef COMMANDGROUPSDEBUG
|
| 109 |
|
|
eventlog(eventlog_level_info,__FUNCTION__,"Added command: %s - with group %u",entry->command,entry->group);
|
| 110 |
|
|
#endif
|
| 111 |
|
|
}
|
| 112 |
|
|
}
|
| 113 |
|
|
file_get_line(NULL); // clear file_get_line buffer
|
| 114 |
|
|
fclose(fp);
|
| 115 |
|
|
return 0;
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
extern int command_groups_unload(void)
|
| 119 |
|
|
{
|
| 120 |
|
|
t_elem * curr;
|
| 121 |
|
|
t_command_groups * entry;
|
| 122 |
|
|
|
| 123 |
|
|
if (command_groups_head) {
|
| 124 |
|
|
LIST_TRAVERSE(command_groups_head,curr) {
|
| 125 |
|
|
if (!(entry = elem_get_data(curr)))
|
| 126 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
|
| 127 |
|
|
else {
|
| 128 |
|
|
xfree(entry->command);
|
| 129 |
|
|
xfree(entry);
|
| 130 |
|
|
}
|
| 131 |
|
|
list_remove_elem(command_groups_head,&curr);
|
| 132 |
|
|
}
|
| 133 |
|
|
list_destroy(command_groups_head);
|
| 134 |
|
|
command_groups_head = NULL;
|
| 135 |
|
|
}
|
| 136 |
|
|
return 0;
|
| 137 |
|
|
}
|
| 138 |
|
|
|
| 139 |
|
|
extern unsigned int command_get_group(char const * command)
|
| 140 |
|
|
{
|
| 141 |
|
|
t_elem const * curr;
|
| 142 |
|
|
t_command_groups * entry;
|
| 143 |
|
|
|
| 144 |
|
|
if (command_groups_head) {
|
| 145 |
|
|
LIST_TRAVERSE(command_groups_head,curr) {
|
| 146 |
|
|
if (!(entry = elem_get_data(curr)))
|
| 147 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"found NULL entry in list");
|
| 148 |
|
|
else if (!(strcmp(entry->command,command)))
|
| 149 |
|
|
return entry->group;
|
| 150 |
|
|
}
|
| 151 |
|
|
}
|
| 152 |
|
|
return 0;
|
| 153 |
|
|
}
|
| 154 |
|
|
|
| 155 |
|
|
extern int command_groups_reload(char const * filename)
|
| 156 |
|
|
{
|
| 157 |
|
|
command_groups_unload();
|
| 158 |
|
|
return command_groups_load(filename);
|
| 159 |
|
|
}
|