Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / loginprompt.c
1 /*
2  * Copyright (c) 1989 - 1993, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
5  * Copyright (c) 2008 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the copyright holders or contributors may not be used to
17  *    endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <config.h>
34
35 #ident "$Id: loginprompt.c 2787 2009-04-24 22:46:06Z nekral-guest $"
36
37 #include <assert.h>
38 #include <stdio.h>
39 #include <signal.h>
40 #include <ctype.h>
41 #include "prototypes.h"
42 #include "defines.h"
43 #include "getdef.h"
44
45 static void login_exit (unused int sig)
46 {
47         exit (EXIT_FAILURE);
48 }
49
50 /*
51  * login_prompt - prompt the user for their login name
52  *
53  * login_prompt() displays the standard login prompt.  If ISSUE_FILE
54  * is set in login.defs, this file is displayed before the prompt.
55  */
56
57 void login_prompt (const char *prompt, char *name, int namesize)
58 {
59         char buf[1024];
60
61 #define MAX_ENV 32
62         char *envp[MAX_ENV];
63         int envc;
64         char *cp;
65         int i;
66         FILE *fp;
67
68         RETSIGTYPE (*sigquit) (int);
69 #ifdef  SIGTSTP
70         RETSIGTYPE (*sigtstp) (int);
71 #endif
72
73         /*
74          * There is a small chance that a QUIT character will be part of
75          * some random noise during a prompt.  Deal with this by exiting
76          * instead of core dumping.  If SIGTSTP is defined, do the same
77          * thing for that signal.
78          */
79
80         sigquit = signal (SIGQUIT, login_exit);
81 #ifdef  SIGTSTP
82         sigtstp = signal (SIGTSTP, login_exit);
83 #endif
84
85         /*
86          * See if the user has configured the issue file to
87          * be displayed and display it before the prompt.
88          */
89
90         if (NULL != prompt) {
91                 cp = getdef_str ("ISSUE_FILE");
92                 if (NULL != cp) {
93                         fp = fopen (cp, "r");
94                         if (NULL != fp) {
95                                 while ((i = getc (fp)) != EOF) {
96                                         (void) putc (i, stdout);
97                                 }
98
99                                 (void) fclose (fp);
100                         }
101                 }
102                 gethostname (buf, sizeof buf);
103                 printf (prompt, buf);
104                 (void) fflush (stdout);
105         }
106
107         /* 
108          * Read the user's response.  The trailing newline will be
109          * removed.
110          */
111
112         memzero (buf, sizeof buf);
113         if (fgets (buf, (int) sizeof buf, stdin) != buf) {
114                 exit (EXIT_FAILURE);
115         }
116
117         cp = strchr (buf, '\n');
118         if (NULL == cp) {
119                 exit (EXIT_FAILURE);
120         }
121         *cp = '\0';             /* remove \n [ must be there ] */
122
123         /*
124          * Skip leading whitespace.  This makes "  username" work right.
125          * Then copy the rest (up to the end or the first "non-graphic"
126          * character into the username.
127          */
128
129         for (cp = buf; *cp == ' ' || *cp == '\t'; cp++);
130
131         for (i = 0; i < namesize - 1 && isgraph (*cp); name[i++] = *cp++);
132         while (isgraph (*cp)) {
133                 cp++;
134         }
135
136         if ('\0' != *cp) {
137                 cp++;
138         }
139
140         name[i] = '\0';
141
142         /*
143          * This is a disaster, at best.  The user may have entered extra
144          * environmental variables at the prompt.  There are several ways
145          * to do this, and I just take the easy way out.
146          */
147
148         if ('\0' != *cp) {      /* process new variables */
149                 char *nvar;
150                 int count = 1;
151
152                 for (envc = 0; envc < MAX_ENV; envc++) {
153                         nvar = strtok ((0 != envc) ? (char *) 0 : cp, " \t,");
154                         if (NULL == nvar) {
155                                 break;
156                         }
157                         if (strchr (nvar, '=') != NULL) {
158                                 envp[envc] = nvar;
159                         } else {
160                                 size_t len = strlen (nvar) + 32;
161                                 int wlen;
162                                 envp[envc] = xmalloc (len);
163                                 wlen = snprintf (envp[envc], len, "L%d=%s", count++, nvar);
164                                 assert (wlen == (int) len -1);
165                         }
166                 }
167                 set_env (envc, envp);
168         }
169
170         /*
171          * Set the SIGQUIT handler back to its original value
172          */
173
174         (void) signal (SIGQUIT, sigquit);
175 #ifdef  SIGTSTP
176         (void) signal (SIGTSTP, sigtstp);
177 #endif
178 }
179