Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iusql fails if the database password contains a semicolon #113

Closed
hughmcmaster opened this issue Jul 14, 2022 · 10 comments · Fixed by #126
Closed

iusql fails if the database password contains a semicolon #113

hughmcmaster opened this issue Jul 14, 2022 · 10 comments · Fixed by #126

Comments

@hughmcmaster
Copy link
Contributor

A Debian user reported a bug against iusql in the unixodbc package. It seems iusql is not escaping the semicolon character when used in connection strings.

The semicolon is usually a delimeter in connection strings.

I expect other special characters will trigger similar behaviour as noted in the bug report (copied below).

I was trying to connect to an MSSQL database using unixodbc/iusql with
the FreeTDS driver. The password for the login was randomly generated
and contained a semicolon ";" in it. This worked fine when using the
FreeTDS tools tsql and fisql. However, the iusql tool from unixodbc
failed as follows:

$ iusql myDSN myLogin 'Y&MI%4VtL?C@OuUwmWkksL;+!#V$JSo6' -v
[FreeTDS][SQL Server]Unable to connect to data source
[FreeTDS][SQL Server]Login failed for user 'myLogin'.
[ISQL]ERROR: Could not SQLDriverConnect

Changing the password in the database worked around the issue, but I
guess iusql needs to do better escaping of special characters in the
password.

@lurcher
Copy link
Owner

lurcher commented Oct 11, 2022 via email

@TallTed
Copy link

TallTed commented Oct 12, 2022

@lurcher — I researched this a while ago for a similar issue, and can't quickly locate the answer I wrote about it at the time, but this StackOverflow answer on the same topic may provide you enough info to quickly patch iusql and any other samples that use SQLDriverConnect() with a simply constructed connection string. TL;DR: Reserved characters in DSN/UID/PWD values in connection string are escaped by wrapping each value (usually only PWD includes such reserved characters, but values without reserved characters may also be safely wrapped, so there's almost no reason to test before wrapping) in braces and doubling any internal close-brace (the one reason you might test before wrapping, but easier to always double this char within the wrapper than to individually address all the special cases).

@lurcher
Copy link
Owner

lurcher commented Oct 12, 2022 via email

@TallTed
Copy link

TallTed commented Oct 15, 2022

@lurcher — I found the results of my prior research, which I think makes plain the fully documented (though possibly hard to understand) solution and links to the relevant Microsoft ODBC docs.

@lurcher
Copy link
Owner

lurcher commented Oct 15, 2022 via email

@v-chojas
Copy link
Contributor

It is probably better to add the quoting only if there are special characters that need it.

@lurcher
Copy link
Owner

lurcher commented Oct 17, 2022 via email

@lurcher
Copy link
Owner

lurcher commented Oct 24, 2022 via email

@hughmcmaster
Copy link
Contributor Author

hughmcmaster commented Dec 6, 2022

Thanks for checking in that commit, @lurcher.

I spent some time debugging this issue further, using MS SQL and the FreeTDS ODBC driver to replicate the original system.

From my testing, I learnt that any password containing a semicolon must be enclosed in braces and must have a closing semicolon appended. This applies whether using a single connection string (as per your commit) or the standard space-separated command-line arguments.

For example, the following are equivalent:

iusql myDSN user '{some;Password123};'
iusql 'DSN=myDSN;UID=user;PWD={some;Password123};'

With braces and trailing semicolon, iusql works correctly with the password reported in the original post.

Suggestions:

  • Do some basic string parsing on the password. We could try to fix the password by adding braces and the closing semicolon. Or we could abort and warn that they need to use the format above. Whichever way we go, I think adding a trailing semicolon should be done on the iusql side, since extra semicolons don't seem to matter. Let me know if you want me to draft a patch.
  • Update iusql documentation to mention the format for this case.

@lurcher
Copy link
Owner

lurcher commented Dec 6, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants