Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

auth-shadow.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 Darren Tucker.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *
00013  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00014  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00015  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00016  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00017  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00018  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00019  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00020  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00021  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00022  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00023  */
00024 
00025 #include "includes.h"
00026 RCSID("$Id: auth-shadow.c,v 1.7 2005/07/17 07:04:47 djm Exp $");
00027 
00028 #if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
00029 #include <shadow.h>
00030 
00031 #include "auth.h"
00032 #include "buffer.h"
00033 #include "log.h"
00034 
00035 #ifdef DAY
00036 # undef DAY
00037 #endif
00038 #define DAY     (24L * 60 * 60) /* 1 day in seconds */
00039 
00040 extern Buffer loginmsg;
00041 
00042 /*
00043  * For the account and password expiration functions, we assume the expiry
00044  * occurs the day after the day specified.
00045  */
00046 
00047 /*
00048  * Check if specified account is expired.  Returns 1 if account is expired,
00049  * 0 otherwise.
00050  */
00051 int
00052 auth_shadow_acctexpired(struct spwd *spw)
00053 {
00054         time_t today;
00055         int daysleft;
00056         char buf[256];
00057 
00058         today = time(NULL) / DAY;
00059         daysleft = spw->sp_expire - today;
00060         debug3("%s: today %d sp_expire %d days left %d", __func__, (int)today,
00061             (int)spw->sp_expire, daysleft);
00062 
00063         if (spw->sp_expire == -1) {
00064                 debug3("account expiration disabled");
00065         } else if (daysleft < 0) {
00066                 logit("Account %.100s has expired", spw->sp_namp);
00067                 return 1;
00068         } else if (daysleft <= spw->sp_warn) {
00069                 debug3("account will expire in %d days", daysleft);
00070                 snprintf(buf, sizeof(buf),
00071                     "Your account will expire in %d day%s.\n", daysleft,
00072                     daysleft == 1 ? "" : "s");
00073                 buffer_append(&loginmsg, buf, strlen(buf));
00074         }
00075 
00076         return 0;
00077 }
00078 
00079 /*
00080  * Checks password expiry for platforms that use shadow passwd files.
00081  * Returns: 1 = password expired, 0 = password not expired
00082  */
00083 int
00084 auth_shadow_pwexpired(Authctxt *ctxt)
00085 {
00086         struct spwd *spw = NULL;
00087         const char *user = ctxt->pw->pw_name;
00088         char buf[256];
00089         time_t today;
00090         int daysleft, disabled = 0;
00091 
00092         if ((spw = getspnam((char *)user)) == NULL) {
00093                 error("Could not get shadow information for %.100s", user);
00094                 return 0;
00095         }
00096 
00097         today = time(NULL) / DAY;
00098         debug3("%s: today %d sp_lstchg %d sp_max %d", __func__, (int)today,
00099             (int)spw->sp_lstchg, (int)spw->sp_max);
00100 
00101 #if defined(__hpux) && !defined(HAVE_SECUREWARE)
00102         if (iscomsec()) {
00103                 struct pr_passwd *pr;
00104 
00105                 pr = getprpwnam((char *)user);
00106 
00107                 /* Test for Trusted Mode expiry disabled */
00108                 if (pr != NULL && pr->ufld.fd_min == 0 &&
00109                     pr->ufld.fd_lifetime == 0 && pr->ufld.fd_expire == 0 &&
00110                     pr->ufld.fd_pw_expire_warning == 0 &&
00111                     pr->ufld.fd_schange != 0)
00112                         disabled = 1;
00113         }
00114 #endif
00115 
00116         /* TODO: check sp_inact */
00117         daysleft = spw->sp_lstchg + spw->sp_max - today;
00118         if (disabled) {
00119                 debug3("password expiration disabled");
00120         } else if (spw->sp_lstchg == 0) {
00121                 logit("User %.100s password has expired (root forced)", user);
00122                 return 1;
00123         } else if (spw->sp_max == -1) {
00124                 debug3("password expiration disabled");
00125         } else if (daysleft < 0) {
00126                 logit("User %.100s password has expired (password aged)", user);
00127                 return 1;
00128         } else if (daysleft <= spw->sp_warn) {
00129                 debug3("password will expire in %d days", daysleft);
00130                 snprintf(buf, sizeof(buf),
00131                     "Your password will expire in %d day%s.\n", daysleft,
00132                     daysleft == 1 ? "" : "s");
00133                 buffer_append(&loginmsg, buf, strlen(buf));
00134         }
00135 
00136         return 0;
00137 }
00138 #endif  /* USE_SHADOW && HAS_SHADOW_EXPIRE */

© sourcejam.com 2005-2008