Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/nss/automount.c
blob: 1297330f31a7f32ceb0c1d5ae2f8daaf9c89fd18 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
   automount.c - NSS lookup functions for automounter maps

   Copyright (C) 2006 West Consulting
   Copyright (C) 2006 Arthur de Jong

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
   MA 02110-1301 USA
*/

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <nss.h>
#include <errno.h>

#include "prototypes.h"
#include "nslcd-client.h"
#include "common.h"

/* macros for expanding the LDF_AUTOMOUNT macro */
#define LDF_STRING(field)     READ_STRING_BUF(fp,field)
#define AUTOMOUNT_KEY         *canon_key
#define AUTOMOUNT_INFO        *value

#define AUTOMOUNT_CONTEXT_MAGIC 0x83451830

struct automount_context
{
  char *mapname;
  FILE *fp;       /* for getautomntent() call */
  int32_t magic;  /* for sanity checks */
};

static enum nss_status read_automount(
        FILE *fp,const char **canon_key,const char **value,
        char *buffer,size_t buflen,int *errnop)
{
  int32_t tmpint32;
  size_t bufptr=0;
  /* auto-genereted read code */
  LDF_AUTOMOUNT;
  /* we're done */
  return NSS_STATUS_SUCCESS;
}

/* this function initiates a structure for doing queries
   using getautomountbyname() and getautomountent() */
enum nss_status _nss_ldap_setautomntent(
        const char *mapname,void **private)
{
  struct automount_context *context;
  /* allocate memory */
  context=malloc(sizeof(struct automount_context));
  if (context==NULL)
    return NSS_STATUS_UNAVAIL;
  /* store mapname */
  context->mapname=strdup(mapname);
  if (context->mapname==NULL)
  {
    free(context);
    return NSS_STATUS_UNAVAIL;
  }
  /* clear file handle and store magic */
  context->fp=NULL;
  context->magic=AUTOMOUNT_CONTEXT_MAGIC;
  /* return context */
  *private=context;
  return NSS_STATUS_SUCCESS;
}

/* this searches for an automounter key within the automounter
   map initialized by setautomountent() */
enum nss_status _nss_ldap_getautomntbyname_r(
        void *private,const char *key,const char **canon_key,
        const char **value,char *buffer,size_t buflen,int *errnop)
{
  struct automount_context *context;
  FILE *fp;
  int32_t tmpint32;
  enum nss_status retv;
  /* check context */
  context=(struct automount_context *)private;
  if ((context==NULL)||(context->magic!=AUTOMOUNT_CONTEXT_MAGIC))
    return NSS_STATUS_UNAVAIL;
  /* open socket and write request */
  OPEN_SOCK(fp);
  WRITE_REQUEST(fp,NSLCD_ACTION_AUTOMOUNT_BYNAME);
  WRITE_STRING(fp,context->mapname);
  WRITE_STRING(fp,key);
  WRITE_FLUSH(fp);
  /* read response header */
  READ_RESPONSEHEADER(fp,NSLCD_ACTION_AUTOMOUNT_BYNAME);
  /* read response */
  READ_RESPONSE_CODE(fp);
  retv=read_automount(fp,canon_key,value,buffer,buflen,errnop);
  if (retv!=NSS_STATUS_SUCCESS)
    return retv;
  /* close socket and we're done */
  fclose(fp);
  return NSS_STATUS_SUCCESS;
}

enum nss_status _nss_ldap_getautomntent_r(
        void *private,const char **canon_key,const char **value,
        char *buffer,size_t buflen,int *errnop)
{
  struct automount_context *context;
  int32_t tmpint32;
  enum nss_status retv;
  /* check context */
  context=(struct automount_context *)private;
  if ((context==NULL)||(context->magic!=AUTOMOUNT_CONTEXT_MAGIC))
    return NSS_STATUS_UNAVAIL;
  /* if we don't have a file descriptor, begin a request now */
  if (context->fp==NULL)
  {
    /* open a new stream and write the request */
    OPEN_SOCK(context->fp);
    WRITE_REQUEST(context->fp,NSLCD_ACTION_AUTOMOUNT_ALL);
    WRITE_FLUSH(context->fp);
    /* read response header */
    READ_RESPONSEHEADER(context->fp,NSLCD_ACTION_AUTOMOUNT_ALL);
  }
  /* read a response */
  READ_RESPONSE_CODE(context->fp);
  retv=read_automount(context->fp,canon_key,value,buffer,buflen,errnop);
  if (retv!=NSS_STATUS_SUCCESS)
  {
    /* remove reference to fp from context */
    context->fp=NULL;
    return retv;
  }
  return NSS_STATUS_SUCCESS;
}

enum nss_status _nss_ldap_endautomntent(void **private)
{
  struct automount_context *context;
  /* check private */
  if (private==NULL)
    return NSS_STATUS_UNAVAIL;
  /* check context */
  context=(struct automount_context *)*private;
  if ((context==NULL)||(context->magic!=AUTOMOUNT_CONTEXT_MAGIC))
    return NSS_STATUS_UNAVAIL;
  /* close any connections */
  if (context->fp!=NULL)
    fclose(context->fp);
  /* free memory */
  free(context->mapname);
  free(context);
  /* invalidate reference */
  *private=NULL;
  /* we're done */
  return NSS_STATUS_SUCCESS;
}