This file is indexed.

/usr/bin/osql is in freetds-bin 0.91-6.1build1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#! /bin/sh
# $Id: osql,v 1.11.2.1 2011/05/11 03:10:33 jklowden Exp $
#
# Check odbc.ini, odbcinst, and, optionally, freetds.conf, 
# then execute isql (assume it's unixODBC's isql).  
#

USAGE="Syntax: $(basename $0) -S server -U user -P password"

while getopts I:S:U:P: OPTION
do
	case ${OPTION} in
		I)	OVER_DIR=${OPTARG}	# override
			;;
		S)	DSN=${OPTARG}
			;;
		U)	USERNAME=${OPTARG}
			;;
		P)	PASSWORD=${OPTARG}
			;;
		\?)     echo $USAGE
			exit 1
			;;
	esac
done

#cho ${DSN} ${USERNAME} ${PASSWORD}
if [ -z "${DSN}" -o -z "${USERNAME}" -o -z "${PASSWORD}" ]
then 
	
	echo $USAGE
	exit 1
fi

ISQL=$(command -v isql)
if [ -z "${ISQL}" ]
then
	echo "$(basename $0): error: no \"isql\" command found.  Is unixODBC installed?"
	exit 1
fi

# Establish ODBC prefix directory

ISQL_DIR=$(strings ${ISQL} | grep ^/ | grep -v elf | grep -v '\.so\.' | head -1 | sed 's/lib$/etc/' )

INI_DIRNAME="/tmp/$(basename $0).$$"
exec 3> ${INI_DIRNAME}

# Check the libraries, too. 
if [ -z "${OVER_DIR}" ]
then 
	echo "checking shared odbc libraries linked to isql for default directories..."
	(echo ${ISQL_DIR}; ldd "${ISQL}" | awk '/libodbc\./ {print $3}') \
	| while read L
	  do 
	  	strings "$L" | grep '^/' | grep -v '/lib' \
		| while read D 
		  do 
		  	if [ ! -s "${INI_DIRNAME}" ]
			then
			  	printf "	trying $D ... "
			  	if [ -d "$D" -a -r "${D}/odbc.ini" ]
				then
					printf $D >&3
					printf "OK"
				else
					printf "no"
				fi
				printf "\n"
			fi
		  done
	  done
fi

ODBC_DIR=$(cat ${INI_DIRNAME}) && rm ${INI_DIRNAME}

if [ -z "${ODBC_DIR}" -a -z "${OVER_DIR}" ]
then
	echo "$(basename $0): problem: no potential directory strings in \"$(command -v isql)\""
	echo "$(basename $0): advice: use \"osql -I DIR\" where DIR unixODBC\'s install prefix e.g. /usr/local"
	echo "isql strings are:"
	strings ${ISQL} | grep ^/ | sed 's/^/+ /'
	#xit 1
fi

if [ "${OVER_DIR}" ]
then
	if [ -d "${ODBC_DIR}" ]
	then
		echo "\"${ODBC_DIR}\" is a directory, overridden by"
	else 
		echo "\"${ODBC_DIR}\" is NOT a directory, overridden by"
	fi
	echo "\"${OVER_DIR}\"."
	
	if [ -d "${OVER_DIR}" ]
	then
		ODBC_DIR=${OVER_DIR}
	else
		echo "$(basename $0): error: \"${OVER_DIR}\" is not a directory"
		exit 1
	fi
fi

# Look for server entry in odbc.ini 
echo 'checking odbc.ini files'
for F in "${HOME}/.odbc.ini" "${ODBC_DIR}/odbc.ini"
do
	if [ ! -d "$(dirname $F)" ]
	then
		echo "warning: $(dirname $F) is not a directory" 
		continue
	fi
	
	if [ -r "$F" ]
	then
		echo "	reading $F"
	else
		echo "	cannot read \"$F\""
		continue
	fi

	grep "[${DSN}]" $F > /dev/null

	if [ $? -eq 0 ]
	then
		echo "[${DSN}] found in $F"
		ODBC_INI=$F
		break
	else
		echo "[${DSN}] not found in $F"
	fi
done

if [ -z "${ODBC_INI}" ]
then
	echo "$(basename $0): error: unable to locate ${DSN} in any odbc.ini"
	exit 1
fi

# Report finding of server entry
echo found this section:
SED_CMD="/^\[${DSN}\]/,/^[[:space:]]*$/ { s/^/	/; p; }"
sed -ne "${SED_CMD}" ${ODBC_INI}

#
# Examine server entry in odbc.ini 
#

# Find the driver in the servername or default section
for D in "${DSN}" 'default' 
do
	echo "looking for driver for DSN [$D] in ${ODBC_INI}"
	grep "$D" ${ODBC_INI} > /dev/null
	if [ $? -eq 0 ]
	then
		CMD="/^\[$D\]/,/^[[:space:]]*$/ { s/^/	/; p; }"
		DRIVER_LINE=$(sed -ne "${CMD}" ${ODBC_INI} \
				| grep -Ei '^[[:space:]]*driver[[:space:]]*=')
		if [ -z "${DRIVER_LINE}" ]
		then
			echo "  no driver mentioned for [$D] in $(basename ${ODBC_INI})"
			continue
		fi

		echo "  found driver line: \"${DRIVER_LINE}\""
		DRIVER=$(echo ${DRIVER_LINE} | awk -F '=' '{print $2}' | sed 's/[[:space:]][[:space:]]*//g')
		if [ "${DRIVER}" ]
		then
			echo "  driver \"${DRIVER}\" found for [$D] in $(basename ${ODBC_INI})"
			break
		else
			echo "  driver line for [$D] incomplete in $(basename ${ODBC_INI})"
			continue
		fi
	fi
done

if [ -z "${DRIVER}" ]
then
	echo "$(basename $0): error: no driver found for [${DSN}] in $(basename ${ODBC_INI})"
	exit 1
fi

# get filename of driver
echo found driver named \"${DRIVER}\"
if [ -d "${DRIVER}" -o ! -x "${DRIVER}" ]
then
	# not a filename, look it up
	DRIVERNAME=${DRIVER}
	ODBC_INST="${ODBC_DIR}/odbcinst.ini"
	echo "\"${DRIVERNAME}\" is not an executable file"
	echo "looking for entry named [${DRIVERNAME}] in ${ODBC_INST}"

	grep "${DRIVERNAME}" ${ODBC_INST} > /dev/null
	if [ $? -ne 0 ]
	then
		if [ $? -eq 1 ]
		then
			echo "$(basename $0): error: no driver entry [${DRIVERNAME}] in ${ODBC_INST}"
		fi
		exit 1;
	fi
	
	CMD="/^\[${DRIVERNAME}\]/,/^[[:space:]]*$/ { s/^/	/; p; }"
	DRIVER_LINE=$(sed -ne "${CMD}" ${ODBC_INST} \
				| grep -Ei '^[[:space:]]*driver[[:space:]]*=')
	if [ -z "${DRIVER_LINE}" ]
	then
		echo "$(basename $0): no driver mentioned for [${DRIVERNAME}] in $(basename ${ODBC_INST})"
		exit 1
	fi

	echo "  found driver line: \"${DRIVER_LINE}\""
	DRIVER=$(echo ${DRIVER_LINE} | awk '{print $3}')
	if [ -z "${DRIVER}" ]
	then
		echo "$(basename $0): driver line incomplete for [${DRIVERNAME}] in $(basename ${ODBC_INST})"
		exit 1
	fi

	echo "  found driver ${DRIVER} for [${DRIVERNAME}] in $(basename ${ODBC_INST})"
fi

if [ -z "${DRIVER}" ]
then
	echo $(basename $0): 'error: sorry, failed sanity check: ${DRIVER} is null' 
	exit 1
fi

if [ -x "${DRIVER}" ]
then
	echo "${DRIVER} is an executable file"
else
	echo "${DRIVER} is not an executable file"
	echo "$(basename $0): error: no driver found for ${DSN}"
	exit 1
fi

# find the server/servername
SERVER_LINE=$(sed -ne "${SED_CMD}" ${ODBC_INI} \
		| grep -Ei '^[[:space:]]*server(name)*[[:space:]]*=')

ATTRIBUTE=$(echo ${SERVER_LINE} | awk '{print $1}')

if [ -z "${ATTRIBUTE}" ]
then
	echo "$(basename $0): neither \"Server\" nor \"Servername\" found for [${DSN}] in $(basename ${ODBC_INI})"
	exit 1
fi

echo ${SERVER_LINE} | grep -i servername >/dev/null

#
# Find the server's hostname 
#
if [ $? -eq 0 ] # ODBC-Combined
then
	TDS_SERVER=$(echo ${SERVER_LINE} | awk -F '=[[:space:]]*' '{print $2}')
	echo 'Using ODBC-Combined strategy'
	echo "DSN [${DSN}] has servername \"${TDS_SERVER}\" (from ${ODBC_INI})"
	if [ -z "${TDS_SERVER}" ]
	then
		exit 1
	fi

	# Look for $TDS_SERVER in freetds.conf 
	
	FREETDS_DIR=$(tsql -C | grep 'freetds.conf directory' | awk -F: '{print $2}' | sed 's/^ *//')
	if [ -z "${FREETDS_DIR}" ]
	then
		echo "$(basename $0): error: unable to locate directory for freetds.conf using \"$(command -v tsql)\""
		exit 1
	fi
	
	for F in "${HOME}/.freetds.conf" "${FREETDS_DIR}/freetds.conf"
	do
		if [ -r "$F" ]
		then
			echo $F is a readable file
		else
			echo cannot read \"$F\"
			continue
		fi

		echo "looking for [${TDS_SERVER}] in $F"
		grep "[${TDS_SERVER}]" $F > /dev/null

		if [ $? -eq 0 ]
		then
			FREETDS_CONF=$F
			break
		else
			echo "[${TDS_SERVER}]" not found in $F
		fi
	done
	
	if [ -z "${FREETDS_CONF}" ]
	then
		echo "$(basename $0): error: unable to locate ${TDS_SERVER} in any freetds.conf"
		exit 1
	fi

	# Examine server entry in freetds.conf 

	echo found this section:
	SED_CMD="/^\[${TDS_SERVER}\]/,/^[[:space:]]*$/ { s/^/	/; p; }"
	sed -ne "${SED_CMD}" ${FREETDS_CONF}

	SERVER_LINE=$(sed -ne "${SED_CMD}" ${FREETDS_CONF} \
			| grep -Ei '^[[:space:]]*host[[:space:]]*=')

	HOST=$(echo ${SERVER_LINE} | awk '{print $3}')

	if [ -z "${HOST}" ]
	then
		echo "$(basename $0): no \"host\" entry found for [${TDS_SERVER}] in $(basename ${FREETDS_CONF})"
		exit 1
	fi
	
else # odbc.ini contains a "server", a DNS host
	HOST=$(echo ${SERVER_LINE} | awk '{print $3}')
	echo "\"${ATTRIBUTE}\" found, not using freetds.conf"
	echo "${ATTRIBUTE} is \"${HOST}\""
	if [ -z "${HOST}" ]
	then
		echo "$(basename $0): no value found for \"${ATTRIBUTE}\" entry in $(basename ${ODBC_INI})"
		exit 1
	fi
fi

# If the "host" is an ip address, look up the name, for neatness, e.g. 
## $ host 10.81.36.39
## 39.36.81.10.IN-ADDR.ARPA domain name pointer ntc5003.eg.com
if [ "${HOST}" = "$(echo ${HOST} | sed 's/[^.0-9]*//')" ]
then 
	ADDRESS=${HOST}
	echo 'looking up hostname for ip address' ${ADDRESS}
	HOST=$(host ${HOST} | awk '/domain/ {print $5}' | sed 's/\.$//')
	if [ -z "${HOST}" ]
	then
		echo "$(basename $0): warning: no DNS hostname found for \"${ADDRESS}\""
	fi
fi

# Now we have a DNS hostname for the server in HOST

ADDRESS=$(host ${HOST} | awk '/has address/ {print $4}' | head -1)
if [ -z "${ADDRESS}" ]
then
	echo "$(basename $0): no IP address found for \"${HOST}\""
	exit 1
fi

#cho ${HOST} has address ${ADDRESS}

# Report what we know and exec isql 

printf "\n"
printf "Configuration looks OK.  Connection details:\n\n"
printf "%22s:\t%-30s\n"  DSN ${DSN}
printf "%22s:\t%-30s\n"  odbc.ini "${ODBC_INI}"
printf "%22s:\t%-30s\n"  Driver ${DRIVER}
printf "%22s:\t%-30s\n" "Server hostname" ${HOST}
printf "%22s:\t%-30s\n"  Address ${ADDRESS}
printf "\n"

echo Attempting connection as ${USERNAME} ... 

if [ -z "${TDSDUMP}" ]
then 
	TDSDUMP_AUTO="/tmp/$(basename $0).dump.$$"
	export TDSDUMP=${TDSDUMP_AUTO}
fi

( set -x; isql ${DSN} ${USERNAME} ${PASSWORD} -v ) \
	|| sed -ne 's/Connecting/FAILED &/p' ${TDSDUMP}

test "${TDSDUMP_AUTO}" && rm -f ${TDSDUMP_AUTO}