Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/at/atx.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1996-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
/*
22
* Glenn Fowler
23
* AT&T Research
24
*
25
* atx -- the at impersonator
26
*
27
* cd AT_JOB_DIR
28
* $PWD/AT_EXEC_FILE $SHELL <job>
29
*/
30
31
static const char id[] = "\n@(#)$Id: atx (AT&T Research) 2012-02-29 $\0\n";
32
33
#include "at.h"
34
35
/*
36
* prepend current date-time to buffer on fd==2
37
* and drop the initial command label if any
38
*/
39
40
static ssize_t
41
stampwrite(int fd, const void* buf, size_t n)
42
{
43
register char* s;
44
register int i;
45
register ssize_t r;
46
register ssize_t z;
47
48
r = 0;
49
if (fd == 2 && (s = fmttime(AT_TIME_FORMAT, time(0))))
50
{
51
i = strlen(s);
52
s[i++] = ' ';
53
if ((z = write(fd, s, i)) < 0)
54
r = -1;
55
else
56
r += z;
57
for (s = (char*)buf; s < ((char*)buf + n - 1) && !isspace(*s); s++)
58
if (*s == ':')
59
{
60
while (++s < ((char*)buf + n - 1) && isspace(*s));
61
n -= s - (char*)buf;
62
buf = (void*)s;
63
break;
64
}
65
}
66
if ((z = write(fd, buf, n)) < 0)
67
r = -1;
68
else if (r >= 0)
69
r += z;
70
return r;
71
}
72
73
int
74
main(int argc, char** argv)
75
{
76
register int n = 0;
77
unsigned long uid;
78
unsigned long gid;
79
unsigned long tid;
80
struct stat ds;
81
struct stat js;
82
struct stat xs;
83
84
error_info.id = "atx";
85
error_info.write = stampwrite;
86
if (argc != 3 ||
87
++n && lstat(".", &ds) ||
88
++n && !AT_DIR_OK(&ds) ||
89
++n && lstat(argv[2], &js) ||
90
++n && !AT_JOB_OK(&ds, &js) ||
91
++n && !S_ISREG(js.st_mode) ||
92
++n && lstat(AT_EXEC_FILE, &xs) ||
93
++n && !AT_EXEC_OK(&ds, &xs) ||
94
95
++n && sfsscanf(argv[2], "%..36lu.%..36lu.%..36lu", &uid, &gid, &tid) != 3)
96
error(3, "%s: command garbled [%d]", argc >= 3 ? argv[2] : (char*)0, n);
97
if (setgid(gid))
98
error(ERROR_SYSTEM|3, "%s %s group denied (gid=%u egid=%u => gid=%d)", argv[2], error_info.id, getgid(), getegid(), gid);
99
if (setuid(uid))
100
error(ERROR_SYSTEM|3, "%s %s user denied (uid=%u euid=%u => uid=%d)", argv[2], error_info.id, getuid(), geteuid(), uid);
101
setsid();
102
argv++;
103
execvp(argv[0], argv);
104
error(ERROR_SYSTEM|3, "%s: exec failed", argv[2]);
105
return 1;
106
}
107
108