File: sql-auth.c

package info (click to toggle)
cvm 0.97-0.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,036 kB
  • sloc: ansic: 4,065; sh: 2,758; makefile: 235; sql: 15
file content (106 lines) | stat: -rw-r--r-- 2,993 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
/* cvm/sql-auth.c - Generic SQL authentication layer
 * 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 <stdlib.h>
#include <string.h>
#include <bglibs/str.h>
#include <pwcmp/client.h>
#include "module.h"
#include "sql.h"

static const char* query;
static const char* postq;

int cvm_module_init(void)
{
  int result;

  if ((query = getenv(sql_query_var)) == 0) return CVME_CONFIG;
  if (!sql_query_validate(query)) return CVME_CONFIG;

  if ((postq = getenv(sql_postq_var)) != 0)
    if (!sql_query_validate(postq)) return CVME_CONFIG;
  
  if ((result = sql_auth_init()) != 0) return result;
  
  if (!pwcmp_start(getenv(sql_pwcmp_var))) return CVME_GENERAL;

  return 0;
}

static str q;

int cvm_module_lookup(void)
{
  int i;

  /* Query the database based on the custom query */  
  if (!sql_query_build(query, &q)) return CVME_GENERAL | CVME_FATAL;
  if ((i = sql_auth_query(&q)) < 0) return -i;

  /* If the result didn't produce a single row, fail the username */
  return (i == 1) ? 0 : CVME_PERMFAIL;
}

int cvm_module_authenticate(void)
{
  const char* cpw;

  CVM_CRED_REQUIRED(PASSWORD);
  
  /* If there is no password field, fail the password */
  cpw = sql_get_field(0);
  if (cpw == 0 || cpw[0] == 0) return CVME_PERMFAIL;

  /* Finally, if the stored pass is not the same, fail the pass */
  switch (pwcmp_check(cvm_module_credentials[CVM_CRED_PASSWORD].s, cpw)) {
  case 0: return 0;
  case -1: return CVME_IO | CVME_FATAL;
  default: return CVME_PERMFAIL;
  }
}

int cvm_module_results(void)
{
  int i;

  if (postq) {
    if (!sql_query_build(postq, &q)) return CVME_GENERAL | CVME_FATAL;
    if ((i = sql_post_query(&q)) != 0) return i;
  }
  
  /* Credentials accepted */
  cvm_fact_username = sql_get_field(1);
  cvm_fact_userid = strtol(sql_get_field(2), 0, 10);
  cvm_fact_groupid = strtol(sql_get_field(3), 0, 10);
  cvm_fact_directory = sql_get_field(4);
  cvm_fact_realname = sql_get_field(5);
  cvm_fact_shell = sql_get_field(6);
  cvm_fact_groupname = sql_get_field(7);
  cvm_fact_domain = sql_get_field(8);
  cvm_fact_sys_username = sql_get_field(9);
  cvm_fact_sys_directory = sql_get_field(10);
  cvm_fact_mailbox = sql_get_field(11);
  
  return 0;
}

void cvm_module_stop(void)
{
  pwcmp_stop();
  sql_auth_stop();
}