File: qmail-getpw.c

package info (click to toggle)
qmail 1.01-5
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,188 kB
  • ctags: 1,711
  • sloc: ansic: 13,993; makefile: 1,914; perl: 448; sh: 377
file content (86 lines) | stat: -rw-r--r-- 1,862 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
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include "readwrite.h"
#include "substdio.h"
#include "subfd.h"
#include "error.h"
#include "exit.h"
#include "byte.h"
#include "str.h"
#include "case.h"
#include "fmt.h"
#include "auto_usera.h"
#include "auto_break.h"
#include "qlx.h"

#define GETPW_USERLEN 32

char *local;
struct passwd *pw;
char *dash;
char *extension;

int userext()
{
  char username[GETPW_USERLEN];
  struct stat st;

  extension = local + str_len(local);
  for (;;) {
    if (extension - local < sizeof(username))
      if (!*extension || (*extension == *auto_break)) {
	byte_copy(username,extension - local,local);
	username[extension - local] = 0;
	case_lowers(username);
	pw = getpwnam(username);
	if (pw)
	  if (pw->pw_uid)
	    if (stat(pw->pw_dir,&st) == 0) {
	      if (st.st_uid == pw->pw_uid) {
		dash = "";
		if (*extension) { ++extension; dash = "-"; }
		return 1;
	      }
	    }
	    else
	      if (error_temp(errno)) _exit(QLX_NFS);
      }
    if (extension == local) return 0;
    --extension;
  }
}

char num[FMT_ULONG];

void main(argc,argv)
int argc;
char **argv;
{
  local = argv[1];
  if (!local) _exit(100);

  if (!userext()) {
    extension = local;
    dash = "-";
    pw = getpwnam(auto_usera);
  }

  if (!pw) _exit(QLX_NOALIAS);

  substdio_puts(subfdoutsmall,pw->pw_name);
  substdio_put(subfdoutsmall,"",1);
  substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_uid));
  substdio_put(subfdoutsmall,"",1);
  substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_gid));
  substdio_put(subfdoutsmall,"",1);
  substdio_puts(subfdoutsmall,pw->pw_dir);
  substdio_put(subfdoutsmall,"",1);
  substdio_puts(subfdoutsmall,dash);
  substdio_put(subfdoutsmall,"",1);
  substdio_puts(subfdoutsmall,extension);
  substdio_put(subfdoutsmall,"",1);
  substdio_flush(subfdoutsmall);

  _exit(0);
}