summaryrefslogtreecommitdiff
path: root/src/qmail-vmailuser.c
blob: 27dc85d9d8fabdba309d0664ace925527f963cee (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "global.h"
#include "auto_qmail.h"
#include "stralloc.h"
#include "case.h"
#include "control.h"
#include "constmap.h"
#include "direntry.h"
#include "error.h"
#include "str.h"
#include "fmt.h"
#include "open.h"
#include "byte.h"
#include "scan.h"
#include "str.h"

#define FDAUTH 3
#define RESPECT_CASE "-C"
#define BUFFER_SIZE 128

/** @file qmail-vmailuser.c
    @return 0: virtual user exists 
            1: virtual user dir not accessible
            2: qmail-vmailuser is misused
            110: can't read controls
            111: temporary problem
*/

char inputbuf[BUFFER_SIZE];
struct constmap mapvdoms;
stralloc vdoms = {0};
stralloc vdomdir = {0};
stralloc vuser = {0};
stralloc vuserdir = {0};

void pam_exit(int fail)
{
  int i;

  close(FDAUTH);
  for (i = 0; i < sizeof(inputbuf); ++i) inputbuf[i] = 0;
  _exit(fail);
}

int main(int argc,char **argv)
{
  DIR *dir;
  char *vdomuser; 
  char *domain = 0;
  int buflen = 0;
  int domlen = 0;
  int flagrespect = 0;
  int i, r;
  char ch;
  char *homedir = "/home";

  if (argv[1])  
    if (!case_diffs(argv[1],RESPECT_CASE)) {
       flagrespect = 1;
    } else {
      homedir = argv[1];
      dir = opendir(homedir);
      if (!dir) pam_exit(2);
    }

  if (argv[2])  
    if (!case_diffs(argv[2],RESPECT_CASE)) flagrespect = 1;

  if (chdir(auto_qmail) == -1) pam_exit(110);
 
  switch (control_readfile(&vdoms,"control/virtualdomains",0)) {
    case -1: pam_exit(110);
    case  0: if (!constmap_init(&mapvdoms,"",0,1)) pam_exit(111);
    case  1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) pam_exit(111);
  }

  for (;;) {						/* read input */
    do
      r = read(FDAUTH,inputbuf + buflen,sizeof(inputbuf) - buflen);
    while ((r == -1) && (errno == EINTR));
    if (r == -1) pam_exit(111);
    if (r == 0) break;
    buflen += r;
    if (buflen >= sizeof(inputbuf)) pam_exit(2);
  }
  close(FDAUTH);

  if ((r = byte_rchr(inputbuf,buflen,'@')))			/* @domain */
    if (r < buflen && inputbuf[r] == '@') {
      domain = inputbuf + r + 1;
      domlen = str_len(domain);
      if (!flagrespect)
        case_lowerb(inputbuf,buflen);
      else
        case_lowerb(domain,domlen);
    }
  vdomuser = constmap(&mapvdoms,domain,domlen);
  if (!vdomuser) pam_exit(1);

  if (!stralloc_copys(&vuser,"")) pam_exit(111);		/* user */
  for (i = 0; i < r; ++i) {
    ch = inputbuf[i];
    if (ch == '.') ch = ':';
    if (!stralloc_append(&vuser,&ch)) pam_exit(111);
  }
  if (!stralloc_0(&vuser)) pam_exit(111); 

  if (!stralloc_copys(&vdomdir,homedir)) pam_exit(111);  	/* vpopmail */
  if (!stralloc_cats(&vdomdir,"/")) pam_exit(111);  
  if (!stralloc_cats(&vdomdir,"vpopmail")) pam_exit(111);
  if (!stralloc_copy(&vuserdir,&vdomdir)) pam_exit(111);  
  if (!stralloc_cats(&vuserdir,"/domains/")) pam_exit(111);
  if (!stralloc_cats(&vuserdir,vdomuser)) pam_exit(111);  
  if (!stralloc_copy(&vdomdir,&vuserdir)) pam_exit(111);  
  if (!stralloc_0(&vdomdir)) pam_exit(111); 

  dir = opendir(vdomdir.s);
  if (dir) {
    if (!stralloc_cats(&vuserdir,"/")) pam_exit(111);  
    if (!stralloc_cat(&vuserdir,&vuser)) pam_exit(111);  
    if (!stralloc_0(&vuserdir)) pam_exit(111); 

    dir = opendir(vuserdir.s);
    if (dir) pam_exit(0);
  }

  if (!stralloc_copys(&vdomdir,homedir)) pam_exit(111);  	/* vmailmgr */
  if (!stralloc_cats(&vdomdir,"/")) pam_exit(111);  
  if (!stralloc_copy(&vuserdir,&vdomdir)) pam_exit(111);  
  if (!stralloc_cats(&vuserdir,vdomuser)) pam_exit(111);  
  if (!stralloc_cats(&vuserdir,"/users")) pam_exit(111);  
  if (!stralloc_copy(&vdomdir,&vuserdir)) pam_exit(111);  
  if (!stralloc_0(&vdomdir)) pam_exit(111); 

  dir = opendir(vdomdir.s);
  if (dir) {
    if (!stralloc_cats(&vuserdir,"/")) pam_exit(111);  
    if (!stralloc_cat(&vuserdir,&vuser)) pam_exit(111);  
    if (!stralloc_0(&vuserdir)) pam_exit(111); 

    dir = opendir(vuserdir.s);
    if (dir) pam_exit(0);
  }
   
  pam_exit(1);
}