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


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

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