/[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.5 by sysadm, Sat Oct 23 18:41:41 2004 UTC Revision 1.21 by sysadm, Tue May 6 12:10:22 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            login.c  -  description                                                    login.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 20 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"  #include "io.h"
25    #include "screen.h"
26    #include "database.h"
27    #include <string.h>
28  #include <mysql.h>  #include <mysql.h>
29  #include <regex.h>  #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.txt");  
   display_file (temp);  
   
   sleep (1);  
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        prints                  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        iflush ();                  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        str_input (username, 19, 0);          return 0;
109        count++;  }
110    
111        if (strcmp (username, "guest") == 0)  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            load_guest_info ();                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
124            return 0;                  iflush();
125                    return 1;
126          }          }
127    
128        if (strcmp (username, "new") == 0)          // Begin transaction
129            if (mysql_query(db, "SET autocommit=0") != 0)
130          {          {
131            if (user_register () == 0)                  log_error("SET autocommit=0 failed\n");
132              return 0;                  return -1;
           else  
            return -2;  
133          }          }
134    
135        if (strlen (username) > 0)          if (mysql_query(db, "BEGIN") != 0)
136          {          {
137            //Input password                  log_error("Begin transaction failed\n");
138            prints ("\033[1;37m请输入密码\033[m: ");                  return -1;
139            iflush ();          }
140    
141            str_input (password, 19, 1);          snprintf(sql, sizeof(sql),
142                            "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            ok = (check_user (username, password) == 0);                  snprintf(sql, sizeof(sql),
196                                    "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        if (count >= 3 && !ok)  
217            // Set AUTOCOMMIT = 1
218            if (mysql_query(db, "SET autocommit=1") != 0)
219          {          {
220            login_fail ();                  log_error("SET autocommit=1 failed\n");
221            return -1;                  return -1;
222          }          }
     }  
223    
224    return 0;          ret = load_user_info(db, BBS_uid);
225    
226            switch (ret)
227            {
228            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            snprintf(sql, sizeof(sql),
247                            "UPDATE user_pubinfo SET visit_count = visit_count + 1, "
248                            "last_login_dt = NOW() WHERE UID = %ld",
249                            BBS_uid);
250            if (mysql_query(db, sql) != 0)
251            {
252                    log_error("Update user_pubinfo failed\n");
253                    return -1;
254            }
255    
256            if (user_online_add(db) != 0)
257            {
258                    return -1;
259            }
260    
261            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    MYSQL *db;          MYSQL_RES *rs;
269    MYSQL_RES *rs;          MYSQL_ROW row;
270    MYSQL_ROW row;          char sql[SQL_BUFFER_LEN];
271    char sql[1024];          int life;
272    long int BBS_uid;          time_t last_login_dt;
273    int ret;  
274            snprintf(sql, sizeof(sql),
275    //Verify format                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
276    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "FROM user_pubinfo WHERE UID = %ld",
277        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          BBS_uid);
278      {          if (mysql_query(db, sql) != 0)
279        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          {
280        iflush ();                  log_error("Query user_pubinfo failed\n");
281        return 1;                  return -1;
282      }          }
283            if ((rs = mysql_store_result(db)) == NULL)
284    db = (MYSQL *) db_open ();          {
285    if (db == NULL)                  log_error("Get user_pubinfo data failed\n");
286      {                  return -1;
287        return -1;          }
288      }          if ((row = mysql_fetch_row(rs)))
289            {
290    sprintf (sql,                  life = atoi(row[0]);
291             "select UID,p_login from user_list where username='%s' "                  last_login_dt = (time_t)atol(row[1]);
292             "and (password=MD5('%s') or password=PASSWORD('%s')) and "          }
293             "enable", username, password, password);          else
294    if (mysql_query (db, sql) != 0)          {
295      {                  mysql_free_result(rs);
296        log_error ("Query user_list failed\n");                  return -1; // Data not found
297        return -1;          }
298      }          mysql_free_result(rs);
299    if ((rs = mysql_store_result (db)) == NULL)  
300      {          if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
301        log_error ("Get user_list data failed\n");                  time(0) - last_login_dt > 60 * 60 * 24 * life)
302        return -1;          {
303      }                  return -3; // Dead
304    if (row = mysql_fetch_row (rs))          }
     {  
       BBS_uid = atol (row[0]);  
       if (atoi (row[1]) == 0)  
         {  
           mysql_free_result (rs);  
           mysql_close (db);  
   
           prints ("\033[1;31m您目前无权登陆...\033[m\r\n");  
           iflush ();  
           return 1;  
         }  
     }  
   else  
     {  
       mysql_free_result (rs);  
   
       sprintf (sql,  
                "insert delayed into user_err_login_log"  
                "(username,password,login_dt,login_ip) values"  
                "('%s','%s',now(),'%s')", username, password, hostaddr_client);  
       if (mysql_query (db, sql) != 0)  
         {  
           log_error ("Insert into user_err_login_log failed\n");  
           return -1;  
         }  
   
       mysql_close (db);  
   
       prints ("\033[1;31m错误的用户名或密码...\033[m\r\n");  
       iflush ();  
       return 1;  
     }  
   mysql_free_result (rs);  
   
   BBS_passwd_complex = verify_pass_complexity (password, username, 6);  
   
   ret = load_user_info (db, BBS_uid);  
   
   switch (ret)  
     {  
     case 0:                     //Login successfully  
       return 0;  
       break;  
     case -1:                    //Load data error  
       prints ("\033[1;31m读取用户数据错误...\033[m\r\n");  
       iflush ();  
       return -1;  
       break;  
     case -2:                    //Unused  
       return 0;  
       break;  
     case -3:                    //Dead  
       prints ("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");  
       iflush ();  
       return 1;  
     default:  
       return -2;  
     }  
305    
306    mysql_close (db);          if (load_priv(db, &BBS_priv, BBS_uid) != 0)
307            {
308                    return -1;
309            }
310    
311    return 0;          return 0;
312  }  }
313    
314  int  int load_guest_info(MYSQL *db)
 load_user_info (MYSQL * db, long int BBS_uid)  
315  {  {
316    MYSQL_RES *rs;          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
317    MYSQL_ROW row;          BBS_username[sizeof(BBS_username) - 1] = '\0';
318    char sql[1024];  
319    long int BBS_auth_uid = 0;          if (load_priv(db, &BBS_priv, 0) != 0)
320    int life;          {
321    time_t last_login_dt;                  return -1;
322            }
   sprintf (sql,  
            "select life,UNIX_TIMESTAMP(last_login_dt) "  
            "from user_pubinfo where UID=%ld limit 1", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_pubinfo failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_pubinfo data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       life = atoi (row[0]);  
       last_login_dt = (time_t) atol (row[1]);  
     }  
   else  
     {  
       mysql_free_result (rs);  
       return (-1);              //Data not found  
     }  
   mysql_free_result (rs);  
   
   if (time (0) - last_login_dt >= 60 * 60 * 24 * life)  
     {  
       return (-3);              //Dead  
     }  
   
   sprintf (sql,  
            "select AUID from user_auth where UID=%ld"  
            " and enable and expire_dt>now()", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Query user_auth failed\n");  
       return -1;  
     }  
   if ((rs = mysql_store_result (db)) == NULL)  
     {  
       log_error ("Get user_auth data failed\n");  
       return -1;  
     }  
   if (row = mysql_fetch_row (rs))  
     {  
       BBS_auth_uid = atol (row[0]);  
     }  
   mysql_free_result (rs);  
   
   sprintf (sql,  
            "insert delayed into user_login_log"  
            "(uid,login_dt,login_ip) values(%ld"  
            ",now(),'%s')", BBS_uid, hostaddr_client);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Insert into user_login_log failed\n");  
       return -1;  
     }  
   
   load_priv (db, &BBS_priv, BBS_uid, BBS_auth_uid,  
              (!BBS_passwd_complex ? S_MAN_M : S_NONE) |  
              (BBS_auth_uid ? S_NONE : S_MAIL));  
   
   BBS_last_access_tm = BBS_login_tm = time (0);  
   BBS_last_sub_tm = time (0) - 60;  
   
   sprintf (sql,  
            "update user_pubinfo set visit_count=visit_count+1,"  
            "last_login_dt=now() where uid=%ld", BBS_uid);  
   if (mysql_query (db, sql) != 0)  
     {  
       log_error ("Update user_pubinfo failed\n");  
       return -1;  
     }  
323    
324    return 0;          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  int user_online_add(MYSQL *db)
 load_guest_info (MYSQL * db, long int BBS_uid)  
335  {  {
336    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
   MYSQL_ROW row;  
337    
338    db = (MYSQL *) db_open ();          if (user_online_del(db) != 0)
339    if (db == NULL)          {
340      {                  return -1;
341        return -1;          }
     }  
342    
343    load_priv (db, &BBS_priv, 0, 0, S_NONE);          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    BBS_last_access_tm = BBS_login_tm = time (0);  int user_online_del(MYSQL *db)
357    BBS_last_sub_tm = time (0) - 60;  {
358            char sql[SQL_BUFFER_LEN];
359    
360    mysql_close (db);          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;          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