2 Copyright (C) 2001-2011 Ben Kibbey <bjk@luxsci.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 * Userinfo module to check file existance in a users home directory. The only
20 * required flag is -f to specify the filename which only checks for the file
21 * existance and not mode or type. This module is not chainable. It gets the
22 * users home directory from the password structure passed from the main
25 * Compile with: cc -O2 -fPIC -shared -o fexists.so fexists.c
26 * Run with: ui -O ./fexists.so -f filename --
28 * Ben Kibbey <bjk@luxsci.net>
36 #include <sys/types.h>
41 #define F_WRITE 0x0002
42 #define F_WRITE_W 0x0004
45 static struct file_s
{
51 extern void add_string(char ***, const char *);
53 void ui_module_init(int *chainable
)
62 for (i
= 0; i
< idx
; i
++)
72 printf(" Check for file existance in a users home directory ([-rwWd] -f <file>).\n");
73 printf("\t-r filename is readable by you\n");
74 printf("\t-w filename is writeable\n");
75 printf("\t-W filename is writeable by you\n");
76 printf("\t-d filename is a directory\n\n");
79 char *ui_module_options_init(char **defaults
)
85 int ui_module_options(int argc
, char **argv
)
90 while ((opt
= getopt(argc
, argv
, "f:rwWd")) != -1) {
108 if ((files
= realloc(files
, (idx
+ 1) * sizeof(struct file_s
))) == NULL
) {
113 files
[idx
].name
= strdup(optarg
);
114 files
[idx
++].flags
= flags
;
122 return !files
? 1 : 0;
125 int ui_module_exec(char ***results
, const struct passwd
*pw
, const int msep
, const int verbose
,
128 char **strings
= *results
;
129 char buf
[FILENAME_MAX
];
134 for (i
= 0; i
< idx
; i
++) {
136 snprintf(buf
, sizeof(buf
), "%s/%s", pw
->pw_dir
, files
[i
].name
);
138 if (stat(buf
, &st
) != -1) {
141 if (files
[i
].flags
& F_READ
) {
142 if (access(buf
, R_OK
) == -1) {
150 if (files
[i
].flags
& F_WRITE
) {
151 if (access(buf
, W_OK
) == -1) {
159 if (files
[i
].flags
& F_WRITE
) {
160 if (st
.st_mode
& S_IWOTH
)
168 if (files
[i
].flags
& F_DIR
) {
169 if (S_ISDIR(st
.st_mode
))
184 add_string(&strings
, "!");
186 add_string(&strings
, (ret
) ? "1" : "0");