@@ -315,6 +315,54 @@ namespace
315315 }
316316 }
317317
318+ void attachDatabase(ISC_STATUS* statusVector, const Firebird::PathName& connName,
319+ isc_db_handle* dbHandle, Firebird::UtilSvc* uSvc, const TEXT* username,
320+ const TEXT* password, const TEXT* role
321+ #ifdef TRUSTED_AUTH
322+ , bool trustedAuth
323+ #endif
324+ )
325+ {
326+ // Check to make sure that the user accessing the database is either
327+ // SYSDBA or owner of the database.
328+
329+ Firebird::ClumpletWriter dpb(Firebird::ClumpletReader::dpbList, MAX_DPB_SIZE);
330+ uSvc->fillDpb(dpb);
331+ dpb.insertTag(isc_dpb_gstat_attach);
332+ dpb.insertTag(isc_dpb_no_garbage_collect);
333+
334+ const unsigned char* authBlock;
335+ unsigned int authBlockSize = uSvc->getAuthBlock(&authBlock);
336+ if (authBlockSize)
337+ {
338+ dpb.insertBytes(isc_dpb_auth_block, authBlock, authBlockSize);
339+ }
340+ else
341+ {
342+ if (*username)
343+ dpb.insertString(isc_dpb_user_name, username, fb_strlen(username));
344+
345+ if (*password)
346+ {
347+ dpb.insertString(uSvc->isService() ? isc_dpb_password_enc : isc_dpb_password,
348+ password, fb_strlen(password));
349+ }
350+
351+ #ifdef TRUSTED_AUTH
352+ if (trustedAuth)
353+ dpb.insertTag(isc_dpb_trusted_auth);
354+ #endif
355+ }
356+
357+ if (*role)
358+ dpb.insertString(isc_dpb_sql_role_name, role, fb_strlen(role));
359+
360+ dpb.insertString(isc_dpb_search_path, SYSTEM_SCHEMA);
361+
362+ isc_attach_database(statusVector, 0, connName.c_str(), dbHandle, dpb.getBufferLength(),
363+ reinterpret_cast<const char*>(dpb.getBuffer()));
364+ }
365+
318366 void getDateTime(char* datetime, FB_SIZE_T sizeof_datetime) noexcept
319367 {
320368 time_t t;
@@ -623,6 +671,22 @@ int gstat(Firebird::UtilSvc* uSvc)
623671
624672 Firebird::PathName fileName = name;
625673 const Firebird::PathName connName = fileName;
674+
675+ if (uSvc->isService())
676+ {
677+ attachDatabase(status_vector, connName, &DB, uSvc, username, password, role
678+ #ifdef TRUSTED_AUTH
679+ , trusted_auth
680+ #endif
681+ );
682+
683+ if (status_vector[1])
684+ dba_exit(FINI_ERROR, tddba);
685+
686+ if (sw_version)
687+ isc_version(&DB, NULL, NULL);
688+ }
689+
626690 Firebird::PathName tempStr;
627691
628692 if (!ISC_analyze_tcp(fileName, tempStr))
@@ -763,48 +827,20 @@ int gstat(Firebird::UtilSvc* uSvc)
763827 dba_exit(FINI_OK, tddba);
764828 }
765829
766- // Check to make sure that the user accessing the database is either
767- // SYSDBA or owner of the database
768-
769- Firebird::ClumpletWriter dpb(Firebird::ClumpletReader::dpbList, MAX_DPB_SIZE);
770- uSvc->fillDpb(dpb);
771- dpb.insertTag(isc_dpb_gstat_attach);
772- dpb.insertTag(isc_dpb_no_garbage_collect);
773-
774- const unsigned char* authBlock;
775- const unsigned int authBlockSize = uSvc->getAuthBlock(&authBlock);
776- if (authBlockSize)
777- {
778- dpb.insertBytes(isc_dpb_auth_block, authBlock, authBlockSize);
779- }
780- else
830+ if (!uSvc->isService())
781831 {
782- if (*username)
783- dpb.insertString(isc_dpb_user_name, username, fb_strlen(username));
784-
785- if (*password)
786- {
787- dpb.insertString(uSvc->isService() ? isc_dpb_password_enc : isc_dpb_password,
788- password, fb_strlen(password));
789- }
790-
832+ attachDatabase(status_vector, connName, &DB, uSvc, username, password, role
791833#ifdef TRUSTED_AUTH
792- if (trusted_auth)
793- dpb.insertTag(isc_dpb_trusted_auth);
834+ , trusted_auth
794835#endif
795- }
796- if (*role)
797- dpb.insertString(isc_dpb_sql_role_name, role, fb_strlen(role));
798-
799- dpb.insertString(isc_dpb_search_path, SYSTEM_SCHEMA);
836+ );
800837
801- isc_attach_database(status_vector, 0, connName.c_str(), &DB, dpb.getBufferLength(),
802- reinterpret_cast<const char*>(dpb.getBuffer()));
803- if (status_vector[1])
804- dba_exit(FINI_ERROR, tddba);
838+ if (status_vector[1])
839+ dba_exit(FINI_ERROR, tddba);
805840
806- if (sw_version)
807- isc_version(&DB, NULL, NULL);
841+ if (sw_version)
842+ isc_version(&DB, NULL, NULL);
843+ }
808844
809845 isc_tr_handle transact1 = 0;
810846 START_TRANSACTION transact1 READ_ONLY;
@@ -1252,8 +1288,8 @@ int gstat(Firebird::UtilSvc* uSvc)
12521288 if ((exit_code != FINI_OK) && uSvc->isService() && tddba->dba_status[1])
12531289 {
12541290 Firebird::UtilSvc::StatusAccessor sa = uSvc->getStatusAccessor();
1255- sa.init();
1256- sa.setServiceStatus(tddba->dba_status);
1291+ if (!(sa->getState() & IStatus::STATE_ERRORS))
1292+ sa.setServiceStatus(tddba->dba_status);
12571293 }
12581294
12591295 return exit_code;
0 commit comments