summaryrefslogtreecommitdiffstats
path: root/driver/passwd-pwent.c
blob: bb0edfc23d26076e0fb38ade9e1aa19294dfb10b (plain) (blame)
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
/* passwd-pwent.c --- verifying typed passwords with the OS.
 * xscreensaver, Copyright (c) 1993-1998 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

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

#ifndef NO_LOCKING  /* whole file */

#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#ifdef HAVE_CRYPT_H
# include <crypt.h>
#endif

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#ifndef VMS
# include <pwd.h>
# include <grp.h>
#else /* VMS */
# include "vms-pwd.h"
#endif /* VMS */


#ifdef __bsdi__
# include <sys/param.h>
# if _BSDI_VERSION >= 199608
#  define BSD_AUTH
# endif
#endif /* __bsdi__ */


#if defined(HAVE_SHADOW_PASSWD)	      /* passwds live in /etc/shadow */

#   include <shadow.h>
#   define PWTYPE   struct spwd *
#   define PWPSLOT  sp_pwdp
#   define GETPW    getspnam

#elif defined(HAVE_ENHANCED_PASSWD)      /* passwds live in /tcb/files/auth/ */
				      /* M.Matsumoto <matsu@yao.sharp.co.jp> */
#   include <sys/security.h>
#   include <prot.h>

#   define PWTYPE   struct pr_passwd *
#   define PWPSLOT  ufld.fd_encrypt
#   define GETPW    getprpwnam

#elif defined(HAVE_ADJUNCT_PASSWD)

#   include <sys/label.h>
#   include <sys/audit.h>
#   include <pwdadj.h>

#   define PWTYPE   struct passwd_adjunct *
#   define PWPSLOT  pwa_passwd
#   define GETPW    getpwanam

#elif defined(HAVE_HPUX_PASSWD)

#   include <hpsecurity.h>
#   include <prot.h>

#   define PWTYPE   struct s_passwd *
#   define PWPSLOT  pw_passwd
#   define GETPW    getspwnam

#   define HAVE_BIGCRYPT

#endif


/* blargh */
#undef  Bool
#undef  True
#undef  False
#define Bool  int
#define True  1
#define False 0


extern const char *blurb(void);

static char *encrypted_root_passwd = 0;
static char *encrypted_user_passwd = 0;

#ifdef VMS
# define ROOT "SYSTEM"
#else
# define ROOT "root"
#endif

#ifndef VMS
Bool pwent_priv_init (int argc, char **argv, Bool verbose_p);
Bool pwent_lock_init (int argc, char **argv, Bool verbose_p);
Bool pwent_passwd_valid_p (const char *typed_passwd, Bool verbose_p);
#endif


#ifndef VMS

static char *
user_name (void)
{
  /* I think that just checking $USER here is not the best idea. */

  const char *u = 0;

  /* It has been reported that getlogin() returns the wrong user id on some
     very old SGI systems...  And I've seen it return the string "rlogin"
     sometimes!  Screw it, using getpwuid() should be enough...
   */
/* u = (char *) getlogin ();
 */

  /* getlogin() fails if not attached to a terminal; in that case, use
     getpwuid().  (Note that in this case, we're not doing shadow stuff, since
     all we're interested in is the name, not the password.  So that should
     still work.  Right?) */
  if (!u || !*u)
    {
      struct passwd *p = getpwuid (getuid ());
      u = (p ? p->pw_name : 0);
    }

  return (u ? strdup(u) : 0);
}

#else  /* VMS */

static char *
user_name (void)
{
  char *u = getenv("USER");
  return (u ? strdup(u) : 0);
}

#endif /* VMS */


static Bool
passwd_known_p (const char *pw)
{
  return (pw &&
	  pw[0] != '*' &&	/* This would be sensible...         */
	  strlen(pw) > 4);	/* ...but this is what Solaris does. */
}


static char *
get_encrypted_passwd(const char *user)
{
  char *result = 0;

#ifdef PWTYPE
  if (user && *user && !result)
    {					/* First check the shadow passwords. */
      PWTYPE p = GETPW((char *) user);
      if (p && passwd_known_p (p->PWPSLOT))
	result = strdup(p->PWPSLOT);
    }
#endif /* PWTYPE */

  if (user && *user && !result)
    {					/* Check non-shadow passwords too. */
      struct passwd *p = getpwnam(user);
      if (p && passwd_known_p (p->pw_passwd))
	result = strdup(p->pw_passwd);
    }

  /* The manual for passwd(4) on HPUX 10.10 says:

	  Password aging is put in effect for a particular user if his
	  encrypted password in the password file is followed by a comma and
	  a nonnull string of characters from the above alphabet.  This
	  string defines the "age" needed to implement password aging.

     So this means that passwd->pw_passwd isn't simply a string of cyphertext,
     it might have trailing junk.  So, if there is a comma in the string, and
     that comma is beyond position 13, terminate the string before the comma.
   */
  if (result && strlen(result) > 13)
    {
      char *s = strchr (result+13, ',');
      if (s)
	*s = 0;
    }

#ifndef HAVE_PAM
  /* We only issue this warning if not compiled with support for PAM.
     If we're using PAM, it's not unheard of that normal pwent passwords
     would be unavailable. */

  if (!result)
    fprintf (stderr, "%s: couldn't get password of \"%s\"\n",
	     blurb(), (user ? user : "(null)"));
#endif /* !HAVE_PAM */

  return result;
}



/* This has to be called before we've changed our effective user ID,
   because it might need privileges to get at the encrypted passwords.
   Returns false if we weren't able to get any passwords, and therefore,
   locking isn't possible.  (It will also have written to stderr.)
 */

#ifndef VMS

Bool
pwent_priv_init (int argc, char **argv, Bool verbose_p)
{
  char *u;

#ifdef HAVE_ENHANCED_PASSWD
  set_auth_parameters(argc, argv);
  check_auth_parameters();
#endif /* HAVE_DEC_ENHANCED */

  u = user_name();
  encrypted_user_passwd = get_encrypted_passwd(u);
  encrypted_root_passwd = get_encrypted_passwd(ROOT);
  if (u) free (u);

  if (encrypted_user_passwd)
    return True;
  else
    return False;
}


Bool
pwent_lock_init (int argc, char **argv, Bool verbose_p)
{
  if (encrypted_user_passwd)
    return True;
  else
    return False;
}



static Bool
passwds_match_p (const char *cleartext, const char *ciphertext)
{
  char *s = 0;  /* note that on some systems, crypt() may return null */

  s = (char *) crypt (cleartext, ciphertext);
  if (s && !strcmp (s, ciphertext))
    return True;

#ifdef HAVE_BIGCRYPT
  /* There seems to be no way to tell at runtime if an HP machine is in
     "trusted" mode, and thereby, which of crypt() or bigcrypt() we should
     be calling to compare passwords.  So call them both, and see which
     one works. */

  s = (char *) bigcrypt (cleartext, ciphertext);
  if (s && !strcmp (s, ciphertext))
    return True;

#endif /* HAVE_BIGCRYPT */
  
  return False;
}



/* This can be called at any time, and says whether the typed password
   belongs to either the logged in user (real uid, not effective); or
   to root.
 */
Bool
pwent_passwd_valid_p (const char *typed_passwd, Bool verbose_p)
{
  if (encrypted_user_passwd &&
      passwds_match_p (typed_passwd, encrypted_user_passwd))
    return True;

#ifdef ALLOW_ROOT_PASSWD
  /* do not allow root to have a null password. */
  else if (typed_passwd[0] &&
	   encrypted_root_passwd &&
           passwds_match_p (typed_passwd, encrypted_root_passwd))
    return True;
#endif /* ALLOW_ROOT_PASSWD */

  else
    return False;
}

#else  /* VMS */
Bool pwent_lock_init (int argc, char **argv, Bool verbose_p) { return True; }
#endif /* VMS */

#endif /* NO_LOCKING -- whole file */