#include "mysql.h" #include "strlcpy.h" #include #include #ifdef HAVE_LIBMYSQLCLIENT extern int enable_mysql; // database information char *DATABASE = NULL; // database name char *HOSTNAME = NULL; // database hostname char *USERNAME = NULL; // username for database char *PASSWORD = NULL; // password for database char *TABLE = NULL; // table name that holds the usernames // // passwd struct isn't really reentrant safe, we'll need to emulate the same expected behavior // the user won't be expected to free it. #define M_USER 32 #define M_PASS 14 #define M_GECOS 128 #define M_DIR 128 #define M_SHELL 64 struct passwd stPass; struct spwd stShadow; char pw_name[M_USER+1]; char pw_passwd[M_PASS+1]; char pw_gecos[M_GECOS+1]; char pw_dir[M_DIR+1]; char pw_shell[M_SHELL+1]; char sp_namp[M_USER+1]; char sp_pwdp[M_PASS+1]; int MSimpleStatement( MYSQL *m, const char *query, ... ) { va_list ap; char *pszQuery; int iRes; va_start( ap, query ); vasprintf( &pszQuery, query, ap ); va_end( ap ); if ( !pszQuery ) { syslog( LOG_ERR, "MSimpleStatement Failed to allocate mem" ); return( -1 ); } iRes = mysql_real_query( m, pszQuery, strlen( pszQuery ) ); if ( iRes != 0 ) { syslog( LOG_ERR, "MSimpleStatement [%s] Failed!", pszQuery ); } free( pszQuery ); return( iRes ); } MYSQL_RES *UserLookup( const char *username ) { char *pszQuery; MYSQL *m; MYSQL_RES *result; int iRes; m = mysql_init( 0 ); if ( !m ) { syslog( LOG_ERR, "MySql failed to initialize." ); return( NULL ); } if ( !mysql_real_connect( m, HOSTNAME, USERNAME, PASSWORD, DATABASE, 0, NULL, 0) ) { syslog( LOG_ERR, "Could not connect to database." ); mysql_close( m ); return( NULL ); } { /* prevent SQL injections */ int i, max = strlen(username); for (i=0;i