Line data Source code
1 : #define TDS_DONT_DEFINE_DEFAULT_FUNCTIONS
2 : #include "common.h"
3 : #include <freetds/replacements.h>
4 :
5 : char USER[512];
6 : char SERVER[512];
7 : char PASSWORD[512];
8 : char DATABASE[512];
9 : /* TODO use another default ?? */
10 : char CHARSET[512] = "ISO-8859-1";
11 :
12 : int read_login_info(void);
13 :
14 : int
15 140 : read_login_info(void)
16 : {
17 140 : FILE *in = NULL;
18 : char line[512];
19 : char *s1, *s2;
20 :
21 140 : s1 = getenv("TDSPWDFILE");
22 140 : if (s1 && s1[0])
23 0 : in = fopen(s1, "r");
24 0 : if (!in)
25 140 : in = fopen("../../../PWD", "r");
26 140 : if (!in) {
27 0 : fprintf(stderr, "Can not open PWD file\n\n");
28 0 : return TDS_FAIL;
29 : }
30 :
31 2772 : while (fgets(line, sizeof(line), in)) {
32 2632 : s1 = strtok(line, "=");
33 2632 : s2 = strtok(NULL, "\n");
34 2632 : if (!s1 || !s2) {
35 1596 : continue;
36 : }
37 1036 : if (!strcmp(s1, "UID")) {
38 140 : strcpy(USER, s2);
39 896 : } else if (!strcmp(s1, "SRV")) {
40 140 : strcpy(SERVER, s2);
41 756 : } else if (!strcmp(s1, "PWD")) {
42 140 : strcpy(PASSWORD, s2);
43 616 : } else if (!strcmp(s1, "DB")) {
44 140 : strcpy(DATABASE, s2);
45 : }
46 : }
47 140 : fclose(in);
48 140 : return TDS_SUCCESS;
49 : }
50 :
51 : TDSCONTEXT *test_context = NULL;
52 :
53 : int
54 140 : try_tds_login(TDSLOGIN ** login, TDSSOCKET ** tds, const char *appname, int verbose)
55 : {
56 : TDSLOGIN *connection;
57 : char *appname_copy;
58 :
59 140 : if (verbose) {
60 10 : printf("Entered tds_try_login()\n");
61 : }
62 140 : if (!login) {
63 0 : fprintf(stderr, "Invalid TDSLOGIN**\n");
64 0 : return TDS_FAIL;
65 : }
66 140 : if (!tds) {
67 0 : fprintf(stderr, "Invalid TDSSOCKET**\n");
68 0 : return TDS_FAIL;
69 : }
70 :
71 140 : if (verbose) {
72 10 : printf("Trying read_login_info()\n");
73 : }
74 140 : read_login_info();
75 :
76 140 : if (verbose) {
77 10 : printf("Setting login parameters\n");
78 : }
79 140 : *login = tds_alloc_login(true);
80 140 : if (!*login) {
81 0 : fprintf(stderr, "tds_alloc_login() failed.\n");
82 0 : return TDS_FAIL;
83 : }
84 140 : appname_copy = strdup(appname);
85 140 : if (!tds_set_passwd(*login, PASSWORD)
86 140 : || !tds_set_user(*login, USER)
87 140 : || !appname_copy
88 140 : || !tds_set_app(*login, basename(appname_copy))
89 140 : || !tds_set_host(*login, "myhost")
90 140 : || !tds_set_library(*login, "TDS-Library")
91 140 : || !tds_set_server(*login, SERVER)
92 140 : || !tds_set_client_charset(*login, CHARSET)
93 140 : || !tds_set_language(*login, "us_english")) {
94 0 : free(appname_copy);
95 0 : fprintf(stderr, "tds_alloc_login() failed.\n");
96 0 : return TDS_FAIL;
97 : }
98 140 : free(appname_copy);
99 :
100 140 : if (verbose) {
101 10 : printf("Connecting to database\n");
102 : }
103 140 : test_context = tds_alloc_context(NULL);
104 140 : *tds = tds_alloc_socket(test_context, 512);
105 140 : tds_set_parent(*tds, NULL);
106 140 : connection = tds_read_config_info(*tds, *login, test_context->locale);
107 140 : if (!connection || tds_connect_and_login(*tds, connection) != TDS_SUCCESS) {
108 0 : if (connection) {
109 0 : tds_free_socket(*tds);
110 0 : *tds = NULL;
111 0 : tds_free_login(connection);
112 : }
113 0 : fprintf(stderr, "tds_connect_and_login() failed\n");
114 0 : return TDS_FAIL;
115 : }
116 140 : tds_free_login(connection);
117 :
118 140 : return TDS_SUCCESS;
119 : }
120 :
121 :
122 : /* Note that this always suceeds */
123 : int
124 140 : try_tds_logout(TDSLOGIN * login, TDSSOCKET * tds, int verbose)
125 : {
126 140 : if (verbose) {
127 10 : printf("Entered tds_try_logout()\n");
128 : }
129 140 : tds_close_socket(tds);
130 140 : tds_free_socket(tds);
131 140 : tds_free_login(login);
132 140 : tds_free_context(test_context);
133 140 : test_context = NULL;
134 140 : return TDS_SUCCESS;
135 : }
136 :
137 : /* Run query for which there should be no return results */
138 : int
139 730 : run_query(TDSSOCKET * tds, const char *query)
140 : {
141 : int rc;
142 : int result_type;
143 :
144 730 : rc = tds_submit_query(tds, query);
145 730 : if (rc != TDS_SUCCESS) {
146 0 : fprintf(stderr, "tds_submit_query() failed for query '%s'\n", query);
147 0 : return TDS_FAIL;
148 : }
149 :
150 1532 : while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) {
151 :
152 802 : switch (result_type) {
153 : case TDS_DONE_RESULT:
154 : case TDS_DONEPROC_RESULT:
155 : case TDS_DONEINPROC_RESULT:
156 : /* ignore possible spurious result (TDS7+ send it) */
157 : case TDS_STATUS_RESULT:
158 : break;
159 0 : default:
160 0 : fprintf(stderr, "Error: query should not return results\n");
161 0 : return TDS_FAIL;
162 : }
163 : }
164 730 : if (rc == TDS_FAIL) {
165 0 : fprintf(stderr, "tds_process_tokens() returned TDS_FAIL for '%s'\n", query);
166 0 : return TDS_FAIL;
167 730 : } else if (rc != TDS_NO_MORE_RESULTS) {
168 0 : fprintf(stderr, "tds_process_tokens() unexpected return\n");
169 0 : return TDS_FAIL;
170 : }
171 :
172 : return TDS_SUCCESS;
173 : }
|