pascal OSStatus filedsn_setdir_clicked (EventHandlerCallRef inHandlerRef, EventRef inEvent, void *inUserData) { TDSNCHOOSER *choose_t = (TDSNCHOOSER *) inUserData; char msg[4096]; if (!choose_t) return noErr; /* confirm setting a directory */ snprintf (msg, sizeof (msg), "Are you sure that you want to make '%s' the default file DSN directory?", choose_t->curr_dir); if (!create_confirm (choose_t->mainwnd, NULL, msg)) return noErr; /* write FileDSNPath value */ if (!SQLWritePrivateProfileString ("ODBC", "FileDSNPath", choose_t->curr_dir, "odbcinst.ini")) { create_error (choose_t->mainwnd, NULL, "Error setting default file DSN directory", NULL); return noErr; } return noErr; }
pascal OSStatus filedsn_remove_clicked (EventHandlerCallRef inHandlerRef, EventRef inEvent, void *inUserData) { TDSNCHOOSER *choose_t = (TDSNCHOOSER *) inUserData; DataBrowserItemID first, last; OSStatus err; if (!choose_t) return noErr; /* Retrieve the DSN name */ if ((err = GetDataBrowserSelectionAnchor (choose_t->fdsnlist, &first, &last)) == noErr) { if (first > DBITEM_ID && first <= DBITEM_ID + FDSN_nrows) { char str[1024]; char *path; if (FDSN_type[first - DBITEM_ID - 1] == 0) return noErr; /* Get the DSN */ CFStringGetCString(FDSN_array[first - DBITEM_ID - 1], str, sizeof(str), kCFStringEncodingUTF8); asprintf (&path, "%s/%s", choose_t->curr_dir, str); if (path) { if (create_confirm (choose_t->mainwnd, path, "Are you sure you want to remove this File DSN ?")) { /* Call the right function */ if (unlink(path) < 0) { create_error (choose_t->mainwnd, NULL, "Error removing file DSN:", strerror (errno)); } } free(path); addfdsns_to_list (choose_t, choose_t->curr_dir, true); } } } if ((err = GetDataBrowserSelectionAnchor (choose_t->fdsnlist, &first, &last)) == noErr) { if (!first && !last) { DeactivateControl (choose_t->fremove); DeactivateControl (choose_t->fconfigure); DeactivateControl (choose_t->ftest); } } return noErr; }
BOOL INSTAPI ConfigDSN ( HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes) { char *dsn = NULL, *connstr = NULL, *curr, *cour = NULL; char dsnread[4096] = { 0 }; char prov[4096] = { 0 }; int driver_type = -1, flags = 0; BOOL retcode = FALSE; UWORD confMode = ODBC_USER_DSN; /* Map the request User/System */ if (fRequest < ODBC_ADD_DSN || fRequest > ODBC_REMOVE_DSN) { SQLPostInstallerError (ODBC_ERROR_INVALID_REQUEST_TYPE, NULL); goto done; } if (!lpszDriver || !STRLEN (lpszDriver)) { SQLPostInstallerError (ODBC_ERROR_INVALID_NAME, NULL); goto done; } /* Retrieve the config mode */ SQLGetConfigMode (&confMode); /* Retrieve the DSN if one exist */ for (curr = (LPSTR) lpszAttributes; curr && *curr; curr += (STRLEN (curr) + 1)) { if (!strncmp (curr, "DSN=", STRLEN ("DSN="))) { dsn = curr + STRLEN ("DSN="); break; } } /* Retrieve the corresponding driver */ if (strstr (lpszDriver, "OpenLink") || strstr (lpszDriver, "Openlink") || strstr (lpszDriver, "oplodbc")) { driver_type = 0; for (curr = (LPSTR) lpszAttributes, cour = prov; curr && *curr; curr += (STRLEN (curr) + 1), cour += (STRLEN (cour) + 1)) { if (!strncasecmp (curr, "Host=", STRLEN ("Host=")) && STRLEN (curr + STRLEN ("Host="))) { STRCPY (cour, curr); flags |= 0x1; continue; } if (!strncasecmp (curr, "ServerType=", STRLEN ("ServerType=")) && STRLEN (curr + STRLEN ("ServerType="))) { STRCPY (cour, curr); flags |= 0x2; continue; } STRCPY (cour, curr); } if (cour && !(flags & 1)) { STRCPY (cour, "Host=localhost\0"); cour += (STRLEN (cour) + 1); } if (cour && !(flags & 2)) { STRCPY (cour, "ServerType=Proxy\0"); cour += (STRLEN (cour) + 1); } if (cour) *cour = 0; } else if ((strstr (lpszDriver, "Virtuoso") || strstr (lpszDriver, "virtodbc"))) driver_type = 1; /* For each request */ switch (fRequest) { case ODBC_ADD_DSN: /* Check if the DSN with this name already exists */ SQLSetConfigMode (confMode); #ifdef WIN32 if (hwndParent && dsn && SQLGetPrivateProfileString ("ODBC 32 bit Data Sources", dsn, "", dsnread, sizeof (dsnread), NULL) && !create_confirm (hwndParent, dsn, "Are you sure you want to overwrite this DSN ?")) #else if (hwndParent && dsn && SQLGetPrivateProfileString ("ODBC Data Sources", dsn, "", dsnread, sizeof (dsnread), NULL) && !create_confirm (hwndParent, dsn, "Are you sure you want to overwrite this DSN ?")) #endif goto done; /* Call the right setup function */ connstr = create_gensetup (hwndParent, dsn, STRLEN (prov) ? prov : lpszAttributes, TRUE); /* Check output parameters */ if (!connstr) { SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL); goto done; } if (connstr == (LPSTR) - 1L) goto done; /* Add the DSN to the ODBC Data Sources */ SQLSetConfigMode (confMode); if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver)) goto done; /* Add each keyword and values */ for (curr = connstr; *curr; curr += (STRLEN (curr) + 1)) { if (strncmp (curr, "DSN=", STRLEN ("DSN="))) { STRCPY (dsnread, curr); cour = strchr (dsnread, '='); if (cour) *cour = 0; SQLSetConfigMode (confMode); if (!SQLWritePrivateProfileString (dsn, dsnread, (cour && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL)) goto done; } } break; case ODBC_CONFIG_DSN: if (!dsn || !STRLEN (dsn)) { SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL); goto done; } /* Call the right setup function */ connstr = create_gensetup (hwndParent, dsn, STRLEN (prov) ? prov : lpszAttributes, FALSE); /* Check output parameters */ if (!connstr) { SQLPostInstallerError (ODBC_ERROR_OUT_OF_MEM, NULL); goto done; } if (connstr == (LPSTR) - 1L) goto done; /* Compare if the DSN changed */ if (strcmp (connstr + STRLEN ("DSN="), dsn)) { /* Remove the previous DSN */ SQLSetConfigMode (confMode); if (!SQLRemoveDSNFromIni (dsn)) goto done; /* Add the new DSN section */ SQLSetConfigMode (confMode); if (!SQLWriteDSNToIni (dsn = connstr + STRLEN ("DSN="), lpszDriver)) goto done; } /* Add each keyword and values */ for (curr = connstr; *curr; curr += (STRLEN (curr) + 1)) { if (strncmp (curr, "DSN=", STRLEN ("DSN="))) { STRCPY (dsnread, curr); cour = strchr (dsnread, '='); if (cour) *cour = 0; SQLSetConfigMode (confMode); if (!SQLWritePrivateProfileString (dsn, dsnread, (cour && STRLEN (cour + 1)) ? cour + 1 : NULL, NULL)) goto done; } } break; case ODBC_REMOVE_DSN: if (!dsn || !STRLEN (dsn)) { SQLPostInstallerError (ODBC_ERROR_INVALID_KEYWORD_VALUE, NULL); goto done; } /* Just remove the DSN */ SQLSetConfigMode (confMode); if (!SQLRemoveDSNFromIni (dsn)) goto done; break; }; quit: retcode = TRUE; done: if (connstr && connstr != (LPSTR) - 1L && connstr != lpszAttributes && connstr != prov) free (connstr); return retcode; }
BOOL INSTAPI ConfigDriver ( HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszArgs, LPSTR lpszMsg, WORD cbMsgMax, WORD * pcbMsgOut) { char *curr, *cour; char driverread[4096] = { 0 }; BOOL retcode = FALSE; UWORD confMode = ODBC_USER_DSN; /* Map the request User/System */ if (fRequest < ODBC_INSTALL_DRIVER || fRequest > ODBC_CONFIG_DRIVER_MAX) { SQLPostInstallerError (ODBC_ERROR_INVALID_REQUEST_TYPE, NULL); goto done; } if (!lpszDriver || !STRLEN (lpszDriver)) { SQLPostInstallerError (ODBC_ERROR_INVALID_NAME, NULL); goto done; } /* Retrieve the config mode */ SQLGetConfigMode (&confMode); /* Treat corresponding to the request */ switch (fRequest) { case ODBC_INSTALL_DRIVER: /* Check if the DRIVER with this name already exists */ SQLSetConfigMode (confMode); #ifdef WIN32 if (hwndParent && SQLGetPrivateProfileString ("ODBC 32 bit Drivers", lpszDriver, "", driverread, sizeof (driverread), "odbcinst.ini") && !create_confirm (hwndParent, NULL, "Are you sure you want to overwrite this driver ?")) #else # ifdef _MACX if (hwndParent && SQLGetPrivateProfileString ("ODBC Drivers", lpszDriver, "", driverread, sizeof (driverread), "odbcinst.ini") && !create_confirm (hwndParent, NULL, "Are you sure you want to overwrite this driver ?")) # else if (hwndParent && SQLGetPrivateProfileString ("ODBC Drivers", lpszDriver, "", driverread, sizeof (driverread), "odbcinst.ini") && !create_confirm (hwndParent, NULL, "Are you sure you want to overwrite this driver ?")) # endif #endif { SQLPostInstallerError (ODBC_ERROR_DRIVER_SPECIFIC, "Driver already installed previously."); goto done; } /* Add the Driver to the ODBC Drivers */ SQLSetConfigMode (confMode); if (!SQLInstallDriverEx (lpszArgs, NULL, driverread, sizeof (driverread), NULL, ODBC_INSTALL_COMPLETE, NULL)) { SQLPostInstallerError (ODBC_ERROR_DRIVER_SPECIFIC, "Could not add the driver information."); goto done; } break; case ODBC_CONFIG_DRIVER: if (!lpszArgs || !STRLEN (lpszArgs)) { SQLPostInstallerError (ODBC_ERROR_DRIVER_SPECIFIC, "No enough parameters for configururation."); goto done; } /* Add each keyword and values */ for (curr = (LPSTR) lpszArgs; *curr; curr += (STRLEN (curr) + 1)) { STRCPY (driverread, curr); cour = strchr (driverread, '='); if (cour) *cour = 0; SQLSetConfigMode (confMode); if (!SQLWritePrivateProfileString (lpszDriver, driverread, (cour && STRLEN (cour + 1)) ? cour + 1 : NULL, "odbcinst.ini")) goto done; } break; case ODBC_REMOVE_DRIVER: /* Remove the Driver to the ODBC Drivers */ SQLSetConfigMode (confMode); if (!SQLRemoveDriver (lpszDriver, TRUE, NULL)) { SQLPostInstallerError (ODBC_ERROR_DRIVER_SPECIFIC, "Could not remove driver information."); goto done; } break; default: SQLPostInstallerError (ODBC_ERROR_REQUEST_FAILED, NULL); goto done; }; quit: retcode = TRUE; done: if (pcbMsgOut) *pcbMsgOut = 0; return retcode; }
static void filedsn_configure (TDSNCHOOSER *choose_t, char *drv, char *dsn, char *in_attrs, BOOL b_add, BOOL verify_conn) { char *connstr = NULL; size_t len; /* current connstr len */ size_t add_len; /* len of appended string */ LPSTR attrs = NULL, curr, tmp, attr_lst = NULL; BOOL b_Save = TRUE; attrs = in_attrs; if (!b_add && !_CheckDriverLoginDlg(drv + STRLEN("DRIVER="))) { /* Get DSN name and additional attributes */ attr_lst = create_gensetup (choose_t->mainwnd, dsn, in_attrs, b_add, &verify_conn); attrs = attr_lst; } if (!attrs) { create_error (choose_t->mainwnd, NULL, "Error adding File DSN:", strerror (ENOMEM)); return; } if (attrs == (LPSTR) - 1L) return; /* Build the connection string */ connstr = strdup (drv); len = strlen (connstr); for (curr = attrs; *curr; curr += (STRLEN (curr) + 1)) { if (!strncasecmp (curr, "DSN=", STRLEN ("DSN="))) { if (dsn == NULL) { /* got dsn name */ dsn = curr + STRLEN ("DSN="); } continue; } /* append attr */ add_len = 1 + strlen (curr); /* +1 for ';' */ tmp = realloc (connstr, len + add_len + 1); /* +1 for NUL */ if (tmp == NULL) { create_error (choose_t->mainwnd, NULL, "Error adding File DSN:", strerror (errno)); goto done; } connstr = tmp; snprintf (connstr + len, add_len + 1, ";%s", curr); len += add_len; } /* Nothing to do if no DSN */ if (!dsn || STRLEN (dsn) == 0) goto done; if (verify_conn) { BOOL ret; /* Append SAVEFILE */ add_len = strlen (";SAVEFILE=") + strlen (dsn); tmp = realloc (connstr, len + add_len + 1); /* +1 for NUL */ if (tmp == NULL) { create_error (choose_t->mainwnd, NULL, "Error adding file DSN:", strerror (errno)); goto done; } connstr = tmp; snprintf (connstr + len, add_len + 1, ";SAVEFILE=%s", dsn); len += add_len; /* Connect to data source */ ret = test_driver_connect (choose_t, connstr); if (!ret && b_add) { if (create_confirm (choose_t->mainwnd, dsn, "Can't check the connection. Do you want to store the FileDSN without verification ?")) b_Save = TRUE; else b_Save = FALSE; } else b_Save = FALSE; } if (b_Save) { char key[512]; char *p; size_t sz; if (drv) { p = strchr(drv, '='); if (!SQLWriteFileDSN (dsn, "ODBC", "DRIVER", p + 1)) { create_error (choose_t->mainwnd, NULL, "Error adding File DSN:", strerror (errno)); goto done; } } for (curr = attrs; *curr; curr += (STRLEN (curr) + 1)) { if (!strncasecmp (curr, "DSN=", STRLEN ("DSN="))) continue; else if (!strncasecmp (curr, "PWD=", STRLEN ("PWD="))) continue; else if (!strncasecmp (curr, "SAVEFILE=", STRLEN ("SAVEFILE="))) continue; else if (!strncasecmp (curr, "FILEDSN=", STRLEN ("FILEDSN="))) continue; p = strchr(curr, '='); sz = p - curr < sizeof(key) ? p - curr : sizeof(key); memset(key, 0, sizeof(key)); strncpy(key, curr, sz); if (!SQLWriteFileDSN (dsn, "ODBC", key, p + 1)) { create_error (choose_t->mainwnd, NULL, "Error adding File DSN:", strerror (errno)); goto done; } } } done: if (attr_lst != NULL) free (attr_lst); if (connstr != NULL) free (connstr); }