]> granicus.if.org Git - shadow/blob - src/expiry.c
[svn-upgrade] Integrating new upstream version, shadow (4.0.1)
[shadow] / src / expiry.c
1 /*
2  * Copyright 1994, Julianne Frances Haugh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include "rcsid.h"
33 RCSID (PKG_VER "$Id: expiry.c,v 1.12 2002/01/05 15:41:43 kloczek Exp $")
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <stdio.h>
37 #include "prototypes.h"
38 #include "defines.h"
39 #include <pwd.h>
40 /* local function prototypes */
41 static RETSIGTYPE catch (int);
42 static void usage (void);
43
44 /*
45  * catch - signal catcher
46  */
47
48 static RETSIGTYPE catch (int sig)
49 {
50         exit (10);
51 }
52
53 /*
54  * usage - print syntax message and exit
55  */
56
57 static void usage (void)
58 {
59         fprintf (stderr, _("Usage: expiry {-f|-c}\n"));
60         exit (10);
61 }
62
63 /* 
64  * expiry - check and enforce password expiration policy
65  *
66  *      expiry checks (-c) the current password expiraction and forces (-f)
67  *      changes when required. It is callable as a normal user command.
68  */
69
70 int main (int argc, char **argv)
71 {
72         struct passwd *pwd;
73
74 #ifdef  SHADOWPWD
75         struct spwd *spwd;
76 #endif
77         char *Prog = argv[0];
78
79         sanitize_env ();
80
81         /* 
82          * Start by disabling all of the keyboard signals.
83          */
84
85         signal (SIGHUP, catch);
86         signal (SIGINT, catch);
87         signal (SIGQUIT, catch);
88 #ifdef  SIGTSTP
89         signal (SIGTSTP, catch);
90 #endif
91
92         /*
93          * expiry takes one of two arguments. The default action is to give
94          * the usage message.
95          */
96
97         setlocale (LC_ALL, "");
98         bindtextdomain (PACKAGE, LOCALEDIR);
99         textdomain (PACKAGE);
100
101         if (argc != 2
102             || (strcmp (argv[1], "-f") && strcmp (argv[1], "-c")))
103                 usage ();
104
105 #if 0                           /* could be setgid shadow with /etc/shadow mode 0640 */
106         /*
107          * Make sure I am root. Can't open /etc/shadow without root
108          * authority.
109          */
110
111         if (geteuid () != 0) {
112                 fprintf (stderr,
113                          _("%s: WARNING!  Must be set-UID root!\n"),
114                          argv[0]);
115                 exit (10);
116         }
117 #endif
118
119         /*
120          * Get user entries for /etc/passwd and /etc/shadow
121          */
122
123         if (!(pwd = get_my_pwent ())) {
124                 fprintf (stderr, _("%s: unknown user\n"), Prog);
125                 exit (10);
126         }
127 #ifdef  SHADOWPWD
128         spwd = getspnam (pwd->pw_name);
129 #endif
130
131         /*
132          * If checking accounts, use agecheck() function.
133          */
134
135         if (strcmp (argv[1], "-c") == 0) {
136
137                 /*
138                  * Print out number of days until expiration.
139                  */
140
141 #ifdef  SHADOWPWD
142                 agecheck (pwd, spwd);
143 #else
144                 agecheck (pwd);
145 #endif
146
147                 /*
148                  * Exit with status indicating state of account.
149                  */
150
151 #ifdef  SHADOWPWD
152                 exit (isexpired (pwd, spwd));
153 #else
154                 exit (isexpired (pwd));
155 #endif
156         }
157
158         /*
159          * If forcing password change, use expire() function.
160          */
161
162         if (strcmp (argv[1], "-f") == 0) {
163
164                 /*
165                  * Just call expire(). It will force the change or give a
166                  * message indicating what to do. And it doesn't return at
167                  * all unless the account is unexpired.
168                  */
169
170 #ifdef  SHADOWPWD
171                 expire (pwd, spwd);
172 #else
173                 expire (pwd);
174 #endif
175                 exit (0);
176         }
177
178         /*
179          * Can't get here ...
180          */
181
182         usage ();
183         exit (1);
184 }