chiark / gitweb /
Upstream qmail 1.01
[qmail] / qmail-getpw.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <pwd.h>
4 #include "readwrite.h"
5 #include "substdio.h"
6 #include "subfd.h"
7 #include "error.h"
8 #include "exit.h"
9 #include "byte.h"
10 #include "str.h"
11 #include "case.h"
12 #include "fmt.h"
13 #include "auto_usera.h"
14 #include "auto_break.h"
15 #include "qlx.h"
16
17 #define GETPW_USERLEN 32
18
19 char *local;
20 struct passwd *pw;
21 char *dash;
22 char *extension;
23
24 int userext()
25 {
26   char username[GETPW_USERLEN];
27   struct stat st;
28
29   extension = local + str_len(local);
30   for (;;) {
31     if (extension - local < sizeof(username))
32       if (!*extension || (*extension == *auto_break)) {
33         byte_copy(username,extension - local,local);
34         username[extension - local] = 0;
35         case_lowers(username);
36         pw = getpwnam(username);
37         if (pw)
38           if (pw->pw_uid)
39             if (stat(pw->pw_dir,&st) == 0) {
40               if (st.st_uid == pw->pw_uid) {
41                 dash = "";
42                 if (*extension) { ++extension; dash = "-"; }
43                 return 1;
44               }
45             }
46             else
47               if (error_temp(errno)) _exit(QLX_NFS);
48       }
49     if (extension == local) return 0;
50     --extension;
51   }
52 }
53
54 char num[FMT_ULONG];
55
56 void main(argc,argv)
57 int argc;
58 char **argv;
59 {
60   local = argv[1];
61   if (!local) _exit(100);
62
63   if (!userext()) {
64     extension = local;
65     dash = "-";
66     pw = getpwnam(auto_usera);
67   }
68
69   if (!pw) _exit(QLX_NOALIAS);
70
71   substdio_puts(subfdoutsmall,pw->pw_name);
72   substdio_put(subfdoutsmall,"",1);
73   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_uid));
74   substdio_put(subfdoutsmall,"",1);
75   substdio_put(subfdoutsmall,num,fmt_ulong(num,(long) pw->pw_gid));
76   substdio_put(subfdoutsmall,"",1);
77   substdio_puts(subfdoutsmall,pw->pw_dir);
78   substdio_put(subfdoutsmall,"",1);
79   substdio_puts(subfdoutsmall,dash);
80   substdio_put(subfdoutsmall,"",1);
81   substdio_puts(subfdoutsmall,extension);
82   substdio_put(subfdoutsmall,"",1);
83   substdio_flush(subfdoutsmall);
84
85   _exit(0);
86 }