|
8 | 8 | *
|
9 | 9 | *
|
10 | 10 | * IDENTIFICATION
|
11 |
| - * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.148 2009/06/11 14:49:03 momjian Exp $ |
| 11 | + * $PostgreSQL: pgsql/src/backend/utils/adt/acl.c,v 1.149 2009/08/03 21:11:39 joe Exp $ |
12 | 12 | *
|
13 | 13 | *-------------------------------------------------------------------------
|
14 | 14 | */
|
|
20 | 20 | #include"catalog/pg_authid.h"
|
21 | 21 | #include"catalog/pg_auth_members.h"
|
22 | 22 | #include"catalog/pg_type.h"
|
| 23 | +#include"catalog/pg_class.h" |
23 | 24 | #include"commands/dbcommands.h"
|
24 | 25 | #include"commands/tablespace.h"
|
25 | 26 | #include"foreign/foreign.h"
|
@@ -88,6 +89,7 @@ static AclMode convert_any_priv_string(text *priv_type_text,
|
88 | 89 |
|
89 | 90 | staticOidconvert_table_name(text*tablename);
|
90 | 91 | staticAclModeconvert_table_priv_string(text*priv_type_text);
|
| 92 | +staticAclModeconvert_sequence_priv_string(text*priv_type_text); |
91 | 93 | staticAttrNumberconvert_column_name(Oidtableoid,text*column);
|
92 | 94 | staticAclModeconvert_column_priv_string(text*priv_type_text);
|
93 | 95 | staticOidconvert_database_name(text*databasename);
|
@@ -1704,6 +1706,216 @@ convert_table_priv_string(text *priv_type_text)
|
1704 | 1706 | returnconvert_any_priv_string(priv_type_text,table_priv_map);
|
1705 | 1707 | }
|
1706 | 1708 |
|
| 1709 | +/* |
| 1710 | + * has_sequence_privilege variants |
| 1711 | + *These are all named "has_sequence_privilege" at the SQL level. |
| 1712 | + *They take various combinations of relation name, relation OID, |
| 1713 | + *user name, user OID, or implicit user = current_user. |
| 1714 | + * |
| 1715 | + *The result is a boolean value: true if user has the indicated |
| 1716 | + *privilege, false if not. The variants that take a relation OID |
| 1717 | + *return NULL if the OID doesn't exist. |
| 1718 | + */ |
| 1719 | + |
| 1720 | +/* |
| 1721 | + * has_sequence_privilege_name_name |
| 1722 | + *Check user privileges on a sequence given |
| 1723 | + *name username, text sequencename, and text priv name. |
| 1724 | + */ |
| 1725 | +Datum |
| 1726 | +has_sequence_privilege_name_name(PG_FUNCTION_ARGS) |
| 1727 | +{ |
| 1728 | +Namerolename=PG_GETARG_NAME(0); |
| 1729 | +text*sequencename=PG_GETARG_TEXT_P(1); |
| 1730 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 1731 | +Oidroleid; |
| 1732 | +Oidsequenceoid; |
| 1733 | +AclModemode; |
| 1734 | +AclResultaclresult; |
| 1735 | + |
| 1736 | +roleid=get_roleid_checked(NameStr(*rolename)); |
| 1737 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1738 | +sequenceoid=convert_table_name(sequencename); |
| 1739 | +if (get_rel_relkind(sequenceoid)!=RELKIND_SEQUENCE) |
| 1740 | +ereport(ERROR, |
| 1741 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1742 | +errmsg("\"%s\" is not a sequence", |
| 1743 | +text_to_cstring(sequencename)))); |
| 1744 | + |
| 1745 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1746 | + |
| 1747 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1748 | +} |
| 1749 | + |
| 1750 | +/* |
| 1751 | + * has_sequence_privilege_name |
| 1752 | + *Check user privileges on a sequence given |
| 1753 | + *text sequencename and text priv name. |
| 1754 | + *current_user is assumed |
| 1755 | + */ |
| 1756 | +Datum |
| 1757 | +has_sequence_privilege_name(PG_FUNCTION_ARGS) |
| 1758 | +{ |
| 1759 | +text*sequencename=PG_GETARG_TEXT_P(0); |
| 1760 | +text*priv_type_text=PG_GETARG_TEXT_P(1); |
| 1761 | +Oidroleid; |
| 1762 | +Oidsequenceoid; |
| 1763 | +AclModemode; |
| 1764 | +AclResultaclresult; |
| 1765 | + |
| 1766 | +roleid=GetUserId(); |
| 1767 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1768 | +sequenceoid=convert_table_name(sequencename); |
| 1769 | +if (get_rel_relkind(sequenceoid)!=RELKIND_SEQUENCE) |
| 1770 | +ereport(ERROR, |
| 1771 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1772 | +errmsg("\"%s\" is not a sequence", |
| 1773 | +text_to_cstring(sequencename)))); |
| 1774 | + |
| 1775 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1776 | + |
| 1777 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1778 | +} |
| 1779 | + |
| 1780 | +/* |
| 1781 | + * has_sequence_privilege_name_id |
| 1782 | + *Check user privileges on a sequence given |
| 1783 | + *name usename, sequence oid, and text priv name. |
| 1784 | + */ |
| 1785 | +Datum |
| 1786 | +has_sequence_privilege_name_id(PG_FUNCTION_ARGS) |
| 1787 | +{ |
| 1788 | +Nameusername=PG_GETARG_NAME(0); |
| 1789 | +Oidsequenceoid=PG_GETARG_OID(1); |
| 1790 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 1791 | +Oidroleid; |
| 1792 | +AclModemode; |
| 1793 | +AclResultaclresult; |
| 1794 | +charrelkind; |
| 1795 | + |
| 1796 | +roleid=get_roleid_checked(NameStr(*username)); |
| 1797 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1798 | +relkind=get_rel_relkind(sequenceoid); |
| 1799 | +if (relkind=='\0') |
| 1800 | +PG_RETURN_NULL(); |
| 1801 | +elseif (relkind!=RELKIND_SEQUENCE) |
| 1802 | +ereport(ERROR, |
| 1803 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1804 | +errmsg("\"%s\" is not a sequence", |
| 1805 | +get_rel_name(sequenceoid)))); |
| 1806 | + |
| 1807 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1808 | + |
| 1809 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1810 | +} |
| 1811 | + |
| 1812 | +/* |
| 1813 | + * has_sequence_privilege_id |
| 1814 | + *Check user privileges on a sequence given |
| 1815 | + *sequence oid, and text priv name. |
| 1816 | + *current_user is assumed |
| 1817 | + */ |
| 1818 | +Datum |
| 1819 | +has_sequence_privilege_id(PG_FUNCTION_ARGS) |
| 1820 | +{ |
| 1821 | +Oidsequenceoid=PG_GETARG_OID(0); |
| 1822 | +text*priv_type_text=PG_GETARG_TEXT_P(1); |
| 1823 | +Oidroleid; |
| 1824 | +AclModemode; |
| 1825 | +AclResultaclresult; |
| 1826 | +charrelkind; |
| 1827 | + |
| 1828 | +roleid=GetUserId(); |
| 1829 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1830 | +relkind=get_rel_relkind(sequenceoid); |
| 1831 | +if (relkind=='\0') |
| 1832 | +PG_RETURN_NULL(); |
| 1833 | +elseif (relkind!=RELKIND_SEQUENCE) |
| 1834 | +ereport(ERROR, |
| 1835 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1836 | +errmsg("\"%s\" is not a sequence", |
| 1837 | +get_rel_name(sequenceoid)))); |
| 1838 | + |
| 1839 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1840 | + |
| 1841 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1842 | +} |
| 1843 | + |
| 1844 | +/* |
| 1845 | + * has_sequence_privilege_id_name |
| 1846 | + *Check user privileges on a sequence given |
| 1847 | + *roleid, text sequencename, and text priv name. |
| 1848 | + */ |
| 1849 | +Datum |
| 1850 | +has_sequence_privilege_id_name(PG_FUNCTION_ARGS) |
| 1851 | +{ |
| 1852 | +Oidroleid=PG_GETARG_OID(0); |
| 1853 | +text*sequencename=PG_GETARG_TEXT_P(1); |
| 1854 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 1855 | +Oidsequenceoid; |
| 1856 | +AclModemode; |
| 1857 | +AclResultaclresult; |
| 1858 | + |
| 1859 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1860 | +sequenceoid=convert_table_name(sequencename); |
| 1861 | +if (get_rel_relkind(sequenceoid)!=RELKIND_SEQUENCE) |
| 1862 | +ereport(ERROR, |
| 1863 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1864 | +errmsg("\"%s\" is not a sequence", |
| 1865 | +text_to_cstring(sequencename)))); |
| 1866 | + |
| 1867 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1868 | + |
| 1869 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1870 | +} |
| 1871 | + |
| 1872 | +/* |
| 1873 | + * has_sequence_privilege_id_id |
| 1874 | + *Check user privileges on a sequence given |
| 1875 | + *roleid, sequence oid, and text priv name. |
| 1876 | + */ |
| 1877 | +Datum |
| 1878 | +has_sequence_privilege_id_id(PG_FUNCTION_ARGS) |
| 1879 | +{ |
| 1880 | +Oidroleid=PG_GETARG_OID(0); |
| 1881 | +Oidsequenceoid=PG_GETARG_OID(1); |
| 1882 | +text*priv_type_text=PG_GETARG_TEXT_P(2); |
| 1883 | +AclModemode; |
| 1884 | +AclResultaclresult; |
| 1885 | +charrelkind; |
| 1886 | + |
| 1887 | +mode=convert_sequence_priv_string(priv_type_text); |
| 1888 | +relkind=get_rel_relkind(sequenceoid); |
| 1889 | +if (relkind=='\0') |
| 1890 | +PG_RETURN_NULL(); |
| 1891 | +elseif (relkind!=RELKIND_SEQUENCE) |
| 1892 | +ereport(ERROR, |
| 1893 | +(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 1894 | +errmsg("\"%s\" is not a sequence", |
| 1895 | +get_rel_name(sequenceoid)))); |
| 1896 | + |
| 1897 | +aclresult=pg_class_aclcheck(sequenceoid,roleid,mode); |
| 1898 | + |
| 1899 | +PG_RETURN_BOOL(aclresult==ACLCHECK_OK); |
| 1900 | +} |
| 1901 | + |
| 1902 | +/* |
| 1903 | + * convert_sequence_priv_string |
| 1904 | + *Convert text string to AclMode value. |
| 1905 | + */ |
| 1906 | +staticAclMode |
| 1907 | +convert_sequence_priv_string(text*priv_type_text) |
| 1908 | +{ |
| 1909 | +staticconstpriv_mapsequence_priv_map[]= { |
| 1910 | +{"USAGE",ACL_USAGE }, |
| 1911 | +{"SELECT",ACL_SELECT }, |
| 1912 | +{"UPDATE",ACL_UPDATE }, |
| 1913 | +{NULL,0 } |
| 1914 | +}; |
| 1915 | + |
| 1916 | +returnconvert_any_priv_string(priv_type_text,sequence_priv_map); |
| 1917 | +} |
| 1918 | + |
1707 | 1919 |
|
1708 | 1920 | /*
|
1709 | 1921 | * has_any_column_privilege variants
|
|