/[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.2 by sysadm, Wed Dec 1 06:04:37 2004 UTC Revision 1.26 by sysadm, Sun Nov 16 11:06:06 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    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
15    #include "database.h"
16    #include "log.h"
17    #include "user_priv.h"
18    #include <stdio.h>
19    #include <stdlib.h>
20  #include <mysql.h>  #include <mysql.h>
21    
22  int  BBS_user_priv BBS_priv;
23  checklevel (BBS_user_priv * p_priv, int level)  
24    inline static int search_priv(BBS_user_priv *p_priv, int sid, int *p_offset)
25  {  {
26    return (((p_priv->level & level)) ^ level ? 0 : 1);          int left = 0;
27            int right = p_priv->s_count - 1;
28            int mid = 0;
29    
30            while (left < right)
31            {
32                    mid = (left + right) / 2;
33    
34                    if (sid < p_priv->s_priv_list[mid].sid)
35                    {
36                            right = mid - 1;
37                    }
38                    else if (sid > p_priv->s_priv_list[mid].sid)
39                    {
40                            left = mid + 1;
41                    }
42                    else // if (sid == p_priv->s_priv_list[mid].sid)
43                    {
44                            left = mid;
45                            break;
46                    }
47            }
48    
49            *p_offset = left;
50    
51            return (sid == p_priv->s_priv_list[left].sid);
52  }  }
53    
54  int  int setpriv(BBS_user_priv *p_priv, int sid, int priv, int is_favor)
 setpriv (BBS_user_priv * p_priv, int sid, int priv)  
55  {  {
56    int i;          int offset;
57    if (sid > 0)          int i;
58      {  
59        for (i = 0; i < p_priv->s_count; i++)          if (sid == 0)
60          {          {
61            if (p_priv->s_priv_list[i].sid == sid)                  p_priv->g_priv = priv;
62              {                  return 0;
               p_priv->s_priv_list[i].s_priv = priv;  
               return 0;  
             }  
63          }          }
64        if (i < BBS_max_section)  
65            if (search_priv(p_priv, sid, &offset)) // found
66          {          {
67            p_priv->s_priv_list[i].s_priv = priv;                  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        else  
72            // not found
73            if (p_priv->s_count >= BBS_max_section)
74          {          {
75            return -1;                  return -1;
76          }          }
     }  
   else  
     {  
       p_priv->g_priv = priv;  
     }  
77    
78    return 0;          // 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;
91  }  }
92    
93  int  int getpriv(BBS_user_priv *p_priv, int sid, int *p_is_favor)
 getpriv (BBS_user_priv * p_priv, int sid)  
94  {  {
95    int i;          int offset;
   for (i = 0; i < p_priv->s_count; i++)  
     {  
       if (p_priv->s_priv_list[i].sid == sid)  
         return p_priv->s_priv_list[i].s_priv;  
     }  
96    
97    return p_priv->g_priv;          if (search_priv(p_priv, sid, &offset)) // found
98            {
99                    *p_is_favor = p_priv->s_priv_list[offset].is_favor;
100                    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);
105  }  }
106    
107  int  int load_priv(MYSQL *db, BBS_user_priv *p_priv, int uid)
 checkpriv (BBS_user_priv * p_priv, int sid, int priv)  
108  {  {
109    return (((getpriv (p_priv, sid) & priv)) ^ priv ? 0 : 1);          MYSQL_RES *rs;
110  }          MYSQL_ROW row;
111            char sql[SQL_BUFFER_LEN];
112            int priv;
113            int is_favor;
114    
115            p_priv->uid = uid;
116            p_priv->level = (uid == 0 ? P_GUEST : P_USER);
117            p_priv->g_priv = S_DEFAULT;
118            p_priv->s_count = 0;
119    
120            if (db == NULL)
121                    return 1;
122    
123            // Permission
124            snprintf(sql, sizeof(sql),
125                             "SELECT p_post, p_msg FROM user_list WHERE UID = %d AND verified",
126                             uid);
127            if (mysql_query(db, sql) != 0)
128            {
129                    log_error("Query user_list error: %s\n", mysql_error(db));
130                    return -1;
131            }
132            if ((rs = mysql_store_result(db)) == NULL)
133            {
134                    log_error("Get user_list data failed\n");
135                    return -1;
136            }
137            if ((row = mysql_fetch_row(rs)))
138            {
139                    p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
140                    p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
141            }
142            mysql_free_result(rs);
143    
144            // Admin
145            snprintf(sql, sizeof(sql),
146                             "SELECT major FROM admin_config WHERE UID = %d "
147                             "AND enable AND (NOW() BETWEEN begin_dt AND end_dt)",
148                             uid);
149            if (mysql_query(db, sql) != 0)
150            {
151                    log_error("Query admin_config error: %s\n", mysql_error(db));
152                    return -1;
153            }
154            if ((rs = mysql_store_result(db)) == NULL)
155            {
156                    log_error("Get admin_config data failed\n");
157                    return -1;
158            }
159            if ((row = mysql_fetch_row(rs)))
160            {
161                    p_priv->level |= (atoi(row[0]) ? P_ADMIN_M : P_ADMIN_S);
162                    p_priv->g_priv |= (atoi(row[0]) ? S_ALL : S_ADMIN);
163            }
164            mysql_free_result(rs);
165    
166  int          // Section Master
167  load_priv (MYSQL * db, BBS_user_priv * p_priv, long int uid,          snprintf(sql, sizeof(sql),
168             long int auth_uid, int priv_excluse)                           "SELECT section_master.SID, major FROM section_master "
169  {                           "INNER JOIN section_config ON section_master.SID = section_config.SID "
170    MYSQL_RES *rs;                           "WHERE UID = %d AND section_master.enable AND section_config.enable "
171    MYSQL_ROW row;                           "AND (NOW() BETWEEN begin_dt AND end_dt)",
172    char sql[1024];                           uid);
173    int i;          if (mysql_query(db, sql) != 0)
174            {
175    p_priv->uid = uid;                  log_error("Query section_master error: %s\n", mysql_error(db));
176    p_priv->auid = auth_uid;                  return -1;
177    p_priv->level = (uid == 0 ? P_GUEST : P_USER);          }
178    p_priv->level |= (auth_uid == 0 ? P_GUEST : P_AUTH_USER);          if ((rs = mysql_store_result(db)) == NULL)
179    p_priv->g_priv = S_DEFAULT;          {
180                    log_error("Get section_master data failed\n");
181    if (db == NULL)                  return -1;
182      return 1;          }
183            while ((row = mysql_fetch_row(rs)))
184    //Admin          {
185    sprintf (sql, "select aid,major from admin_config where UID=%ld"                  p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
186             " and enable and (now() between begin_dt and end_dt)", uid);                  priv = (getpriv(p_priv, atoi(row[0]), &is_favor) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
187    if (mysql_query (db, sql) != 0)                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
188      {          }
189        log_error ("Query admin_config failed\n");          mysql_free_result(rs);
190        return -1;  
191      }          // Section status
192    if ((rs = mysql_store_result (db)) == NULL)          snprintf(sql, sizeof(sql),
193      {                           "SELECT SID, exp_get, read_user_level, write_user_level FROM section_config "
194        log_error ("Get admin_config data failed\n");                           "INNER JOIN section_class ON section_config.CID = section_class.CID "
195        return -1;                           "WHERE section_config.enable AND section_class.enable "
196      }                           "ORDER BY SID");
197    if (row = mysql_fetch_row (rs))          if (mysql_query(db, sql) != 0)
198      {          {
199        p_priv->level |= (atoi (row[1]) ? P_ADMIN_M : P_ADMIN_S);                  log_error("Query section_config error: %s\n", mysql_error(db));
200        p_priv->g_priv |= (atoi (row[1]) ? S_ALL : S_ADMIN);                  return -1;
201      }          }
202    mysql_free_result (rs);          if ((rs = mysql_store_result(db)) == NULL)
203            {
204    //Permission                  log_error("Get section_config data failed\n");
205    sprintf (sql, "select p_post,p_msg,p_mail "                  return -1;
206             "from user_list where UID=%ld", uid);          }
207    if (mysql_query (db, sql) != 0)          while ((row = mysql_fetch_row(rs)))
208      {          {
209        log_error ("Query user_list failed\n");                  int priv = getpriv(p_priv, atoi(row[0]), &is_favor);
210        return -1;                  if (p_priv->level < atoi(row[2]))
211      }                  {
212    if ((rs = mysql_store_result (db)) == NULL)                          priv &= (~S_LIST);
213      {                  }
214        log_error ("Get user_list data failed\n");                  if (p_priv->level < atoi(row[3]))
215        return -1;                  {
216      }                          priv &= (~S_POST);
217    if (row = mysql_fetch_row (rs))                  }
218      {                  if (!atoi(row[1]))
219        p_priv->g_priv |= (atoi (row[0]) ? S_POST : 0);                  {
220        p_priv->g_priv |= (atoi (row[1]) ? S_MSG : 0);                          priv &= (~S_GETEXP);
221        p_priv->g_priv |= (atoi (row[2]) ? S_MAIL : 0);                  }
222      }                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
223    mysql_free_result (rs);          }
224            mysql_free_result(rs);
225    //Verified  
226    sprintf (sql, "select verified from user_list where" " UID=%ld", uid);          // Section ban
227    if (mysql_query (db, sql) != 0)          snprintf(sql, sizeof(sql),
228      {                           "SELECT SID FROM ban_user_list WHERE UID = %d AND enable "
229        log_error ("Query user_list failed\n");                           "AND (NOW() BETWEEN ban_dt AND unban_dt)",
230        return -1;                           uid);
231      }          if (mysql_query(db, sql) != 0)
232    if ((rs = mysql_store_result (db)) == NULL)          {
233      {                  log_error("Query ban_user_list error: %s\n", mysql_error(db));
234        log_error ("Get user_list data failed\n");                  return -1;
235        return -1;          }
236      }          if ((rs = mysql_store_result(db)) == NULL)
237    if (row = mysql_fetch_row (rs))          {
238      p_priv->g_priv &= (atoi (row[0]) ? p_priv->g_priv : S_DEFAULT);                  log_error("Get ban_user_list data failed\n");
239    mysql_free_result (rs);                  return -1;
240            }
241    //IP ban          while ((row = mysql_fetch_row(rs)))
242    sprintf (sql, "select begin_ip,end_ip from ban_ip_list"          {
243             " where ('%s' between begin_ip and end_ip) and enable",                  priv = getpriv(p_priv, atoi(row[0]), &is_favor) & (~S_POST);
244             hostaddr_client);                  setpriv(p_priv, atoi(row[0]), priv, is_favor);
245    if (mysql_query (db, sql) != 0)          }
246      {          mysql_free_result(rs);
       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;  
     }  
   if (mysql_num_rows (rs) > 0)  
     p_priv->g_priv &= S_DEFAULT;  
   mysql_free_result (rs);  
   
   //Section Class Master  
   sprintf (sql, "select SID from section_class_master"  
            " left join section_config on section_class_master.CID"  
            "=section_config.CID where UID=%ld and section_class_master.enable"  
            " and (now() between begin_dt and end_dt)", uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query section_class_master failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get section_class_master data failed\n");  
       return -1;  
     }  
   while (row = mysql_fetch_row (rs))  
     {  
       p_priv->level |= P_MAN_C;  
       setpriv (p_priv, atoi (row[0]), getpriv (p_priv, atoi (row[0]))  
                | S_MAN_M);  
     }  
   mysql_free_result (rs);  
   
   //Section Master  
   sprintf (sql, "select SID,major from section_master where"  
            " UID=%ld and enable and (now() between begin_dt and"  
            " end_dt)", uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query section_master failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get section_master data failed\n");  
       return -1;  
     }  
   while (row = mysql_fetch_row (rs))  
     {  
       p_priv->level |= (atoi (row[1]) ? P_MAN_M : P_MAN_S);  
       setpriv (p_priv, atoi (row[0]), getpriv (p_priv, atoi (row[0]))  
                | (atoi (row[1]) ? S_MAN_M : S_MAN_S));  
     }  
   mysql_free_result (rs);  
   
   //Section status  
   sprintf (sql, "select SID,exp_get,read_user_level,"  
            "write_user_level from section_config"  
            " left join section_class on section_config.CID="  
            "section_class.CID where section_config.enable and"  
            " section_class.enable order by SID");  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query section_config failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get section_config data failed\n");  
       return -1;  
     }  
   while (row = mysql_fetch_row (rs))  
     {  
       if (p_priv->level < atoi (row[2]))  
         setpriv (p_priv, atoi (row[0]),  
                  getpriv (p_priv, atoi (row[0])) & (~S_LIST));  
       if (p_priv->level < atoi (row[3]))  
         setpriv (p_priv, atoi (row[0]),  
                  getpriv (p_priv, atoi (row[0])) & (~S_POST));  
       if (!atoi (row[1]))  
         setpriv (p_priv, atoi (row[0]),  
                  getpriv (p_priv, atoi (row[0])) & (~S_GETEXP));  
     }  
   mysql_free_result (rs);  
   
   //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));  
     }  
   mysql_free_result (rs);  
   
   //Section ban  
   sprintf (sql, "select SID from ban_user_list where"  
            " UID=%ld and enable and unban_UID=0 and"  
            " (now() between ban_dt and unban_dt)", uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query ban_user_list failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get ban_user_list data failed\n");  
       return -1;  
     }  
   while (row = mysql_fetch_row (rs))  
     {  
       setpriv (p_priv, atoi (row[0]),  
                getpriv (p_priv, atoi (row[0])) & (~S_POST));  
     }  
   mysql_free_result (rs);  
   
   //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);  
247    
248    if (priv_excluse & S_MAN_M)          // User favor section
249      p_priv->level &= (P_AUTH_USER | P_USER);          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);
272    
273    return 0;          return 0;
274  }  }


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

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