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


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

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