Mercurial > hg > toybox
comparison toys/other/login.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | 6df4ccc0acbe |
children | 786841fdb1e0 |
comparison
equal
deleted
inserted
replaced
673:c102f31a753e | 674:7e846e281e38 |
---|---|
19 -p Preserve environment | 19 -p Preserve environment |
20 -h The name of the remote host for this login | 20 -h The name of the remote host for this login |
21 -f Do not perform authentication | 21 -f Do not perform authentication |
22 */ | 22 */ |
23 | 23 |
24 #define FOR_login | |
24 #include "toys.h" | 25 #include "toys.h" |
25 | 26 |
26 #define LOGIN_TIMEOUT 60 | 27 #define LOGIN_TIMEOUT 60 |
27 #define LOGIN_FAIL_TIMEOUT 3 | 28 #define LOGIN_FAIL_TIMEOUT 3 |
28 #define USER_NAME_MAX_SIZE 32 | 29 #define USER_NAME_MAX_SIZE 32 |
29 #define HOSTNAME_SIZE 32 | 30 #define HOSTNAME_SIZE 32 |
30 | 31 |
31 DEFINE_GLOBALS( | 32 GLOBALS( |
32 char *hostname; | 33 char *hostname; |
33 ) | 34 ) |
34 #define TT this.login | |
35 | 35 |
36 static void login_timeout_handler(int sig __attribute__((unused))) | 36 static void login_timeout_handler(int sig __attribute__((unused))) |
37 { | 37 { |
38 printf("\nLogin timed out after %d seconds.\n", LOGIN_TIMEOUT); | 38 printf("\nLogin timed out after %d seconds.\n", LOGIN_TIMEOUT); |
39 exit(0); | 39 exit(0); |
160 setenv("SHELL", pwd->pw_shell, 1); | 160 setenv("SHELL", pwd->pw_shell, 1); |
161 } | 161 } |
162 | 162 |
163 void login_main(void) | 163 void login_main(void) |
164 { | 164 { |
165 int f_flag = (toys.optflags & 4) >> 2; | 165 int f_flag = toys.optflags & FLAG_f; |
166 int p_flag = (toys.optflags & 2) >> 1; | 166 int h_flag = toys.optflags & FLAG_h; |
167 int h_flag = toys.optflags & 1; | |
168 char username[USER_NAME_MAX_SIZE+1], *pass = NULL, **ss; | 167 char username[USER_NAME_MAX_SIZE+1], *pass = NULL, **ss; |
169 struct passwd * pwd = NULL; | 168 struct passwd * pwd = NULL; |
170 struct spwd * spwd = NULL; | 169 struct spwd * spwd = NULL; |
171 int auth_fail_cnt = 0; | 170 int auth_fail_cnt = 0; |
172 | 171 |
213 query_pass: | 212 query_pass: |
214 if (!verify_password(pass)) break; | 213 if (!verify_password(pass)) break; |
215 | 214 |
216 f_flag = 0; | 215 f_flag = 0; |
217 syslog(LOG_WARNING, "invalid password for '%s' on %s %s %s", username, | 216 syslog(LOG_WARNING, "invalid password for '%s' on %s %s %s", username, |
218 ttyname(0), (h_flag)?"from":"", (h_flag)?TT.hostname:""); | 217 ttyname(0), h_flag?"from":"", h_flag?TT.hostname:""); |
219 | 218 |
220 sleep(LOGIN_FAIL_TIMEOUT); | 219 sleep(LOGIN_FAIL_TIMEOUT); |
221 puts("Login incorrect"); | 220 puts("Login incorrect"); |
222 | 221 |
223 if (++auth_fail_cnt == 3) | 222 if (++auth_fail_cnt == 3) |
232 | 231 |
233 if (pwd->pw_uid) handle_nologin(); | 232 if (pwd->pw_uid) handle_nologin(); |
234 | 233 |
235 if (change_identity(pwd)) error_exit("Failed to change identity"); | 234 if (change_identity(pwd)) error_exit("Failed to change identity"); |
236 | 235 |
237 setup_environment(pwd, !p_flag); | 236 setup_environment(pwd, !(toys.optflags & FLAG_p)); |
238 | 237 |
239 handle_motd(); | 238 handle_motd(); |
240 | 239 |
241 syslog(LOG_INFO, "%s logged in on %s %s %s", pwd->pw_name, | 240 syslog(LOG_INFO, "%s logged in on %s %s %s", pwd->pw_name, |
242 ttyname(0), (h_flag)?"from":"", (h_flag)?TT.hostname:""); | 241 ttyname(0), h_flag?"from":"", h_flag?TT.hostname:""); |
243 | 242 |
244 spawn_shell(pwd->pw_shell); | 243 spawn_shell(pwd->pw_shell); |
245 } | 244 } |