]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_guest.c
Initial patch for authenticated printing. Requires LaserWriter 8.5.1 or
[netatalk.git] / etc / uams / uams_guest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <pwd.h>
5 #include <syslog.h>
6
7 #include <atalk/afp.h>
8 #include <atalk/uam.h>
9
10 static int noauth_login(void *obj, struct passwd **uam_pwd,
11                         char *ibuf, int ibuflen, 
12                         char *rbuf, int *rbuflen)
13 {
14     struct passwd *pwent;
15     char *guest, *username;
16
17     *rbuflen = 0;
18     syslog( LOG_INFO, "login noauth" );
19
20     if (uam_afpserver_option(obj, UAM_OPTION_GUEST, (void *) &guest,
21                              NULL) < 0)
22       return AFPERR_MISC;
23
24     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, 
25                              (void *) &username, NULL) < 0)
26       return AFPERR_MISC;
27
28     strcpy(username, guest);
29     if ((pwent = getpwnam(guest)) == NULL) {
30         syslog( LOG_ERR, "noauth_login: getpwnam( %s ): %m", guest);
31         return( AFPERR_BADUAM );
32     }
33
34 #ifdef AFS
35     if ( setpag() < 0 ) {
36         syslog( LOG_ERR, "noauth_login: setpag: %m" );
37         return( AFPERR_BADUAM );
38     }
39 #endif /* AFS */
40
41     *uam_pwd = pwent;
42     return( AFP_OK );
43 }
44
45
46 /* Printer NoAuthUAM Login */
47 int noauth_printer(start, stop, username, out)
48         char    *start, *stop, *username;
49         struct papfile  *out;
50 {
51     char        *data, *p, *q;
52     static const char *loginok = "0\r";
53
54     data = (char *)malloc(stop - start + 1);
55     strncpy(data, start, stop - start + 1);
56
57     /* We are looking for the following format in data:
58      * (username)
59      *
60      * Hopefully username doesn't contain a ")"
61      */
62
63     if ((p = strchr(data, '(' )) == NULL) {
64         syslog(LOG_INFO,"Bad Login NoAuthUAM: username not found in string");
65         free(data);
66         return(-1);
67     }
68     p++;
69     if ((q = strchr(data, ')' )) == NULL) {
70         syslog(LOG_INFO,"Bad Login NoAuthUAM: username not found in string");
71         free(data);
72         return(-1);
73     }
74     strncpy(username, p, q - p);
75
76     /* Done copying username, clean up */
77     free(data);
78
79     if (getpwnam(username) == NULL) {
80         syslog(LOG_INFO, "Bad Login NoAuthUAM: %s: %m", username);
81         return(-1);
82     }
83
84     /* Login successful */
85     append(out, loginok, strlen(loginok));
86     syslog(LOG_INFO, "Login NoAuthUAM: %s", username);
87     return(0);
88 }
89
90
91 static int uam_setup(const char *path)
92 {
93   if (uam_register(UAM_SERVER_LOGIN, path, "No User Authent",
94                noauth_login, NULL, NULL) < 0)
95         return -1;
96   if (uam_register(UAM_SERVER_PRINTAUTH, path, "NoAuthUAM",
97                 noauth_printer) < 0)
98         return -1;
99
100   return 0;
101 }
102
103 static void uam_cleanup()
104 {
105   uam_unregister(UAM_SERVER_LOGIN, "No User Authent");
106   uam_unregister(UAM_SERVER_PRINTAUTH, "NoAuthUAM");
107 }
108
109 UAM_MODULE_EXPORT struct uam_export uams_guest = {
110   UAM_MODULE_SERVER,
111   UAM_MODULE_VERSION,
112   uam_setup, uam_cleanup
113 };