| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
user_priv.c - description |
user_priv.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 22 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#include "user_priv.h" |
| 18 |
#include "bbs.h" |
#include "bbs.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
|
#include "database.h" |
| 21 |
|
#include "log.h" |
| 22 |
|
#include <stdio.h> |
| 23 |
#include <mysql.h> |
#include <mysql.h> |
| 24 |
|
|
| 25 |
int checklevel(BBS_user_priv *p_priv, int level) |
BBS_user_priv BBS_priv; |
|
{ |
|
|
return (((p_priv->level & level)) ^ level ? 0 : 1); |
|
|
} |
|
| 26 |
|
|
| 27 |
int setpriv(BBS_user_priv *p_priv, int sid, int priv) |
int setpriv(BBS_user_priv *p_priv, int sid, int priv) |
| 28 |
{ |
{ |
| 66 |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
return (sid >= 0 ? p_priv->g_priv : S_NONE); |
| 67 |
} |
} |
| 68 |
|
|
|
int checkpriv(BBS_user_priv *p_priv, int sid, int priv) |
|
|
{ |
|
|
return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1); |
|
|
} |
|
|
|
|
| 69 |
int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid) |
int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid) |
| 70 |
{ |
{ |
| 71 |
MYSQL_RES *rs; |
MYSQL_RES *rs; |
| 72 |
MYSQL_ROW row; |
MYSQL_ROW row; |
| 73 |
char sql[1024]; |
char sql[SQL_BUFFER_LEN]; |
|
int i; |
|
| 74 |
|
|
| 75 |
p_priv->uid = uid; |
p_priv->uid = uid; |
| 76 |
p_priv->level = (uid == 0 ? P_GUEST : P_USER); |
p_priv->level = (uid == 0 ? P_GUEST : P_USER); |
| 77 |
p_priv->g_priv = S_DEFAULT; |
p_priv->g_priv = S_DEFAULT; |
| 78 |
|
p_priv->s_count = 0; |
| 79 |
|
|
| 80 |
if (db == NULL) |
if (db == NULL) |
| 81 |
return 1; |
return 1; |
| 82 |
|
|
| 83 |
// Admin |
// Permission |
| 84 |
sprintf(sql, "select aid,major from admin_config where UID=%ld" |
snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified", |
|
" and enable and (now() between begin_dt and end_dt)", |
|
| 85 |
uid); |
uid); |
| 86 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 87 |
{ |
{ |
| 88 |
log_error("Query admin_config failed\n"); |
log_error("Query user_list failed\n"); |
| 89 |
return -1; |
return -1; |
| 90 |
} |
} |
| 91 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 92 |
{ |
{ |
| 93 |
log_error("Get admin_config data failed\n"); |
log_error("Get user_list data failed\n"); |
| 94 |
return -1; |
return -1; |
| 95 |
} |
} |
| 96 |
if (row = mysql_fetch_row(rs)) |
if ((row = mysql_fetch_row(rs))) |
| 97 |
{ |
{ |
| 98 |
p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S); |
p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0); |
| 99 |
p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN); |
p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0); |
| 100 |
} |
} |
| 101 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 102 |
|
|
| 103 |
// Permission |
// Admin |
| 104 |
sprintf(sql, "select p_post,p_msg,p_mail " |
snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld " |
| 105 |
"from user_list where UID=%ld", |
"AND enable AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 106 |
uid); |
uid); |
| 107 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 108 |
{ |
{ |
| 109 |
log_error("Query user_list failed\n"); |
log_error("Query admin_config failed\n"); |
| 110 |
return -1; |
return -1; |
| 111 |
} |
} |
| 112 |
if ((rs = mysql_store_result(db)) == NULL) |
if ((rs = mysql_store_result(db)) == NULL) |
| 113 |
{ |
{ |
| 114 |
log_error("Get user_list data failed\n"); |
log_error("Get admin_config data failed\n"); |
| 115 |
return -1; |
return -1; |
| 116 |
} |
} |
| 117 |
if (row = mysql_fetch_row(rs)) |
if ((row = mysql_fetch_row(rs))) |
| 118 |
{ |
{ |
| 119 |
p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0); |
p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S); |
| 120 |
p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0); |
p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN); |
|
p_priv->g_priv |= (atoi(row[2]) ? S_MAIL : 0); |
|
| 121 |
} |
} |
| 122 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 123 |
|
|
| 124 |
// Section Master |
// Section Master |
| 125 |
sprintf(sql, "select SID,major from section_master where" |
snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master " |
| 126 |
" UID=%ld and enable and (now() between begin_dt and" |
"INNER JOIN section_config ON section_master.SID = section_config.SID " |
| 127 |
" end_dt)", |
"WHERE UID = %ld AND section_master.enable AND section_config.enable " |
| 128 |
|
"AND (NOW() BETWEEN begin_dt AND end_dt)", |
| 129 |
uid); |
uid); |
| 130 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 131 |
{ |
{ |
| 137 |
log_error("Get section_master data failed\n"); |
log_error("Get section_master data failed\n"); |
| 138 |
return -1; |
return -1; |
| 139 |
} |
} |
| 140 |
while (row = mysql_fetch_row(rs)) |
while ((row = mysql_fetch_row(rs))) |
| 141 |
{ |
{ |
| 142 |
p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S); |
p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S); |
| 143 |
setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S)); |
setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S)); |
| 145 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 146 |
|
|
| 147 |
// Section status |
// Section status |
| 148 |
sprintf(sql, "select SID,exp_get,read_user_level," |
snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config " |
| 149 |
"write_user_level from section_config" |
"INNER JOIN section_class ON section_config.CID = section_class.CID " |
| 150 |
" left join section_class on section_config.CID=" |
"WHERE section_config.enable AND section_class.enable " |
| 151 |
"section_class.CID where section_config.enable and" |
"ORDER BY SID"); |
|
" section_class.enable order by SID"); |
|
| 152 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 153 |
{ |
{ |
| 154 |
log_error("Query section_config failed\n"); |
log_error("Query section_config failed\n"); |
| 159 |
log_error("Get section_config data failed\n"); |
log_error("Get section_config data failed\n"); |
| 160 |
return -1; |
return -1; |
| 161 |
} |
} |
| 162 |
while (row = mysql_fetch_row(rs)) |
while ((row = mysql_fetch_row(rs))) |
| 163 |
{ |
{ |
| 164 |
|
int priv = getpriv(p_priv, atoi(row[0])); |
| 165 |
if (p_priv->level < atoi(row[2])) |
if (p_priv->level < atoi(row[2])) |
| 166 |
setpriv(p_priv, atoi(row[0]), |
{ |
| 167 |
getpriv(p_priv, atoi(row[0])) & (~S_LIST)); |
priv &= (~S_LIST); |
| 168 |
|
} |
| 169 |
if (p_priv->level < atoi(row[3])) |
if (p_priv->level < atoi(row[3])) |
| 170 |
setpriv(p_priv, atoi(row[0]), |
{ |
| 171 |
getpriv(p_priv, atoi(row[0])) & (~S_POST)); |
priv &= (~S_POST); |
| 172 |
|
} |
| 173 |
if (!atoi(row[1])) |
if (!atoi(row[1])) |
| 174 |
setpriv(p_priv, atoi(row[0]), |
{ |
| 175 |
getpriv(p_priv, atoi(row[0])) & (~S_GETEXP)); |
priv &= (~S_GETEXP); |
| 176 |
} |
} |
| 177 |
mysql_free_result(rs); |
setpriv(p_priv, atoi(row[0]), priv); |
|
|
|
|
// Section User priv |
|
|
sprintf(sql, "select SID,`read`,`write` from section_user_priv" |
|
|
" where UID=%ld order by SID", |
|
|
uid); |
|
|
if (mysql_query(db, sql) != 0) |
|
|
{ |
|
|
log_error("Query section_user_priv failed\n"); |
|
|
return -1; |
|
|
} |
|
|
if ((rs = mysql_store_result(db)) == NULL) |
|
|
{ |
|
|
log_error("Get section_user_priv data failed\n"); |
|
|
return -1; |
|
|
} |
|
|
while (row = mysql_fetch_row(rs)) |
|
|
{ |
|
|
setpriv(p_priv, atoi(row[0]), |
|
|
atoi(row[1]) ? (getpriv(p_priv, atoi(row[0])) | S_LIST) |
|
|
: (getpriv(p_priv, atoi(row[0])) & ~S_LIST)); |
|
|
setpriv(p_priv, atoi(row[0]), |
|
|
atoi(row[2]) ? (getpriv(p_priv, atoi(row[0])) | S_POST) |
|
|
: (getpriv(p_priv, atoi(row[0])) & ~S_POST)); |
|
| 178 |
} |
} |
| 179 |
mysql_free_result(rs); |
mysql_free_result(rs); |
| 180 |
|
|
| 181 |
// Section ban |
// Section ban |
| 182 |
sprintf(sql, "select SID from ban_user_list where" |
snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable " |
| 183 |
" UID=%ld and enable and unban_UID=0 and" |
"AND (NOW() BETWEEN ban_dt AND unban_dt)", |
|
" (now() between ban_dt and unban_dt)", |
|
| 184 |
uid); |
uid); |
| 185 |
if (mysql_query(db, sql) != 0) |
if (mysql_query(db, sql) != 0) |
| 186 |
{ |
{ |
| 192 |
log_error("Get ban_user_list data failed\n"); |
log_error("Get ban_user_list data failed\n"); |
| 193 |
return -1; |
return -1; |
| 194 |
} |
} |
| 195 |
while (row = mysql_fetch_row(rs)) |
while ((row = mysql_fetch_row(rs))) |
| 196 |
{ |
{ |
| 197 |
setpriv(p_priv, atoi(row[0]), |
setpriv(p_priv, atoi(row[0]), |
| 198 |
getpriv(p_priv, atoi(row[0])) & (~S_POST)); |
getpriv(p_priv, atoi(row[0])) & (~S_POST)); |