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


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

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