]> granicus.if.org Git - shadow/blob - src/expiry.c
Make sure every source files are distributed with a copyright and license.
[shadow] / src / expiry.c
1 /*
2  * Copyright (c) 1994       , Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2008, 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$"
36
37 #include <pwd.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include "defines.h"
42 #include "prototypes.h"
43 /* local function prototypes */
44 static RETSIGTYPE catch_signals (int);
45 static void usage (void);
46
47 /*
48  * catch_signals - signal catcher
49  */
50 static RETSIGTYPE catch_signals (unused int sig)
51 {
52         exit (10);
53 }
54
55 /*
56  * usage - print syntax message and exit
57  */
58 static void usage (void)
59 {
60         fputs (_("Usage: expiry {-f|-c}\n"), stderr);
61         exit (10);
62 }
63
64 /* 
65  * expiry - check and enforce password expiration policy
66  *
67  *      expiry checks (-c) the current password expiration and forces (-f)
68  *      changes when required. It is callable as a normal user command.
69  */
70 int main (int argc, char **argv)
71 {
72         struct passwd *pwd;
73
74         struct spwd *spwd;
75         char *Prog = argv[0];
76
77         sanitize_env ();
78
79         /* 
80          * Start by disabling all of the keyboard signals.
81          */
82         signal (SIGHUP, catch_signals);
83         signal (SIGINT, catch_signals);
84         signal (SIGQUIT, catch_signals);
85 #ifdef  SIGTSTP
86         signal (SIGTSTP, catch_signals);
87 #endif
88
89         /*
90          * expiry takes one of two arguments. The default action is to give
91          * the usage message.
92          */
93         setlocale (LC_ALL, "");
94         bindtextdomain (PACKAGE, LOCALEDIR);
95         textdomain (PACKAGE);
96
97         if (argc != 2 || (strcmp (argv[1], "-f") && strcmp (argv[1], "-c")))
98                 usage ();
99
100         /*
101          * Get user entries for /etc/passwd and /etc/shadow
102          */
103         if (!(pwd = get_my_pwent ())) {
104                 fprintf (stderr, _("%s: unknown user\n"), Prog);
105                 exit (10);
106         }
107         spwd = getspnam (pwd->pw_name); /* !USE_PAM, No need for xgetspnam */
108
109         /*
110          * If checking accounts, use agecheck() function.
111          */
112         if (strcmp (argv[1], "-c") == 0) {
113
114                 /*
115                  * Print out number of days until expiration.
116                  */
117                 agecheck (pwd, spwd);
118
119                 /*
120                  * Exit with status indicating state of account.
121                  */
122                 exit (isexpired (pwd, spwd));
123         }
124
125         /*
126          * If forcing password change, use expire() function.
127          */
128         if (strcmp (argv[1], "-f") == 0) {
129
130                 /*
131                  * Just call expire(). It will force the change or give a
132                  * message indicating what to do. And it doesn't return at
133                  * all unless the account is unexpired.
134                  */
135                 expire (pwd, spwd);
136                 exit (0);
137         }
138
139         /*
140          * Can't get here ...
141          */
142         usage ();
143         exit (1);
144 }