/[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.14 by sysadm, Wed Apr 30 09:18:19 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            main.c  -  description                                                    login.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 11 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"
26    #include "screen.h"
27    #include "database.h"
28    #include <string.h>
29    #include <mysql.h>
30    #include <regex.h>
31    #include <unistd.h>
32    
33  void  void login_fail()
 login_fail ()  
34  {  {
35    char temp[256];          char temp[256];
36    
37    strcpy (temp, app_home_dir);          strcpy(temp, app_home_dir);
38    strcat (temp, "data/login_error.dat");          strcat(temp, "data/login_error.txt");
39    display_file (temp);          display_file(temp);
40    
41            sleep(1);
42    }
43    
44    int bbs_login()
45    {
46            char username[20], password[20];
47            int count, ok;
48    
49            // Input username
50            count = 0;
51            ok = 0;
52            while (!ok)
53            {
54                    prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
55                               "注册请输入`\033[1;31mnew\033[m'): ");
56                    iflush();
57    
58                    str_input(username, 19, DOECHO);
59                    count++;
60    
61                    if (strcmp(username, "guest") == 0)
62                    {
63                            MYSQL * db = db_open();
64                            if (db == NULL)
65                            {
66                                    return -1;
67                            }
68                    
69                            load_guest_info(db);
70    
71                            mysql_close(db);
72    
73                            return 0;
74                    }
75    
76                    if (strcmp(username, "new") == 0)
77                    {
78                            if (user_register() == 0)
79                                    return 0;
80                            else
81                                    return -2;
82                    }
83    
84                    if (strlen(username) > 0)
85                    {
86                            // Input password
87                            prints("\033[1;37m请输入密码\033[m: ");
88                            iflush();
89    
90                            str_input(password, 19, 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[1024];
117            long int BBS_uid;
118            int ret;
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            sprintf(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                    strcpy(BBS_username, row[1]);
160                    int p_login = atoi(row[2]);
161    
162                    mysql_free_result(rs);
163    
164                    // Add user login log
165                    sprintf(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                    sprintf(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    
217    //Input username          // Set AUTOCOMMIT = 1
218    count = 0;          if (mysql_query(db, "SET autocommit=1") != 0)
219    ok = 0;          {
220    while (!ok)                  log_error("SET autocommit=1 failed\n");
221      {                  return -1;
222        printf          }
         ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "  
          "注册请输入`\033[1;31mnew\033[m'): ");  
       fflush (stdout);  
223    
224        str_input (username, 19, 0);          ret = load_user_info(db, BBS_uid);
       count++;  
225    
226        if (strcmp (username, "guest") == 0)          switch (ret)
227          return 1;          {
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        if (strlen (username) > 0)          sprintf(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            //Input password                  log_error("Update user_pubinfo failed\n");
253            printf ("\033[1;37m请输入密码\033[m: ");                  return -1;
254            fflush (stdout);          }
255    
256            str_input (password, 19, 0);          BBS_last_access_tm = BBS_login_tm = time(0);
257    
258            if (strlen (password) > 0)          return 0;
259              {  }
               ok = check_user (username, password);  
             }  
260    
261            if (!ok)  int load_user_info(MYSQL *db, long int BBS_uid)
262              {  {
263                printf ("\033[1;31m错误的用户名或密码...\r\n");          MYSQL_RES *rs;
264                fflush (stdout);          MYSQL_ROW row;
265              }          char sql[1024];
266            int life;
267            time_t last_login_dt;
268    
269            sprintf(sql,
270                            "SELECT life, UNIX_TIMESTAMP(last_login_dt) "
271                            "FROM user_pubinfo WHERE UID = %ld",
272                            BBS_uid);
273            if (mysql_query(db, sql) != 0)
274            {
275                    log_error("Query user_pubinfo failed\n");
276                    return -1;
277          }          }
278        if (count >= 3)          if ((rs = mysql_store_result(db)) == NULL)
279          {          {
280            login_fail ();                  log_error("Get user_pubinfo data failed\n");
281            return -1;                  return -1;
282            }
283            if (row = mysql_fetch_row(rs))
284            {
285                    life = atoi(row[0]);
286                    last_login_dt = (time_t)atol(row[1]);
287            }
288            else
289            {
290                    mysql_free_result(rs);
291                    return (-1); // Data not found
292            }
293            mysql_free_result(rs);
294    
295            if (life != 333 && life != 365 && life != 666 && life != 999 && // Not immortal
296                    time(0) - last_login_dt > 60 * 60 * 24 * life)
297            {
298                    return (-3); // Dead
299          }          }
     }  
300    
301    return 0;          load_priv(db, &BBS_priv, BBS_uid);
302    
303            return 0;
304  }  }
305    
306  int  int load_guest_info(MYSQL *db)
 check_user(char *username, char *password)  
307  {  {
308    return 1;          MYSQL_RES *rs;
309            MYSQL_ROW row;
310    
311            strcpy(BBS_username, "guest");
312    
313            load_priv(db, &BBS_priv, 0);
314    
315            BBS_last_access_tm = BBS_login_tm = time(0);
316    
317            return 0;
318  }  }


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

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