/[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.12 by sysadm, Mon Apr 28 04:23:38 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 17  Line 17 
17    
18  #include "bbs.h"  #include "bbs.h"
19  #include "common.h"  #include "common.h"
20    #include "io.h"
21    #include "database.h"
22    #include <mysql.h>
23    #include <regex.h>
24    
25  void  void login_fail()
 login_fail ()  
26  {  {
27    char temp[256];          char temp[256];
28    
29    strcpy (temp, app_home_dir);          strcpy(temp, app_home_dir);
30    strcat (temp, "data/login_error.dat");          strcat(temp, "data/login_error.txt");
31    display_file (temp);          display_file(temp);
32    
33            sleep(1);
34    }
35    
36    int bbs_login()
37    {
38            char username[20], password[20];
39            int count, ok;
40    
41            // Input username
42            count = 0;
43            ok = 0;
44            while (!ok)
45            {
46                    prints("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "
47                               "注册请输入`\033[1;31mnew\033[m'): ");
48                    iflush();
49    
50                    str_input(username, 19, DOECHO);
51                    count++;
52    
53                    if (strcmp(username, "guest") == 0)
54                    {
55                            load_guest_info();
56                            return 0;
57                    }
58    
59                    if (strcmp(username, "new") == 0)
60                    {
61                            if (user_register() == 0)
62                                    return 0;
63                            else
64                                    return -2;
65                    }
66    
67                    if (strlen(username) > 0)
68                    {
69                            // Input password
70                            prints("\033[1;37m请输入密码\033[m: ");
71                            iflush();
72    
73                            str_input(password, 19, NOECHO);
74    
75                            ok = (check_user(username, password) == 0);
76                    }
77                    if (count >= 3 && !ok)
78                    {
79                            login_fail();
80                            return -1;
81                    }
82            }
83    
84            return 0;
85  }  }
86    
87  int  int check_user(char *username, char *password)
 bbs_login ()  
88  {  {
89    char username[20], password[20];          MYSQL *db;
90    int count, ok;          MYSQL_RES *rs;
91            MYSQL_ROW row;
92            char sql[1024];
93            long int BBS_uid;
94            int ret;
95    
96            // Verify format
97            if (ireg("^[A-Za-z0-9_]{3,14}$", username, 0, NULL) != 0 ||
98                    ireg("^[A-Za-z0-9]{5,12}$", password, 0, NULL) != 0)
99            {
100                    prints("\033[1;31m用户名或密码格式错误...\033[m\r\n");
101                    iflush();
102                    return 1;
103            }
104    
105            db = (MYSQL *)db_open();
106            if (db == NULL)
107            {
108                    return -1;
109            }
110    
111    //Input username          sprintf(sql,
112    count = 0;                          "select UID,username,p_login from user_list where username='%s' "
113    ok = 0;                          "and (password=MD5('%s') or password=SHA2('%s', 256)) and "
114    while (!ok)                          "enable",
115      {                          username, password, password);
116        printf          if (mysql_query(db, sql) != 0)
117          ("\033[1;33m请输入帐号\033[m(试用请输入 `\033[1;36mguest\033[m', "          {
118           "注册请输入`\033[1;31mnew\033[m'): ");                  log_error("Query user_list failed\n");
119        fflush (stdout);                  return -1;
120            }
121            if ((rs = mysql_store_result(db)) == NULL)
122            {
123                    log_error("Get user_list data failed\n");
124                    return -1;
125            }
126            if (row = mysql_fetch_row(rs))
127            {
128                    BBS_uid = atol(row[0]);
129                    strcpy(BBS_username, row[1]);
130                    if (atoi(row[2]) == 0)
131                    {
132                            mysql_free_result(rs);
133                            mysql_close(db);
134    
135                            prints("\033[1;31m您目前无权登陆...\033[m\r\n");
136                            iflush();
137                            return 1;
138                    }
139            }
140            else
141            {
142                    mysql_free_result(rs);
143    
144        str_input (username, 19, 0);                  sprintf(sql,
145        count++;                                  "insert into user_err_login_log"
146                                    "(username,password,login_dt,login_ip) values"
147                                    "('%s','%s',now(),'%s')",
148                                    username, password, hostaddr_client);
149                    if (mysql_query(db, sql) != 0)
150                    {
151                            log_error("Insert into user_err_login_log failed\n");
152                            return -1;
153                    }
154    
155                    mysql_close(db);
156    
157                    prints("\033[1;31m错误的用户名或密码...\033[m\r\n");
158                    iflush();
159                    return 1;
160            }
161            mysql_free_result(rs);
162    
163        if (strcmp (username, "guest") == 0)          ret = load_user_info(db, BBS_uid);
         return 1;  
164    
165        if (strlen (username) > 0)          switch (ret)
166          {          {
167            //Input password          case 0: // Login successfully
168            printf ("\033[1;37m请输入密码\033[m: ");                  return 0;
169            fflush (stdout);                  break;
170            case -1: // Load data error
171                    prints("\033[1;31m读取用户数据错误...\033[m\r\n");
172                    iflush();
173                    return -1;
174                    break;
175            case -2: // Unused
176                    return 0;
177                    break;
178            case -3: // Dead
179                    prints("\033[1;31m很遗憾,您已经永远离开了我们的世界!\033[m\r\n");
180                    iflush();
181                    return 1;
182            default:
183                    return -2;
184            }
185    
186            mysql_close(db);
187    
188            return 0;
189    }
190    
191            str_input (password, 19, 0);  int load_user_info(MYSQL *db, long int BBS_uid)
192    {
193            MYSQL_RES *rs;
194            MYSQL_ROW row;
195            char sql[1024];
196            long int BBS_auth_uid = 0;
197            int life;
198            time_t last_login_dt;
199    
200            sprintf(sql,
201                            "select life,UNIX_TIMESTAMP(last_login_dt) "
202                            "from user_pubinfo where UID=%ld limit 1",
203                            BBS_uid);
204            if (mysql_query(db, sql) != 0)
205            {
206                    log_error("Query user_pubinfo failed\n");
207                    return -1;
208            }
209            if ((rs = mysql_store_result(db)) == NULL)
210            {
211                    log_error("Get user_pubinfo data failed\n");
212                    return -1;
213            }
214            if (row = mysql_fetch_row(rs))
215            {
216                    life = atoi(row[0]);
217                    last_login_dt = (time_t)atol(row[1]);
218            }
219            else
220            {
221                    mysql_free_result(rs);
222                    return (-1); // Data not found
223            }
224            mysql_free_result(rs);
225    
226            if (strlen (password) > 0)          if (time(0) - last_login_dt >= 60 * 60 * 24 * life)
227              {          {
228                ok = check_user (username, password);                  return (-3); // Dead
229              }          }
230    
231            if (!ok)          sprintf(sql,
232              {                          "insert into user_login_log"
233                printf ("\033[1;31m错误的用户名或密码...\r\n");                          "(uid,login_dt,login_ip) values(%ld"
234                fflush (stdout);                          ",now(),'%s')",
235              }                          BBS_uid, hostaddr_client);
236            if (mysql_query(db, sql) != 0)
237            {
238                    log_error("Insert into user_login_log failed\n");
239                    return -1;
240          }          }
241        if (count >= 3)  
242            load_priv(db, &BBS_priv, BBS_uid);
243    
244            BBS_last_access_tm = BBS_login_tm = time(0);
245            BBS_last_sub_tm = time(0) - 60;
246    
247            sprintf(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            login_fail ();                  log_error("Update user_pubinfo failed\n");
254            return -1;                  return -1;
255          }          }
     }  
256    
257    return 0;          return 0;
258  }  }
259    
260  int  int load_guest_info(MYSQL *db, long int BBS_uid)
 check_user(char *username, char *password)  
261  {  {
262    return 1;          MYSQL_RES *rs;
263            MYSQL_ROW row;
264    
265            db = (MYSQL *)db_open();
266            if (db == NULL)
267            {
268                    return -1;
269            }
270    
271            strcpy(BBS_username, "guest");
272    
273            load_priv(db, &BBS_priv, 0, 0, S_NONE);
274    
275            BBS_last_access_tm = BBS_login_tm = time(0);
276            BBS_last_sub_tm = time(0) - 60;
277    
278            mysql_close(db);
279    
280            return 0;
281  }  }


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

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