static void _mongoc_database_find_collections_legacy_mutate (const bson_t *bson, bson_t *out, void *ctx_) { mongoc_database_find_collections_legacy_ctx_t *ctx; ctx = ctx_; bson_copy_to_excluding_noinit (bson, out, "name", NULL); BSON_APPEND_UTF8 (out, "name", ctx->name + (ctx->dbname_len + 1)); /* +1 for the '.' */ }
static mongoc_cursor_state_t _prime (mongoc_cursor_t *cursor) { data_cmd_t *data = (data_cmd_t *) cursor->impl.data; bson_t copied_opts; bson_init (&copied_opts); cursor->operation_id = ++cursor->client->cluster.operation_id; /* commands like agg have a cursor field, so copy opts without "batchSize" */ bson_copy_to_excluding_noinit ( &cursor->opts, &copied_opts, "batchSize", NULL); /* server replies to aggregate/listIndexes/listCollections with: * {cursor: {id: N, firstBatch: []}} */ _mongoc_cursor_response_refresh ( cursor, &data->cmd, &copied_opts, &data->response); data->reading_from = CMD_RESPONSE; bson_destroy (&copied_opts); return IN_BATCH; }
/* Uses old way of querying system.namespaces. */ mongoc_cursor_t * _mongoc_database_find_collections_legacy (mongoc_database_t *database, const bson_t *filter, bson_error_t *error) { mongoc_collection_t *col; mongoc_cursor_t *cursor = NULL; mongoc_read_prefs_t *read_prefs; uint32_t dbname_len; bson_t legacy_filter; bson_iter_t iter; const char *col_filter; bson_t q = BSON_INITIALIZER; mongoc_database_find_collections_legacy_ctx_t *ctx; BSON_ASSERT (database); col = mongoc_client_get_collection (database->client, database->name, "system.namespaces"); BSON_ASSERT (col); dbname_len = (uint32_t)strlen (database->name); ctx = bson_malloc (sizeof (*ctx)); ctx->dbname = database->name; ctx->dbname_len = dbname_len; /* Filtering on name needs to be handled differently for old servers. */ if (filter && bson_iter_init_find (&iter, filter, "name")) { bson_string_t *buf; /* on legacy servers, this must be a string (i.e. not a regex) */ if (!BSON_ITER_HOLDS_UTF8 (&iter)) { bson_set_error (error, MONGOC_ERROR_NAMESPACE, MONGOC_ERROR_NAMESPACE_INVALID_FILTER_TYPE, "On legacy servers, a filter on name can only be a string."); goto cleanup_filter; } BSON_ASSERT (BSON_ITER_HOLDS_UTF8 (&iter)); col_filter = bson_iter_utf8 (&iter, NULL); bson_init (&legacy_filter); bson_copy_to_excluding_noinit (filter, &legacy_filter, "name", NULL); /* We must db-qualify filters on name. */ buf = bson_string_new (database->name); bson_string_append_c (buf, '.'); bson_string_append (buf, col_filter); BSON_APPEND_UTF8 (&legacy_filter, "name", buf->str); bson_string_free (buf, true); filter = &legacy_filter; } read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); cursor = mongoc_collection_find (col, MONGOC_QUERY_NONE, 0, 0, 0, filter ? filter : &q, NULL, read_prefs); _mongoc_cursor_transform_init ( cursor, _mongoc_database_find_collections_legacy_filter, _mongoc_database_find_collections_legacy_mutate, &bson_free, ctx); mongoc_read_prefs_destroy (read_prefs); cleanup_filter: mongoc_collection_destroy (col); return cursor; }
static bool mongoc_database_add_user_legacy (mongoc_database_t *database, const char *username, const char *password, bson_error_t *error) { mongoc_collection_t *collection; mongoc_cursor_t *cursor = NULL; const bson_t *doc; bool ret = false; bson_t query; bson_t user; char *input; char *pwd = NULL; ENTRY; bson_return_val_if_fail(database, false); bson_return_val_if_fail(username, false); bson_return_val_if_fail(password, false); /* * Users are stored in the <dbname>.system.users virtual collection. */ collection = mongoc_client_get_collection(database->client, database->name, "system.users"); BSON_ASSERT(collection); /* * Hash the users password. */ input = bson_strdup_printf("%s:mongo:%s", username, password); pwd = _mongoc_hex_md5(input); bson_free(input); /* * Check to see if the user exists. If so, we will update the * password instead of inserting a new user. */ bson_init(&query); bson_append_utf8(&query, "user", 4, username, -1); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 1, 0, &query, NULL, NULL); if (!mongoc_cursor_next(cursor, &doc)) { if (mongoc_cursor_error(cursor, error)) { GOTO (failure); } bson_init(&user); bson_append_utf8(&user, "user", 4, username, -1); bson_append_bool(&user, "readOnly", 8, false); bson_append_utf8(&user, "pwd", 3, pwd, -1); } else { bson_init(&user); bson_copy_to_excluding_noinit(doc, &user, "pwd", (char *)NULL); bson_append_utf8(&user, "pwd", 3, pwd, -1); } if (!mongoc_collection_save(collection, &user, NULL, error)) { GOTO (failure_with_user); } ret = true; failure_with_user: bson_destroy(&user); failure: if (cursor) { mongoc_cursor_destroy(cursor); } mongoc_collection_destroy(collection); bson_destroy(&query); bson_free(pwd); RETURN (ret); }
static bool txn_finish (mongoc_client_session_t *session, mongoc_txn_intent_t intent, bson_t *reply, bson_error_t *error) { const char *cmd_name; bson_t cmd = BSON_INITIALIZER; bson_t opts = BSON_INITIALIZER; bson_error_t err_local; bson_error_t *err_ptr = error ? error : &err_local; bson_t reply_local = BSON_INITIALIZER; mongoc_write_err_type_t error_type; bool r = false; _mongoc_bson_init_if_set (reply); cmd_name = (intent == TXN_COMMIT ? "commitTransaction" : "abortTransaction"); if (!mongoc_client_session_append (session, &opts, err_ptr)) { GOTO (done); } if (session->txn.opts.write_concern) { if (!mongoc_write_concern_append (session->txn.opts.write_concern, &opts)) { bson_set_error (err_ptr, MONGOC_ERROR_TRANSACTION, MONGOC_ERROR_TRANSACTION_INVALID_STATE, "Invalid transaction write concern"); GOTO (done); } } BSON_APPEND_INT32 (&cmd, cmd_name, 1); /* will be reinitialized by mongoc_client_write_command_with_opts */ bson_destroy (&reply_local); r = mongoc_client_write_command_with_opts ( session->client, "admin", &cmd, &opts, &reply_local, err_ptr); /* Transactions Spec: "Drivers MUST retry the commitTransaction command once * after it fails with a retryable error", same for abort */ error_type = _mongoc_write_error_get_type (r, err_ptr, &reply_local); if (error_type == MONGOC_WRITE_ERR_RETRY) { bson_destroy (&reply_local); r = mongoc_client_write_command_with_opts ( session->client, "admin", &cmd, &opts, &reply_local, err_ptr); error_type = _mongoc_write_error_get_type (r, err_ptr, &reply_local); } /* Transactions Spec: "add the UnknownTransactionCommitResult error label * when commitTransaction fails with a network error, server selection * error, or write concern failed / timeout." */ if (intent == TXN_COMMIT && reply) { if ((!r && err_ptr->domain == MONGOC_ERROR_SERVER_SELECTION) || error_type == MONGOC_WRITE_ERR_RETRY || error_type == MONGOC_WRITE_ERR_WRITE_CONCERN) { bson_copy_to_excluding_noinit ( &reply_local, reply, "errorLabels", NULL); copy_labels_plus_unknown_commit_result (&reply_local, reply); } else { /* maintain invariants: reply & reply_local are valid until the end */ bson_destroy (reply); bson_steal (reply, &reply_local); bson_init (&reply_local); } } else if (intent == TXN_ABORT && !r) { /* we won't return an error from abortTransaction, so warn */ MONGOC_WARNING ("Error in %s: %s", cmd_name, err_ptr->message); } done: bson_destroy (&reply_local); bson_destroy (&cmd); bson_destroy (&opts); return r; }
/* Uses old way of querying system.namespaces. */ bson_t * _mongoc_database_get_collection_info_legacy (mongoc_database_t *database, const bson_t *filter, bson_error_t *error) { mongoc_collection_t *col; mongoc_cursor_t *cursor; mongoc_read_prefs_t *read_prefs; uint32_t dbname_len; const bson_t *doc; bson_t legacy_filter; bson_iter_t iter; const char *name; const char *col_filter; bson_t q = BSON_INITIALIZER; bson_t *ret = NULL; bson_t col_array = BSON_INITIALIZER; const char *key; char keystr[16]; uint32_t n_cols = 0; BSON_ASSERT (database); col = mongoc_client_get_collection (database->client, database->name, "system.namespaces"); BSON_ASSERT (col); dbname_len = (uint32_t)strlen (database->name); /* Filtering on name needs to be handled differently for old servers. */ if (filter && bson_iter_init_find (&iter, filter, "name")) { /* on legacy servers, this must be a string (i.e. not a regex) */ if (!BSON_ITER_HOLDS_UTF8 (&iter)) { bson_set_error (error, MONGOC_ERROR_NAMESPACE, MONGOC_ERROR_NAMESPACE_INVALID_FILTER_TYPE, "On legacy servers, a filter on name can only be a string."); goto cleanup_filter; } BSON_ASSERT (BSON_ITER_HOLDS_UTF8 (&iter)); col_filter = bson_iter_utf8 (&iter, NULL); bson_init (&legacy_filter); bson_copy_to_excluding_noinit (filter, &legacy_filter, "name", NULL); /* We must db-qualify filters on name. */ bson_string_t *buf = bson_string_new (database->name); bson_string_append_c (buf, '.'); bson_string_append (buf, col_filter); BSON_APPEND_UTF8 (&legacy_filter, "name", buf->str); bson_string_free (buf, true); filter = &legacy_filter; } read_prefs = mongoc_read_prefs_new (MONGOC_READ_PRIMARY); cursor = mongoc_collection_find (col, MONGOC_QUERY_NONE, 0, 0, 0, filter ? filter : &q, NULL, read_prefs); ret = bson_new(); BSON_APPEND_ARRAY_BEGIN (ret, "collections", &col_array); while (mongoc_cursor_more (cursor) && !mongoc_cursor_error (cursor, error)) { if (mongoc_cursor_next (cursor, &doc)) { /* 2 gotchas here. * 1. need to ignore any system collections (prefixed with $) * 2. need to remove the database name from the collection so that clients * don't need to specialize their logic for old versions of the server. */ if (bson_iter_init_find (&iter, doc, "name") && BSON_ITER_HOLDS_UTF8 (&iter) && (name = bson_iter_utf8 (&iter, NULL)) && !strchr (name, '$') && (0 == strncmp (name, database->name, dbname_len))) { bson_t nprefix_col = BSON_INITIALIZER; bson_copy_to_excluding_noinit (doc, &nprefix_col, "name", NULL); BSON_APPEND_UTF8 (&nprefix_col, "name", name + (dbname_len + 1)); /* +1 for the '.' */ /* need to construct a key for this array element. */ bson_uint32_to_string(n_cols, &key, keystr, sizeof (keystr)); BSON_APPEND_DOCUMENT (&col_array, key, &nprefix_col); ++n_cols; } } } bson_append_array_end (ret, &col_array); mongoc_cursor_destroy (cursor); mongoc_read_prefs_destroy (read_prefs); cleanup_filter: mongoc_collection_destroy (col); return ret; }
static bool txn_commit (mongoc_client_session_t *session, bool explicitly_retrying, bson_t *reply, bson_error_t *error) { bson_t cmd = BSON_INITIALIZER; bson_t opts = BSON_INITIALIZER; bson_error_t err_local; bson_error_t *err_ptr = error ? error : &err_local; bson_t reply_local = BSON_INITIALIZER; mongoc_write_err_type_t error_type; bool r = false; bool retrying_after_error = false; mongoc_write_concern_t *retry_wc = NULL; _mongoc_bson_init_if_set (reply); BSON_APPEND_INT32 (&cmd, "commitTransaction", 1); retry: if (!mongoc_client_session_append (session, &opts, err_ptr)) { GOTO (done); } /* Transactions Spec: "When commitTransaction is retried, either by the * driver's internal retry-once logic or explicitly by the user calling * commitTransaction again, drivers MUST apply w:majority to the write * concern of the commitTransaction command." */ if (!retry_wc && (retrying_after_error || explicitly_retrying)) { retry_wc = create_commit_retry_wc (session->txn.opts.write_concern ? session->txn.opts.write_concern : session->client->write_concern); } if (retry_wc || session->txn.opts.write_concern) { if (!mongoc_write_concern_append ( retry_wc ? retry_wc : session->txn.opts.write_concern, &opts)) { bson_set_error (err_ptr, MONGOC_ERROR_TRANSACTION, MONGOC_ERROR_TRANSACTION_INVALID_STATE, "Invalid transaction write concern"); GOTO (done); } } /* will be reinitialized by mongoc_client_write_command_with_opts */ bson_destroy (&reply_local); r = mongoc_client_write_command_with_opts ( session->client, "admin", &cmd, &opts, &reply_local, err_ptr); /* Transactions Spec: "Drivers MUST retry the commitTransaction command once * after it fails with a retryable error", same for abort */ error_type = _mongoc_write_error_get_type (r, err_ptr, &reply_local); if (!retrying_after_error && error_type == MONGOC_WRITE_ERR_RETRY) { retrying_after_error = true; /* retry after error only once */ bson_reinit (&opts); GOTO (retry); } /* Transactions Spec: "add the UnknownTransactionCommitResult error label * when commitTransaction fails with a network error, server selection * error, or write concern failed / timeout." */ if (reply) { if ((!r && err_ptr->domain == MONGOC_ERROR_SERVER_SELECTION) || error_type == MONGOC_WRITE_ERR_RETRY || error_type == MONGOC_WRITE_ERR_WRITE_CONCERN) { bson_copy_to_excluding_noinit ( &reply_local, reply, "errorLabels", NULL); copy_labels_plus_unknown_commit_result (&reply_local, reply); } else { /* maintain invariants: reply & reply_local are valid until the end */ bson_destroy (reply); bson_steal (reply, &reply_local); bson_init (&reply_local); } } done: bson_destroy (&reply_local); bson_destroy (&cmd); bson_destroy (&opts); if (retry_wc) { mongoc_write_concern_destroy (retry_wc); } return r; }
mongoc_change_stream_t * _mongoc_change_stream_new (const mongoc_collection_t *coll, const bson_t *pipeline, const bson_t *opts) { bool full_doc_set = false; mongoc_change_stream_t *stream = (mongoc_change_stream_t *) bson_malloc0 (sizeof (mongoc_change_stream_t)); BSON_ASSERT (coll); BSON_ASSERT (pipeline); stream->max_await_time_ms = -1; stream->batch_size = -1; stream->coll = mongoc_collection_copy ((mongoc_collection_t *) coll); bson_init (&stream->pipeline_to_append); bson_init (&stream->full_document); bson_init (&stream->opts); bson_init (&stream->resume_token); bson_init (&stream->err_doc); /* * The passed options may consist of: * fullDocument: 'default'|'updateLookup', passed to $changeStream stage * resumeAfter: optional<Doc>, passed to $changeStream stage * maxAwaitTimeMS: Optional<Int64>, set on the cursor * batchSize: Optional<Int32>, passed as agg option, {cursor: { batchSize: }} * standard command options like "sessionId", "maxTimeMS", or "collation" */ if (opts) { bson_iter_t iter; if (bson_iter_init_find (&iter, opts, "fullDocument")) { SET_BSON_OR_ERR (&stream->full_document, "fullDocument"); full_doc_set = true; } if (bson_iter_init_find (&iter, opts, "resumeAfter")) { SET_BSON_OR_ERR (&stream->resume_token, "resumeAfter"); } if (bson_iter_init_find (&iter, opts, "batchSize")) { if (BSON_ITER_HOLDS_INT32 (&iter)) { stream->batch_size = bson_iter_int32 (&iter); } } if (bson_iter_init_find (&iter, opts, "maxAwaitTimeMS") && BSON_ITER_HOLDS_INT (&iter)) { stream->max_await_time_ms = bson_iter_as_int64 (&iter); } /* save the remaining opts for mongoc_collection_read_command_with_opts */ bson_copy_to_excluding_noinit (opts, &stream->opts, "fullDocument", "resumeAfter", "batchSize", "maxAwaitTimeMS", NULL); } if (!full_doc_set) { if (!BSON_APPEND_UTF8 ( &stream->full_document, "fullDocument", "default")) { CHANGE_STREAM_ERR ("fullDocument"); } } /* Accept two forms of user pipeline: * 1. A document like: { "pipeline": [...] } * 2. An array-like document: { "0": {}, "1": {}, ... } * If the passed pipeline is invalid, we pass it along and let the server * error instead. */ if (!bson_empty (pipeline)) { bson_iter_t iter; if (bson_iter_init_find (&iter, pipeline, "pipeline") && BSON_ITER_HOLDS_ARRAY (&iter)) { SET_BSON_OR_ERR (&stream->pipeline_to_append, "pipeline"); } else { if (!BSON_APPEND_ARRAY ( &stream->pipeline_to_append, "pipeline", pipeline)) { CHANGE_STREAM_ERR ("pipeline"); } } } if (stream->err.code == 0) { (void) _make_cursor (stream); } return stream; }