* Initialize user identity during normal backend startup */
| 705 | * Initialize user identity during normal backend startup |
| 706 | */ |
| 707 | void |
| 708 | InitializeSessionUserId(const char *rolename, Oid roleid) |
| 709 | { |
| 710 | HeapTuple roleTup; |
| 711 | Form_pg_authid rform; |
| 712 | char *rname; |
| 713 | |
| 714 | /* |
| 715 | * Don't do scans if we're bootstrapping, none of the system catalogs |
| 716 | * exist yet, and they should be owned by postgres anyway. |
| 717 | */ |
| 718 | AssertState(!IsBootstrapProcessingMode()); |
| 719 | |
| 720 | /* call only once */ |
| 721 | AssertState(!OidIsValid(AuthenticatedUserId)); |
| 722 | |
| 723 | /* |
| 724 | * Make sure syscache entries are flushed for recent catalog changes. This |
| 725 | * allows us to find roles that were created on-the-fly during |
| 726 | * authentication. |
| 727 | */ |
| 728 | AcceptInvalidationMessages(); |
| 729 | |
| 730 | if (rolename != NULL) |
| 731 | { |
| 732 | roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename)); |
| 733 | if (!HeapTupleIsValid(roleTup)) |
| 734 | ereport(FATAL, |
| 735 | (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), |
| 736 | errmsg("role \"%s\" does not exist", rolename))); |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | roleTup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid)); |
| 741 | if (!HeapTupleIsValid(roleTup)) |
| 742 | ereport(FATAL, |
| 743 | (errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION), |
| 744 | errmsg("role with OID %u does not exist", roleid))); |
| 745 | } |
| 746 | |
| 747 | rform = (Form_pg_authid) GETSTRUCT(roleTup); |
| 748 | roleid = rform->oid; |
| 749 | rname = NameStr(rform->rolname); |
| 750 | |
| 751 | AuthenticatedUserId = roleid; |
| 752 | AuthenticatedUserIsSuperuser = rform->rolsuper; |
| 753 | |
| 754 | /* This sets OuterUserId/CurrentUserId too */ |
| 755 | SetSessionUserId(roleid, AuthenticatedUserIsSuperuser); |
| 756 | |
| 757 | /* Also mark our PGPROC entry with the authenticated user id */ |
| 758 | /* (We assume this is an atomic store so no lock is needed) */ |
| 759 | MyProc->roleId = roleid; |
| 760 | |
| 761 | /* |
| 762 | * These next checks are not enforced when in standalone mode, so that |
| 763 | * there is a way to recover from sillinesses like "UPDATE pg_authid SET |
| 764 | * rolcanlogin = false;". |
no test coverage detected