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


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

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