2010-01-09 19:51:25 +03:00
# include <stdio.h>
# include <simpleconfig.h>
2010-01-14 21:38:37 +03:00
# include <static_map.h>
2010-01-09 19:51:25 +03:00
# include <string.h>
# include <signal.h>
# include <stdlib.h>
# include <assert.h>
# include <stdio.h>
# include <list.h>
# include <debug.h>
2010-01-13 18:53:55 +03:00
# include "serial.h"
2010-01-14 23:00:43 +03:00
# include "uuid-test.h"
2010-01-13 18:53:55 +03:00
2010-01-09 19:51:25 +03:00
struct perm_entry {
list_head ( ) ;
char name [ 128 ] ;
} ;
struct perm_group {
list_head ( ) ;
2010-01-14 23:00:43 +03:00
struct perm_entry * uuids ;
struct perm_entry * ips ;
2010-01-09 19:51:25 +03:00
char name [ 128 ] ;
} ;
2010-01-14 21:38:37 +03:00
static void
static_map_cleanup ( void * * info )
2010-01-09 19:51:25 +03:00
{
struct perm_group * groups = ( struct perm_group * ) info ;
struct perm_group * group ;
struct perm_entry * entry ;
while ( groups ) {
group = groups ;
list_remove ( & groups , group ) ;
2010-01-14 23:00:43 +03:00
while ( group - > uuids ) {
entry = group - > uuids ;
list_remove ( & group - > uuids , entry ) ;
free ( entry ) ;
}
while ( group - > ips ) {
entry = group - > ips ;
list_remove ( & group - > ips , entry ) ;
2010-01-09 19:51:25 +03:00
free ( entry ) ;
}
free ( group ) ;
}
2010-01-14 21:38:37 +03:00
* info = NULL ;
2010-01-09 19:51:25 +03:00
}
2010-01-14 21:38:37 +03:00
static int
2010-01-09 19:51:25 +03:00
static_map_check ( void * info , const char * value1 , const char * value2 )
{
struct perm_group * groups = ( struct perm_group * ) info ;
struct perm_group * group ;
struct perm_entry * left , * tmp ;
2010-01-14 23:00:43 +03:00
int x , y , uuid = 0 ;
2010-01-09 19:51:25 +03:00
2010-01-13 17:26:32 +03:00
if ( ! info )
return 1 ; /* no maps == wide open */
2010-01-14 23:00:43 +03:00
uuid = is_uuid ( value1 ) ;
2010-01-09 19:51:25 +03:00
list_for ( & groups , group , x ) {
left = NULL ;
2010-01-14 23:00:43 +03:00
if ( uuid ) {
list_for ( & group - > uuids , tmp , y ) {
if ( ! strcasecmp ( tmp - > name , value1 ) ) {
left = tmp ;
break ;
}
}
} else {
list_for ( & group - > ips , tmp , y ) {
if ( ! strcasecmp ( tmp - > name , value1 ) ) {
left = tmp ;
break ;
}
2010-01-09 19:51:25 +03:00
}
}
if ( ! left )
continue ;
2010-01-14 23:00:43 +03:00
list_for ( & group - > uuids , tmp , y ) {
2010-01-09 19:51:25 +03:00
if ( ! strcasecmp ( tmp - > name , value2 ) ) {
return 1 ;
}
}
}
return 0 ;
}
2010-01-14 21:38:37 +03:00
static int
static_map_load ( void * config_ptr , void * * perm_info )
2010-01-09 19:51:25 +03:00
{
2010-01-14 21:38:37 +03:00
config_object_t * config = config_ptr ;
2010-01-09 19:51:25 +03:00
int group_idx = 0 ;
int entry_idx = 0 ;
int found ;
char value [ 128 ] ;
char buf [ 256 ] ;
char buf2 [ 512 ] ;
struct perm_group * group = NULL , * groups = NULL ;
struct perm_entry * entry = NULL ;
if ( ! perm_info )
return - 1 ;
do {
snprintf ( buf , sizeof ( buf ) - 1 , " groups/group[%d] " , + + group_idx ) ;
if ( sc_get ( config , buf , value , sizeof ( value ) ) ! = 0 ) {
2010-01-14 23:00:43 +03:00
snprintf ( buf2 , sizeof ( buf2 ) - 1 , " %s/@uuid " , buf ) ;
2010-01-09 19:51:25 +03:00
if ( sc_get ( config , buf2 , value , sizeof ( value ) ) ! = 0 ) {
2010-01-14 23:00:43 +03:00
snprintf ( buf2 , sizeof ( buf2 ) - 1 , " %s/@ip " , buf ) ;
if ( sc_get ( config , buf2 , value ,
sizeof ( value ) ) ! = 0 ) {
break ;
}
2010-01-09 19:51:25 +03:00
}
2010-01-14 23:00:43 +03:00
snprintf ( value , sizeof ( value ) , " unnamed-%d " ,
group_idx ) ;
2010-01-09 19:51:25 +03:00
}
group = malloc ( sizeof ( * group ) ) ;
assert ( group ) ;
memset ( group , 0 , sizeof ( * group ) ) ;
strncpy ( group - > name , value , sizeof ( group - > name ) ) ;
dbg_printf ( 3 , " Group: %s \n " , value ) ;
found = 0 ;
2010-01-14 23:00:43 +03:00
entry_idx = 0 ;
2010-01-09 19:51:25 +03:00
do {
2010-01-14 23:00:43 +03:00
snprintf ( buf2 , sizeof ( buf2 ) - 1 , " %s/@uuid[%d] " ,
buf , + + entry_idx ) ;
2010-01-09 19:51:25 +03:00
if ( sc_get ( config , buf2 , value , sizeof ( value ) ) ! = 0 ) {
break ;
}
+ + found ;
entry = malloc ( sizeof ( * entry ) ) ;
assert ( entry ) ;
memset ( entry , 0 , sizeof ( * entry ) ) ;
strncpy ( entry - > name , value , sizeof ( entry - > name ) ) ;
2010-01-14 23:00:43 +03:00
dbg_printf ( 3 , " - UUID Entry: %s \n " , value ) ;
2010-01-09 19:51:25 +03:00
2010-01-14 23:00:43 +03:00
list_insert ( & group - > uuids , entry ) ;
2010-01-09 19:51:25 +03:00
} while ( 1 ) ;
2010-01-14 23:00:43 +03:00
entry_idx = 0 ;
do {
snprintf ( buf2 , sizeof ( buf2 ) - 1 , " %s/@ip[%d] " ,
buf , + + entry_idx ) ;
if ( sc_get ( config , buf2 , value , sizeof ( value ) ) ! = 0 ) {
break ;
}
+ + found ;
entry = malloc ( sizeof ( * entry ) ) ;
assert ( entry ) ;
memset ( entry , 0 , sizeof ( * entry ) ) ;
strncpy ( entry - > name , value , sizeof ( entry - > name ) ) ;
dbg_printf ( 3 , " - IP Entry: %s \n " , value ) ;
list_insert ( & group - > ips , entry ) ;
} while ( 1 ) ;
2010-01-09 19:51:25 +03:00
if ( ! found )
free ( group ) ;
else
list_insert ( & groups , group ) ;
} while ( 1 ) ;
* perm_info = groups ;
return 0 ;
}
2010-01-14 21:38:37 +03:00
static const map_object_t static_map_obj = {
. load = static_map_load ,
. check = static_map_check ,
. cleanup = static_map_cleanup ,
. info = NULL
} ;
void *
map_init ( void )
{
map_object_t * o ;
o = malloc ( sizeof ( * o ) ) ;
if ( ! o )
return NULL ;
memset ( o , 0 , sizeof ( * o ) ) ;
memcpy ( o , & static_map_obj , sizeof ( * o ) ) ;
return ( void * ) o ;
}
void
map_release ( void * c )
{
map_object_t * o = ( map_object_t * ) c ;
static_map_cleanup ( & o - > info ) ;
free ( c ) ;
}