/[LeafOK_CVS]/lbbs/src/user_priv.c
ViewVC logotype

Contents of /lbbs/src/user_priv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations)
Mon Apr 28 03:31:00 2025 UTC (10 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.4: +263 -263 lines
Content type: text/x-csrc
Update

1 /***************************************************************************
2 user_priv.c - description
3 -------------------
4 begin : Mon Oct 22 2004
5 copyright : (C) 2004 by Leaflet
6 email : leaflet@leafok.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "bbs.h"
19 #include "common.h"
20 #include <mysql.h>
21
22 int checklevel(BBS_user_priv *p_priv, int level)
23 {
24 return (((p_priv->level & level)) ^ level ? 0 : 1);
25 }
26
27 int setpriv(BBS_user_priv *p_priv, int sid, int priv)
28 {
29 int i;
30 if (sid > 0)
31 {
32 for (i = 0; i < p_priv->s_count; i++)
33 {
34 if (p_priv->s_priv_list[i].sid == sid)
35 {
36 p_priv->s_priv_list[i].s_priv = priv;
37 return 0;
38 }
39 }
40 if (i < BBS_max_section)
41 {
42 p_priv->s_priv_list[i].s_priv = priv;
43 }
44 else
45 {
46 return -1;
47 }
48 }
49 else
50 {
51 p_priv->g_priv = priv;
52 }
53
54 return 0;
55 }
56
57 int getpriv(BBS_user_priv *p_priv, int sid)
58 {
59 int i;
60 for (i = 0; i < p_priv->s_count; i++)
61 {
62 if (p_priv->s_priv_list[i].sid == sid)
63 return p_priv->s_priv_list[i].s_priv;
64 }
65
66 return (sid >= 0 ? p_priv->g_priv : S_NONE);
67 }
68
69 int checkpriv(BBS_user_priv *p_priv, int sid, int priv)
70 {
71 return (((getpriv(p_priv, sid) & priv)) ^ priv ? 0 : 1);
72 }
73
74 int load_priv(MYSQL *db, BBS_user_priv *p_priv, long int uid,
75 long int auth_uid, int priv_excluse)
76 {
77 MYSQL_RES *rs;
78 MYSQL_ROW row;
79 char sql[1024];
80 int i;
81
82 p_priv->uid = uid;
83 p_priv->auid = auth_uid;
84 p_priv->level = (uid == 0 ? P_GUEST : P_USER);
85 p_priv->level |= (auth_uid == 0 ? P_GUEST : P_AUTH_USER);
86 p_priv->g_priv = S_DEFAULT;
87
88 if (db == NULL)
89 return 1;
90
91 // Admin
92 sprintf(sql, "select aid,major from admin_config where UID=%ld"
93 " and enable and (now() between begin_dt and end_dt)",
94 uid);
95 if (mysql_query(db, sql) != 0)
96 {
97 log_error("Query admin_config failed\n");
98 return -1;
99 }
100 if ((rs = mysql_store_result(db)) == NULL)
101 {
102 log_error("Get admin_config data failed\n");
103 return -1;
104 }
105 if (row = mysql_fetch_row(rs))
106 {
107 p_priv->level |= (atoi(row[1]) ? P_ADMIN_M : P_ADMIN_S);
108 p_priv->g_priv |= (atoi(row[1]) ? S_ALL : S_ADMIN);
109 }
110 mysql_free_result(rs);
111
112 // Permission
113 sprintf(sql, "select p_post,p_msg,p_mail "
114 "from user_list where UID=%ld",
115 uid);
116 if (mysql_query(db, sql) != 0)
117 {
118 log_error("Query user_list failed\n");
119 return -1;
120 }
121 if ((rs = mysql_store_result(db)) == NULL)
122 {
123 log_error("Get user_list data failed\n");
124 return -1;
125 }
126 if (row = mysql_fetch_row(rs))
127 {
128 p_priv->g_priv |= (atoi(row[0]) ? S_POST : 0);
129 p_priv->g_priv |= (atoi(row[1]) ? S_MSG : 0);
130 p_priv->g_priv |= (atoi(row[2]) ? S_MAIL : 0);
131 }
132 mysql_free_result(rs);
133
134 // Verified
135 sprintf(sql, "select verified from user_list where"
136 " UID=%ld",
137 uid);
138 if (mysql_query(db, sql) != 0)
139 {
140 log_error("Query user_list failed\n");
141 return -1;
142 }
143 if ((rs = mysql_store_result(db)) == NULL)
144 {
145 log_error("Get user_list data failed\n");
146 return -1;
147 }
148 if (row = mysql_fetch_row(rs))
149 p_priv->g_priv &= (atoi(row[0]) ? p_priv->g_priv : S_DEFAULT);
150 mysql_free_result(rs);
151
152 // IP ban
153 sprintf(sql, "select begin_ip,end_ip from ban_ip_list"
154 " where ('%s' between begin_ip and end_ip) and enable",
155 hostaddr_client);
156 if (mysql_query(db, sql) != 0)
157 {
158 log_error("Query ban_ip_list failed\n");
159 return -1;
160 }
161 if ((rs = mysql_store_result(db)) == NULL)
162 {
163 log_error("Get ban_ip_list data failed\n");
164 return -1;
165 }
166 if (mysql_num_rows(rs) > 0)
167 p_priv->g_priv &= S_DEFAULT;
168 mysql_free_result(rs);
169
170 // Section Class Master
171 sprintf(sql, "select SID from section_class_master"
172 " left join section_config on section_class_master.CID"
173 "=section_config.CID where UID=%ld and section_class_master.enable"
174 " and (now() between begin_dt and end_dt)",
175 uid);
176 if (mysql_query(db, sql) != 0)
177 {
178 log_error("Query section_class_master failed\n");
179 return -1;
180 }
181 if ((rs = mysql_store_result(db)) == NULL)
182 {
183 log_error("Get section_class_master data failed\n");
184 return -1;
185 }
186 while (row = mysql_fetch_row(rs))
187 {
188 p_priv->level |= P_MAN_C;
189 setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | S_MAN_M);
190 }
191 mysql_free_result(rs);
192
193 // Section Master
194 sprintf(sql, "select SID,major from section_master where"
195 " UID=%ld and enable and (now() between begin_dt and"
196 " end_dt)",
197 uid);
198 if (mysql_query(db, sql) != 0)
199 {
200 log_error("Query section_master failed\n");
201 return -1;
202 }
203 if ((rs = mysql_store_result(db)) == NULL)
204 {
205 log_error("Get section_master data failed\n");
206 return -1;
207 }
208 while (row = mysql_fetch_row(rs))
209 {
210 p_priv->level |= (atoi(row[1]) ? P_MAN_M : P_MAN_S);
211 setpriv(p_priv, atoi(row[0]), getpriv(p_priv, atoi(row[0])) | (atoi(row[1]) ? S_MAN_M : S_MAN_S));
212 }
213 mysql_free_result(rs);
214
215 // Section status
216 sprintf(sql, "select SID,exp_get,read_user_level,"
217 "write_user_level from section_config"
218 " left join section_class on section_config.CID="
219 "section_class.CID where section_config.enable and"
220 " section_class.enable order by SID");
221 if (mysql_query(db, sql) != 0)
222 {
223 log_error("Query section_config failed\n");
224 return -1;
225 }
226 if ((rs = mysql_store_result(db)) == NULL)
227 {
228 log_error("Get section_config data failed\n");
229 return -1;
230 }
231 while (row = mysql_fetch_row(rs))
232 {
233 if (p_priv->level < atoi(row[2]))
234 setpriv(p_priv, atoi(row[0]),
235 getpriv(p_priv, atoi(row[0])) & (~S_LIST));
236 if (p_priv->level < atoi(row[3]))
237 setpriv(p_priv, atoi(row[0]),
238 getpriv(p_priv, atoi(row[0])) & (~S_POST));
239 if (!atoi(row[1]))
240 setpriv(p_priv, atoi(row[0]),
241 getpriv(p_priv, atoi(row[0])) & (~S_GETEXP));
242 }
243 mysql_free_result(rs);
244
245 // Section User priv
246 sprintf(sql, "select SID,`read`,`write` from section_user_priv"
247 " where UID=%ld order by SID",
248 uid);
249 if (mysql_query(db, sql) != 0)
250 {
251 log_error("Query section_user_priv failed\n");
252 return -1;
253 }
254 if ((rs = mysql_store_result(db)) == NULL)
255 {
256 log_error("Get section_user_priv data failed\n");
257 return -1;
258 }
259 while (row = mysql_fetch_row(rs))
260 {
261 setpriv(p_priv, atoi(row[0]),
262 atoi(row[1]) ? (getpriv(p_priv, atoi(row[0])) | S_LIST)
263 : (getpriv(p_priv, atoi(row[0])) & ~S_LIST));
264 setpriv(p_priv, atoi(row[0]),
265 atoi(row[2]) ? (getpriv(p_priv, atoi(row[0])) | S_POST)
266 : (getpriv(p_priv, atoi(row[0])) & ~S_POST));
267 }
268 mysql_free_result(rs);
269
270 // Section ban
271 sprintf(sql, "select SID from ban_user_list where"
272 " UID=%ld and enable and unban_UID=0 and"
273 " (now() between ban_dt and unban_dt)",
274 uid);
275 if (mysql_query(db, sql) != 0)
276 {
277 log_error("Query ban_user_list failed\n");
278 return -1;
279 }
280 if ((rs = mysql_store_result(db)) == NULL)
281 {
282 log_error("Get ban_user_list data failed\n");
283 return -1;
284 }
285 while (row = mysql_fetch_row(rs))
286 {
287 setpriv(p_priv, atoi(row[0]),
288 getpriv(p_priv, atoi(row[0])) & (~S_POST));
289 }
290 mysql_free_result(rs);
291
292 // Priv exclusion
293 p_priv->g_priv &= (~priv_excluse);
294 for (i = 0; i < p_priv->s_count; i++)
295 p_priv->s_priv_list[i].s_priv &= (~priv_excluse);
296
297 if (priv_excluse & S_MAN_M)
298 p_priv->level &= (P_AUTH_USER | P_USER);
299
300 return 0;
301 }

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