File: v1client.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 (173 lines) | stat: -rw-r--r-- 5,063 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
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
/* cvm/client.c - CVM client library
 * 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 <sys/types.h>
#include <netdb.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

#include <bglibs/sysdeps.h>
#include <bglibs/socket.h>

#include "v1client.h"
#include "protocol.h"

const char* cvm_client_account_split_chars = "@";

static struct cvm_packet request;
static struct cvm_packet response;

/* Packet management code ****************************************************/
static int parse_packet(struct cvm_packet* p)
{
  if (p->data[0] != 0)
    return p->data[0];
  if (p->length < 3
      || p->data[p->length-1] != 0
      || p->data[p->length-2] != 0)
    return CVME_BAD_MODDATA;
  if (cvm_client_fact_str(CVM_FACT_USERNAME, &cvm_fact_username) ||
      cvm_client_fact_uint(CVM_FACT_USERID, &cvm_fact_userid) ||
      cvm_client_fact_uint(CVM_FACT_GROUPID, &cvm_fact_groupid) ||
      cvm_client_fact_str(CVM_FACT_DIRECTORY, &cvm_fact_directory))
    return CVME_BAD_MODDATA;
  cvm_client_fact_str(CVM_FACT_SHELL, &cvm_fact_shell);
  cvm_client_fact_str(CVM_FACT_REALNAME, &cvm_fact_realname);
  cvm_client_fact_str(CVM_FACT_GROUPNAME, &cvm_fact_groupname);
  cvm_client_fact_str(CVM_FACT_SYS_USERNAME, &cvm_fact_sys_username);
  cvm_client_fact_str(CVM_FACT_SYS_DIRECTORY, &cvm_fact_sys_directory);
  cvm_client_fact_str(CVM_FACT_DOMAIN, &cvm_fact_domain);
  cvm_client_fact_str(CVM_FACT_MAILBOX, &cvm_fact_mailbox);
  return 0;
}

static int packet_add(struct cvm_packet* p,
		      const char* str, unsigned len)
{
  unsigned char* ptr;
  if (p->length + len + 1 >= CVM_BUFSIZE-1)
    return 0;
  ptr = p->data + p->length;
  memcpy(ptr, str, len);
  ptr[len] = 0;
  p->length += len + 1;
  return 1;
}

static unsigned build_packet(struct cvm_packet* p,
			     const char* account, const char* domain,
			     const char** credentials, int parse_domain)
{
  unsigned i;
  unsigned actlen;
  
  p->data[0] = CVM1_PROTOCOL;
  p->length = 1;

  actlen = strlen(account);
  if (parse_domain) {
    const char* sc;
    if ((sc = getenv("CVM_ACCOUNT_SPLIT_CHARS")) == 0)
      sc = cvm_client_account_split_chars;
    i = strlen(account);
    while (i-- > 0) {
      if (strchr(sc, account[i]) != 0) {
	domain = account + i + 1;
	actlen = i;
	break;
      }
    }
  }
  
  if (!packet_add(p, account, actlen)) return 0;
  if (!packet_add(p, domain, strlen(domain))) return 0;

  for (i = 0; credentials[i] != 0; i++)
    if (!packet_add(p, credentials[i], strlen(credentials[i])))
      return 0;

  p->data[p->length++] = 0;
  return 1;
}

int cvm_client_fact_str(unsigned number, const char** data)
{
  static unsigned char* ptr = 0;
  static unsigned last_number = -1;
  
  if (!ptr || number != last_number)
    ptr = response.data+1;
  last_number = number;
  
  while (*ptr) {
    unsigned char* tmp = ptr;
    ptr += strlen((char*)ptr) + 1;
    if (*tmp == number) {
      *data = (char*)tmp + 1;
      return 0;
    }
  }
  return CVME_NOFACT;
}

int cvm_client_fact_uint(unsigned number, unsigned long* data)
{
  const char* str;
  unsigned long i;
  int err;
  
  if ((err = cvm_client_fact_str(number, &str)) != 0) return err;

  for (i = 0; *str >= '0' && *str <= '9'; ++str) {
    unsigned long tmp = i;
    i = (i * 10) + (*str - '0');
    if (i < tmp)
      return CVME_BAD_MODDATA;
  }
  if (*str)
    return CVME_BAD_MODDATA;
  *data = i;
  return 0;
}

/* Top-level wrapper *********************************************************/
int cvm_client_authenticate(const char* module, const char* account,
			    const char* domain, const char** credentials,
			    int parse_domain)
{
  int result;
  void (*oldsig)(int);
  if (domain == 0) domain = "";
  if (!build_packet(&request, account, domain, credentials, parse_domain))
    return CVME_GENERAL;
  
  oldsig = signal(SIGPIPE, SIG_IGN);
  if (!memcmp(module, "cvm-udp:", 8))
    result = cvm_xfer_udp_packets(module+8, &request, &response);
  else if (!memcmp(module, "cvm-local:", 10))
    result = cvm_xfer_local_packets(module+10, &request, &response);
  else {
    if (!memcmp(module, "cvm-command:", 12)) module += 12;
    result = cvm_xfer_command_packets(module, &request, &response);
  }
  signal(SIGPIPE, oldsig);
  if (result != 0) return result;
  return parse_packet(&response);
}