mndPrivilege.c 5.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
17
#include "mndPrivilege.h"
18
#include "mndDb.h"
19
#include "mndUser.h"
S
Shengliang Guan 已提交
20

21
int32_t mndInitPrivilege(SMnode *pMnode) { return 0; }
S
Shengliang Guan 已提交
22

23
void mndCleanupPrivilege(SMnode *pMnode) {}
S
Shengliang Guan 已提交
24

25
int32_t mndCheckOperPrivilege(SMnode *pMnode, const char *user, EOperType operType) {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  int32_t   code = 0;
  SUserObj *pUser = mndAcquireUser(pMnode, user);

  if (pUser == NULL) {
    terrno = TSDB_CODE_MND_NO_USER_FROM_CONN;
    code = -1;
    goto _OVER;
  }

  if (pUser->superUser) {
    goto _OVER;
  }

  if (!pUser->enable) {
    terrno = TSDB_CODE_MND_USER_DISABLED;
    code = -1;
    goto _OVER;
  }

S
Shengliang Guan 已提交
45 46
  switch (operType) {
    case MND_OPER_CONNECT:
47 48
    case MND_OPER_CREATE_FUNC:
    case MND_OPER_DROP_FUNC:
49
    case MND_OPER_SHOW_VARIBALES:
S
Shengliang Guan 已提交
50 51 52 53 54
      break;
    default:
      terrno = TSDB_CODE_MND_NO_RIGHTS;
      code = -1;
  }
55 56 57 58

_OVER:
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
59 60
}

61
int32_t mndCheckAlterUserPrivilege(SUserObj *pOperUser, SUserObj *pUser, SAlterUserReq *pAlter) {
62 63 64 65 66
  if (pUser->superUser && pAlter->alterType != TSDB_ALTER_USER_PASSWD) {
    terrno = TSDB_CODE_MND_NO_RIGHTS;
    return -1;
  }

67
  if (pOperUser->superUser) return 0;
68

69 70 71 72
  if (!pOperUser->enable) {
    terrno = TSDB_CODE_MND_USER_DISABLED;
    return -1;
  }
S
Shengliang Guan 已提交
73

74 75 76
  if (pAlter->alterType == TSDB_ALTER_USER_PASSWD) {
    if (strcmp(pUser->user, pOperUser->user) == 0) {
      if (pOperUser->sysInfo) return 0;
S
Shengliang Guan 已提交
77
    }
78
  }
S
Shengliang Guan 已提交
79 80 81 82 83

  terrno = TSDB_CODE_MND_NO_RIGHTS;
  return -1;
}

84
int32_t mndCheckShowPrivilege(SMnode *pMnode, const char *user, EShowType showType, const char *dbname) {
85 86
  int32_t   code = 0;
  SUserObj *pUser = mndAcquireUser(pMnode, user);
S
Shengliang Guan 已提交
87

88 89 90 91
  if (pUser == NULL) {
    code = -1;
    goto _OVER;
  }
S
Shengliang Guan 已提交
92

93 94 95 96 97 98 99 100 101 102
  if (pUser->superUser) {
    goto _OVER;
  }

  if (!pUser->enable) {
    terrno = TSDB_CODE_MND_USER_DISABLED;
    code = -1;
    goto _OVER;
  }

103
  if (pUser->sysInfo) {
104 105
    goto _OVER;
  }
S
Shengliang Guan 已提交
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  switch (showType) {
    case TSDB_MGMT_TABLE_DB:
    case TSDB_MGMT_TABLE_STB:
    case TSDB_MGMT_TABLE_INDEX:
    case TSDB_MGMT_TABLE_STREAMS:
    case TSDB_MGMT_TABLE_CONSUMERS:
    case TSDB_MGMT_TABLE_TOPICS:
    case TSDB_MGMT_TABLE_SUBSCRIPTIONS:
    case TSDB_MGMT_TABLE_FUNC:
    case TSDB_MGMT_TABLE_QUERIES:
    case TSDB_MGMT_TABLE_CONNS:
    case TSDB_MGMT_TABLE_APPS:
    case TSDB_MGMT_TABLE_TRANS:
      code = 0;
      break;
    default:
      terrno = TSDB_CODE_MND_NO_RIGHTS;
      code = -1;
      goto _OVER;
  }

  if (showType == TSDB_MGMT_TABLE_STB || showType == TSDB_MGMT_TABLE_VGROUP || showType == TSDB_MGMT_TABLE_INDEX) {
    code = mndCheckDbPrivilegeByName(pMnode, user, MND_OPER_READ_OR_WRITE_DB, dbname);
  }
131 132 133 134

_OVER:
  mndReleaseUser(pMnode, pUser);
  return code;
S
Shengliang Guan 已提交
135
}
S
Shengliang Guan 已提交
136

137
int32_t mndCheckDbPrivilege(SMnode *pMnode, const char *user, EOperType operType, SDbObj *pDb) {
138 139
  int32_t   code = 0;
  SUserObj *pUser = mndAcquireUser(pMnode, user);
S
Shengliang Guan 已提交
140

141 142 143
  if (pUser == NULL) {
    code = -1;
    goto _OVER;
S
Shengliang Guan 已提交
144 145
  }

146
  if (pUser->superUser) goto _OVER;
S
Shengliang Guan 已提交
147

148 149 150 151 152
  if (!pUser->enable) {
    terrno = TSDB_CODE_MND_USER_DISABLED;
    code = -1;
    goto _OVER;
  }
S
Shengliang Guan 已提交
153

154 155
  if (operType == MND_OPER_CREATE_DB) {
    if (pUser->sysInfo) goto _OVER;
S
Shengliang Guan 已提交
156 157
  }

158
  if (operType == MND_OPER_ALTER_DB || operType == MND_OPER_DROP_DB || operType == MND_OPER_COMPACT_DB) {
159
    if (strcmp(pUser->user, pDb->createUser) == 0 && pUser->sysInfo) goto _OVER;
S
Shengliang Guan 已提交
160 161
  }

162
  if (operType == MND_OPER_USE_DB || operType == MND_OPER_READ_OR_WRITE_DB) {
163 164 165 166 167 168 169 170 171 172 173 174 175
    if (strcmp(pUser->user, pDb->createUser) == 0) goto _OVER;
    if (taosHashGet(pUser->readDbs, pDb->name, strlen(pDb->name) + 1) != NULL) goto _OVER;
    if (taosHashGet(pUser->writeDbs, pDb->name, strlen(pDb->name) + 1) != NULL) goto _OVER;
  }

  if (operType == MND_OPER_WRITE_DB) {
    if (strcmp(pUser->user, pDb->createUser) == 0) goto _OVER;
    if (taosHashGet(pUser->writeDbs, pDb->name, strlen(pDb->name) + 1) != NULL) goto _OVER;
  }

  if (operType == MND_OPER_READ_DB) {
    if (strcmp(pUser->user, pDb->createUser) == 0) goto _OVER;
    if (taosHashGet(pUser->readDbs, pDb->name, strlen(pDb->name) + 1) != NULL) goto _OVER;
S
Shengliang Guan 已提交
176 177 178
  }

  terrno = TSDB_CODE_MND_NO_RIGHTS;
179 180 181 182 183
  code = -1;

_OVER:
  mndReleaseUser(pMnode, pUser);
  return code;
184
}
185

186 187
int32_t mndCheckDbPrivilegeByName(SMnode *pMnode, const char *user, EOperType operType, const char *dbname) {
  SDbObj *pDb = mndAcquireDb(pMnode, dbname);
188 189 190 191 192 193
  if (pDb == NULL) return -1;

  int32_t code = mndCheckDbPrivilege(pMnode, user, operType, pDb);
  mndReleaseDb(pMnode, pDb);
  return code;
}