main(int argc, char **argv) { TDSSOCKET *tds; TDSLOGIN login; TDSRESULTINFO *resinfo; tds = tds_listen(atoi(argv[1])); /* get_incoming(tds->s); */ tds_read_login(tds, &login); dump_login(&login); if (!strcmp(login.user_name, "guest") && !strcmp(login.password, "sybase")) { tds->out_flag = 4; tds_env_change(tds, 1, "master", "pubs2"); tds_send_msg(tds, 5701, 2, 10, "Changed database context to 'pubs2'.", "JDBC", "ZZZZZ", 1); if (!login.suppress_language) { tds_env_change(tds, 2, NULL, "us_english"); tds_send_msg(tds, 5703, 1, 10, "Changed language setting to 'us_english'.", "JDBC", "ZZZZZ", 1); } tds_env_change(tds, 4, NULL, "512"); tds_send_login_ack(tds, "sql server"); tds_send_capabilities_token(tds); tds_send_253_token(tds, 0, 1); } else { /* send nack before exiting */ exit(1); } tds_flush_packet(tds); /* printf("incoming packet %d\n", tds_read_packet(tds)); */ printf("query : %s\n", tds_get_query(tds)); tds->out_flag = 4; resinfo = tds_alloc_results(1); resinfo->columns[0]->column_type = SYBVARCHAR; resinfo->columns[0]->column_size = 30; strcpy(resinfo->columns[0]->column_name, "name"); resinfo->columns[0]->column_namelen = 4; resinfo->current_row = "pubs2"; tds_send_result(tds, resinfo); tds_send_174_token(tds, 1); tds_send_row(tds, resinfo); tds_send_253_token(tds, 16, 1); tds_flush_packet(tds); sleep(30); }
bool pool_user_send_login_ack(TDS_POOL * pool, TDS_POOL_USER * puser) { char msg[256]; char block[32]; TDSSOCKET *tds = puser->sock.tds, *mtds = puser->assigned_member->sock.tds; TDSLOGIN *login = puser->login; const char *database; const char *server = mtds->conn->server ? mtds->conn->server : "JDBC"; bool dbname_mismatch, odbc_mismatch; pool->user_logins++; /* copy a bit of information, resize socket with block */ tds->conn->tds_version = mtds->conn->tds_version; tds->conn->product_version = mtds->conn->product_version; memcpy(tds->conn->collation, mtds->conn->collation, sizeof(tds->conn->collation)); tds->conn->tds71rev1 = mtds->conn->tds71rev1; free(tds->conn->product_name); tds->conn->product_name = strdup(mtds->conn->product_name); tds_realloc_socket(tds, mtds->conn->env.block_size); tds->conn->env.block_size = mtds->conn->env.block_size; tds->conn->client_spid = mtds->conn->spid; /* if database is different use USE statement */ database = pool->database; dbname_mismatch = !tds_dstr_isempty(&login->database) && strcasecmp(tds_dstr_cstr(&login->database), database) != 0; odbc_mismatch = (login->option_flag2 & TDS_ODBC_ON) == 0; if (dbname_mismatch || odbc_mismatch) { char *str; int len = 128 + tds_quote_id(mtds, NULL, tds_dstr_cstr(&login->database),-1); TDSRET ret; if ((str = (char *) malloc(len)) == NULL) return false; str[0] = 0; /* swicth to dblib options */ if (odbc_mismatch) strcat(str, "SET ANSI_DEFAULTS OFF\nSET CONCAT_NULL_YIELDS_NULL OFF\n"); if (dbname_mismatch) { strcat(str, "USE "); tds_quote_id(mtds, strchr(str, 0), tds_dstr_cstr(&login->database), -1); } ret = tds_submit_query(mtds, str); free(str); if (TDS_FAILED(ret) || TDS_FAILED(tds_process_simple_query(mtds))) return false; if (dbname_mismatch) database = tds_dstr_cstr(&login->database); else database = mtds->conn->env.database; } // 7.0 // env database // database change message (with server name correct) // env language // language change message // env 0x3 charset ("iso_1") // env 0x5 lcid ("1033") // env 0x6 ("196609" ?? 0x30001) // loginack // env 0x4 packet size // done // // 7.1/7.2/7.3 // env database // database change message (with server name correct) // env 0x7 collation // env language // language change message // loginack // env 0x4 packet size // done tds->out_flag = TDS_REPLY; tds_env_change(tds, TDS_ENV_DATABASE, "master", database); sprintf(msg, "Changed database context to '%s'.", database); tds_send_msg(tds, 5701, 2, 0, msg, server, NULL, 1); if (!login->suppress_language) { tds_env_change(tds, TDS_ENV_LANG, NULL, "us_english"); tds_send_msg(tds, 5703, 1, 0, "Changed language setting to 'us_english'.", server, NULL, 1); } if (IS_TDS71_PLUS(tds->conn)) { tds_put_byte(tds, TDS_ENVCHANGE_TOKEN); tds_put_smallint(tds, 8); tds_put_byte(tds, TDS_ENV_SQLCOLLATION); tds_put_byte(tds, 5); tds_put_n(tds, tds->conn->collation, 5); tds_put_byte(tds, 0); } tds_send_login_ack(tds, mtds->conn->product_name); sprintf(block, "%d", tds->conn->env.block_size); tds_env_change(tds, TDS_ENV_PACKSIZE, block, block); /* tds_send_capabilities_token(tds); */ tds_send_done_token(tds, 0, 0); /* send it! */ tds_flush_packet(tds); tds_free_login(login); puser->login = NULL; return true; }
int main(int argc, char **argv) { TDSCONTEXT *ctx; TDSSOCKET *tds; TDSLOGIN *login; TDSRESULTINFO *resinfo; if (argc < 2 || atoi(argv[1]) <= 0) { fprintf(stderr, "syntax: %s <port>\n", argv[0]); return 1; } ctx = tds_alloc_context(NULL); tds = tds_listen(ctx, atoi(argv[1])); if (!tds) return 1; /* get_incoming(tds->s); */ login = tds_alloc_read_login(tds); if (!login) { fprintf(stderr, "Error reading login\n"); exit(1); } dump_login(login); if (!strcmp(tds_dstr_cstr(&login->user_name), "guest") && !strcmp(tds_dstr_cstr(&login->password), "sybase")) { tds->out_flag = TDS_REPLY; tds_env_change(tds, TDS_ENV_DATABASE, "master", "pubs2"); tds_send_msg(tds, 5701, 2, 10, "Changed database context to 'pubs2'.", "JDBC", "ZZZZZ", 1); if (!login->suppress_language) { tds_env_change(tds, TDS_ENV_LANG, NULL, "us_english"); tds_send_msg(tds, 5703, 1, 10, "Changed language setting to 'us_english'.", "JDBC", "ZZZZZ", 1); } tds_env_change(tds, TDS_ENV_PACKSIZE, NULL, "512"); /* TODO set mssql if tds7+ */ tds_send_login_ack(tds, "sql server"); if (IS_TDS50(tds)) tds_send_capabilities_token(tds); tds_send_done_token(tds, 0, 1); } else { /* send nack before exiting */ exit(1); } tds_flush_packet(tds); tds_free_login(login); login = NULL; /* printf("incoming packet %d\n", tds_read_packet(tds)); */ printf("query : %s\n", tds_get_generic_query(tds)); tds->out_flag = TDS_REPLY; resinfo = tds_alloc_results(1); resinfo->columns[0]->column_type = SYBVARCHAR; resinfo->columns[0]->column_size = 30; strcpy(resinfo->columns[0]->column_name, "name"); resinfo->columns[0]->column_namelen = 4; resinfo->current_row = (TDS_UCHAR*) "pubs2"; resinfo->columns[0]->column_data = resinfo->current_row; tds_send_result(tds, resinfo); tds_send_control_token(tds, 1); tds_send_row(tds, resinfo); tds_send_done_token(tds, 16, 1); tds_flush_packet(tds); sleep(30); tds_free_results(resinfo); tds_free_socket(tds); tds_free_context(ctx); return 0; }