/[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.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);
   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, sizeof(username) - 1, 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                            {
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        str_input (username, 19, 0);          return 0;
110        count++;  }
111    
112        if (strcmp (username, "guest") == 0)  int check_user(MYSQL *db, char *username, char *password)
113    {
114            MYSQL_RES *rs;
115            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            load_guest_info ();                  prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
125            return 0;                  iflush();
126                    return 1;
127          }          }
128    
129        if (strcmp (username, "new") == 0)          // Begin transaction
130            if (mysql_query(db, "SET autocommit=0") != 0)
131          {          {
132            if (user_register () == 0)                  log_error("SET autocommit=0 failed\n");
133              return 0;                  return -1;
           else  
            return -2;  
134          }          }
135    
136        if (strlen (username) > 0)          if (mysql_query(db, "BEGIN") != 0)
137          {          {
138            //Input password                  log_error("Begin transaction failed\n");
139            prints ("\033[1;37m请输入密码\033[m: ");                  return -1;
140            iflush ();          }
141    
142            str_input (password, 19, 1);          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            ok = (check_user (username, password) == 0);                  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        if (count >= 3 && !ok)  
218            // Set AUTOCOMMIT = 1
219            if (mysql_query(db, "SET autocommit=1") != 0)
220          {          {
221            login_fail ();                  log_error("SET autocommit=1 failed\n");
222            return -1;                  return -1;
223          }          }
     }  
224    
225    return 0;          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            if (user_online_add(db) != 0)
258            {
259                    return -1;
260            }
261    
262            BBS_last_access_tm = BBS_login_tm = time(0);
263    
264            return 0;
265  }  }
266    
267  int  int load_user_info(MYSQL *db, long int BBS_uid)
 check_user (char *username, char *password)  
268  {  {
269    MYSQL *db;          MYSQL_RES *rs;
270    MYSQL_RES *rs;          MYSQL_ROW row;
271    MYSQL_ROW row;          char sql[SQL_BUFFER_LEN];
272    char sql[1024];          int life;
273    long int BBS_uid;          time_t last_login_dt;
274    int ret;  
275            snprintf(sql, sizeof(sql),
276    //Verify format                          "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
277    if (ireg ("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||                          "FROM user_pubinfo WHERE UID = %ld",
278        ireg ("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)                          BBS_uid);
279      {          if (mysql_query(db, sql) != 0)
280        prints ("\033[1;31m用户名或密码格式错误...\033[m\r\n");          {
281        iflush ();                  log_error("Query user_pubinfo failed\n");
282        return 1;                  return -1;
283      }          }
284            if ((rs = mysql_store_result(db)) == NULL)
285    db = (MYSQL *) db_open ();          {
286    if (db == NULL)                  log_error("Get user_pubinfo data failed\n");
287      {                  return -1;
288        return -1;          }
289      }          if ((row = mysql_fetch_row(rs)))
290            {
291    sprintf (sql,                  life = atoi(row[0]);
292             "select UID,username,p_login from user_list where username='%s' "                  last_login_dt = (time_t)atol(row[1]);
293             "and (password=MD5('%s') or password=PASSWORD('%s')) and "          }
294             "enable", username, password, password);          else
295    if (mysql_query (db, sql) != 0)          {
296      {                  mysql_free_result(rs);
297        log_error ("Query user_list failed\n");                  return -1; // Data not found
298        return -1;          }
299      }          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;  
     }  
300    
301    mysql_close (db);          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            if (load_priv(db, &BBS_priv, BBS_uid) != 0)
308            {
309                    return -1;
310            }
311    
312    return 0;          return 0;
313  }  }
314    
315  int  int load_guest_info(MYSQL *db)
 load_user_info (MYSQL * db, long int BBS_uid)  
316  {  {
317    MYSQL_RES *rs;          strncpy(BBS_username, "guest", sizeof(BBS_username) - 1);
318    MYSQL_ROW row;          BBS_username[sizeof(BBS_username) - 1] = '\0';
319    char sql[1024];  
320    long int BBS_auth_uid = 0;          if (load_priv(db, &BBS_priv, 0) != 0)
321    int life;          {
322    time_t last_login_dt;                  return -1;
323            }
324    sprintf (sql,  
325             "select life,UNIX_TIMESTAMP(last_login_dt) "          if (user_online_add(db) != 0)
326             "from user_pubinfo where UID=%ld limit 1", BBS_uid);          {
327    if (mysql_query (db, sql) != 0)                  return -1;
328      {          }
       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;  
     }  
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)
 load_guest_info (MYSQL * db, long int BBS_uid)  
336  {  {
337    MYSQL_RES *rs;          char sql[SQL_BUFFER_LEN];
   MYSQL_ROW row;  
338    
339    db = (MYSQL *) db_open ();          if (user_online_del(db) != 0)
340    if (db == NULL)          {
341      {                  return -1;
342        return -1;          }
     }  
343    
344    strcpy (BBS_username, "guest");          snprintf(sql, sizeof(sql),
345                            "INSERT INTO user_online(SID, UID, ip, login_tm, last_tm) "
346                            "VALUES('Telnet_Process_%d', %ld, '%s', NOW(), NOW())",
347                            getpid(), BBS_priv.uid, hostaddr_client);
348            if (mysql_query(db, sql) != 0)
349            {
350                    log_error("Add user_online failed\n");
351                    return -1;
352            }
353    
354    load_priv (db, &BBS_priv, 0, 0, S_NONE);          return 0;
355    }
356    
357    BBS_last_access_tm = BBS_login_tm = time (0);  int user_online_del(MYSQL *db)
358    BBS_last_sub_tm = time (0) - 60;  {
359            char sql[SQL_BUFFER_LEN];
360    
361    mysql_close (db);          snprintf(sql, sizeof(sql),
362                            "DELETE FROM user_online WHERE SID = 'Telnet_Process_%d'",
363                            getpid());
364            if (mysql_query(db, sql) != 0)
365            {
366                    log_error("Delete user_online failed\n");
367                    return -1;
368            }
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