#ifndef KNETD_SUID_H_
#define KNETD_SUID_H_

#define ERROR_CONFIG_OPEN	(1)
#define ERROR_CONFIG_READ	(2)
#define ERROR_INVALID_PARAMETER	(3)
#define ERROR_ACCESS		(4)

#define USERS_LENGTH (1024)
/* in theory maximum path length could be up to 4kb tho */
#define PATH_LENGTH (512)

/**
 * This is the configuration file's structer.
 * We'll read to and write from a binary configuration file.
 * Since this setuid tool will be called many times in second
 * it should be extremely fast. As of this writing it adds like
 * 0.001 seconds (at most) on my box. With a C++ replacement
 * using KDE libs (KProcess) it takes like 0.060ms :/
 */
struct Config
{
	char warning[512];
	unsigned short int allow_all;
	char users[USERS_LENGTH];
	char ifconfig[PATH_LENGTH];
	char iwconfig[PATH_LENGTH];
	char ethtool[PATH_LENGTH];
	char miitool[PATH_LENGTH];
	char ip[PATH_LENGTH];
	char dhcp[PATH_LENGTH];
};

/**
 * Checks if the real user id is on the allowed users list
 */
int has_access( char* user_list );
int is_valid_device( const char* dev );
int is_valid_ip( const char* ip );

/**
 * Reads the config file if it exists, otherwise falls back
 * to sane values.
 */
void read_config( const char* );

/**
 * All parse_* familiy functions parses the command for one
 * specific action. The knetdsuid is supposed to be run like:
 * $ knetdsuid ACTION [PARAMETERS...]
 * Where action is ifconfig, iwconfig, ip, ...
 */
void parse_ifconfig( int, char** );
void parse_iwconfig( int, char** );
void parse_ip( int, char** );
void parse_ethtool( int, char** );
void parse_miitool( int, char** );
void parse_dhcp( int, char** );

/**
 * exec_ACTION_* methods are restricted command execution routines.
 * For instance, exec_iwconfig_enc will only run "enc" parameter
 * on device dev.
 */
void exec_iwconfig_dev( char** argv );
void exec_iwconfig_enc( char** argv );

void exec_ifconfig_dev( char** argv );

void exec_ethtool_dev( char** argv );

void exec_miitool_dev( char** argv );

void exec_ip_add( char** argv );

void exec_dhcp_dev( char** dev );

/**
 * This function gets root access and executes the given program
 * with given parameters.
 */
void root_exec( const char* path, char *const argv[] );

#endif
