void tds_send_result(TDSSOCKET * tds, TDSRESULTINFO * resinfo) { TDSCOLUMN *curcol; int i, totlen; size_t len; tds_put_byte(tds, TDS_RESULT_TOKEN); totlen = 2; for (i = 0; i < resinfo->num_cols; i++) { curcol = resinfo->columns[i]; len = tds_dstr_len(&curcol->column_name); totlen += 8; totlen += len; curcol = resinfo->columns[i]; if (!is_fixed_type(curcol->column_type)) { totlen++; } } tds_put_smallint(tds, totlen); tds_put_smallint(tds, resinfo->num_cols); for (i = 0; i < resinfo->num_cols; i++) { curcol = resinfo->columns[i]; len = tds_dstr_len(&curcol->column_name); tds_put_byte(tds, tds_dstr_len(&curcol->column_name)); tds_put_n(tds, tds_dstr_cstr(&curcol->column_name), len); tds_put_byte(tds, '0'); tds_put_int(tds, curcol->column_usertype); tds_put_byte(tds, curcol->column_type); if (!is_fixed_type(curcol->column_type)) { tds_put_byte(tds, curcol->column_size); } tds_put_byte(tds, 0); } }
void tds7_send_result(TDSSOCKET * tds, TDSRESULTINFO * resinfo) { int i, j; TDSCOLUMN *curcol; /* TDS7+ uses TDS7_RESULT_TOKEN to send column names and info */ tds_put_byte(tds, TDS7_RESULT_TOKEN); /* send the number of columns */ tds_put_smallint(tds, resinfo->num_cols); /* send info about each column */ for (i = 0; i < resinfo->num_cols; i++) { /* usertype, flags, and type */ curcol = resinfo->columns[i]; tds_put_smallint(tds, curcol->column_usertype); tds_put_smallint(tds, curcol->column_flags); tds_put_byte(tds, curcol->column_type); /* smallint? */ /* bytes in "size" field varies */ if (is_blob_type(curcol->column_type)) { tds_put_int(tds, curcol->column_size); } else if (curcol->column_type>=128) { /*is_large_type*/ tds_put_smallint(tds, curcol->column_size); } else { tds_put_tinyint(tds, curcol->column_size); } /* some types have extra info */ if (is_numeric_type(curcol->column_type)) { tds_put_tinyint(tds, curcol->column_prec); tds_put_tinyint(tds, curcol->column_scale); } else if (is_blob_type(curcol->column_type)) { size_t len = tds_dstr_len(&curcol->table_name); const char *name = tds_dstr_cstr(&curcol->table_name); tds_put_smallint(tds, 2 * len); for (j = 0; name[j] != '\0'; j++){ tds_put_byte(tds, name[j]); tds_put_byte(tds, 0); } } /* finally the name, in UCS16 format */ tds_put_byte(tds, tds_dstr_len(&curcol->column_name)); for (j = 0; j < tds_dstr_len(&curcol->column_name); j++) { tds_put_byte(tds, tds_dstr_cstr(&curcol->column_name)[j]); tds_put_byte(tds, 0); } } }
void tds_send_col_name(TDSSOCKET * tds, TDSRESULTINFO * resinfo) { int col, hdrsize = 0; TDSCOLUMN *curcol; tds_put_byte(tds, TDS_COLNAME_TOKEN); for (col = 0; col < resinfo->num_cols; col++) { curcol = resinfo->columns[col]; hdrsize += tds_dstr_len(&curcol->column_name) + 1; } tds_put_smallint(tds, hdrsize); for (col = 0; col < resinfo->num_cols; col++) { curcol = resinfo->columns[col]; tds_put_byte(tds, tds_dstr_len(&curcol->column_name)); /* exclude the null */ tds_put_n(tds, tds_dstr_cstr(&curcol->column_name), tds_dstr_len(&curcol->column_name)); } }
/** * Help to build query to be sent to server. * Append column declaration to the query. * Only for TDS 7.0+. * \tds * \param[out] clause output string * \param bcpcol column to append * \param first true if column is the first * \return TDS_SUCCESS or TDS_FAIL. */ static TDSRET tds7_build_bulk_insert_stmt(TDSSOCKET * tds, TDSPBCB * clause, TDSCOLUMN * bcpcol, int first) { char column_type[40]; tdsdump_log(TDS_DBG_FUNC, "tds7_build_bulk_insert_stmt(%p, %p, %p, %d)\n", tds, clause, bcpcol, first); if (TDS_FAILED(tds_get_column_declaration(tds, bcpcol, column_type))) { tdserror(tds_get_ctx(tds), tds, TDSEBPROBADTYP, errno); tdsdump_log(TDS_DBG_FUNC, "error: cannot build bulk insert statement. unrecognized server datatype %d\n", bcpcol->on_server.column_type); return TDS_FAIL; } if (clause->cb < strlen(clause->pb) + tds_quote_id(tds, NULL, tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name)) + strlen(column_type) + ((first) ? 2u : 4u)) { char *temp = (char*) malloc(2 * clause->cb); if (!temp) { tdserror(tds_get_ctx(tds), tds, TDSEMEM, errno); return TDS_FAIL; } strcpy(temp, clause->pb); if (clause->from_malloc) free(clause->pb); clause->from_malloc = 1; clause->pb = temp; clause->cb *= 2; } if (!first) strcat(clause->pb, ", "); tds_quote_id(tds, strchr(clause->pb, 0), tds_dstr_cstr(&bcpcol->column_name), tds_dstr_len(&bcpcol->column_name)); strcat(clause->pb, " "); strcat(clause->pb, column_type); return TDS_SUCCESS; }
/** * \ingroup odbc_bcp * \brief Prepare for bulk copy operation on a table * * \param dbc ODBC database connection object * \param tblname the name of the table receiving or providing the data. * \param hfile the data file opposite the table, if any. NB: The current * implementation does not support file I/O so this must be NULL * \param errfile the "error file" captures messages and, if errors are * encountered. NB: The current implementation does not support * file I/O so this must be NULL * \param direction one of * - \b DB_IN writing to the table * - \b DB_OUT writing to the host file (Not currently supported) * . * \remarks bcp_init() sets the host file data format and acquires the table metadata. * It is called before the other bulk copy functions. * * The ODBC BCP functionality should be accessed via the inline functions in * odbcss.h. * * After calling this function, call bcp_bind() to associate your data with * the appropriate table column. * * \sa SQL_COPT_SS_BCP, odbc_bcp_bind(), odbc_bcp_done(), odbc_bcp_exec() */ void odbc_bcp_init(TDS_DBC *dbc, const ODBC_CHAR *tblname, const ODBC_CHAR *hfile, const ODBC_CHAR *errfile, int direction _WIDE) { /* TODO convert unicode for printing */ tdsdump_log(TDS_DBG_FUNC, "bcp_init(%p, %s, %s, %s, %d)\n", dbc, tblname, hfile, errfile, direction); if (!tblname) ODBCBCP_ERROR_RETURN("HY009"); /* Free previously allocated storage in dbproc & initialise flags, etc. */ odbc_bcp_free_storage(dbc); /* * Validate other parameters */ if (dbc->tds_socket->conn->tds_version < 0x500) ODBCBCP_ERROR_RETURN("HYC00"); if (direction != BCP_DIRECTION_IN || hfile || errfile) ODBCBCP_ERROR_RETURN("HYC00"); /* Allocate storage */ dbc->bcpinfo = tds_alloc_bcpinfo(); if (dbc->bcpinfo == NULL) ODBCBCP_ERROR_RETURN("HY001"); if (!odbc_dstr_copy(dbc, &dbc->bcpinfo->tablename, SQL_NTS, tblname)) { odbc_bcp_free_storage(dbc); ODBCBCP_ERROR_RETURN("HY001"); } if (tds_dstr_len(&dbc->bcpinfo->tablename) > 92 && !IS_TDS7_PLUS(dbc->tds_socket->conn)) { /* 30.30.30 */ odbc_bcp_free_storage(dbc); ODBCBCP_ERROR_RETURN("HYC00"); } dbc->bcpinfo->direction = direction; dbc->bcpinfo->xfer_init = 0; dbc->bcpinfo->bind_count = 0; if (TDS_FAILED(tds_bcp_init(dbc->tds_socket, dbc->bcpinfo))) { /* TODO return proper error */ /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */ ODBCBCP_ERROR_RETURN("HY000"); } }
/** * Build a SSPI packet to send to server * @param tds A pointer to the TDSSOCKET structure managing a client/server operation. */ TDSAUTHENTICATION * tds_sspi_get_auth(TDSSOCKET * tds) { SecBuffer buf; SecBufferDesc desc; SECURITY_STATUS status; ULONG attrs; TimeStamp ts; SEC_WINNT_AUTH_IDENTITY identity; const char *p, *user_name, *server_name; TDSSSPIAUTH *auth; TDSCONNECTION *connection = tds->connection; /* check connection */ if (!connection) return NULL; if (!tds_init_secdll()) return NULL; /* parse username/password informations */ memset(&identity, 0, sizeof(identity)); user_name = tds_dstr_cstr(&connection->user_name); if ((p = strchr(user_name, '\\')) != NULL) { identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI; identity.Password = (void *) tds_dstr_cstr(&connection->password); identity.PasswordLength = tds_dstr_len(&connection->password); identity.Domain = (void *) user_name; identity.DomainLength = p - user_name; user_name = p + 1; identity.User = (void *) user_name; identity.UserLength = strlen(user_name); } auth = (TDSSSPIAUTH *) calloc(1, sizeof(TDSSSPIAUTH)); if (!auth || !tds->connection) return NULL; auth->tds_auth.free = tds_sspi_free; auth->tds_auth.handle_next = tds_sspi_handle_next; /* using Negotiate system will use proper protocol (either NTLM or Kerberos) */ if (sec_fn->AcquireCredentialsHandleA(NULL, (char *)"Negotiate", SECPKG_CRED_OUTBOUND, NULL, identity.Domain ? &identity : NULL, NULL, NULL, &auth->cred, &ts) != SEC_E_OK) { free(auth); return NULL; } /* allocate buffer */ auth->tds_auth.packet = (TDS_UCHAR *) malloc(NTLMBUF_LEN); if (!auth->tds_auth.packet) { sec_fn->FreeCredentialsHandle(&auth->cred); free(auth); return NULL; } desc.ulVersion = SECBUFFER_VERSION; desc.cBuffers = 1; desc.pBuffers = &buf; buf.cbBuffer = NTLMBUF_LEN; buf.BufferType = SECBUFFER_TOKEN; buf.pvBuffer = auth->tds_auth.packet; /* build SPN */ server_name = tds_dstr_cstr(&connection->server_host_name); if (strchr(server_name, '.') == NULL) { struct hostent *host = gethostbyname(server_name); if (host && strchr(host->h_name, '.') != NULL) server_name = host->h_name; } if (strchr(server_name, '.') != NULL) { if (asprintf(&auth->sname, "MSSQLSvc/%s:%d", server_name, connection->port) < 0) { free(auth->tds_auth.packet); sec_fn->FreeCredentialsHandle(&auth->cred); free(auth); return NULL; } tdsdump_log(TDS_DBG_NETWORK, "kerberos name %s\n", auth->sname); } status = sec_fn->InitializeSecurityContextA(&auth->cred, NULL, auth->sname, ISC_REQ_CONFIDENTIALITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_CONNECTION, 0, SECURITY_NETWORK_DREP, NULL, 0, &auth->cred_ctx, &desc, &attrs, &ts); if (status == SEC_I_COMPLETE_AND_CONTINUE || status == SEC_I_CONTINUE_NEEDED) { sec_fn->CompleteAuthToken(&auth->cred_ctx, &desc); } else if(status != SEC_E_OK) { free(auth->sname); free(auth->tds_auth.packet); sec_fn->FreeCredentialsHandle(&auth->cred); free(auth); return NULL; } auth->tds_auth.packet_len = buf.cbBuffer; return &auth->tds_auth; }
static void test(const char *buf) { char query[1024]; char tmp[129 * 3]; int i; int rc; TDS_INT result_type; int done_flags; to_utf8(buf, tmp); sprintf(query, "SELECT 1 AS [%s]", tmp); /* do a select and check all results */ rc = tds_submit_query(tds, query); if (rc != TDS_SUCCESS) { fprintf(stderr, "tds_submit_query() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROWFMT_RESULT) { fprintf(stderr, "expected row fmt() failed\n"); exit(1); } if (tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS) != TDS_SUCCESS) { fprintf(stderr, "tds_process_tokens() failed\n"); exit(1); } if (result_type != TDS_ROW_RESULT) { fprintf(stderr, "expected row result() failed\n"); exit(1); } i = 0; while ((rc = tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_STOPAT_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)) == TDS_SUCCESS) { TDSCOLUMN *curcol; if (result_type != TDS_ROW_RESULT) break; curcol = tds->current_results->columns[0]; if (strcmp(tmp, tds_dstr_cstr(&curcol->column_name)) != 0) { strlcpy(query, tds_dstr_cstr(&curcol->column_name), sizeof(query)); fprintf(stderr, "Wrong result Got: '%s' len %u\n Expected: '%s' len %u\n", query, (unsigned) tds_dstr_len(&curcol->column_name), tmp, (unsigned int) strlen(tmp)); exit(1); } ++i; } if (rc != TDS_SUCCESS || result_type == TDS_ROW_RESULT || result_type == TDS_COMPUTE_RESULT) { fprintf(stderr, "tds_process_tokens() unexpected return\n"); exit(1); } while ((rc = tds_process_tokens(tds, &result_type, &done_flags, TDS_TOKEN_RESULTS)) == TDS_SUCCESS) { switch (result_type) { case TDS_NO_MORE_RESULTS: return; case TDS_DONE_RESULT: case TDS_DONEPROC_RESULT: case TDS_DONEINPROC_RESULT: if (!(done_flags & TDS_DONE_ERROR)) break; default: fprintf(stderr, "tds_proces_tokens() unexpected result_type\n"); exit(1); break; } } }