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


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

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