/[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.11 by sysadm, Mon May 5 11:46:04 2025 UTC Revision 1.25 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    user_priv.c  -  description  /*
3                                                           -------------------   * user_priv
4          begin                : Mon Oct 22 2004   *   - basic operations of user privilege
5          copyright            : (C) 2004 by Leaflet   *
6          email                : leaflet@leafok.com   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7   ***************************************************************************/   */
8    
9  /***************************************************************************  #ifdef HAVE_CONFIG_H
10   *                                                                         *  #include "config.h"
11   *   This program is free software; you can redistribute it and/or modify  *  #endif
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 2 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
 #include "user_priv.h"  
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15  #include "database.h"  #include "database.h"
16  #include "log.h"  #include "log.h"
17    #include "user_priv.h"
18  #include <stdio.h>  #include <stdio.h>
19  #include <mysql.h>  #include <stdlib.h>
20    #include <mysql/mysql.h>
21    
22  BBS_user_priv BBS_priv;  BBS_user_priv BBS_priv;
23    
24  int checklevel(BBS_user_priv *p_priv, int level)  inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset)
25  {  {
26          if (level == P_GUEST)          int left = 0;
27          {          int right = p_priv->s_count - 1;
28                  return 1;          int mid = 0;
         }  
   
         return ((p_priv->level & level) ? 1 : 0);  
 }  
29    
30  int setpriv(BBS_user_priv *p_priv, int sid, int priv)          while (left < right)
 {  
         int i;  
         if (sid > 0)  
31          {          {
32                  for (i = 0; i < p_priv->s_count; i++)                  mid = (left + right) / 2;
33    
34                    if (sid < p_priv->s_priv_list[mid].sid)
35                  {                  {
36                          if (p_priv->s_priv_list[i].sid == sid)                          right = mid - 1;
                         {  
                                 p_priv->s_priv_list[i].s_priv = priv;  
                                 return 0;  
                         }  
37                  }                  }
38                  if (i < BBS_max_section)                  else if (sid > p_priv->s_priv_list[mid].sid)
39                  {                  {
40                          p_priv->s_priv_list[i].s_priv = priv;                          left = mid + 1;
41                  }                  }
42                  else                  else // if (sid == p_priv->s_priv_list[mid].sid)
43                  {                  {
44                          return -1;                          left = mid;
45                            break;
46                  }                  }
47          }          }
48          else  
49            *p_offset = left;
50    
51            return (sid == p_priv->s_priv_list[left].sid);
52    }
53    
54    int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
55    {
56            int offset;
57            int i;
58    
59            if (sid == 0)
60          {          {
61                  p_priv->g_priv = priv;                  p_priv->g_priv = priv;
62                    return 0;
63            }
64    
65            if (search_priv(p_priv, sid, &offset)) // found
66            {
67                    p_priv->s_priv_list[offset].s_priv = priv;
68                    p_priv->s_priv_list[offset].is_favor = is_favor;
69                    return 0;
70            }
71    
72            // not found
73            if (p_priv->s_count >= BBS_max_section)
74            {
75                    return -1;
76          }          }
77    
78            // move items at [left, p_priv->s_count - 1] to [left + 1, p_priv->s_count]
79            for (i = p_priv->s_count - 1; i >= offset; i--)
80            {
81                    p_priv->s_priv_list[i + 1] = p_priv->s_priv_list[i];
82            }
83            p_priv->s_count++;
84    
85            // insert new item at offset left
86            p_priv->s_priv_list[offset].sid = sid;
87            p_priv->s_priv_list[offset].s_priv = priv;
88            p_priv->s_priv_list[offset].is_favor = is_favor;
89    
90          return 0;          return 0;
91  }  }
92    
93  int getpriv(BBS_user_priv *p_priv, int sid)  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
94  {  {
95          int i;          int offset;
96          for (i = 0; i < p_priv->s_count; i++)  
97            if (search_priv(p_priv, sid, &offset)) // found
98          {          {
99                  if (p_priv->s_priv_list[i].sid == sid)                  *p_is_favor = p_priv->s_priv_list[offset].is_favor;
100                          return p_priv->s_priv_list[i].s_priv;                  return p_priv->s_priv_list[offset].s_priv;
101          }          }
102    
103            *p_is_favor = 0;
104          return (sid >= 0 ? p_priv->g_priv : S_NONE);          return (sid >= 0 ? p_priv->g_priv : S_NONE);
105  }  }
106    
107  int checkpriv(BBS_user_priv *p_priv, int sid, int priv)  int load_priv(MYSQL *db, BBS_user_priv *p_priv, int uid)
 {  
         return (((getpriv(p_priv, sid) & priv)) == priv ? 1 : 0);  
 }  
   
 int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid)  
108  {  {
109          MYSQL_RES *rs;          MYSQL_RES *rs;
110          MYSQL_ROW row;          MYSQL_ROW row;
111          char sql[SQL_BUFFER_LEN];          char sql[SQL_BUFFER_LEN];
112            int priv;
113            int is_favor;
114    
115          p_priv->uid = uid;          p_priv->uid = uid;
116          p_priv->level = (uid == 0 ? P_GUEST : P_USER);          p_priv->level = (uid == 0 ? P_GUEST : P_USER);
117          p_priv->g_priv = S_DEFAULT;          p_priv->g_priv = S_DEFAULT;
118            p_priv->s_count = 0;
119    
120          if (db == NULL)          if (db == NULL)
121                  return 1;                  return 1;
122    
123          // Permission          // Permission
124          snprintf(sql, sizeof(sql), "SELECT p_post, p_msg FROM user_list WHERE UID = %ld AND verified",          snprintf(sql, sizeof(sql),
125                          uid);                           "SELECT p_post, p_msg FROM user_list WHERE UID = %d AND verified",
126                             uid);
127          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
128          {          {
129                  log_error("Query user_list failed\n");                  log_error("Query user_list error: %s\n", mysql_error(db));
130                  return -1;                  return -1;
131          }          }
132          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 116  int load_priv(MYSQL *db, BBS_user_priv * Line 142  int load_priv(MYSQL *db, BBS_user_priv *
142          mysql_free_result(rs);          mysql_free_result(rs);
143    
144          // Admin          // Admin
145          snprintf(sql, sizeof(sql), "SELECT major FROM admin_config WHERE UID = %ld "          snprintf(sql, sizeof(sql),
146                                   "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",                           "SELECT major FROM admin_config WHERE UID = %d "
147                          uid);                           "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
148                             uid);
149          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
150          {          {
151                  log_error("Query admin_config failed\n");                  log_error("Query admin_config error: %s\n", mysql_error(db));
152                  return -1;                  return -1;
153          }          }
154          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 137  int load_priv(MYSQL *db, BBS_user_priv * Line 164  int load_priv(MYSQL *db, BBS_user_priv *
164          mysql_free_result(rs);          mysql_free_result(rs);
165    
166          // Section Master          // Section Master
167          snprintf(sql, sizeof(sql), "SELECT section_master.SID, major FROM section_master "          snprintf(sql, sizeof(sql),
168                                   "INNER JOIN section_config ON section_master.SID = section_config.SID "                           "SELECT section_master.SID, major FROM section_master "
169                                   "WHERE UID = %ld AND section_master.enable AND section_config.enable "                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
170                                   "AND (NOW() BETWEEN begin_dt AND end_dt)",                           "WHERE UID = %d AND section_master.enable AND section_config.enable "
171                          uid);                           "AND (NOW() BETWEEN begin_dt AND end_dt)",
172                             uid);
173          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
174          {          {
175                  log_error("Query section_master failed\n");                  log_error("Query section_master error: %s\n", mysql_error(db));
176                  return -1;                  return -1;
177          }          }
178          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 155  int load_priv(MYSQL *db, BBS_user_priv * Line 183  int load_priv(MYSQL *db, BBS_user_priv *
183          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
184          {          {
185                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
186                  setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));                  priv = (getpriv(p_priv, atoi(row[0]), &is_favor) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
187                    setpriv(p_priv, atoi(row[0]), priv, is_favor);
188          }          }
189          mysql_free_result(rs);          mysql_free_result(rs);
190    
191          // Section status          // Section status
192          snprintf(sql, sizeof(sql), "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "          snprintf(sql, sizeof(sql),
193                                   "INNER JOIN section_class ON section_config.CID = section_class.CID "                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
194                                   "WHERE section_config.enable AND section_class.enable "                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
195                                   "ORDER BY SID");                           "WHERE section_config.enable AND section_class.enable "
196                             "ORDER BY SID");
197          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
198          {          {
199                  log_error("Query section_config failed\n");                  log_error("Query section_config error: %s\n", mysql_error(db));
200                  return -1;                  return -1;
201          }          }
202          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 176  int load_priv(MYSQL *db, BBS_user_priv * Line 206  int load_priv(MYSQL *db, BBS_user_priv *
206          }          }
207          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
208          {          {
209                  int priv = getpriv(p_priv, atoi(row[0]));                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
210                  if (p_priv->level < atoi(row[2]))                  if (p_priv->level < atoi(row[2]))
211                  {                  {
212                          priv &= (~S_LIST);                          priv &= (~S_LIST);
# Line 189  int load_priv(MYSQL *db, BBS_user_priv * Line 219  int load_priv(MYSQL *db, BBS_user_priv *
219                  {                  {
220                          priv &= (~S_GETEXP);                          priv &= (~S_GETEXP);
221                  }                  }
222                  setpriv(p_priv, atoi(row[0]), priv);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
223          }          }
224          mysql_free_result(rs);          mysql_free_result(rs);
225    
226          // Section ban          // Section ban
227          snprintf(sql, sizeof(sql), "SELECT SID FROM ban_user_list WHERE UID = %ld AND enable "          snprintf(sql, sizeof(sql),
228                                   "AND (NOW() BETWEEN ban_dt AND unban_dt)",                           "SELECT SID FROM ban_user_list WHERE UID = %d AND enable "
229                          uid);                           "AND (NOW() BETWEEN ban_dt AND unban_dt)",
230                             uid);
231          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
232          {          {
233                  log_error("Query ban_user_list failed\n");                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
234                  return -1;                  return -1;
235          }          }
236          if ((rs = mysql_store_result(db)) == NULL)          if ((rs = mysql_store_result(db)) == NULL)
# Line 209  int load_priv(MYSQL *db, BBS_user_priv * Line 240  int load_priv(MYSQL *db, BBS_user_priv *
240          }          }
241          while ((row = mysql_fetch_row(rs)))          while ((row = mysql_fetch_row(rs)))
242          {          {
243                  setpriv(p_priv, atoi(row[0]),                  priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
244                                  getpriv(p_priv, atoi(row[0])) & (~S_POST));                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
245            }
246            mysql_free_result(rs);
247    
248            // User favor section
249            snprintf(sql, sizeof(sql),
250                             "SELECT SID FROM section_favorite WHERE UID = %d",
251                             uid);
252            if (mysql_query(db, sql) != 0)
253            {
254                    log_error("Query section_favorite error: %s\n", mysql_error(db));
255                    return -1;
256            }
257            if ((rs = mysql_store_result(db)) == NULL)
258            {
259                    log_error("Get section_favorite data failed\n");
260                    return -1;
261            }
262            while ((row = mysql_fetch_row(rs)))
263            {
264                    priv = getpriv(p_priv, atoi(row[0]), &is_favor);
265                    if (!is_favor)
266                    {
267                            setpriv(p_priv, atoi(row[0]), priv, 1);
268                            priv = getpriv(p_priv, atoi(row[0]), &is_favor);
269                    }
270          }          }
271          mysql_free_result(rs);          mysql_free_result(rs);
272    


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

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