-
Notifications
You must be signed in to change notification settings - Fork 0
/
prsvunix.c
135 lines (119 loc) · 2.97 KB
/
prsvunix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* prsvunix.c */
/* This file contains the UNIX-specific parts of the "elvprsv" program. */
#if OSK
# define ELVPRSV
# include "osk.c"
#else
# include <sys/stat.h>
# include <pwd.h>
#endif
#ifndef __STDC__
/* some older systems don't declare this in pwd.h, I guess. */
extern struct passwd *getpwuid();
#endif
/* This variable is used to add extra error messages for mail sent to root */
char *ps;
/* This function returns the login name of the owner of a file */
char *ownername(filename)
char *filename; /* name of a file */
{
struct stat st;
struct passwd *pw;
/* stat the file, to get its uid */
if (stat(filename, &st) < 0)
{
ps = "stat() failed";
return "root";
}
/* get the /etc/passwd entry for that user */
pw = getpwuid(st.st_uid);
if (!pw)
{
ps = "uid not found in password file";
return "root";
}
/* return the user's name */
return pw->pw_name;
}
/* This function sends a mail message to a given user, saying that a file
* has been preserved.
*/
void mail(user, file, when, tmp)
char *user; /* name of user who should receive the mail */
char *file; /* name of original text file that was preserved */
char *when; /* description of why the file was preserved */
char *tmp; /* NULL normally; else name of tmp file if user should run elvprsv -R */
{
char cmd[80];/* buffer used for constructing a "mail" command */
FILE *m; /* stream used for giving text to the "mail" program */
char *base; /* basename of the file */
/* separate the directory name from the basename. */
for (base = file + strlen(file); --base > file && *base != SLASH; )
{
}
if (*base == SLASH)
{
*base++ = '\0';
}
/* for anonymous buffers, pretend the name was "foo" */
if (!strcmp(base, "*"))
{
base = "foo";
}
/* open a pipe to the "mail" program */
#if OSK
sprintf(cmd, "%s \"-s=%s preserved!\" %s", MAILER, base, user);
#else /* ANY_UNIX */
sprintf(cmd, "%s -s Graceland %s", MAILER, user);
switch (fork())
{
case -1: /* error */
return;
case 0: /* child */
/* surrender any special privileges */
setuid(getuid());
break; /* continue with the rest of this function */
default: /* parent */
wait(NULL);
return;
}
#endif
m = popen(cmd, "w");
if (!m)
{
perror(cmd);
/* Can't send mail! Hope the user figures it out. */
return;
}
/* Tell the user that the file was preserved */
fprintf(m, "A version of your file \"%s%c%s\"\n", file, SLASH, base);
fprintf(m, "was preserved when %s.\n", when);
fprintf(m, "To recover this file, do the following:\n");
fprintf(m, "\n");
#if OSK
fprintf(m, " chd %s\n", file);
#else /* ANY_UNIX */
fprintf(m, " cd %s\n", file);
#endif
if (tmp)
{
fprintf(m, " elvprsv %s\n", tmp);
}
else
{
fprintf(m, " elvrec %s\n", base);
}
fprintf(m, "\n");
fprintf(m, "With fond wishes for a speedy recovery,\n");
fprintf(m, " Elvis\n");
if (ps)
{
fprintf(m, "\nP.S. %s\n", ps);
ps = (char *)0;
}
/* close the stream */
pclose(m);
#if ANY_UNIX
exit(0);
#endif
}