File: authenticated.c

package info (click to toggle)
francine 0.99.8%2Borig-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 608 kB
  • ctags: 262
  • sloc: ansic: 2,375; sh: 330; makefile: 15
file content (348 lines) | stat: -rw-r--r-- 9,386 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* $Id: authenticated.c,v 1.6 2001/04/24 17:18:55 japh Exp $
 * authenticated.c  -- fancylogin user-authentication.
 *
 *             fancylogin uses ncurses to display a colorful login-
 *             screen with input-masks.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Written by Richard Bergmair.
 * ANSI-conformance testing by Andreas Krennmair
 */



#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <time.h>
#ifdef HAVE_SHADOW_H
# include <shadow.h>
#endif

#include "definitions.h"
#include "limits.h"
#include "log_message.h"
#include "login_permissions.h"
#include "md5crypt.h"
#include "pam_login.h"
#include "environment.h"



#define __AUTHENTICATED__		0
#define __WRONG_PASSWORD__		1
#define __USER_EXPIRED__		2
#define __CANT_FETCH_SPWD__		3





static int check_password(char *crypted, char *clear);
static int auth_normal(struct passwd *passwd, char *clear);
static int auth_shadow(char * username, char *clear);
struct passwd *authenticated(char *username, char *password, char *rmthost);





/*****************************************************************************/
/* check_password: compares the given crypted password with the encrypted    */
/*                 cleartextpassword.                                        */
/*****************************************************************************/

static int
check_password (char *crypted, char *clear)
{
  /*
   * crypt the cleartext-password
   * using the salt-string, and finally
   * see if it's the same as entered
   *
   * to put it simply
   *
   * return (strcmp ((char *)crypt(clear, passwd->pw_passwd),
   *         passwd->pw_passwd) == 0);
   */

#ifdef ALLOW_DES
  return (strcmp ((char *)crypt(clear, crypted), crypted)==0);
#else
# ifdef ALLOW_MD5
  return (strcmp ((char *)md5crypt(clear,crypted), crypted)==0);
# else
# error "Either DES or MD5 must be allowed"
# endif
#endif

#ifdef ALLOW_MD5
#ifdef ALLOW_DES

  if ((strcmp ((char *)crypt(clear, crypted), crypted) != 0))
    return (strcmp ((char *)md5crypt(clear, crypted),crypted) == 0);
  else
    return TRUE;

#endif
#endif

}



#ifdef ALLOW_NORMAL

/*****************************************************************************/
/* auth_normal: tries to authenticate the given password from passwd         */
/*              (passwd) and the password entered by the user (password)     */
/*              using standard-authentication.                               */
/*****************************************************************************/

static int
auth_normal (struct passwd *passwd, char *clear)
{
  if (check_password(passwd->pw_passwd, clear))
    return __AUTHENTICATED__;
  else
    return __WRONG_PASSWORD__;
}

#endif



#ifdef HAVE_SHADOW_H

/*****************************************************************************/
/* auth_shadow: tries to authenticate the given password from passwd         */
/*              (passwd) and the password entered by the user (password)     */
/*              using shadow-authentication                                  */
/*****************************************************************************/

static int
auth_shadow (char * username, char *clear)
{
  /*
   * same as auth_normal, but before
   * verifying the password, we first get it
   * from shadow
   */

  struct spwd *entry;
  time_t now;

  entry = getspnam (username);
 
  /* entry will usually be NULL when the program runs with UID!=0.
  if (entry==NULL)
    return __CANT_FETCH_SPWD__;


  /* what time is it? */
  time (&now);
  
  /* has the account expired? */
  if ((entry->sp_expire != -1) && ((unsigned long)now/(60*60*24) > entry->sp_expire))
    {
      char errormessage[__MAX_STR_LEN__];

      sprintf (errormessage, "the account for %s has expired",
               username);
      log_message (40000, errormessage);
      log_message (49999, errormessage);
      return __USER_EXPIRED__;
    }

  if (check_password(entry->sp_pwdp, clear))
    return __AUTHENTICATED__;
  else
    return __WRONG_PASSWORD__;
}

#endif



/*****************************************************************************/
/* authenticated: determines whether username and password are correct       */
/*                returns NULL if not authenticated, else return the user-   */
/*                structure.                                                 */
/*****************************************************************************/

struct passwd *
authenticated (char *username, char *password, char *rmthost)
{
  struct passwd *passwd_entry;
  char errormessage [__MAX_STR_LEN__];
  int x;
  int rc;

  /* has the user entered a name? */
  if (username[0]=='\0')
    return NULL;

  /* is the user known to the system? */
  if ((passwd_entry = getpwnam (username)) == NULL)
    {
      sprintf (errormessage, "%s is not known to the system!", username);
      log_message (49998, errormessage);
      log_message (49999, errormessage);
      return NULL;
    }


  /*
   * is root trying to log on to a terminal, he is not allowed
   * to log on to ?
   */

#ifdef SUPPORT_SECURETTY

  if ((passwd_entry->pw_uid==0) && !(root_allowed()))
    {
      sprintf (errormessage, "accorting to /etc/securetty root is not allowed to log here");
      log_message (40000, errormessage); 
      log_message (49999, errormessage); 
      return NULL;
    }

#endif

 
  /*
   * is the user locked?
   */

  if ((passwd_entry->pw_passwd[0])=='!')
    {
      sprintf (errormessage, "%s is locked", passwd_entry->pw_name);
      log_message (40000+passwd_entry->pw_uid, errormessage);
      log_message (49999, errormessage);
      return NULL;
    }


  /*
   * Is the user allowed to log on to this terminal
   * at this time, according to /etc/usertty?
   */

#ifdef SUPPORT_USERTTY

  if ((passwd_entry->pw_uid != 0) && (!(user_allowed (passwd_entry, rmthost))))
    {
      sprintf (errormessage, "according to /etc/usertty %s is not allowed to log on here.", passwd_entry->pw_name);
      log_message (40000+passwd_entry->pw_uid, errormessage);
      log_message (49999, errormessage);
      return NULL;
    }

#endif


  /*
   * does the user have a password?
   */

  if (passwd_entry->pw_passwd[0]=='\0')
    {
      sprintf (errormessage, "%s logged in", passwd_entry->pw_name);
      log_message (30000+passwd_entry->pw_uid, errormessage);
      log_message (39999, errormessage);
      return passwd_entry;
    }


  /*
   * can the user be authenticated to the system? 
   */

#ifdef HAVE_LIBPAM
  rc=pam_authenticatepam();
  if (rc!=PAM_SUCCESS)
    return NULL;
#endif

  if (strcmp(passwd_entry->pw_passwd,"x")==0)  /* Do we use shadow? */
    {                                         

#ifdef HAVE_SHADOW_H

      switch (auth_shadow (username, password)) 
        {
        case __AUTHENTICATED__:
          sprintf (errormessage, "%s logged in", passwd_entry->pw_name);
          log_message (30000+passwd_entry->pw_uid, errormessage);
          log_message (39999, errormessage);
          return passwd_entry;
        case __USER_EXPIRED__:
          sprintf (errormessage, "%s expired",
                   passwd_entry->pw_name);
          log_message (40000+passwd_entry->pw_uid, errormessage);
          log_message (49999, errormessage);
          break;
        case __CANT_FETCH_SPWD__:
          sprintf(errormessage,"couldn't fetch struct spwd for %s."
                               " Maybe not running as root?",
                               passwd_entry->pw_name);
          log_message(40000+passwd_entry->pw_uid, errormessage);
          log_message(49999, errormessage);
        }
	return NULL;
#endif

    }
  else                                        
    {

#ifdef ALLOW_NORMAL

      if (auth_normal (passwd_entry, password)==__AUTHENTICATED__)
        {
          sprintf (errormessage, "%s logged in", passwd_entry->pw_name);
          log_message (30000+passwd_entry->pw_uid, errormessage);
          log_message (39999, errormessage);
          return passwd_entry;
        }
      else
        {
          /* Log that the authentication failed */
          sprintf (errormessage, "%s entered a wrong password",
                   passwd_entry->pw_name);
          log_message (40000+passwd_entry->pw_uid, errormessage);
          log_message (49999, errormessage);
          return NULL;
        }

#endif

    }

  return NULL;
}



/*****************************************************************************/
/* (c) Copyright 1999-2000 Richard Bergmair, 2000-2001 Andreas Krennmair     */
/*****************************************************************************/