]> granicus.if.org Git - shadow/blob - src/expiry.c
[svn-upgrade] Integrating new upstream version, shadow (19990709)
[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.8 1999/06/07 16:40:45 marekm Exp $")
34
35 #include <sys/types.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include "prototypes.h"
39 #include "defines.h"
40 #include <pwd.h>
41
42 #ifndef AGING
43 #if defined(HAVE_USERSEC_H) || defined(SHADOWPWD)
44 #define AGING   1
45 #endif
46 #endif
47
48 int main P_((int, char **));
49
50 #if !defined(SHADOWPWD) && !defined(AGING) /*{*/
51
52 /*
53  * Not much to do here ...
54  */
55
56 int
57 main(int argc, char **argv)
58 {
59         exit (0);
60 }
61
62 #else   /*} AGING || SHADOWPWD {*/
63
64 /* local function prototypes */
65 static RETSIGTYPE catch P_((int));
66 static void usage P_((void));
67
68 /*
69  * catch - signal catcher
70  */
71
72 static RETSIGTYPE
73 catch(int sig)
74 {
75         exit (10);
76 }
77
78 /*
79  * usage - print syntax message and exit
80  */
81
82 static void
83 usage(void)
84 {
85         fprintf(stderr, _("Usage: expiry { -f | -c }\n"));
86         exit(10);
87 }
88
89 /* 
90  * expiry - check and enforce password expiration policy
91  *
92  *      expiry checks (-c) the current password expiraction and
93  *      forces (-f) changes when required.  It is callable as a
94  *      normal user command.
95  */
96
97 int
98 main(int argc, char **argv)
99 {
100         struct  passwd  *pwd;
101 #ifdef  SHADOWPWD
102         struct  spwd    *spwd;
103 #endif
104         char *Prog = argv[0];
105
106         sanitize_env();
107
108         /* 
109          * Start by disabling all of the keyboard signals.
110          */
111
112         signal (SIGHUP, catch);
113         signal (SIGINT, catch);
114         signal (SIGQUIT, catch);
115 #ifdef  SIGTSTP
116         signal (SIGTSTP, catch);
117 #endif
118
119         /*
120          * expiry takes one of two arguments.  The default action
121          * is to give the usage message.
122          */
123
124         setlocale(LC_ALL, "");
125         bindtextdomain(PACKAGE, LOCALEDIR);
126         textdomain(PACKAGE);
127
128         if (argc != 2 || (strcmp (argv[1], "-f")  && strcmp (argv[1], "-c")))
129                 usage ();
130
131 #if 0  /* could be setgid shadow with /etc/shadow mode 0640 */
132         /*
133          * Make sure I am root.  Can't open /etc/shadow without root
134          * authority.
135          */
136
137         if (geteuid () != 0) {
138                 fprintf(stderr, _("%s: WARNING!  Must be set-UID root!\n"),
139                         argv[0]);
140                 exit(10);
141         }
142 #endif
143
144         /*
145          * Get user entries for /etc/passwd and /etc/shadow
146          */
147
148         if (!(pwd = get_my_pwent())) {
149                 fprintf(stderr, _("%s: unknown user\n"), Prog);
150                 exit(10);
151         }
152 #ifdef  SHADOWPWD
153         spwd = getspnam(pwd->pw_name);
154 #endif
155
156         /*
157          * If checking accounts, use agecheck() function.
158          */
159
160         if (strcmp (argv[1], "-c") == 0) {
161
162                 /*
163                  * Print out number of days until expiration.
164                  */
165
166 #ifdef  SHADOWPWD
167                 agecheck (pwd, spwd);
168 #else
169                 agecheck (pwd);
170 #endif
171
172                 /*
173                  * Exit with status indicating state of account --
174                  */
175
176 #ifdef  SHADOWPWD
177                 exit (isexpired (pwd, spwd));
178 #else
179                 exit (isexpired (pwd));
180 #endif
181         }
182
183         /*
184          * If forcing password change, use expire() function.
185          */
186
187         if (strcmp (argv[1], "-f") == 0) {
188
189                 /*
190                  * Just call expire().  It will force the change
191                  * or give a message indicating what to do.  And
192                  * it doesn't return at all unless the account
193                  * is unexpired.
194                  */
195
196 #ifdef  SHADOWPWD
197                 expire (pwd, spwd);
198 #else
199                 expire (pwd);
200 #endif
201                 exit (0);
202         }
203
204         /*
205          * Can't get here ...
206          */
207
208         usage ();
209         exit (1);
210 }
211 #endif  /*}*/