/[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.6 by sysadm, Mon Apr 28 04:23:38 2025 UTC Revision 1.16 by sysadm, Fri May 30 00:58:02 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    #include <stdlib.h>
25    
26  int checklevel(BBS_user_priv *p_priv, int level)  BBS_user_priv BBS_priv;
 {  
         return (((p_priv->level & level)) ^ level ? 0 : 1);  
 }  
27    
28  int setpriv(BBS_user_priv *p_priv, int sid, int priv)  int setpriv(BBS_user_priv *p_priv, int sid, int priv)
29  {  {
30          int i;          int left = 0;
31          if (sid > 0)          int right = p_priv->s_count - 1;
32            int mid;
33    
34            if (sid == 0)
35          {          {
36                  for (i = 0; i < p_priv->s_count; i++)                  p_priv->g_priv = priv;
37                  {                  return 0;
38                          if (p_priv->s_priv_list[i].sid == sid)          }
39                          {  
40                                  p_priv->s_priv_list[i].s_priv = priv;          while (left < right)
41                                  return 0;          {
42                          }                  mid = (left + right) / 2;
43                  }  
44                  if (i < BBS_max_section)                  if (sid <= p_priv->s_priv_list[mid].sid)
45                  {                  {
46                          p_priv->s_priv_list[i].s_priv = priv;                          right = mid;
47                  }                  }
48                  else                  else
49                  {                  {
50                          return -1;                          left = mid + 1;
51                  }                  }
52          }          }
53          else  
54            if (sid == p_priv->s_priv_list[left].sid) // found
55          {          {
56                  p_priv->g_priv = priv;                  p_priv->s_priv_list[left].s_priv = priv;
57                    return 0;
58            }
59    
60            // not found
61            if (p_priv->s_count >= BBS_max_section)
62            {
63                    return -1;
64          }          }
65    
66            // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
67            for (right = p_priv->s_count - 1; right >= left; right--)
68            {
69                    p_priv->s_priv_list[right + 1] = p_priv->s_priv_list[right];
70            }
71            // insert new item at offset left
72            p_priv->s_priv_list[left].s_priv = priv;
73    
74          return 0;          return 0;
75  }  }
76    
77  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid)
78  {  {
79          int i;          int left = 0;
80          for (i = 0; i < p_priv->s_count; i++)          int right = p_priv->s_count - 1;
81            int mid;
82    
83            while (left < right)
84            {
85                    mid = (left + right) / 2;
86    
87                    if (sid <= p_priv->s_priv_list[mid].sid)
88                    {
89                            right = mid;
90                    }
91                    else
92                    {
93                            left = mid + 1;
94                    }
95            }
96    
97            if (sid == p_priv->s_priv_list[left].sid) // found
98          {          {
99                  if (p_priv->s_priv_list[i].sid == sid)                  return p_priv->s_priv_list[left].s_priv;
                         return p_priv->s_priv_list[i].s_priv;  
100          }          }
101    
102          return (sid >= 0 ? p_priv->g_priv : S_NONE);          return (sid >= 0 ? p_priv->g_priv : S_NONE);
103  }  }
104    
 int checkpriv(BBS_user_priv *p_priv, int sid, int priv)  
 {  
         return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1);  
 }  
   
105  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)
106  {  {
107          MYSQL_RES *rs;          MYSQL_RES *rs;
108          MYSQL_ROW row;          MYSQL_ROW row;
109          char sql[1024];          char sql[SQL_BUFFER_LEN];
         int i;  
110    
111          p_priv->uid = uid;          p_priv->uid = uid;
112          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
113          p_priv->g_priv = S_DEFAULT;          p_priv->g_priv = S_DEFAULT;
114            p_priv->s_count = 0;
115    
116          if (db == NULL)          if (db == NULL)
117                  return 1;                  return 1;
118    
119          // Admin          // Permission
120          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",
121                                   " and enable and (now() between begin_dt and end_dt)",                           uid);
                         uid);  
122          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
123          {          {
124                  log_error("Query admin_config failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
125                  return -1;                  return -1;
126          }          }
127          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
128          {          {
129                  log_error("Get admin_config data failed\n");                  log_error("Get user_list data failed\n");
130                  return -1;                  return -1;
131          }          }
132          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
133          {          {
134                  p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S);                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
135                  p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN);                  p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
136          }          }
137          mysql_free_result(rs);          mysql_free_result(rs);
138    
139          // Permission          // Admin
140          sprintf(sql, "select p_post,p_msg,p_mail "          snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "
141                                   "from user_list where UID=%ld",                                                             "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
142                          uid);                           uid);
143          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
144          {          {
145                  log_error("Query user_list failed\n");                  log_error("Query admin_config error: %s\n", mysql_error(db));
146                  return -1;                  return -1;
147          }          }
148          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
149          {          {
150                  log_error("Get user_list data failed\n");                  log_error("Get admin_config data failed\n");
151                  return -1;                  return -1;
152          }          }
153          if (row = mysql_fetch_row(rs))          if ((row = mysql_fetch_row(rs)))
154          {          {
155                  p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);                  p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
156                  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);  
157          }          }
158          mysql_free_result(rs);          mysql_free_result(rs);
159    
160          // Section Master          // Section Master
161          sprintf(sql, "select SID,major from section_master where"          snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "
162                                   " UID=%ld and enable and (now() between begin_dt and"                                                             "INNER JOIN section_config ON section_master.SID = section_config.SID "
163                                   " end_dt)",                                                             "WHERE UID = %ld AND section_master.enable AND section_config.enable "
164                          uid);                                                             "AND (NOW() BETWEEN begin_dt AND end_dt)",
165                             uid);
166          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
167          {          {
168                  log_error("Query section_master failed\n");                  log_error("Query section_master error: %s\n", mysql_error(db));
169                  return -1;                  return -1;
170          }          }
171          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 143  int load_priv(MYSQL *db, BBS_user_priv * Line 173  int load_priv(MYSQL *db, BBS_user_priv *
173                  log_error("Get section_master data failed\n");                  log_error("Get section_master data failed\n");
174                  return -1;                  return -1;
175          }          }
176          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
177          {          {
178                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
179                  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 151  int load_priv(MYSQL *db, BBS_user_priv * Line 181  int load_priv(MYSQL *db, BBS_user_priv *
181          mysql_free_result(rs);          mysql_free_result(rs);
182    
183          // Section status          // Section status
184          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 "
185                                   "write_user_level from section_config"                                                             "INNER JOIN section_class ON section_config.CID = section_class.CID "
186                                   " left join section_class on section_config.CID="                                                             "WHERE section_config.enable AND section_class.enable "
187                                   "section_class.CID where section_config.enable and"                                                             "ORDER BY SID");
                                  " section_class.enable order by SID");  
188          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
189          {          {
190                  log_error("Query section_config failed\n");                  log_error("Query section_config error: %s\n", mysql_error(db));
191                  return -1;                  return -1;
192          }          }
193          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 166  int load_priv(MYSQL *db, BBS_user_priv * Line 195  int load_priv(MYSQL *db, BBS_user_priv *
195                  log_error("Get section_config data failed\n");                  log_error("Get section_config data failed\n");
196                  return -1;                  return -1;
197          }          }
198          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
199          {          {
200                    int priv = getpriv(p_priv, atoi(row[0]));
201                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
202                          setpriv(p_priv, atoi(row[0]),                  {
203                                          getpriv(p_priv, atoi(row[0])) & (~S_LIST));                          priv &= (~S_LIST);
204                    }
205                  if (p_priv->level < atoi(row[3]))                  if (p_priv->level < atoi(row[3]))
206                          setpriv(p_priv, atoi(row[0]),                  {
207                                          getpriv(p_priv, atoi(row[0])) & (~S_POST));                          priv &= (~S_POST);
208                    }
209                  if (!atoi(row[1]))                  if (!atoi(row[1]))
210                          setpriv(p_priv, atoi(row[0]),                  {
211                                          getpriv(p_priv, atoi(row[0])) & (~S_GETEXP));                          priv &= (~S_GETEXP);
212          }                  }
213          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));  
214          }          }
215          mysql_free_result(rs);          mysql_free_result(rs);
216    
217          // Section ban          // Section ban
218          sprintf(sql, "select SID from ban_user_list where"          snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "
219                                   " UID=%ld and enable and unban_UID=0 and"                                                             "AND (NOW() BETWEEN ban_dt AND unban_dt)",
220                                   " (now() between ban_dt and unban_dt)",                           uid);
                         uid);  
221          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
222          {          {
223                  log_error("Query ban_user_list failed\n");                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
224                  return -1;                  return -1;
225          }          }
226          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 220  int load_priv(MYSQL *db, BBS_user_priv * Line 228  int load_priv(MYSQL *db, BBS_user_priv *
228                  log_error("Get ban_user_list data failed\n");                  log_error("Get ban_user_list data failed\n");
229                  return -1;                  return -1;
230          }          }
231          while (row = mysql_fetch_row(rs))          while ((row = mysql_fetch_row(rs)))
232          {          {
233                  setpriv(p_priv, atoi(row[0]),                  setpriv(p_priv, atoi(row[0]),
234                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));


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

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