File: ngetty-argv.c

package info (click to toggle)
ngetty 1.0-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 304 kB
  • ctags: 207
  • sloc: ansic: 1,503; makefile: 236; sh: 109; asm: 100
file content (156 lines) | stat: -rw-r--r-- 3,856 bytes parent folder | download
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <alloca.h>
#include <grp.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "str_defs.h"
#include "scan_defs.h"
#include "fmt_defs.h"
#define UUU "unable to "

void w(char *s) { write(2, s, str_len(s)); }
void e(char *x0, char *x1) 
  { w("\nngetty-argv: "); w(x0); w(x1); w("\n"); sleep(1); }
/* sleep is needed to see the erros.  see the option clear also */

unsigned int expand_string(char *tmp, char *string, char *user, char *tty) {
  char *s=string, *pos=tmp, *exp, ch, found_one = 0;
  for (; (ch=*s); s++) {
    if (ch == '%') {
      found_one = 1;
      ch = s[1];
      exp = user;

      switch (ch) {
      case 'T': exp = tty;
      case 'U':
	pos += (tmp) ? str_copy(pos, exp) : str_len(exp);
	++s;
	break;
      case '%': /*  %%U -> %U */
	++s;
      default:
	ch = *s;
	goto non_special;
      }
    } else {
    non_special:
      if (tmp) *pos = ch;
      ++pos;
    }
  }

  if (found_one) {
    if (tmp==0) return pos - tmp;
    else *pos = 0;
  }
  return 0;
}

int main(int argc, char **argv, char **env) {
  char **arg, **aa, *in, *s, *tmp, ch;
  char *user, *tty, flagsetsid=0, flagdaemon=0, flagtty=0, *flagpid=0;
  unsigned long n;
  uid_t uid=0;
  gid_t gid=0;

  if (argc<2) {
  usage:
    e("usage:\n\tngetty-argv [Options][--]ArgvString [user [tty]]\n\n",
      "\tArgvString:\t:/bin/sleep:hacker_sleep:39:other:args...\n"
      "\tOptions:\t:-D:-S:-N:-C:-u123:-g59:-a45:-s3:-d/tmp:-r/var/tmp\n"
      "\tOptions:\t:-e,HOME=/,TERM=linux:-p/var/run/ngetty.pid\n"
      "\tExample:\t:-u106:-g506:-d/var/qmail:--:./bin/qmail-qread:queue\n");
    return 100;
  }
  
  in	= argv[1];
  user	=(argc>2) ? argv[2] : "";
  tty	=(argc>3) ? argv[3] : "";

  ch = *in++;	 /* don't use '%' as split char */
  if (ch == 0 || *in == 0 ||
      (n=splitmem(0, in, ch)) < 2) goto usage;

  arg = alloca((n+5) * sizeof(char *)); if (arg==0) _exit(111);
  splitmem(arg, in, ch);

  for (aa=arg; (s=*aa); ++aa) {
    n = expand_string(0, s, user, tty);
    if (n==0) continue;
    tmp = alloca(n + 4); if (tmp==0) _exit(111);
    *aa = tmp;
    expand_string(tmp, s, user, tty);
  }

  for (aa=arg; (s=*aa); aa++) {
    if (*s != '-') break;
    s += 2;
    switch (s[-1]) {
    case 'e':
      if (*s == 0) { env = 0; }
      else {
	ch = *s++;
	n=splitmem(0,s,ch);
	env=alloca((n+1) * sizeof(char*)); if (env==0) _exit(1);
	splitmem(env,s,ch);
      }
      break;
    case 'D': flagdaemon=1; break;
    case 'S': flagsetsid=1; break;
    case 'N': flagtty=1; break;
    case 'C': flagtty=2; break;
    case 'p': if (*s) flagpid=s; break;
    case 'g': scan_ulong(s, &n); gid = n; break;
    case 'u': scan_ulong(s, &n); uid = n; break;
    case 'a': scan_ulong(s, &n); alarm(n); break;
    case 's': scan_ulong(s, &n); sleep(n); break;
    case 'd': if (chdir(s))  { e(s, ": chdir error");  return 100; } break;
    case 'r': if (chroot(s)) { e(s, ": chroot error"); return 100; } break;
    case '-': aa++; goto do_it;
    default:
      e(s-2, " : unknown option");
      return 1;
    }
  }

 do_it:
  if (flagdaemon) {
    int pid;
    while ((pid=fork()) <0) sleep(1);
    if (pid) _exit(0);
  }

  if (flagsetsid) setsid();
  if (flagtty==1) ioctl(0, TIOCNOTTY, (void *)1);
  if (flagtty==2) ioctl(0, TIOCSCTTY, (void *)1);

  if (flagpid) {
    int fd = open(flagpid, O_RDWR | O_TRUNC | O_CREAT, 0644);
    if (fd>=0) {
      char tmp[12];
      n=fmt_ulong(tmp, getpid());
      tmp[n] = '\n';
      write(fd, tmp, n+1);
      close(fd);
    }
  }

  if (uid || gid) {
    if (gid==0) gid = uid;
    if (setgroups(1,&gid) ||
	setgid(gid) ||
	setuid(uid)) { e(UUU, "set uidgid"); return 100; }
  }
  
  s = *aa++;
  if (s==0 || *aa==0) return 0;
  if (aa[0][0] == 0) aa[0] = s;

  execve(s, aa, env);
  e(s, ": exec error");
  return 127;
}