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

Diff of /lbbs/src/login.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Thu Oct 21 17:28:46 2004 UTC Revision 1.21 by sysadm, Tue May 6 12:10:22 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            main.c  -  description                                                    login.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 11 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 "login.h"
18    #include "register.h"
19  #include "bbs.h"  #include "bbs.h"
20    #include "user_priv.h"
21    #include "reg_ex.h"
22  #include "common.h"  #include "common.h"
23    #include "log.h"
24    #include "io.h"
25    #include "screen.h"
26    #include "database.h"
27    #include <string.h>
28    #include <mysql.h>
29    #include <regex.h>
30    #include <unistd.h>
31    
32  void  void login_fail()
 login_fail ()  
33  {  {
34    char temp[256];          display_file(DATA_LOGIN_ERROR);
35    
36    strcpy (temp, app_home_dir);          sleep(1);
   strcat (temp, "data/login_error.dat");  
   display_file (temp);  
37  }  }
38    
39  int  int bbs_login()
 bbs_login ()  
40  {  {
41    char username[20], password[20];          char username[BBS_username_max_len + 1];
42    int count, ok;          char password[BBS_password_max_len + 1];
43            int count = 0;
44            int ok = 0;
45    
46    //Input username          // Input username
47    count = 0;          while (!ok)
48    ok = 0;          {
49    while (!ok)                  prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
50      {                             "注册请输入`\033[1;31mnew\033[m'): ");
51        printf                  iflush();
52          ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "  
53           "注册请输入`\033[1;31mnew\033[m'): ");                  str_input(username, sizeof(username), DOECHO);
54        fflush (stdout);                  count++;
55    
56                    if (strcmp(username, "guest") == 0)
57                    {
58                            MYSQL *db = db_open();
59                            if (db == NULL)
60                            {
61                                    return -1;
62                            }
63    
64                            load_guest_info(db);
65    
66                            mysql_close(db);
67    
68                            return 0;
69                    }
70    
71                    if (strcmp(username, "new") == 0)
72                    {
73                            if (user_register() == 0)
74                            {
75                                    return 0;
76                            }
77                            else
78                            {
79                                    return -2;
80                            }
81                    }
82    
83                    if (username[0] != '\0')
84                    {
85                            // Input password
86                            prints("\033[1;37m请输入密码\033[m: ");
87                            iflush();
88    
89                            str_input(password, sizeof(password), NOECHO);
90    
91                            MYSQL *db = db_open();
92                            if (db == NULL)
93                            {
94                                    return -1;
95                            }
96    
97                            ok = (check_user(db, username, password) == 0);
98    
99                            mysql_close(db);
100                    }
101                    if (count >= 3 && !ok)
102                    {
103                            login_fail();
104                            return -1;
105                    }
106            }
107    
108            return 0;
109    }
110    
111    int check_user(MYSQL *db, char *username, char *password)
112    {
113            MYSQL_RES *rs;
114            MYSQL_ROW row;
115            char sql[SQL_BUFFER_LEN];
116            int ret;
117            long BBS_uid = 0;
118    
119            // Verify format
120            if (ireg("^[A-Za-z][A-Za-z0-9]{2,11}$", username, 0, NULL) != 0 ||
121                    ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
122            {
123                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
124                    iflush();
125                    return 1;
126            }
127    
128            // Begin transaction
129            if (mysql_query(db, "SET autocommit=0") != 0)
130            {
131                    log_error("SET autocommit=0 failed\n");
132                    return -1;
133            }
134    
135            if (mysql_query(db, "BEGIN") != 0)
136            {
137                    log_error("Begin transaction failed\n");
138                    return -1;
139            }
140    
141        str_input (username, 19, 0);          snprintf(sql, sizeof(sql),
142        count++;                          "SELECT UID, username, p_login FROM user_list "
143                            "WHERE username = '%s' AND password = SHA2('%s', 256) AND enable",
144                            username, password);
145            if (mysql_query(db, sql) != 0)
146            {
147                    log_error("Query user_list failed\n");
148                    return -1;
149            }
150            if ((rs = mysql_store_result(db)) == NULL)
151            {
152                    log_error("Get user_list data failed\n");
153                    return -1;
154            }
155            if ((row = mysql_fetch_row(rs)))
156            {
157                    BBS_uid = atol(row[0]);
158                    strncpy(BBS_username, row[1], sizeof(BBS_username) - 1);
159                    BBS_username[sizeof(BBS_username) - 1] = '\0';
160                    int p_login = atoi(row[2]);
161    
162                    mysql_free_result(rs);
163    
164                    // Add user login log
165                    snprintf(sql, sizeof(sql),
166                                    "INSERT INTO user_login_log(UID, login_dt, login_ip) "
167                                    "VALUES(%ld, NOW(), '%s')",
168                                    BBS_uid, hostaddr_client);
169                    if (mysql_query(db, sql) != 0)
170                    {
171                            log_error("Insert into user_login_log failed\n");
172                            return -1;
173                    }
174    
175                    // Commit transaction
176                    if (mysql_query(db, "COMMIT") != 0)
177                    {
178                            log_error("Commit transaction failed\n");
179                            return -1;
180                    }
181    
182                    if (p_login == 0)
183                    {
184                            mysql_free_result(rs);
185    
186                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
187                            iflush();
188                            return 1;
189                    }
190            }
191            else
192            {
193                    mysql_free_result(rs);
194    
195        if (strcmp (username, "guest") == 0)                  snprintf(sql, sizeof(sql),
196          return 1;                                  "INSERT INTO user_err_login_log(username, password, login_dt, login_ip) "
197                                    "VALUES('%s', '%s', NOW(), '%s')",
198                                    username, password, hostaddr_client);
199                    if (mysql_query(db, sql) != 0)
200                    {
201                            log_error("Insert into user_err_login_log failed\n");
202                            return -1;
203                    }
204    
205                    // Commit transaction
206                    if (mysql_query(db, "COMMIT") != 0)
207                    {
208                            log_error("Commit transaction failed\n");
209                            return -1;
210                    }
211    
212                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
213                    iflush();
214                    return 1;
215            }
216    
217        if (strlen (username) > 0)          // Set AUTOCOMMIT = 1
218            if (mysql_query(db, "SET autocommit=1") != 0)
219          {          {
220            //Input password                  log_error("SET autocommit=1 failed\n");
221            printf ("\033[1;37m请输入密码\033[m: ");                  return -1;
222            fflush (stdout);          }
223    
224            str_input (password, 19, 0);          ret = load_user_info(db, BBS_uid);
225    
226            if (strlen (password) > 0)          switch (ret)
227              {          {
228                ok = check_user (username, password);          case 0: // Login successfully
229              }                  break;
230            case -1: // Load data error
231                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
232                    iflush();
233                    return -1;
234            case -2: // Unused
235                    prints("\033[1;31m请通过Web登录更新用户许可协议...\033[m\r\n");
236                    iflush();
237                    return 1;
238            case -3: // Dead
239                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
240                    iflush();
241                    return 1;
242            default:
243                    return -2;
244            }
245    
246            if (!ok)          snprintf(sql, sizeof(sql),
247              {                          "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
248                printf ("\033[1;31m错误的用户名或密码...\r\n");                          "last_login_dt = NOW() WHERE UID = %ld",
249                fflush (stdout);                          BBS_uid);
250              }          if (mysql_query(db, sql) != 0)
251            {
252                    log_error("Update user_pubinfo failed\n");
253                    return -1;
254          }          }
255        if (count >= 3)  
256            if (user_online_add(db) != 0)
257          {          {
258            login_fail ();                  return -1;
           return -1;  
259          }          }
     }  
260    
261    return 0;          BBS_last_access_tm = BBS_login_tm = time(0);
262    
263            return 0;
264  }  }
265    
266  int  int load_user_info(MYSQL *db, long int BBS_uid)
 check_user(char *username, char *password)  
267  {  {
268    return 1;          MYSQL_RES *rs;
269            MYSQL_ROW row;
270            char sql[SQL_BUFFER_LEN];
271            int life;
272            time_t last_login_dt;
273    
274            snprintf(sql, sizeof(sql),
275                            "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
276                            "FROM user_pubinfo WHERE UID = %ld",
277                            BBS_uid);
278            if (mysql_query(db, sql) != 0)
279            {
280                    log_error("Query user_pubinfo failed\n");
281                    return -1;
282            }
283            if ((rs = mysql_store_result(db)) == NULL)
284            {
285                    log_error("Get user_pubinfo data failed\n");
286                    return -1;
287            }
288            if ((row = mysql_fetch_row(rs)))
289            {
290                    life = atoi(row[0]);
291                    last_login_dt = (time_t)atol(row[1]);
292            }
293            else
294            {
295                    mysql_free_result(rs);
296                    return -1; // Data not found
297            }
298            mysql_free_result(rs);
299    
300            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
301                    time(0) - last_login_dt > 60 * 60 * 24 * life)
302            {
303                    return -3; // Dead
304            }
305    
306            if (load_priv(db, &BBS_priv, BBS_uid) != 0)
307            {
308                    return -1;
309            }
310    
311            return 0;
312    }
313    
314    int load_guest_info(MYSQL *db)
315    {
316            strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
317            BBS_username[sizeof(BBS_username) - 1] = '\0';
318    
319            if (load_priv(db, &BBS_priv, 0) != 0)
320            {
321                    return -1;
322            }
323    
324            if (user_online_add(db) != 0)
325            {
326                    return -1;
327            }
328    
329            BBS_last_access_tm = BBS_login_tm = time(0);
330    
331            return 0;
332    }
333    
334    int user_online_add(MYSQL *db)
335    {
336            char sql[SQL_BUFFER_LEN];
337    
338            if (user_online_del(db) != 0)
339            {
340                    return -1;
341            }
342    
343            snprintf(sql, sizeof(sql),
344                            "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
345                            "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
346                            getpid(), BBS_priv.uid, hostaddr_client);
347            if (mysql_query(db, sql) != 0)
348            {
349                    log_error("Add user_online failed\n");
350                    return -1;
351            }
352    
353            return 0;
354    }
355    
356    int user_online_del(MYSQL *db)
357    {
358            char sql[SQL_BUFFER_LEN];
359    
360            snprintf(sql, sizeof(sql),
361                            "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
362                            getpid());
363            if (mysql_query(db, sql) != 0)
364            {
365                    log_error("Delete user_online failed\n");
366                    return -1;
367            }
368    
369            return 0;
370  }  }


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

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