BOOL SQLInstallDriverManager(	LPSTR	pszPath,
								WORD	nPathMax,
								WORD	*pnPathOut )
{
    char  szIniName[ INI_MAX_OBJECT_NAME + 1 ];
	char  b1[ ODBC_FILENAME_MAX + 1 ];

    inst_logClear();

	/* SANITY CHECKS */
	if ( pszPath == NULL || nPathMax < 2 )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return 0;
	}

    sprintf( szIniName, "%s", odbcinst_system_file_path( b1 ) );

	/* DO SOMETHING */
	strncpy( pszPath, szIniName, nPathMax );
	if ( pnPathOut != NULL )
		*pnPathOut = strlen( pszPath );

	return TRUE;
}
Example #2
0
void CODBCCreate::reject()
{
	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_USER_CANCELED, "" );
	ret_code = false;

#ifdef QT_V4LAYOUT
	Q3Wizard::reject();
#else
	QWizard::reject();
#endif
}
BOOL SQLRemoveDriverManager(	LPDWORD	pnUsageCount )
{
    inst_logClear();

	if ( pnUsageCount == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}

	*pnUsageCount = 1;

	return TRUE;
}
Example #4
0
void CODBCCreate::accept()
{
	if ( fds->isOn())
	{
		QString conn_str;
		char out_str[ 4095 ];
		const char *in_str;
		SQLHENV henv;
		SQLHDBC hdbc;
		SQLSMALLINT len;
		SQLRETURN ret;
		QString fname = file_edit->text();

		// Make sure it ends with .dsn
	
		if ( fname.right( 4 ).lower().compare( ".dsn" ))
		{
			fname.append( ".dsn" );
			file_edit->setText( fname );
		}

		conn_str = "DRIVER={" + current_driver + "};SAVEFILE=" + fname + ";";

		if ( extra_keywords.length() > 0 )
		{
			int start = 0;
			int end = 0;

			while( start < extra_keywords.length() )
			{
				end = extra_keywords.find( '\n', start );

				if ( end == -1 )
				{
					end = extra_keywords.length();
				}

				conn_str += extra_keywords.mid( start, end-start ) + ";";
				start = end + 1;
			}
		}
		in_str = (const char*)conn_str;

		if ( verify )
		{
			SQLAllocEnv( &henv );
			SQLAllocConnect( henv, &hdbc );

			ret = SQLDriverConnect( hdbc, (SQLHWND)1, 
					(SQLCHAR*)in_str, strlen( in_str ), (SQLCHAR*)out_str, 
					sizeof( out_str ), &len, SQL_DRIVER_COMPLETE );
	
			SQLFreeConnect( hdbc );
			SQLFreeEnv( henv );

			if ( ret != SQL_SUCCESS ) 
			{
				int create = QMessageBox::No;

				create = QMessageBox::information( NULL, "Create Data Source", "A connection could not be made using the file data source parameters entered. Save non-verified file DSN?", QMessageBox::Yes, QMessageBox::No );
				if ( create == QMessageBox::No )
				{
					inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_CREATE_DSN_FAILED, "" );
					ret_code = false;
#ifdef QT_V4LAYOUT
					Q3Wizard::reject();
#else
					QWizard::reject();
#endif
					return;
				}
				else 
				{
					strcpy( out_str, in_str );
					if ( !createDsn())
					{
						ret_code = false;
#ifdef QT_V4LAYOUT
						Q3Wizard::reject();
#else
						QWizard::reject();
#endif
						return;
					}
				}
			}
		}
		else
		{
			strcpy( out_str, in_str );
			if ( !createDsn())
			{
				ret_code = false;
#ifdef QT_V4LAYOUT
				Q3Wizard::reject();
#else
				QWizard::reject();
#endif
				return;
			}
		}
	
		ret_code = true;
	}
	else
	{
		int mode;

		if ( sds -> isOn())
		{
			mode = ODBC_ADD_SYS_DSN;
		}
		else
		{
			mode = ODBC_ADD_DSN;
		}

		if ( dsn.length() > 0 )
		{
			ret_code = SQLConfigDataSource((HWND) 1, mode, current_driver, dsn.prepend( "DSN=" ));
		}
		else
		{
			ret_code = SQLConfigDataSource((HWND) 1, mode, current_driver, "" );
		}
	}

#ifdef QT_V4LAYOUT
	Q3Wizard::accept();
#else
	QWizard::accept();
#endif
}
Example #5
0
bool CODBCCreate::createDsn()
{
	BOOL ret;
	QString fname = file_edit->text();

	// Make sure it ends with .dsn
	
	if ( fname.right( 4 ).lower().compare( ".dsn" ))
	{
		fname.append( ".dsn" );
	}

	if ( access( fname, F_OK ) == 0 )
	{
		int replace = QMessageBox::information( NULL, "Save File DSN", "Data source file exists. Overwrite?", QMessageBox::Yes, QMessageBox::No );
		if ( replace == QMessageBox::No )
		{
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_CREATE_DSN_FAILED, "" );
			return false;
		}
	}

	if ( unlink( fname ))
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_CREATE_DSN_FAILED, "" );
		return false;
	}

	ret = SQLWriteFileDSN((const char*) fname,
			"ODBC",
			"DRIVER",
			(const char *)current_driver );

	if ( !ret )
	{
		return false;
	}

	if ( extra_keywords.length() > 0 )
	{
		int start = 0;
		int end = 0;
		int eq;
		QString str;

		while( start < extra_keywords.length() )
		{
			end = extra_keywords.find( '\n', start );

			if ( end == -1 )
			{
				end = extra_keywords.length();
			}

			str = extra_keywords.mid( start, end-start ) + ";";
			eq = str.find( '=', 0 );
			if ( eq > 0 )
			{
				ret = SQLWriteFileDSN((const char*) fname,
							"ODBC",
							(const char *) str.left( eq ),
							(const char *) str.right( str.length() - eq - 1 ));
				if ( !ret )
				{
					return false;
				}
			}
			start = end + 1;
		}
	}

	return true;
}
Example #6
0
BOOL SQLWriteFileDSN(			LPCSTR	pszFileName,
								LPCSTR	pszAppName,
								LPCSTR	pszKeyName,
								LPCSTR	pszString )
{
	HINI	hIni;
	char	szFileName[ODBC_FILENAME_MAX+1];

	if ( pszFileName[0] == '/' )
	{
		strcpy( szFileName, pszFileName );
	}
	else
	{	
		char szPath[ODBC_FILENAME_MAX+1];
		*szPath = '\0';
		_odbcinst_FileINI( szPath );
		sprintf( szFileName, "%s/%s", szPath, pszFileName );
	}

    if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
    {
        strcat( szFileName, ".dsn" );
    }

#ifdef __OS2__
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
#endif
	{
       	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" );
		return FALSE;
	}

	/* delete section */
	if ( pszString == NULL && pszKeyName == NULL )
	{
		if ( iniObjectSeek( hIni, (char *)pszAppName ) == INI_SUCCESS )
        {
			iniObjectDelete( hIni );
        }
	}
	/* delete entry */
	else if	( pszString == NULL )
	{
		if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
        {
			iniPropertyDelete( hIni );
        }
	}
	else
	{
		/* add section */
		if ( iniObjectSeek( hIni, (char *)pszAppName ) != INI_SUCCESS )
        {
			iniObjectInsert( hIni, (char *)pszAppName );
        }
		/* update entry */
		if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
		{
			iniObjectSeek( hIni, (char *)pszAppName );
			iniPropertyUpdate( hIni, (char *)pszKeyName, (char *)pszString );
		}
		/* add entry */
		else
		{
			iniObjectSeek( hIni, (char *)pszAppName );
			iniPropertyInsert( hIni, (char *)pszKeyName, (char *)pszString );
		}
	}

	if ( iniCommit( hIni ) != INI_SUCCESS )
	{
		iniClose( hIni );
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}

	iniClose( hIni );

	return TRUE;
}
BOOL SQLGetInstalledDrivers(	LPSTR	pszBuf,
								WORD	nBufMax,
								WORD	*pnBufOut )
{
	HINI	hIni;
	WORD	nBufPos		= 0;
	WORD	nToCopySize	= 0;
	char	szObjectName[INI_MAX_OBJECT_NAME+1];
    char    szIniName[ INI_MAX_OBJECT_NAME + 1 ];
	char 	b1[ 256 ], b2[ 256 ];

    inst_logClear();

#ifdef VMS
    sprintf( szIniName, "%s:%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) );
#else
    sprintf( szIniName, "%s/%d", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) );
#endif

#ifdef __OS2__
	if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', TRUE, 1L ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
#endif
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
		return FALSE;
	}
	
	memset( pszBuf, '\0', nBufMax );

	iniObjectFirst( hIni );
	while ( iniObjectEOL( hIni ) == FALSE )
	{
		iniObject( hIni, szObjectName );
		if ( strcmp( szObjectName, "ODBC" ) == 0 )
		{
		    iniObjectNext( hIni );
		    continue;
		}

		if ( (strlen( szObjectName )+1) > (nBufMax - nBufPos) )
		{
			nToCopySize = nBufMax - nBufPos;
			strncpy( &(pszBuf[nBufPos]), szObjectName, nToCopySize );
			nBufPos = nBufMax;
			break;
		}
		else
		{
			strcpy( &(pszBuf[nBufPos]), szObjectName );
			nBufPos += strlen( szObjectName )+1;
		}
		iniObjectNext( hIni );
	}
	iniClose( hIni );

	if ( pnBufOut )
		*pnBufOut = nBufPos-1;
	
	return TRUE;
}
BOOL SQLCreateDataSource(		HWND	hWnd,
								LPCSTR	pszDS )
{
	BOOL	nReturn;
	void 	*hDLL;
	BOOL	(*pSQLCreateDataSource)(HWND, LPCSTR);
	char 	*p;
	char    szGUILibFile[FILENAME_MAX];
	int		found;

	if ( !hWnd )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_HWND, "" );
		return FALSE;
	}

    /*
     * initialize libtool
     */

    lt_dlinit();

    /*
     * DETERMINE PATH FOR GUI PLUGIN
     *
     */

    /*
     * first look in the environment
     */

	p  = getenv( "ODBCINSTQ" );
	if ( p )
	{
		strcpy( szGUILibFile, p );
	}
	else
	{
		SQLGetPrivateProfileString( "ODBC", "ODBCINSTQ", "", szGUILibFile, sizeof( szGUILibFile ), "odbcinst.ini" );

		if ( strlen( szGUILibFile ) == 0 )
		{
			/*
			 * we need to find the extension to use as well
			 */

			get_lib_file( szGUILibFile, NULL );

			if ( lt_dladdsearchdir( DEFLIB_PATH ) )
			{
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, 
						ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
			}
		}
	}

    /*
     * USE libtool TO LOAD PLUGIN AND CALL FUNCTION
     */

    nReturn = FALSE;
	found = FALSE;
    hDLL = lt_dlopen( szGUILibFile );
	if ( hDLL )
	{
        /* change the name, as it avoids it finding it in the calling lib */
		pSQLCreateDataSource = (BOOL (*)(HWND, LPSTR))lt_dlsym( hDLL, "QTSQLCreateDataSources" );
		if ( pSQLCreateDataSource )
			nReturn = pSQLCreateDataSource(NULL,pszDS);
		else
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );

		found = TRUE;
	}
	else
   	{
        /*
         * try after adding the explicit path
         */

		get_lib_file( szGUILibFile, DEFLIB_PATH );
        hDLL = lt_dlopen( szGUILibFile );
        if ( hDLL )
        {
            /* change the name, as it avoids it finding it in the calling lib */
            pSQLCreateDataSource = (BOOL (*)(HWND))lt_dlsym( hDLL, "QTSQLCreateDataSources" );
            if ( pSQLCreateDataSource )
                nReturn = pSQLCreateDataSource(NULL,pszDS);
            else
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );

			found = TRUE;
        }
    }

	if ( !found )
	{
		/* TRY TO PASS THROUGH TO GUI SHADOW LIB */
		if ( (hDLL = lt_dlopen( "libodbcinstG.so" ))  )
		{
			pSQLCreateDataSource = (BOOL(*)(HWND,LPSTR))lt_dlsym( hDLL, "SQLCreateDataSource" );
			if ( pSQLCreateDataSource )
				nReturn = pSQLCreateDataSource( NULL,pszDS );
			else
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );

			found = TRUE;
		}
	}

	if ( !found )
	{
		inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
	}

	return nReturn;
}
int SQLGetPrivateProfileString( LPCSTR  pszSection,
                                LPCSTR  pszEntry,
                                LPCSTR  pszDefault,
                                LPSTR   pRetBuffer,
                                int     nRetBuffer,
                                LPCSTR  pszFileName
                              )
{
    HINI    hIni;
    int     nBufPos         = 0;
    char    szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szFileName[ODBC_FILENAME_MAX+1];
    UWORD   nConfigMode;
    int     ini_done = 0;
    int     ret;

    inst_logClear();

    if ( check_ini_cache( &ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName ))
    {
        return ret;
    }

    /* SANITY CHECKS */
    if ( pRetBuffer == NULL || nRetBuffer < 2 )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return -1;
    }
    if ( pszSection != NULL && pszEntry != NULL && pszDefault == NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "need default value - try empty string" );
        return -1;
    }

    *pRetBuffer = '\0';

    /*****************************************************
     * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO GET ODBCINST INFO SO...
     *****************************************************/
    if ( pszFileName != NULL )
    {
        if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) )
        {
            ret = _SQLGetInstalledDrivers(  pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer );

            if ( ret == -1 )
            {
                /* try to use any default provided */
                if ( pRetBuffer && nRetBuffer > 0 )
                {
                    if ( pszDefault )
                    {
                        strncpy( pRetBuffer, pszDefault, nRetBuffer );
                        pRetBuffer[ nRetBuffer - 1 ] = '\0';
                    }
                }
            }
            else
            {
                save_ini_cache( ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName );
            }

            return ret;
        }
    }

    /*****************************************************
     * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni
     *****************************************************/
    if ( pszFileName != 0 && pszFileName[0] == '/' )
    {
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*)pszFileName, "#;", '[', ']', '=', TRUE, 1L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*)pszFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
            return -1;
        }
    }
    else
    {
        nConfigMode     = __get_config_mode();
        nBufPos         = 0;
        szFileName[0]   = '\0';

        switch ( nConfigMode )
        {
        case ODBC_BOTH_DSN:
            if ( _odbcinst_UserINI( szFileName, TRUE ))
            {
#ifdef __OS2__
                if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE, 1L )
                     == INI_SUCCESS )
#else
                if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE )
                     == INI_SUCCESS )
#endif
                {
                    ini_done = 1;
                }
            }
            _odbcinst_SystemINI( szFileName, TRUE );
            if ( !ini_done )
            {
#ifdef __OS2__
                if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                     != INI_SUCCESS )
#else
                if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                     != INI_SUCCESS )
#endif
                {
                    inst_logPushMsg( __FILE__, __FILE__, __LINE__,
                                     LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                    return -1;
                }
            }
            else
            {
                iniAppend( hIni, szFileName );
            }
            break;

        case ODBC_USER_DSN:
            _odbcinst_UserINI( szFileName, TRUE );
#ifdef __OS2__
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                 != INI_SUCCESS )
#else
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                 != INI_SUCCESS )
#endif
            {
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                                 ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                return -1;
            }
            break;

        case ODBC_SYSTEM_DSN:
            _odbcinst_SystemINI( szFileName, TRUE );
#ifdef __OS2__
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                 != INI_SUCCESS )
#else
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                 != INI_SUCCESS )
#endif
            {
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                                 ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                return -1;
            }
            break;

        default:
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_GENERAL_ERR, "Invalid Config Mode" );
            return -1;
        }
    }

    /*****************************************************
     * EXTRACT SECTIONS
     *****************************************************/
    if ( pszSection == NULL )
    {
        _odbcinst_GetSections( hIni, pRetBuffer, nRetBuffer, &nBufPos );
    }
    /*****************************************************
     * EXTRACT ENTRIES
     *****************************************************/
    else if ( pszEntry == NULL )
    {
        _odbcinst_GetEntries( hIni, pszSection, pRetBuffer, nRetBuffer, &nBufPos );
    }
    /*****************************************************
     * EXTRACT AN ENTRY
     *****************************************************/
    else
    {
        if ( pszSection == NULL || pszEntry == NULL || pszDefault == NULL )
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
            return -1;
        }

        /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
        if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) != INI_SUCCESS )
        {
            /*
             * (NG) this seems to be ignoring the length of pRetBuffer !!!
             */
            /* strncpy( pRetBuffer, pszDefault, INI_MAX_PROPERTY_VALUE ); */
            if ( pRetBuffer && nRetBuffer > 0 && pszDefault )
            {
                strncpy( pRetBuffer, pszDefault, nRetBuffer );
                pRetBuffer[ nRetBuffer - 1 ] = '\0';
            }
        }
        else
        {
            iniValue( hIni, szValue );
	    if ( pRetBuffer ) 
	    {
	        strncpy( pRetBuffer, szValue, nRetBuffer );
	        pRetBuffer[ nRetBuffer - 1 ] = '\0';
	    }
            nBufPos = strlen( szValue );
        }
    }

    iniClose( hIni );

    ret = strlen( pRetBuffer );

    save_ini_cache( ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName );

    return ret;
}
BOOL SQLWritePrivateProfileString(
								LPCSTR	pszSection,
								LPCSTR	pszEntry,
								LPCSTR	pszString,
								LPCSTR	pszFileName )
{
	HINI	hIni;
	char	szFileName[ODBC_FILENAME_MAX+1];

        inst_logClear();

	/* SANITY CHECKS */
	if ( pszSection == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}
	if ( pszSection[0] == '\0' )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}
	if ( pszFileName == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}

	/*****************************************************
	 * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO WRITE ODBCINST INFO SO...
	 *****************************************************/
	if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) )
		return _SQLWriteInstalledDrivers( pszSection, pszEntry, pszString );

	if ( pszFileName[0] == '/' )
	{
		strcpy( szFileName, pszFileName );
	}
	else
	{	
		if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE )
		{
        	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
			return FALSE;
		}
	}
#ifdef __OS2__
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L  ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE  ) != INI_SUCCESS )
#endif
	{
       	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}

	/* delete section */
	if ( pszEntry == NULL )
	{
		if ( iniObjectSeek( hIni, (char *)pszSection ) == INI_SUCCESS )
			iniObjectDelete( hIni );
	}
	/* delete entry */
	else if	( pszString == NULL )
	{
		if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS )
        {
			iniPropertyDelete( hIni );
        }
	}
	else
	{
		/* add section */
		if ( iniObjectSeek( hIni, (char *)pszSection ) != INI_SUCCESS )
			iniObjectInsert( hIni, (char *)pszSection );
		/* update entry */
		if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS )
		{
			iniObjectSeek( hIni, (char *)pszSection );
            /*
             * Get the correct property to update
             */
		    iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" );
			iniPropertyUpdate( hIni, (char *)pszEntry, (char *)pszString );
		}
		/* add entry */
		else
		{
			iniObjectSeek( hIni, (char *)pszSection );
			iniPropertyInsert( hIni, (char *)pszEntry, (char *)pszString );
		}

	}

	if ( iniCommit( hIni ) != INI_SUCCESS )
	{
		iniClose( hIni );
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}

	iniClose( hIni );

	return TRUE;
}
Example #11
0
BOOL SQLReadFileDSN(            LPCSTR  pszFileName,
                                LPCSTR  pszAppName,
                                LPCSTR  pszKeyName,
                                LPSTR   pszString,
                                WORD    nString,
                                WORD    *pnString )
{
    HINI    hIni;
    int     nBufPos         = 0;
    char    szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szFileName[ODBC_FILENAME_MAX+1];

    inst_logClear();

    /* SANITY CHECKS */
    if ( pszString == NULL || nString < 1  )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_BUFF_LEN, "" );
        return FALSE;
    }
    if ( pszFileName == NULL && pszAppName == NULL && pszKeyName == NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return FALSE;
    }
    if ( pszAppName == NULL && pszKeyName != NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_REQUEST_TYPE, "" );
        return FALSE;
    }
    if ( pszFileName && strlen( pszFileName ) > ODBC_FILENAME_MAX ) {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" );
        return FALSE;
    }

    *pszString = '\0';

    /*****************************************************
     * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni
     *****************************************************/
    if ( pszFileName && pszFileName[0] == '/' )
    {
        strcpy( szFileName, pszFileName );
        if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
        {
            strcat( szFileName, ".dsn" );
        }

/* on OS/2 the file DSN is a text file */
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE, 0L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*)szFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_INVALID_PATH, "" );

            return FALSE;
        }
    }
    else if ( pszFileName )
    {
        char szPath[ODBC_FILENAME_MAX+1];
        *szPath = '\0';
        _odbcinst_FileINI( szPath );
        sprintf( szFileName, "%s/%s", szPath, pszFileName );

        if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
        {
            strcat( szFileName, ".dsn" );
        }

/* on OS/2 the file DSN is a text file */
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE, 0L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_INVALID_PATH, "" );

            return FALSE;
        }
    }

    if ( pszAppName == NULL && pszKeyName == NULL )
    {
        GetSections( hIni, pszString, nString );
    }
    else if ( pszAppName != NULL && pszKeyName == NULL )
    {
        GetEntries( hIni, pszAppName, pszString, nString );
    }
    else
    {
        /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
        if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) != INI_SUCCESS )
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_REQUEST_FAILED, "" );

            return FALSE;
        }
        else
        {
            iniValue( hIni, szValue );
            strncpy( pszString, szValue, nString );
            pszString[ nString - 1 ] = '\0';
            nBufPos = strlen( szValue );
        }
    }

    if ( pszFileName )
    {
        iniClose( hIni );
    }

    if ( pnString )
    {
        *pnString = strlen( pszString );
    }

    return TRUE;
}
Example #12
0
BOOL SQLWriteDSNToIni(			LPCSTR	pszDSN,
								LPCSTR	pszDriver )
{
	HINI	hIni;
	char	szFileName[ODBC_FILENAME_MAX+1];

	SQLRemoveDSNFromIni( pszDSN );

	/* SANITY CHECKS */
	if ( pszDSN == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}
	if ( pszDSN[0] == '\0' )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}
	if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver == NULL ) )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" );
		return FALSE;
	}
	if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver[0] == '\0') )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" );
		return FALSE;
	}
	if ( SQLValidDSN( pszDSN ) == FALSE )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_DSN, "" );
		return FALSE;
	}

	/* OK */
	if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}
#ifdef __OS2__
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L  ) != INI_SUCCESS )
#else
	if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE  ) != INI_SUCCESS )
#endif
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}
	iniObjectInsert( hIni, (char *)pszDSN );
	if ( pszDriver != NULL )
	{
		iniPropertyInsert( hIni, "Driver", (char *)pszDriver );
	}
	if ( iniCommit( hIni ) != INI_SUCCESS )
	{
		iniClose( hIni );
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
		return FALSE;
	}

	iniClose( hIni );

	return TRUE;
}
/*! 
 * \brief   UI to manage most ODBC system information.
 * 
 *          This calls into the UI plugin library to do our work for us. The caller can provide
 *          the name (base name) of the library or let us determine which library to use.
 *          See \sa _getUIPluginName for details on how the choice is made.
 *          
 * \param   hWnd    Input. Parent window handle. This is HWND as per the ODBC
 *                  specification but in unixODBC we use a generic window
 *                  handle. Caller must cast a HODBCINSTWND to HWND at call. 
 * 
 * \return  BOOL
 *
 * \sa      ODBCINSTWND
 */
BOOL SQLManageDataSources( HWND hWnd )
{
    HODBCINSTWND    hODBCInstWnd    = (HODBCINSTWND)hWnd;
    char            szName[FILENAME_MAX];
    char            szNameAndExtension[FILENAME_MAX];
    char            szPathAndName[FILENAME_MAX];
	void *          hDLL;
	BOOL	        (*pSQLManageDataSources)(HWND);

    inst_logClear();

    /* ODBC specification states that hWnd is mandatory. */
	if ( !hWnd )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_HWND, "No hWnd" );
		return FALSE;
	}

    /* initialize libtool */
    if ( lt_dlinit() )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "lt_dlinit() failed" );
		return FALSE;
    }

    /* get plugin name */
    _appendUIPluginExtension( szNameAndExtension, _getUIPluginName( szName, hODBCInstWnd->szUI ) );

    /* lets try loading the plugin using an implicit path */
    hDLL = lt_dlopen( szNameAndExtension );
    if ( hDLL )
    {
        /* change the name (SQLManageDataSources to ODBCManageDataSources) to prevent us from calling ourself */
        pSQLManageDataSources = (BOOL (*)(HWND))lt_dlsym( hDLL, "ODBCManageDataSources" );
        if ( pSQLManageDataSources )
            return pSQLManageDataSources( ( *(hODBCInstWnd->szUI) ? hODBCInstWnd->hWnd : NULL ) );
        else
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
    }
    else
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_WARNING, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
        /* try with explicit path */
        _prependUIPluginPath( szPathAndName, szNameAndExtension );
        hDLL = lt_dlopen( szPathAndName );
        if ( hDLL )
        {
            /* change the name (SQLManageDataSources to ODBCManageDataSources) to prevent us from calling ourself   */
            /* its only safe to use hWnd if szUI was specified by the caller                                        */
            pSQLManageDataSources = (BOOL (*)(HWND))lt_dlsym( hDLL, "ODBCManageDataSources" );
            if ( pSQLManageDataSources )
                return pSQLManageDataSources( ( *(hODBCInstWnd->szUI) ? hODBCInstWnd->hWnd : NULL ) );
            else
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
        }
        else
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, (char*)lt_dlerror() );
    }

    /* report failure to caller */
    inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "Failed to load/use a UI plugin." );

    return FALSE;
}
Example #14
0
BOOL SQLRemoveDriver(			LPCSTR	pszDriver,
								BOOL	nRemoveDSN,
								LPDWORD	pnUsageCount )
{
	HINI	hODBCInstIni;
	char	szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szIniName[ INI_MAX_OBJECT_NAME + 1 ];
	char	b1[ 256 ], b2[ 256 ];

    inst_logClear();

	/* SANITY CHECKS */
	if ( pszDriver == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" );
		return FALSE;
	}
	if ( pszDriver[0] == '\0' )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" );
		return FALSE;
	}
	if ( nRemoveDSN != TRUE && nRemoveDSN != FALSE )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return FALSE;
	}

	(*pnUsageCount) = 0;

#ifdef VMS
    sprintf( szIniName, "%s:%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) );
#else
    sprintf( szIniName, "%s/%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) );
#endif

	/* PROCESS ODBC INST INI FILE */
#ifdef __OS2__	
	if ( iniOpen( &hODBCInstIni, szIniName, "#;", '[', ']', '=', TRUE, 1L ) != INI_SUCCESS )
#else
	if ( iniOpen( &hODBCInstIni, szIniName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
#endif
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
		return FALSE;
	}

	/* LETS GET ITS FILE USAGE VALUE (if any) */
	if ( iniPropertySeek( hODBCInstIni, (char *)pszDriver, "UsageCount", "" ) == INI_SUCCESS )
	{
		iniValue( hODBCInstIni, szValue );
        (*pnUsageCount) = atoi( szValue );
	}

	/* DOES THE OBJECT ALREADY EXIST? (also ensures that we have correct current object) */	
	if ( iniObjectSeek( hODBCInstIni, (char *)pszDriver ) == INI_SUCCESS )
	{
        if ( (*pnUsageCount) == 0 )
			(*pnUsageCount) = 1;

		(*pnUsageCount)--;
		if ( (*pnUsageCount) == 0 )
		{
			iniObjectDelete( hODBCInstIni );
			if ( nRemoveDSN )
			{
				/***********************************
				 * TO DO
				 ***********************************/
			}
		}
		else
		{
			if ( iniPropertySeek( hODBCInstIni, (char *)pszDriver, "UsageCount", "" ) == INI_SUCCESS )
			{
				sprintf( szValue, "%ld", (long int)(*pnUsageCount) );
				iniPropertyUpdate( hODBCInstIni, "UsageCount", szValue );
			}
			else
			{
				iniPropertyInsert( hODBCInstIni, "UsageCount", szValue );
			}
		}
		if ( iniCommit( hODBCInstIni ) != INI_SUCCESS )
		{
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
			iniClose( hODBCInstIni );
			return FALSE;
		}
	}


	iniClose( hODBCInstIni );

	return TRUE;
}
int _SQLGetInstalledDrivers(    LPCSTR  pszSection,
                                LPCSTR  pszEntry,
                                LPCSTR  pszDefault,
                                LPCSTR  pRetBuffer,
                                int     nRetBuffer )
{
    HINI    hIni;
    int     nBufPos         = 0;
    int     nStrToCopy;
    char    szObjectName[INI_MAX_OBJECT_NAME+1];
    char    szPropertyName[INI_MAX_PROPERTY_NAME+1];
    char    szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szIniName[ INI_MAX_OBJECT_NAME + 1 ];
    char    *ptr;
    char    b1[ 256 ], b2[ 256 ];

    /* SANITY CHECKS */
    if ( pRetBuffer == NULL || nRetBuffer < 2 )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return -1;
    }

    /*
     * first try in the system odbcinst.ini
     */

#ifdef VMS
    sprintf( szIniName, "%s:%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ));
#else
    sprintf( szIniName, "%s/%s", odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ));
#endif

    /* PROCESS ODBC INI FILE */
#ifdef __OS2__
    if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', 1, 1L ) != INI_SUCCESS )
#else
    if ( iniOpen( &hIni, szIniName, "#;", '[', ']', '=', 1 ) != INI_SUCCESS )
#endif
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
        return -1;
    }

    /*
     * now try the user odbcinst.ini if it exists
     */

#ifdef VMS
    sprintf( szIniName, "%s:%s", odbcinst_user_file_path( b1 ), odbcinst_user_file_name( b2 ));
#else
    sprintf( szIniName, "%s/%s", odbcinst_user_file_path( b1 ), odbcinst_user_file_name( b2 ));
#endif

    /* PROCESS .ODBCINST INI FILE */
    iniAppend( hIni, szIniName );

    nBufPos = 0;
    if ( pszSection == NULL )
    {
        ptr = (char*) pRetBuffer;
        *ptr = '\0';

        /* JUST COLLECT SECTION NAMES */

        for( iniObjectFirst( hIni ); iniObjectEOL( hIni ) != TRUE; iniObjectNext( hIni ))
        {
            iniObject( hIni, szObjectName );

            if ( strcasecmp( szObjectName, "ODBC" ) == 0 )
            {
                continue;
            }
            else if ( nBufPos + 1 + strlen( szObjectName ) >= nRetBuffer )
            {
                break;
            }
            else
            {
                strcpy( ptr, szObjectName );
                ptr += strlen( ptr ) + 1;
                nBufPos += strlen( szObjectName ) + 1;
            }
        }

        /*
         * Add final NULL
         */

        if ( nBufPos == 0 )
        {
            ptr ++;
        }

        *ptr = '\0';
    }
    else if ( pszEntry == NULL )
    {
        ptr = (char*) pRetBuffer;
        *ptr = '\0';

        iniObjectSeek( hIni, (char *)pszSection );

        /* COLLECT ALL ENTRIES FOR THE GIVEN SECTION */

        for( iniPropertyFirst( hIni ); iniPropertyEOL( hIni ) != TRUE; iniPropertyNext( hIni ))
        {
            iniProperty( hIni, szPropertyName );

            if ( nBufPos + 1 + strlen( szPropertyName ) >= nRetBuffer )
            {
                break;
            }
            else
            {
                strcpy( ptr, szPropertyName );
                ptr += strlen( ptr ) + 1;
                nBufPos += strlen( szPropertyName ) + 1;
            }
        }

        /*
         * Add final NULL
         */

        if ( nBufPos == 0 )
        {
            ptr ++;
        }
    }
    else
    {
        /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
        if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) != INI_SUCCESS )
        {
            /* try to use any default provided */
            if ( pRetBuffer && nRetBuffer > 0 )
            {
                if ( pszDefault )
                {
                    strncpy( (char *)pRetBuffer, pszDefault, nRetBuffer );
                    ((char*)pRetBuffer)[ nRetBuffer - 1 ] = '\0';
                }
            }
        }
        else
        {
            iniValue( hIni, szValue );
            nStrToCopy = strlen( szValue ) + 1;                 /* factor NULL terminator for string */
            if ( nBufPos + nStrToCopy + 1 > nRetBuffer )        /* factor NULL terminator for buffer */
                nStrToCopy = nRetBuffer - nBufPos - 2;
            strncpy( (char *)&(pRetBuffer[nBufPos]), szValue, nStrToCopy );
            nBufPos += nStrToCopy;
			/*
			 * length doesn't include NULL
			 */
			nBufPos--;
        }
    }

    /* CLOSE */
    iniClose( hIni );

    return nBufPos;
}