/[LeafOK_CVS]/lbbs/src/user_priv.c
ViewVC logotype

Diff of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Mon Apr 28 03:31:00 2025 UTC Revision 1.12 by sysadm, Tue May 6 05:31:26 2025 UTC
# Line 1  Line 1 
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    BBS_user_priv BBS_priv;
26    
27  int checklevel(BBS_user_priv *p_priv, int level)  int checklevel(BBS_user_priv *p_priv, int level)
28  {  {
29          return (((p_priv->level & level)) ^ level ? 0 : 1);          if (level == P_GUEST)
30            {
31                    return 1;
32            }
33    
34            return ((p_priv->level & level) ? 1 : 0);
35  }  }
36    
37  int setpriv(BBS_user_priv *p_priv, int sid, int priv)  int setpriv(BBS_user_priv *p_priv, int sid, int priv)
# Line 68  int getpriv(BBS_user_priv *p_priv, int s Line 78  int getpriv(BBS_user_priv *p_priv, int s
78    
79  int checkpriv(BBS_user_priv *p_priv, int sid, int priv)  int checkpriv(BBS_user_priv *p_priv, int sid, int priv)
80  {  {
81          return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1);          return (((getpriv(p_priv, sid) & priv)) == priv ? 1 : 0);
82  }  }
83    
84  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)
                           long int auth_uid, int priv_excluse)  
85  {  {
86          MYSQL_RES *rs;          MYSQL_RES *rs;
87          MYSQL_ROW row;          MYSQL_ROW row;
88          char sql[1024];          char sql[SQL_BUFFER_LEN];
         int i;  
89    
90          p_priv->uid = uid;          p_priv->uid = uid;
         p_priv->auid = auth_uid;  
91          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
         p_priv->level |= (auth_uid == 0 ? P_GUEST : P_AUTH_USER);  
92          p_priv->g_priv = S_DEFAULT;          p_priv->g_priv = S_DEFAULT;
93    
94          if (db == NULL)          if (db == NULL)
95                  return 1;                  return 1;
96    
         // Admin  
         sprintf(sql, "select aid,major from admin_config where UID=%ld"  
                                  " and enable and (now() between begin_dt and end_dt)",  
                         uid);  
         if (mysql_query(db, sql) != 0)  
         {  
                 log_error("Query admin_config failed\n");  
                 return -1;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get admin_config data failed\n");  
                 return -1;  
         }  
         if (row = mysql_fetch_row(rs))  
         {  
                 p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S);  
                 p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN);  
         }  
         mysql_free_result(rs);  
   
97          // Permission          // Permission
98          sprintf(sql, "select p_post,p_msg,p_mail "          snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",
                                  "from user_list where UID=%ld",  
99                          uid);                          uid);
100          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
101          {          {
# Line 123  int load_priv(MYSQL *db, BBS_user_priv * Line 107  int load_priv(MYSQL *db, BBS_user_priv *
107                  log_error("Get user_list data failed\n");                  log_error("Get user_list data failed\n");
108                  return -1;                  return -1;
109          }          }
110          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
111          {          {
112                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
113                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
                 p_priv->g_priv |= (atoi(row[2]) ? S_MAIL : 0);  
         }  
         mysql_free_result(rs);  
   
         // Verified  
         sprintf(sql, "select verified from user_list where"  
                                  " UID=%ld",  
                         uid);  
         if (mysql_query(db, sql) != 0)  
         {  
                 log_error("Query user_list failed\n");  
                 return -1;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get user_list data failed\n");  
                 return -1;  
         }  
         if (row = mysql_fetch_row(rs))  
                 p_priv->g_priv &= (atoi(row[0]) ? p_priv->g_priv : S_DEFAULT);  
         mysql_free_result(rs);  
   
         // IP ban  
         sprintf(sql, "select begin_ip,end_ip from ban_ip_list"  
                                  " where ('%s' between begin_ip and end_ip) and enable",  
                         hostaddr_client);  
         if (mysql_query(db, sql) != 0)  
         {  
                 log_error("Query ban_ip_list failed\n");  
                 return -1;  
         }  
         if ((rs = mysql_store_result(db)) == NULL)  
         {  
                 log_error("Get ban_ip_list data failed\n");  
                 return -1;  
114          }          }
         if (mysql_num_rows(rs) > 0)  
                 p_priv->g_priv &= S_DEFAULT;  
115          mysql_free_result(rs);          mysql_free_result(rs);
116    
117          // Section Class Master          // Admin
118          sprintf(sql, "select SID from section_class_master"          snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "
119                                   " left join section_config on section_class_master.CID"                                   "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
                                  "=section_config.CID where UID=%ld and section_class_master.enable"  
                                  " and (now() between begin_dt and end_dt)",  
120                          uid);                          uid);
121          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
122          {          {
123                  log_error("Query section_class_master failed\n");                  log_error("Query admin_config failed\n");
124                  return -1;                  return -1;
125          }          }
126          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
127          {          {
128                  log_error("Get section_class_master data failed\n");                  log_error("Get admin_config data failed\n");
129                  return -1;                  return -1;
130          }          }
131          while (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
132          {          {
133                  p_priv->level |= P_MAN_C;                  p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
134                  setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | S_MAN_M);                  p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
135          }          }
136          mysql_free_result(rs);          mysql_free_result(rs);
137    
138          // Section Master          // Section Master
139          sprintf(sql, "select SID,major from section_master where"          snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "
140                                   " UID=%ld and enable and (now() between begin_dt and"                                   "INNER JOIN section_config ON section_master.SID = section_config.SID "
141                                   " end_dt)",                                   "WHERE UID = %ld AND section_master.enable AND section_config.enable "
142                                     "AND (NOW() BETWEEN begin_dt AND end_dt)",
143                          uid);                          uid);
144          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
145          {          {
# Line 205  int load_priv(MYSQL *db, BBS_user_priv * Line 151  int load_priv(MYSQL *db, BBS_user_priv *
151                  log_error("Get section_master data failed\n");                  log_error("Get section_master data failed\n");
152                  return -1;                  return -1;
153          }          }
154          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
155          {          {
156                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
157                  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));
# Line 213  int load_priv(MYSQL *db, BBS_user_priv * Line 159  int load_priv(MYSQL *db, BBS_user_priv *
159          mysql_free_result(rs);          mysql_free_result(rs);
160    
161          // Section status          // Section status
162          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 "
163                                   "write_user_level from section_config"                                   "INNER JOIN section_class ON section_config.CID = section_class.CID "
164                                   " left join section_class on section_config.CID="                                   "WHERE section_config.enable AND section_class.enable "
165                                   "section_class.CID where section_config.enable and"                                   "ORDER BY SID");
                                  " section_class.enable order by SID");  
166          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
167          {          {
168                  log_error("Query section_config failed\n");                  log_error("Query section_config failed\n");
# Line 228  int load_priv(MYSQL *db, BBS_user_priv * Line 173  int load_priv(MYSQL *db, BBS_user_priv *
173                  log_error("Get section_config data failed\n");                  log_error("Get section_config data failed\n");
174                  return -1;                  return -1;
175          }          }
176          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
177          {          {
178                    int priv = getpriv(p_priv, atoi(row[0]));
179                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
180                          setpriv(p_priv, atoi(row[0]),                  {
181                                          getpriv(p_priv, atoi(row[0])) & (~S_LIST));                          priv &= (~S_LIST);
182                    }
183                  if (p_priv->level < atoi(row[3]))                  if (p_priv->level < atoi(row[3]))
184                          setpriv(p_priv, atoi(row[0]),                  {
185                                          getpriv(p_priv, atoi(row[0])) & (~S_POST));                          priv &= (~S_POST);
186                    }
187                  if (!atoi(row[1]))                  if (!atoi(row[1]))
188                          setpriv(p_priv, atoi(row[0]),                  {
189                                          getpriv(p_priv, atoi(row[0])) & (~S_GETEXP));                          priv &= (~S_GETEXP);
190          }                  }
191          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));  
192          }          }
193          mysql_free_result(rs);          mysql_free_result(rs);
194    
195          // Section ban          // Section ban
196          sprintf(sql, "select SID from ban_user_list where"          snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
197                                   " UID=%ld and enable and unban_UID=0 and"                                   "AND (NOW() BETWEEN ban_dt AND unban_dt)",
                                  " (now() between ban_dt and unban_dt)",  
198                          uid);                          uid);
199          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
200          {          {
# Line 282  int load_priv(MYSQL *db, BBS_user_priv * Line 206  int load_priv(MYSQL *db, BBS_user_priv *
206                  log_error("Get ban_user_list data failed\n");                  log_error("Get ban_user_list data failed\n");
207                  return -1;                  return -1;
208          }          }
209          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
210          {          {
211                  setpriv(p_priv, atoi(row[0]),                  setpriv(p_priv, atoi(row[0]),
212                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));
213          }          }
214          mysql_free_result(rs);          mysql_free_result(rs);
215    
         // Priv exclusion  
         p_priv->g_priv &= (~priv_excluse);  
         for (i = 0; i < p_priv->s_count; i++)  
                 p_priv->s_priv_list[i].s_priv &= (~priv_excluse);  
   
         if (priv_excluse & S_MAN_M)  
                 p_priv->level &= (P_AUTH_USER | P_USER);  
   
216          return 0;          return 0;
217  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1