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


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

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