gale client

root / ginsu-mdk

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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python2

import xmlrpclib, sys, getopt, os, getpass, string


defaultserver= 'https://yammer.net:1115/mdk.psp'

def usage():
  print """usage:

    Normal usage:
    %(cmd)s [-m URI]
      interactive dialog to issue and write a key

    More fine-grained commands:
    %(cmd)s [-m URI] ls
      list available domains
    %(cmd)s [-m URI] gen USER@DOM.AIN [-r PR] [-u PU] -e EM /FN
      generate key
    %(cmd)s [-m URI] get USER@DOM.AIN [-r PR] [-u PU]
      issue a key (you must have the account password)
    %(cmd)s [-m URI] regen USER@DOM.AIN [-r PR] [-u PU]
      reissue a key (you must have the account password)
    %(cmd)s [-m URI] revoke USER@DOM.AIN
      revoke a key (you must have the account password)
    %(cmd)s [-m URI] change-password USER@DOM.AIN
      change the account password for a key
    %(cmd)s [-m URI] reset-password USER@DOM.AIN
      reset the account password for a key and email it to the key's owner

    -m URI   the Marmaduke server URI (default: %(defaultserver)s)
    -r PR    write the pRivate key to file PR instead of key directory
    -u PU    write the pUblic key to file PU instead of key directory
    -e EM    email address; passwords are sent to this address
    /FN      full name of user, preceded by a slash

    Marmaduke command line client version 1.0pre.2161,
    generated on Yammer.
    """ % {'cmd': sys.argv[0], 'defaultserver': defaultserver}

def handleLs(server, args):
  domains= server.listDomains()
  print string.join(domains)

def handleGen(server, args):
  requiredArguments('key email fullname'.split(), args)
  privPath= '%(galedir)s/%(key)s.gpri' % args
  if args.has_key('priv') and args['priv'] is not None:
    privPath= priv
  pubPath= '%(galedir)s/%(key)s.gpub' % args
  if args.has_key('pub') and args['pub'] is not None:
    pubPath= pub
  keys= server.generateKey(args['key'], args['fullname'], args['email'])
  saveKeys(keys, privPath, pubPath)
  kgenMsg()

def handleGet(server, args):
  requiredArguments('key'.split(), args)
  privPath= '%(galedir)s/%(key)s.gpri' % args
  if args.has_key('priv') and args['priv'] is not None:
    privPath= priv
  pubPath= '%(galedir)s/%(key)s.gpub' % args
  if args.has_key('pub') and args['pub'] is not None:
    pubPath= pub
  passwd= getPassword()
  keys= server.issue(args['key'], passwd)
  saveKeys(keys, privPath, pubPath)

def handleRegen(server, args):
  requiredArguments('key'.split(), args)
  privPath= '%(galedir)s/%(key)s.gpri' % args
  if args.has_key('priv') and args['priv'] is not None:
    privPath= priv
  pubPath= '%(galedir)s/%(key)s.gpub' % args
  if args.has_key('pub') and args['pub'] is not None:
    pubPath= pub
  passwd= getPassword()
  keys= server.reissue(args['key'], passwd)
  saveKeys(keys, privPath, pubPath)

def handleRevoke(server, args):
  requiredArguments('key'.split(), args)
  privPath= '%(galedir)s/%(key)s.gpri' % args
  pubPath= '%(galedir)s/%(key)s.gpub' % args
  passwd= getPassword()
  keys= server.revoke(args['key'], passwd)
  eraseKeys(privPath, pubPath)

def handleChPwd(server, args):
  requiredArguments('key'.split(), args)
  repeat= 1
  while repeat:
    oldpasswd= getPassword('Current password: ')
    newpasswd1= getPassword('New password: ')
    newpasswd2= getPassword('Re-enter new password: ')
    if newpasswd1 != newpasswd2:
      sys.stderr.write('new passwords do not match\n')
    else:
      repeat= 0
  server.changePassword(args['key'], oldpasswd, newpasswd1)
  sys.stderr.write('password changed\n')

def handleRePwd(server, args):
  requiredArguments('key'.split(), args)
  repeat= 1
  server.forgotPassword(args['key'])
  sys.stderr.write('password reset and emailed to user\n')

def handleWizard(server, args):
  domains= server.listDomains()
  print 'Available domains: ' + string.join(domains)
  print
  repeat= 1
  user, fullname, email= None, None, None
  while repeat:
    print '\nWhat username do you want?  Include the domain (example, ' + \
      'user@whatever.com).'
    user= readLine()
    if user.find('@') < 1:
      user= user + '@' + domains[0]
      print 'Adding default domain -> ' + user
    args['user']= user
    print '\nWhat is your full name?'
    if fullname is not None:
      print '[hit enter to use "%s"]' % fullname
    fullname= readLine(fullname)
    print '\nWhat is your email address?'
    if email is not None:
      print '[hit enter to use "%s"]' % email
    email= readLine(email)
    try:
      keys= server.generateKey(user, fullname, email)
      print
      saveKeys(keys, '%(galedir)s/%(user)s.gpri' % args,
        '%(galedir)s/%(user)s.gpub' % args)
      kgenMsg()
      repeat= 0
    except xmlrpclib.Fault, fault:
      handleFault(fault)
      if fault.faultCode == 111:
        print 'If you have the password for %s, enter it now.' % user
        print 'Otherwise, just hit enter.'
        if regenWizard(server, user, args['galedir']):
          return
        print

def regenWizard(server, keyid, galedir):
  repeat= 1
  while repeat:
    password= getPassword('password for %s: ' % keyid)
    if len(password) == 0:
      return 0
    try:
      keys= server.reissue(keyid, password)
      print
      saveKeys(keys, '%(galedir)s/%(keyid)s.gpri' % locals(),
        '%(galedir)s/%(keyid)s.gpub' % locals())
      repeat= 0
      return 1
    except xmlrpclib.Fault, fault:
      handleFault(fault)




def kgenMsg():
  sys.stderr.write('\nKey generated.  Watch your mailbox for an account ' + \
    'password;\nthis will be needed if you want to reissue or revoke\n' + \
    'your key, and can also be used to log in to the appropriate instance\n' + \
    'of Yammer.\n')



def eraseKeys(privPath, pubPath):
  sys.stderr.write('\n')
  if os.path.exists(privPath):
    sys.stderr.write('erasing private key %s\n' % privPath)
    os.unlink(privPath)
  if os.path.exists(pubPath):
    sys.stderr.write('erasing public key %s\n' % pubPath)
    os.unlink(pubPath)

def saveKeys(keys, privPath=None, pubPath=None):
  sys.stderr.write('writing private key to %s\n' % privPath)
  mkdirDashP(privPath)
  p= open(privPath, 'w')
  p.write(keys['private'].data)
  p.close()
  mkdirDashP(pubPath)
  sys.stderr.write('writing public key to %s\n' % pubPath)
  p= open(pubPath, 'w')
  p.write(keys['public'].data)
  p.close()

def mkdirDashP(filename):
  path= os.path.dirname(filename)
  if not os.path.exists(path):
    os.makedirs(path)

def requiredArguments(reqs, args):
  a= [x for x in reqs if x not in args or args[x] is None]
  if len(a) > 0:
    print 'missing argument(s): ' + string.join(a)
    usage()
    sys.exit(1)

def getPassword(prompt= None):
  if prompt is None:
    prompt= 'Password: '
  return getpass.getpass(prompt)

def readLine(default= None):
  a= sys.stdin.readline().strip()
  if len(a) == 0 and default is not None:
    return default
  return a

def getGaleDir():
  return os.environ['HOME'] + '/.gale/auth/private'

def getServer(mdkuri):
  return xmlrpclib.ServerProxy(mdkuri)


def handleFault(fault):
  sys.stderr.write('\n%s\n\n' %
    (fault.faultString))


commands= {'ls': handleLs, 'gen': handleGen, 'regen': handleRegen,
           'get': handleGet, 'revoke': handleRevoke,
           'change-password': handleChPwd, 'reset-password': handleRePwd}

argu= sys.argv[1:]
dashes, squares= [], []
i= 0
while i < len(argu):
  if argu[i].startswith('-'):
    dashes.append(argu[i])
    if argu[i][1] != 'h':
      dashes.append(argu[i+1])
      i= i + 1
    i= i + 1
  else:
    squares.append(argu[i])
    i= i + 1
opts, args= getopt.getopt(dashes + squares, 'm:r:u:e:h')
mdkserver, priv, pub, email= defaultserver, None, None, None
for opt, value in opts:
  if opt == '-m':
    mdkserver= value
  if opt == '-r':
    priv= value
  if opt == '-u':
    pub= value
  if opt == '-e':
    email= value
  if opt == '-h':
    usage()
    sys.exit(1)

cmd= None
if len(args) > 0:
  cmd= args[0]
fullname= [x[1:] for x in args if x.startswith('/')]
if len(fullname) > 0:
  fullname= fullname[0]
else:
  fullname= None
key= [x for x in args if not x.startswith('/') and x.find('@') > 0]
if len(key) > 0:
  key= key[0]
else:
  key= None

try:
  server= getServer(mdkserver)
  galedir= getGaleDir()
  if cmd is not None and commands.has_key(cmd):
    commands[cmd](server, locals())
  else:
    handleWizard(server, locals())
except xmlrpclib.Fault, fault:
  handleFault(fault)
  sys.exit(fault.faultCode)