File: cvm-qmail.c

package info (click to toggle)
cvm 0.97-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,064 kB
  • sloc: ansic: 4,065; sh: 2,730; makefile: 29; sql: 15
file content (179 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download | duplicates (4)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* cvm-qmail.c - qmail lookup-only CVM
 * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <bglibs/sysdeps.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <bglibs/ibuf.h>
#include <bglibs/striter.h>

#include "module.h"
#include "qmail.h"

const char program[] = "cvm-qmail";

static struct qmail_user user;
static str domain;
static str username;
static str ext;
static str programs;
static int check_perms = 0;
static uid_t saved_uid;
static gid_t saved_gid;

int cvm_module_init(void)
{
  const char* tmp;
  if (qmail_lookup_init() == -1)
    return CVME_IO;
  if ((tmp = getenv("CVM_QMAIL_LOOKASIDE")) != 0) {
    if (!str_copys(&programs, tmp))
      return CVME_IO | CVME_FATAL;
    str_subst(&programs, ' ', 0);
  }

  if ((tmp = getenv("CVM_QMAIL_CHECK_PERMS")) != 0 && tmp[0] != 0) {
    check_perms = (tmp[0] == '-') ? CVME_PERMFAIL : CVME_IO;
    saved_uid = getuid();
    saved_gid = getgid();
  }

  return 0;
}

static int lookup_programs(const str* path)
{
  static str dotqmail;
  striter line;
  striter progname;
  const char* start;
  const char* end;
  unsigned long left;

  if (!ibuf_openreadclose(path->s, &dotqmail))
    return -1;
  striter_loop(&line, &dotqmail, '\n') {
    /* skip over spaces preceding '|' */
    for (start = line.startptr, left = line.len;
	 left > 0 && isspace(*start);
	 --left, ++start)
      ;
    if (left > 0 && *start == '|') {
      /* skip spaces preceding the program name */
      for (++start, --left;
	   left > 0 && isspace(*start);
	   --left, ++start)
	;
      /* the program name ends at the first space */
      for (end = start;
	   left > 0 && !isspace(*end);
	   --left, ++end)
	;
      if (end > start) {
	striter_loop(&progname, &programs, 0) {
	  if ((unsigned long)(end - start) == progname.len
	      && memcmp(progname.startptr, start, progname.len) == 0)
	    return 1;
	}
      }
    }
  }
  return 0;
}

/* Account name is either "baseuser-virtuser" or "virtuser@domain" */
int cvm_module_lookup(void)
{
  static str path;
  int r;

  switch (qmail_lookup_cvm(&user, &domain, &username, &ext)) {
  case -1:
    return CVME_IO;
  case 0:
    break;
  case 1:
    cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 1);
    return CVME_PERMFAIL;
  default:
    cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 0);
    return CVME_PERMFAIL;
  }

  if (check_perms) {
    setegid(user.gid);
    seteuid(user.uid);
  }
  r = qmail_dotfile_exists(&user, ext.s, &path);
  if (check_perms) {
    seteuid(saved_uid);
    setegid(saved_gid);
  }
  switch (r) {
  case -1:
    if (errno == EACCES && check_perms == CVME_PERMFAIL) {
      cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 0);
      return CVME_PERMFAIL;
    }
    return CVME_IO;
  case 0:
    cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 0);
    return CVME_PERMFAIL;
  }

  if (programs.len > 0) {
    switch (lookup_programs(&path)) {
    case -1:
      return CVME_IO;
    case 0:
      break;
    default:
      cvm_module_fact_uint(CVM_FACT_OUTOFSCOPE, 1);
      return CVME_PERMFAIL;
    }
  }

  return 0;
}

int cvm_module_authenticate(void)
{
  return CVME_CONFIG;
}

int cvm_module_results(void)
{
  cvm_fact_username = user.user.s;
  cvm_fact_userid = user.uid;
  cvm_fact_groupid = user.gid;
  cvm_fact_realname = 0;
  cvm_fact_directory = user.homedir.s;
  cvm_fact_shell = 0;
  cvm_fact_sys_username = user.user.s;
  cvm_fact_sys_directory = user.homedir.s;
  cvm_fact_domain = domain.s;
  cvm_fact_mailbox = user.homedir.s;
  return 0;
}

void cvm_module_stop(void)
{
}