]> granicus.if.org Git - shadow/blob - libmisc/hushed.c
* libmisc/chowntty.c: Improve the logs for fchown and fchmod
[shadow] / libmisc / hushed.c
1 /*
2  * Copyright (c) 1991 - 1993, Julianne Frances Haugh
3  * Copyright (c) 1991 - 1993, Chip Rosenthal
4  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
5  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
6  * Copyright (c) 2008       , Nicolas François
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the copyright holders or contributors may not be used to
18  *    endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <config.h>
35
36 #ident "$Id$"
37
38 #include <sys/types.h>
39 #include <stdio.h>
40 #include <pwd.h>
41 #include "defines.h"
42 #include "prototypes.h"
43 #include "getdef.h"
44 /*
45  * hushed - determine if a user receives login messages
46  *
47  * Look in the hushed-logins file (or user's home directory) to see
48  * if the user is to receive the login-time messages.
49  */
50 bool hushed (const struct passwd *pw)
51 {
52         char *hushfile;
53         char buf[BUFSIZ];
54         bool found;
55         FILE *fp;
56
57         /*
58          * Get the name of the file to use.  If this option is not
59          * defined, default to a noisy login.
60          */
61
62         hushfile = getdef_str ("HUSHLOGIN_FILE");
63         if (NULL == hushfile) {
64                 return false;
65         }
66
67         /*
68          * If this is not a fully rooted path then see if the
69          * file exists in the user's home directory.
70          */
71
72         if (hushfile[0] != '/') {
73                 snprintf (buf, sizeof (buf), "%s/%s", pw->pw_dir, hushfile);
74                 return (access (buf, F_OK) == 0);
75         }
76
77         /*
78          * If this is a fully rooted path then go through the file
79          * and see if this user, or its shell is in there.
80          */
81
82         fp = fopen (hushfile, "r");
83         if (NULL == fp) {
84                 return false;
85         }
86         for (found = false; !found && (fgets (buf, (int) sizeof buf, fp) == buf);) {
87                 buf[strlen (buf) - 1] = '\0';
88                 found = (strcmp (buf, pw->pw_shell) == 0) ||
89                         (strcmp (buf, pw->pw_name) == 0);
90         }
91         (void) fclose (fp);
92         return found;
93 }
94