Debian Codegraph @

src/apps/osql

raw symbols SCIP
1 #! /bin/shscip-shell filesystem . . `/bin/sh`.
2 # $Id: osql,v 1.12 2011-05-11 03:08:13 jklowden Exp $
3 #
4 # Check odbc.ini, odbcinst, and, optionally, freetds.conf,
5 # then execute isql (assume it's unixODBC's isql).
6 #
7
8 USAGE="Syntax: $(basenamescip-shell system . . basename. "$0") -S server -U user -P password"
9
10 while getopts I:S:U:P: OPTION
11 do
12 case ${OPTION} in
13 I) OVER_DIR=${OPTARG} # override
14 ;;
15 S) DSN=${OPTARG}
16 ;;
17 U) USERNAME=${OPTARG}
18 ;;
19 P) PASSWORD=${OPTARG}
20 ;;
21 \?) echoscip-shell system . . echo. "$USAGE"
22 exit 1
23 ;;
24 esac
25 done
26
27 #cho ${DSN} ${USERNAME} ${PASSWORD}
28 if [scip-shell system . . `[`. -z "${DSN}" ] || [scip-shell system . . `[`. -z "${USERNAME}" ] || [scip-shell system . . `[`. -z "${PASSWORD}" ]
29 then
30 echoscip-shell system . . echo. "$USAGE"
31 exit 1
32 fi
33
34 ISQL=$(command -v isql)
35 if [scip-shell system . . `[`. -z "${ISQL}" ]
36 then
37 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: no \"isql\" command found. Is unixODBC installed?"
38 exit 1
39 fi
40
41 # Establish ODBC prefix directory
42
43 ISQL_DIR=$(stringsscip-shell system . . strings. "${ISQL}" | grepscip-shell system . . grep. ^/ | grepscip-shell system . . grep. -v elf | grepscip-shell system . . grep. -v '\.so\.' | headscip-shell system . . head. -1 | sedscip-shell system . . sed. 's/lib$/etc/' )
44
45 INI_DIRNAME="/tmp/$(basenamescip-shell system . . basename. "$0").$$"
46 exec 3> "${INI_DIRNAME}"
47
48 # Check the libraries, too.
49 if [scip-shell system . . `[`. -z "${OVER_DIR}" ]
50 then
51 libcmd='ldd "${ISQL}" | awk "/libodbc\./ {print \$3}"'
52 if ! command -V ldd >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 && command -V otool >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1
53 then
54 libcmd='otool -L "${ISQL}" | awk "/libodbc\./ {print \$1}"'
55 fi
56 echoscip-shell system . . echo. "checking shared odbc libraries linked to isql for default directories..."
57 (echoscip-shell system . . echo. ${ISQL_DIR}; eval "$libcmd") \
58 | while read L
59 do
60 stringsscip-shell system . . strings. "$L" | grepscip-shell system . . grep. '^/' | grepscip-shell system . . grep. -v '/libscip-shell filesystem . . `/lib`.' \
61 | while read D
62 do
63 if [scip-shell system . . `[`. ! -s "${INI_DIRNAME}" ]
64 then
65 printfscip-shell system . . printf. " trying %s ... " "$D"
66 if [scip-shell system . . `[`. -d "$D" ] && [scip-shell system . . `[`. -r "${D}/odbc.ini" ]
67 then
68 echoscip-shell system . . echo. "$D" >&3
69 printfscip-shell system . . printf. "OK"
70 else
71 printfscip-shell system . . printf. "no"
72 fi
73 printfscip-shell system . . printf. "\n"
74 fi
75 done
76 done
77 fi
78 exec 3>&-
79
80 ODBC_DIR=$(catscip-shell system . . cat. "${INI_DIRNAME}") && rmscip-shell system . . rm. "${INI_DIRNAME}"
81
82 if [scip-shell system . . `[`. -z "${ODBC_DIR}" ] && [scip-shell system . . `[`. -z "${OVER_DIR}" ]
83 then
84 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): problem: no potential directory strings in \"$(command -v isql)\""
85 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): advice: use \"osql -I DIR\" where DIR unixODBC\'s install prefix e.g. /usr/local"
86 echoscip-shell system . . echo. "isql strings are:"
87 stringsscip-shell system . . strings. "${ISQL}" | grepscip-shell system . . grep. ^/ | sedscip-shell system . . sed. 's/^/+ /'
88 #xit 1
89 fi
90
91 if [scip-shell system . . `[`. "${OVER_DIR}" ]
92 then
93 if [scip-shell system . . `[`. -d "${ODBC_DIR}" ]
94 then
95 echoscip-shell system . . echo. "\"${ODBC_DIR}\" is a directory, overridden by"
96 else
97 echoscip-shell system . . echo. "\"${ODBC_DIR}\" is NOT a directory, overridden by"
98 fi
99 echoscip-shell system . . echo. "\"${OVER_DIR}\"."
100
101 if [scip-shell system . . `[`. -d "${OVER_DIR}" ]
102 then
103 ODBC_DIR=${OVER_DIR}
104 else
105 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: \"${OVER_DIR}\" is not a directory"
106 exit 1
107 fi
108 fi
109
110 # Look for server entry in odbc.ini
111 echoscip-shell system . . echo. 'checking odbc.ini files'
112 for F in "${HOME}/.odbc.ini" "${ODBC_DIR}/odbc.ini"
113 do
114 if [scip-shell system . . `[`. ! -d "$(dirnamescip-shell system . . dirname. "$F")" ]
115 then
116 echoscip-shell system . . echo. "warning: $(dirnamescip-shell system . . dirname. "$F") is not a directory"
117 continue
118 fi
119
120 if [scip-shell system . . `[`. -r "$F" ]
121 then
122 echoscip-shell system . . echo. " reading $F"
123 else
124 echoscip-shell system . . echo. " cannot read \"$F\""
125 continue
126 fi
127
128 grepscip-shell system . . grep. -F "[${DSN}]" "$F" > /dev/nullscip-shell filesystem . . `/dev/null`.
129
130 if [scip-shell system . . `[`. $? -eq 0 ]
131 then
132 echoscip-shell system . . echo. "[${DSN}] found in $F"
133 ODBC_INI=$F
134 break
135 else
136 echoscip-shell system . . echo. "[${DSN}] not found in $F"
137 fi
138 done
139
140 if [scip-shell system . . `[`. -z "${ODBC_INI}" ]
141 then
142 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: unable to locate ${DSN} in any odbc.ini"
143 exit 1
144 fi
145
146 # Report finding of server entry
147 echoscip-shell system . . echo. found this section:
148 SED_CMD="/^\[${DSN}\]/,/^[[:space:]]*$/ { s/^/ /; p; }"
149 sedscip-shell system . . sed. -ne "${SED_CMD}" "${ODBC_INI}"
150
151 #
152 # Examine server entry in odbc.ini
153 #
154
155 # Find the driver in the servername or default section
156 for D in "${DSN}" 'default'
157 do
158 echoscip-shell system . . echo. "looking for driver for DSN [$D] in ${ODBC_INI}"
159 grepscip-shell system . . grep. "$D" "${ODBC_INI}" > /dev/nullscip-shell filesystem . . `/dev/null`.
160 if [scip-shell system . . `[`. $? -eq 0 ]
161 then
162 CMD="/^\[$D\]/,/^[[:space:]]*$/ { s/^/ /; p; }"
163 DRIVER_LINE=$(sedscip-shell system . . sed. -ne "${CMD}" "${ODBC_INI}" \
164 | grep -Ei '^[[:space:]]*driver[[:space:]]*=')
165 if [scip-shell system . . `[`. -z "${DRIVER_LINE}" ]
166 then
167 echoscip-shell system . . echo. " no driver mentioned for [$D] in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
168 continue
169 fi
170
171 echoscip-shell system . . echo. " found driver line: \"${DRIVER_LINE}\""
172 DRIVER=$(echoscip-shell system . . echo. "${DRIVER_LINE}" \
173 | awk -F ' *= *' '{print $2}' \
174 | sed 's/[[:space:]][[:space:]]*//g')
175 if [scip-shell system . . `[`. "${DRIVER}" ]
176 then
177 echoscip-shell system . . echo. " driver \"${DRIVER}\" found for [$D] in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
178 break
179 else
180 echoscip-shell system . . echo. " driver line for [$D] incomplete in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
181 continue
182 fi
183 fi
184 done
185
186 if [scip-shell system . . `[`. -z "${DRIVER}" ]
187 then
188 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: no driver found for [${DSN}] in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
189 exit 1
190 fi
191
192 # get filename of driver
193 echoscip-shell system . . echo. "found driver named \"${DRIVER}\""
194 if [scip-shell system . . `[`. -d "${DRIVER}" ] || [scip-shell system . . `[`. ! -x "${DRIVER}" ]
195 then
196 # not a filename, look it up
197 DRIVERNAME=${DRIVER}
198 ODBC_INST="${ODBC_DIR}/odbcinst.ini"
199 echoscip-shell system . . echo. "\"${DRIVERNAME}\" is not an executable file"
200 echoscip-shell system . . echo. "looking for entry named [${DRIVERNAME}] in ${ODBC_INST}"
201
202 grepscip-shell system . . grep. "${DRIVERNAME}" "${ODBC_INST}" > /dev/nullscip-shell filesystem . . `/dev/null`.
203 if [scip-shell system . . `[`. $? -ne 0 ]
204 then
205 if [scip-shell system . . `[`. $? -eq 1 ]
206 then
207 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: no driver entry [${DRIVERNAME}] in ${ODBC_INST}"
208 fi
209 exit 1;
210 fi
211
212 CMD="/^\[${DRIVERNAME}\]/,/^[[:space:]]*$/ { s/^/ /; p; }"
213 DRIVER_LINE=$(sedscip-shell system . . sed. -ne "${CMD}" "${ODBC_INST}" \
214 | grep -Ei '^[[:space:]]*driver[[:space:]]*=')
215 if [scip-shell system . . `[`. -z "${DRIVER_LINE}" ]
216 then
217 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): no driver mentioned for [${DRIVERNAME}] in $(basenamescip-shell system . . basename. "${ODBC_INST}")"
218 exit 1
219 fi
220
221 echoscip-shell system . . echo. " found driver line: \"${DRIVER_LINE}\""
222 DRIVER=$(echoscip-shell system . . echo. "${DRIVER_LINE}" | awkscip-shell system . . awk. -F ' *= *' '{print $2}')
223 if [scip-shell system . . `[`. -z "${DRIVER}" ]
224 then
225 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): driver line incomplete for [${DRIVERNAME}] in $(basenamescip-shell system . . basename. "${ODBC_INST}")"
226 exit 1
227 fi
228
229 echoscip-shell system . . echo. " found driver ${DRIVER} for [${DRIVERNAME}] in $(basenamescip-shell system . . basename. "${ODBC_INST}")"
230 fi
231
232 if [scip-shell system . . `[`. -z "${DRIVER}" ]
233 then
234 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: sorry, failed sanity check: \${DRIVER} is null"
235 exit 1
236 fi
237
238 if [scip-shell system . . `[`. -x "${DRIVER}" ]
239 then
240 echoscip-shell system . . echo. "${DRIVER} is an executable file"
241 else
242 echoscip-shell system . . echo. "${DRIVER} is not an executable file"
243 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: no driver found for ${DSN}"
244 exit 1
245 fi
246
247 # find the server/servername
248 SERVER_LINE=$(sedscip-shell system . . sed. -ne "${SED_CMD}" "${ODBC_INI}" \
249 | grep -Ei '^[[:space:]]*server(name)*[[:space:]]*=')
250
251 ATTRIBUTE=$(echoscip-shell system . . echo. "${SERVER_LINE}" | awkscip-shell system . . awk. -F' *= *' '{print $1}')
252
253 if [scip-shell system . . `[`. -z "${ATTRIBUTE}" ]
254 then
255 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): neither \"Server\" nor \"Servername\" found for [${DSN}] in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
256 exit 1
257 fi
258
259 echoscip-shell system . . echo. "${SERVER_LINE}" | grepscip-shell system . . grep. -i servername >/dev/nullscip-shell filesystem . . `/dev/null`.
260
261 #
262 # Find the server's hostname
263 #
264 if [scip-shell system . . `[`. $? -eq 0 ] # ODBC-Combined
265 then
266 TDS_SERVER=$(echoscip-shell system . . echo. "${SERVER_LINE}" | awkscip-shell system . . awk. -F ' *= *' '{print $2}')
267 echoscip-shell system . . echo. 'Using ODBC-Combined strategy'
268 echoscip-shell system . . echo. "DSN [${DSN}] has servername \"${TDS_SERVER}\" (from ${ODBC_INI})"
269 if [scip-shell system . . `[`. -z "${TDS_SERVER}" ]
270 then
271 exit 1
272 fi
273
274 # Look for $TDS_SERVER in freetds.conf
275
276 FREETDS_DIR=$(tsql -C | grepscip-shell system . . grep. 'freetds.conf directory' | awkscip-shell system . . awk. -F: '{print $2}' | sedscip-shell system . . sed. 's/^ *//')
277 if [scip-shell system . . `[`. -z "${FREETDS_DIR}" ]
278 then
279 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: unable to locate directory for freetds.conf using \"$(command -v tsql)\""
280 exit 1
281 fi
282
283 for F in "${HOME}/.freetds.conf" "${FREETDS_DIR}/freetds.conf"
284 do
285 if [scip-shell system . . `[`. -r "$F" ]
286 then
287 echoscip-shell system . . echo. "$F is a readable file"
288 else
289 echoscip-shell system . . echo. "cannot read \"$F\""
290 continue
291 fi
292
293 echoscip-shell system . . echo. "looking for [${TDS_SERVER}] in $F"
294 grepscip-shell system . . grep. -F "[${TDS_SERVER}]" "$F" > /dev/nullscip-shell filesystem . . `/dev/null`.
295
296 if [scip-shell system . . `[`. $? -eq 0 ]
297 then
298 FREETDS_CONF=$F
299 break
300 else
301 echoscip-shell system . . echo. "[${TDS_SERVER}] not found in $F"
302 fi
303 done
304
305 if [scip-shell system . . `[`. -z "${FREETDS_CONF}" ]
306 then
307 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): error: unable to locate ${TDS_SERVER} in any freetds.conf"
308 exit 1
309 fi
310
311 # Examine server entry in freetds.conf
312
313 echoscip-shell system . . echo. found this section:
314 SED_CMD="/^\[${TDS_SERVER}\]/,/^[[:space:]]*$/ { s/^/ /; p; }"
315 sedscip-shell system . . sed. -ne "${SED_CMD}" "${FREETDS_CONF}"
316
317 SERVER_LINE=$(sedscip-shell system . . sed. -ne "${SED_CMD}" "${FREETDS_CONF}" \
318 | grep -Ei '^[[:space:]]*host[[:space:]]*=')
319
320 # No character class support in mawk,
321 # cf. "Mawk does not support Posix character classes in expressions"
322 # https://bugs.launchpad.net/ubuntu/+source/mawk/+bug/69724
323 HOST=$(echoscip-shell system . . echo. "${SERVER_LINE}" | awkscip-shell system . . awk. -F' *= *' '{print $2}')
324
325 if [scip-shell system . . `[`. -z "${HOST}" ]
326 then
327 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): no \"host\" entry found for [${TDS_SERVER}] in $(basenamescip-shell system . . basename. "${FREETDS_CONF}")"
328 exit 1
329 fi
330
331 else # odbc.ini contains a "server", a DNS host
332 HOST=$(echoscip-shell system . . echo. "${SERVER_LINE}" | awkscip-shell system . . awk. -F ' *= *' '{print $2}')
333 echoscip-shell system . . echo. "\"${ATTRIBUTE}\" found, not using freetds.conf"
334 echoscip-shell system . . echo. "${ATTRIBUTE} is \"${HOST}\""
335 if [scip-shell system . . `[`. -z "${HOST}" ]
336 then
337 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): no value found for \"${ATTRIBUTE}\" entry in $(basenamescip-shell system . . basename. "${ODBC_INI}")"
338 exit 1
339 fi
340 fi
341
342 # If the "host" is an ip address, look up the name, for neatness, e.g.
343 ## $ host 10.81.36.39
344 ## 39.36.81.10.IN-ADDR.ARPA domain name pointer ntc5003.eg.com
345 if [scip-shell system . . `[`. "${HOST}" = "$(echoscip-shell system . . echo. "${HOST}" | sedscip-shell system . . sed. 's/[^.0-9]*//')" ]
346 then
347 ADDRESS=${HOST}
348 echoscip-shell system . . echo. "looking up hostname for ip address ${ADDRESS}"
349 HOST=$(host "${HOST}" | awkscip-shell system . . awk. '/domain/ {print $5}scip-shell filesystem . . `/domain/ {print $5}`.' | sedscip-shell system . . sed. 's/\.$//')
350 if [scip-shell system . . `[`. -z "${HOST}" ]
351 then
352 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): warning: no DNS hostname found for \"${ADDRESS}\""
353 HOST=${ADDRESS} # restore host address string
354 fi
355 fi
356
357 # Now we have a DNS hostname for the server in HOST
358 if [scip-shell system . . `[`. "${HOST}" != "${ADDRESS}" ]
359 then
360 ADDRESS=$(host "${HOST}" | awkscip-shell system . . awk. '/has address/ {print $4}scip-shell filesystem . . `/has address/ {print $4}`.' | headscip-shell system . . head. -1)
361 fi
362 if [scip-shell system . . `[`. -z "${ADDRESS}" ]
363 then
364 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. "$0"): no IP address found for \"${HOST}\""
365 exit 1
366 fi
367
368 #cho ${HOST} has address ${ADDRESS}
369
370 # Report what we know and exec isql
371
372 printfscip-shell system . . printf. "\n"
373 printfscip-shell system . . printf. "Configuration looks OK. Connection details:\n\n"
374 printfscip-shell system . . printf. "%22s:\t%-30s\n" DSN "${DSN}"
375 printfscip-shell system . . printf. "%22s:\t%-30s\n" odbc.ini "${ODBC_INI}"
376 printfscip-shell system . . printf. "%22s:\t%-30s\n" Driver "${DRIVER}"
377 printfscip-shell system . . printf. "%22s:\t%-30s\n" "Server hostname" "${HOST}"
378 printfscip-shell system . . printf. "%22s:\t%-30s\n" Address "${ADDRESS}"
379 printfscip-shell system . . printf. "\n"
380
381 echoscip-shell system . . echo. "Attempting connection as ${USERNAME} ..."
382
383 if [scip-shell system . . `[`. -z "${TDSDUMP}" ]
384 then
385 TDSDUMP_AUTO="/tmp/$(basenamescip-shell system . . basename. "$0").dump.$$"
386 export TDSDUMP=${TDSDUMP_AUTO}
387 fi
388
389 ( set -x; isql "${DSN}" "${USERNAME}" "${PASSWORD}" -v ) \
390 || sedscip-shell system . . sed. -ne 's/Connecting/FAILED &/p' "${TDSDUMP}"
391
392 testscip-shell system . . test. "${TDSDUMP_AUTO}" && rmscip-shell system . . rm. -f "${TDSDUMP_AUTO}"
393